| 
									
										
										
										
											2021-05-24 01:24:22 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 17:31:17 +08:00
										 |  |  | require 'test_helper' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EncryptionTest < MiniTest::Test | 
					
						
							|  |  |  |   ENCRYPT_ZIP_TEST_FILE = 'test/data/zipWithEncryption.zip' | 
					
						
							|  |  |  |   INPUT_FILE1 = 'test/data/file1.txt' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 12:05:58 +08:00
										 |  |  |   def setup | 
					
						
							|  |  |  |     Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def teardown | 
					
						
							| 
									
										
										
										
											2020-06-21 00:34:57 +08:00
										 |  |  |     Zip.reset! | 
					
						
							| 
									
										
										
										
											2015-01-17 12:05:58 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 17:31:17 +08:00
										 |  |  |   def test_encrypt | 
					
						
							| 
									
										
										
										
											2020-04-29 21:41:21 +08:00
										 |  |  |     content = File.open(INPUT_FILE1, 'r').read | 
					
						
							|  |  |  |     test_filename = 'top_secret_file.txt' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     password = 'swordfish' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-06 08:36:09 +08:00
										 |  |  |     encrypted_zip = Zip::OutputStream.write_buffer(::StringIO.new(+''), Zip::TraditionalEncrypter.new(password)) do |out| | 
					
						
							| 
									
										
										
										
											2020-04-29 21:41:21 +08:00
										 |  |  |       out.put_next_entry(test_filename) | 
					
						
							|  |  |  |       out.write content | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Zip::InputStream.open(encrypted_zip, 0, Zip::TraditionalDecrypter.new(password)) do |zis| | 
					
						
							|  |  |  |       entry = zis.get_next_entry | 
					
						
							|  |  |  |       assert_equal test_filename, entry.name | 
					
						
							|  |  |  |       assert_equal 1327, entry.size | 
					
						
							|  |  |  |       assert_equal content, zis.read | 
					
						
							| 
									
										
										
										
											2015-01-08 17:31:17 +08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-29 21:41:21 +08:00
										 |  |  |     assert_raises(Zip::DecompressionError) do | 
					
						
							| 
									
										
										
										
											2021-06-18 23:10:57 +08:00
										 |  |  |       Zip::InputStream.open(encrypted_zip, 0, Zip::TraditionalDecrypter.new("#{password}wrong")) do |zis| | 
					
						
							| 
									
										
										
										
											2020-04-29 21:41:21 +08:00
										 |  |  |         zis.get_next_entry | 
					
						
							|  |  |  |         assert_equal content, zis.read | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-01-08 17:31:17 +08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def test_decrypt | 
					
						
							|  |  |  |     Zip::InputStream.open(ENCRYPT_ZIP_TEST_FILE, 0, Zip::TraditionalDecrypter.new('password')) do |zis| | 
					
						
							|  |  |  |       entry = zis.get_next_entry | 
					
						
							|  |  |  |       assert_equal 'file1.txt', entry.name | 
					
						
							|  |  |  |       assert_equal 1327, entry.size | 
					
						
							| 
									
										
										
										
											2019-09-14 22:56:02 +08:00
										 |  |  |       assert_equal ::File.open(INPUT_FILE1, 'r').read, zis.read | 
					
						
							| 
									
										
										
										
											2015-01-08 17:31:17 +08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |