test/pgp_writer_test.rb in iostreams-0.14.0 vs test/pgp_writer_test.rb in iostreams-0.15.0

- old
+ new

@@ -1,92 +1,93 @@ require_relative 'test_helper' -module Streams - class PgpWriterTest < Minitest::Test - describe IOStreams::Pgp::Writer do - before do - file_name = File.join(File.dirname(__FILE__), 'files', 'text.txt') - @data = File.read(file_name) +class PgpWriterTest < Minitest::Test + describe IOStreams::Pgp::Writer do + let :temp_file do + Tempfile.new('iostreams') + end - @temp_file = Tempfile.new('iostreams') - @file_name = @temp_file.to_path - end + let :file_name do + temp_file.path + end - after do - @temp_file.delete if @temp_file - end + let :decrypted do + file_name = File.join(File.dirname(__FILE__), 'files', 'text.txt') + File.read(file_name) + end - describe '.open' do - it 'writes encrypted text file' do - IOStreams::Pgp::Writer.open(@file_name, recipient: 'receiver@example.org', binary: false) do |io| - io.write(@data) - end + after do + temp_file.delete + end - result = IOStreams::Pgp::Reader.open(@file_name, passphrase: 'receiver_passphrase', binary: false) { |file| file.read } - assert_equal @data, result + describe '.open' do + it 'writes encrypted text file' do + IOStreams::Pgp::Writer.open(file_name, recipient: 'receiver@example.org', binary: false) do |io| + io.write(decrypted) end - it 'writes encrypted binary file' do - binary_file_name = File.join(File.dirname(__FILE__), 'files', 'spreadsheet.xlsx') - binary_data = File.open(binary_file_name, 'rb') { |file| file.read } + result = IOStreams::Pgp::Reader.open(file_name, passphrase: 'receiver_passphrase', binary: false) { |file| file.read } + assert_equal decrypted, result + end - File.open(binary_file_name, 'rb') do |input| - IOStreams::Pgp::Writer.open(@file_name, recipient: 'receiver@example.org') do |output| - IOStreams.copy(input, output, 65535) - end - end + it 'writes encrypted binary file' do + binary_file_name = File.join(File.dirname(__FILE__), 'files', 'spreadsheet.xlsx') + binary_data = File.open(binary_file_name, 'rb') { |file| file.read } - result = IOStreams::Pgp::Reader.open(@file_name, passphrase: 'receiver_passphrase') { |file| file.read } - assert_equal binary_data, result + File.open(binary_file_name, 'rb') do |input| + IOStreams::Pgp::Writer.open(file_name, recipient: 'receiver@example.org') do |output| + IOStreams.copy(input, output) + end end - it 'writes and signs encrypted file' do - IOStreams::Pgp::Writer.open(@file_name, recipient: 'receiver@example.org', signer: 'sender@example.org', signer_passphrase: 'sender_passphrase') do |io| - io.write(@data) - end + result = IOStreams::Pgp::Reader.open(file_name, passphrase: 'receiver_passphrase') { |file| file.read } + assert_equal binary_data, result + end - result = IOStreams::Pgp::Reader.open(@file_name, passphrase: 'receiver_passphrase') { |file| file.read } - assert_equal @data, result + it 'writes and signs encrypted file' do + IOStreams::Pgp::Writer.open(file_name, recipient: 'receiver@example.org', signer: 'sender@example.org', signer_passphrase: 'sender_passphrase') do |io| + io.write(decrypted) end - it 'fails with bad signer passphrase' do - skip 'GnuPG v2.1 and above passes when it should not' if IOStreams::Pgp.pgp_version.to_f >= 2.1 - assert_raises IOStreams::Pgp::Failure do - IOStreams::Pgp::Writer.open(@file_name, recipient: 'receiver@example.org', signer: 'sender@example.org', signer_passphrase: 'BAD') do |io| - io.write(@data) - end + result = IOStreams::Pgp::Reader.open(file_name, passphrase: 'receiver_passphrase') { |file| file.read } + assert_equal decrypted, result + end + + it 'fails with bad signer passphrase' do + skip 'GnuPG v2.1 and above passes when it should not' if IOStreams::Pgp.pgp_version.to_f >= 2.1 + assert_raises IOStreams::Pgp::Failure do + IOStreams::Pgp::Writer.open(file_name, recipient: 'receiver@example.org', signer: 'sender@example.org', signer_passphrase: 'BAD') do |io| + io.write(decrypted) end end + end - it 'fails with bad recipient' do - assert_raises IOStreams::Pgp::Failure do - IOStreams::Pgp::Writer.open(@file_name, recipient: 'BAD@example.org', signer: 'sender@example.org', signer_passphrase: 'sender_passphrase') do |io| - io.write(@data) - # Allow process to terminate - sleep 1 - io.write(@data) - end + it 'fails with bad recipient' do + assert_raises IOStreams::Pgp::Failure do + IOStreams::Pgp::Writer.open(file_name, recipient: 'BAD@example.org', signer: 'sender@example.org', signer_passphrase: 'sender_passphrase') do |io| + io.write(decrypted) + # Allow process to terminate + sleep 1 + io.write(decrypted) end end + end - it 'fails with bad signer' do - assert_raises IOStreams::Pgp::Failure do - IOStreams::Pgp::Writer.open(@file_name, recipient: 'receiver@example.org', signer: 'BAD@example.org', signer_passphrase: 'sender_passphrase') do |io| - io.write(@data) - end + it 'fails with bad signer' do + assert_raises IOStreams::Pgp::Failure do + IOStreams::Pgp::Writer.open(file_name, recipient: 'receiver@example.org', signer: 'BAD@example.org', signer_passphrase: 'sender_passphrase') do |io| + io.write(decrypted) end end + end - it 'fails with stream output' do - string_io = StringIO.new - assert_raises NotImplementedError do - IOStreams::Pgp::Writer.open(string_io, recipient: 'receiver@example.org') do |io| - io.write(@data) - end + it 'fails with stream output' do + string_io = StringIO.new + assert_raises NotImplementedError do + IOStreams::Pgp::Writer.open(string_io, recipient: 'receiver@example.org') do |io| + io.write(decrypted) end end - end - end end end