test/reader_test.rb in symmetric-encryption-4.1.0.beta1 vs test/reader_test.rb in symmetric-encryption-4.1.0

- old
+ new

@@ -4,22 +4,22 @@ # Unit Test for SymmetricEncrypted::ReaderStream # class ReaderTest < Minitest::Test describe SymmetricEncryption::Reader do before do - @data = [ + @data = [ "Hello World\n", "Keep this secret\n", 'And keep going even further and further...' ] @data_str = @data.inject('') { |sum, str| sum << str } @data_len = @data_str.length # Use Cipher 0 since it does not always include a header @cipher = SymmetricEncryption.cipher(0) @data_encrypted_without_header = @cipher.binary_encrypt(@data_str, header: false) - header = SymmetricEncryption::Header.new( + header = SymmetricEncryption::Header.new( version: @cipher.version, iv: @cipher.iv, key: @cipher.send(:key), cipher_name: @cipher.cipher_name ) @@ -85,11 +85,11 @@ end end [ # No Header - {header: false, random_key: false, random_iv: false}, + {header: false, random_key: false, random_iv: false, compress: false}, # Default Header with random key and iv {}, # Header with no compression ( default anyway ) {compress: false}, # Compress and use Random key, iv @@ -182,10 +182,30 @@ else assert_nil data end end + it '#read(size, outbuf)' do + file = SymmetricEncryption::Reader.open(@file_name) + # Not supported with compressed files + if file.is_a?(SymmetricEncryption::Reader) + eof = file.eof? + output_buffer = 'buffer' + data = file.read(4096, output_buffer) + file.close + + assert_equal @eof, eof + if @data_size.positive? + assert_equal @data_str, data + assert_equal data.object_id, output_buffer.object_id + else + assert_nil data + assert_empty output_buffer + end + end + end + it '#each_line' do SymmetricEncryption::Reader.open(@file_name) do |file| i = 0 file.each_line do |line| assert_equal @data[i], line @@ -210,32 +230,32 @@ file.close assert_equal @eof, eof if @data_size.positive? assert_equal @data_str, data + elsif defined?(JRuby) # On JRuby Zlib::GzipReader.new(file) returns '' instead of nil on an empty file - elsif defined?(JRuby) && options[:compress] && (usecase == :empty) - assert_equal '', data + assert data.blank? else assert_nil data end end it '#gets(delim)' do SymmetricEncryption::Reader.open(@file_name) do |file| - i = 0 + i = 0 while (line = file.gets("\n")) assert_equal @data[i], line i += 1 end assert_equal (@data_size.positive? ? 3 : 0), i end end it '#gets(delim,size)' do SymmetricEncryption::Reader.open(@file_name) do |file| - i = 0 + i = 0 i += 1 while file.gets("\n", 128) assert_equal (@data_size.positive? ? 3 : 0), i end end end @@ -282,10 +302,10 @@ describe 'reading from files with previous keys without a header' do before do @file_name = '_test' # Create encrypted file with old encryption key - SymmetricEncryption::Writer.open(@file_name, version: 0, header: false, random_key: false, random_iv: false) do |file| + SymmetricEncryption::Writer.open(@file_name, version: 0, header: false, random_key: false, random_iv: false, compress: false) do |file| @data.inject(0) { |sum, str| sum + file.write(str) } end end after do