test/reader_test.rb in symmetric-encryption-2.2.0 vs test/reader_test.rb in symmetric-encryption-3.0.0
- old
+ new
@@ -20,61 +20,70 @@
"Keep this secret\n",
"And keep going even further and further..."
]
@data_str = @data.inject('') {|sum,str| sum << str}
@data_len = @data_str.length
- @data_encrypted_without_header = SymmetricEncryption.cipher.binary_encrypt(@data_str)
+ # Use Cipher 0 since it does not always include a header
+ @cipher = SymmetricEncryption.cipher(0)
+ @data_encrypted_without_header = @cipher.binary_encrypt(@data_str)
- @data_encrypted_with_header = SymmetricEncryption::Cipher.magic_header(
- SymmetricEncryption.cipher.version,
+ @data_encrypted_with_header = SymmetricEncryption::Cipher.build_header(
+ @cipher.version,
compress = false,
- SymmetricEncryption.cipher.send(:iv),
- SymmetricEncryption.cipher.send(:key),
- SymmetricEncryption.cipher.cipher_name)
- @data_encrypted_with_header << SymmetricEncryption.cipher.binary_encrypt(@data_str)
+ @cipher.send(:iv),
+ @cipher.send(:key),
+ @cipher.cipher_name,
+ binary=false,
+ )
+ @data_encrypted_with_header << @cipher.binary_encrypt(@data_str)
# Verify regular decrypt can decrypt this string
- SymmetricEncryption.cipher.binary_decrypt(@data_encrypted_without_header)
- SymmetricEncryption.cipher.binary_decrypt(@data_encrypted_with_header)
+ @cipher.binary_decrypt(@data_encrypted_without_header)
+ @cipher.binary_decrypt(@data_encrypted_with_header)
+ assert @data_encrypted_without_header != @data_encrypted_with_header
end
[true, false].each do |header|
context header do
setup do
@data_encrypted = header ? @data_encrypted_with_header : @data_encrypted_without_header
end
should "#read()" do
stream = StringIO.new(@data_encrypted)
- decrypted = SymmetricEncryption::Reader.open(stream) {|file| file.read}
+ # Version 0 supplied if the file/stream does not have a header
+ decrypted = SymmetricEncryption::Reader.open(stream, version: 0) {|file| file.read}
assert_equal @data_str, decrypted
end
should "#read(size) followed by #read()" do
stream = StringIO.new(@data_encrypted)
- decrypted = SymmetricEncryption::Reader.open(stream) do |file|
+ # Version 0 supplied if the file/stream does not have a header
+ decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
file.read(10)
file.read
end
assert_equal @data_str[10..-1], decrypted
end
should "#each_line" do
stream = StringIO.new(@data_encrypted)
i = 0
- decrypted = SymmetricEncryption::Reader.open(stream) do |file|
+ # Version 0 supplied if the file/stream does not have a header
+ decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
file.each_line do |line|
assert_equal @data[i], line
i += 1
end
end
end
should "#read(size)" do
stream = StringIO.new(@data_encrypted)
i = 0
- SymmetricEncryption::Reader.open(stream) do |file|
+ # Version 0 supplied if the file/stream does not have a header
+ decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
index = 0
[0,10,5,5000].each do |size|
buf = file.read(size)
if size == 0
assert_equal '', buf
@@ -303,10 +312,10 @@
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}
+ SymmetricEncryption::Reader.open(@filename, :version => 2) {|file| file.read}
end
end
end
end