test/reader_test.rb in symmetric-encryption-4.0.0 vs test/reader_test.rb in symmetric-encryption-4.0.1

- old
+ new

@@ -35,28 +35,28 @@ describe header do before do @data_encrypted = header ? @data_encrypted_with_header : @data_encrypted_without_header end - it "#read()" do + it '#read()' do stream = StringIO.new(@data_encrypted) # Version 0 supplied if the file/stream does not have a header - decrypted = SymmetricEncryption::Reader.open(stream, version: 0) { |file| file.read } + decrypted = SymmetricEncryption::Reader.open(stream, version: 0, &:read) assert_equal @data_str, decrypted end - it "#read(size) followed by #read()" do + it '#read(size) followed by #read()' do stream = StringIO.new(@data_encrypted) # 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 - it "#each_line" do + it '#each_line' do stream = StringIO.new(@data_encrypted) i = 0 # Version 0 supplied if the file/stream does not have a header SymmetricEncryption::Reader.open(stream, version: 0) do |file| file.each_line do |line| @@ -64,21 +64,21 @@ i += 1 end end end - it "#read(size)" do + it '#read(size)' do stream = StringIO.new(@data_encrypted) # Version 0 supplied if the file/stream does not have a header SymmetricEncryption::Reader.open(stream, version: 0) do |file| index = 0 [0, 10, 5, 5000].each do |size| buf = file.read(size) - if size == 0 + if size.zero? assert_equal '', buf else - assert_equal @data_str[index..index+size-1], buf + assert_equal @data_str[index..index + size - 1], buf end index += size end end end @@ -97,15 +97,14 @@ # Header but not random key or iv {random_key: false}, # Random iv only {random_key: false, random_iv: true}, # Random iv only with compression - {random_iv: true, compress: true}, + {random_iv: true, compress: true} ].each do |options| - [:data, :empty, :blank].each do |usecase| - + %i[data empty blank].each do |usecase| describe "read from #{usecase} file with options: #{options.inspect}" do before do case usecase when :data # Create encrypted file @@ -138,11 +137,11 @@ after do File.delete(@file_name) if File.exist?(@file_name) && !@file_name.end_with?('empty.csv') end it '.empty?' do - assert_equal (@data_size==0), SymmetricEncryption::Reader.empty?(@file_name) + assert_equal @data_size.zero?, SymmetricEncryption::Reader.empty?(@file_name) assert_raises Errno::ENOENT do SymmetricEncryption::Reader.empty?('missing_file') end end @@ -153,11 +152,11 @@ end end it '.open return Zlib::GzipReader when compressed' do file = SymmetricEncryption::Reader.open(@file_name) - #assert_equal (@header && (options[:compress]||false)), file.is_a?(Zlib::GzipReader) + # assert_equal (@header && (options[:compress]||false)), file.is_a?(Zlib::GzipReader) file.close end it '#read' do data = nil @@ -176,16 +175,15 @@ eof = file.eof? data = file.read(4096) file.close assert_equal @eof, eof - if @data_size > 0 + if @data_size.positive? assert_equal @data_str, data else assert_nil data end - end it '#each_line' do SymmetricEncryption::Reader.open(@file_name) do |file| i = 0 @@ -210,44 +208,38 @@ eof = file.eof? data = file.gets(nil, 4096) file.close assert_equal @eof, eof - if @data_size > 0 + if @data_size.positive? assert_equal @data_str, data + # On JRuby Zlib::GzipReader.new(file) returns '' instead of nil on an empty file + elsif defined?(JRuby) && options[:compress] && (usecase == :empty) + assert_equal '', data else - # On JRuby Zlib::GzipReader.new(file) returns '' instead of nil - # on an empty file - if defined?(JRuby) && options[:compress] && (usecase == :empty) - assert_equal '', data - else - assert_nil data - end + assert_nil data end end it '#gets(delim)' do SymmetricEncryption::Reader.open(@file_name) do |file| i = 0 - while line = file.gets("\n") + while (line = file.gets("\n")) assert_equal @data[i], line i += 1 end - assert_equal (@data_size > 0 ? 3 : 0), i + assert_equal (@data_size.positive? ? 3 : 0), i end end it '#gets(delim,size)' do SymmetricEncryption::Reader.open(@file_name) do |file| i = 0 - while file.gets("\n", 128) - i += 1 - end - assert_equal (@data_size > 0 ? 3 : 0), i + i += 1 while file.gets("\n", 128) + assert_equal (@data_size.positive? ? 3 : 0), i end end - end end end describe 'reading from files with previous keys' do @@ -262,11 +254,11 @@ after do File.delete(@file_name) if File.exist?(@file_name) end it 'decrypt from file in a single read' do - decrypted = SymmetricEncryption::Reader.open(@file_name) { |file| file.read } + decrypted = SymmetricEncryption::Reader.open(@file_name, &:read) assert_equal @data_str, decrypted end it 'decrypt from file a line at a time' do SymmetricEncryption::Reader.open(@file_name) do |file| @@ -300,23 +292,23 @@ after do begin File.delete(@file_name) if File.exist?(@file_name) rescue Errno::EACCES # Required for Windows + nil end end it 'decrypt from file in a single read' do - decrypted = SymmetricEncryption::Reader.open(@file_name, version: 0) { |file| file.read } + decrypted = SymmetricEncryption::Reader.open(@file_name, version: 0, &:read) assert_equal @data_str, decrypted end it 'decrypt from file in a single read with different version' do # Should fail since file was encrypted using version 0 key assert_raises OpenSSL::Cipher::CipherError do SymmetricEncryption::Reader.read(@file_name, version: 1) end end end - end end