test/reader_test.rb in symmetric-encryption-1.0.0 vs test/reader_test.rb in symmetric-encryption-1.1.1
- old
+ new
@@ -11,11 +11,11 @@
SymmetricEncryption.load!(File.join(File.dirname(__FILE__), 'config', 'symmetric-encryption.yml'), 'test')
# Unit Test for SymmetricEncrypted::ReaderStream
#
class ReaderTest < Test::Unit::TestCase
- context 'Reader' do
+ context SymmetricEncryption::Reader do
setup do
@data = [
"Hello World\n",
"Keep this secret\n",
"And keep going even further and further..."
@@ -68,11 +68,11 @@
end
end
context "reading from file" do
# With and without header
- [{:header => false}, {:compress => false}, {:compress => true}].each_with_index do |options, i|
+ [{:header => false, :version => 1}, {:header => false, :random_key => false, :version => 1}, {:compress => false}, {:compress => true}, {:random_key => false}].each_with_index do |options, i|
context "with#{'out' unless options[:header]} header #{i}" do
setup do
@filename = '._test'
# Create encrypted file
SymmetricEncryption::Writer.open(@filename, options) do |file|
@@ -110,7 +110,72 @@
end
end
end
+
+ context "reading from files with previous keys" do
+ setup do
+ @filename = '._test'
+ # Create encrypted file with old encryption key
+ SymmetricEncryption::Writer.open(@filename, :version => 0) do |file|
+ @data.inject(0) {|sum,str| sum + file.write(str)}
+ end
+ end
+
+ teardown do
+ File.delete(@filename) if File.exist?(@filename)
+ end
+
+ should "decrypt from file in a single read" do
+ decrypted = SymmetricEncryption::Reader.open(@filename) {|file| file.read}
+ assert_equal @data_str, decrypted
+ end
+
+ should "decrypt from file a line at a time" do
+ decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
+ i = 0
+ file.each_line do |line|
+ assert_equal @data[i], line
+ i += 1
+ end
+ end
+ end
+
+ should "support rewind" do
+ decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
+ file.read
+ file.rewind
+ file.read
+ end
+ assert_equal @data_str, decrypted
+ end
+ end
+
+ context "reading from files with previous keys without a header" do
+ setup do
+ @filename = '._test'
+ # Create encrypted file with old encryption key
+ SymmetricEncryption::Writer.open(@filename, :version => 0, :header => false, :random_key => false) do |file|
+ @data.inject(0) {|sum,str| sum + file.write(str)}
+ end
+ end
+
+ teardown do
+ File.delete(@filename) if File.exist?(@filename)
+ end
+
+ should "decrypt from file in a single read" do
+ decrypted = SymmetricEncryption::Reader.open(@filename, :version => 0) {|file| file.read}
+ assert_equal @data_str, decrypted
+ end
+
+ should "decrypt from file in a single read with different version" do
+ # Should fail since file was encrypted using version 0 key
+ assert_raise OpenSSL::Cipher::CipherError do
+ SymmetricEncryption::Reader.open(@filename, :version => 1) {|file| file.read}
+ end
+ end
+ end
+
end
-end
\ No newline at end of file
+end