Commit f0ca960

mokha <mo@mokhan.ca>
2019-01-28 23:21:45
ensure ascii and utf-8 content can be decrypted
1 parent e6bbb54
Changed files (1)
spec
spec/xml/kit/crypto/symmetric_cipher_spec.rb
@@ -30,20 +30,40 @@ RSpec.describe ::Xml::Kit::Crypto::SymmetricCipher do
         let(:secret) { SecureRandom.hex }
         let(:data) { "#{iv}#{secret}".strip }
 
-        before do
-          IO.write(original_file, data, encoding: Encoding::ASCII_8BIT)
-          execute_shell([
-            "openssl enc -#{openssl_algorithm} -p -e -A -nosalt",
-            "-in #{original_file}",
-            "-out #{encrypted_file}",
-            "-K #{key.unpack('H*')[0].upcase}",
-            "-iv #{iv.unpack('H*')[0].upcase}"
-          ].join(' '))
+        context "when encoded as ASCII" do
+          before do
+            IO.write(original_file, data, encoding: Encoding::ASCII_8BIT)
+            execute_shell([
+              "openssl enc -#{openssl_algorithm} -p -e -A -nosalt",
+              "-in #{original_file}",
+              "-out #{encrypted_file}",
+              "-K #{key.unpack('H*')[0].upcase}",
+              "-iv #{iv.unpack('H*')[0].upcase}"
+            ].join(' '))
+          end
+
+          specify do
+            cipher_text = IO.read(encrypted_file, encoding: Encoding::ASCII_8BIT)
+            expect(subject.decrypt(cipher_text)).to eql(secret)
+          end
         end
 
-        specify do
-          cipher_text = IO.read(encrypted_file, encoding: Encoding::ASCII_8BIT)
-          expect(subject.decrypt(cipher_text)).to eql(secret)
+        context "when encoded as UTF-8" do
+          before do
+            IO.write(original_file, data)
+            execute_shell([
+              "openssl enc -#{openssl_algorithm} -p -e -A -nosalt",
+              "-in #{original_file}",
+              "-out #{encrypted_file}",
+              "-K #{key.unpack('H*')[0].upcase}",
+              "-iv #{iv.unpack('H*')[0].upcase}"
+            ].join(' '))
+          end
+
+          specify do
+            cipher_text = IO.read(encrypted_file)
+            expect(subject.decrypt(cipher_text)).to eql(secret)
+          end
         end
       end