summaryrefslogtreecommitdiff
path: root/spec/xml/kit/encrypted_data_spec.rb
blob: 229b7bb92f7c48ad8512bea8cbfaa6be2b710206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

RSpec.describe Xml::Kit::EncryptedData do
  describe '#to_xml' do
    [
      ::Xml::Kit::Crypto::RsaCipher::ALGORITHM,
      ::Xml::Kit::Crypto::OaepCipher::ALGORITHM,
    ].each do |asymmetric_algorithm|
      subject do
        described_class.new(xml, asymmetric_cipher: asymmetric_cipher)
      end

      let(:symmetric_cipher) { ::Xml::Kit::Crypto::SymmetricCipher.new(symmetric_algorithm) }
      let(:symmetric_algorithm) { Xml::Kit::Crypto::SymmetricCipher::DEFAULT_ALGORITHM }
      let(:asymmetric_cipher) { ::Xml::Kit::Crypto.cipher_for(asymmetric_algorithm, key_pair.public_key) }
      let(:key_pair) { Xml::Kit::KeyPair.generate(use: :encryption, algorithm: symmetric_algorithm) }
      let(:decryptor) { Xml::Kit::Decryption.new(private_keys: [key_pair.private_key]) }
      let(:xml) do
        xml = ::Builder::XmlMarkup.new
        xml.HellWorld do
          xml.Now Time.now.iso8601
        end
        xml.target!
      end

      specify { expect(decryptor.decrypt_xml(subject.to_xml)).to eql(xml) }

      [
        "#{::Xml::Kit::Namespaces::XMLENC}tripledes-cbc",
        "#{::Xml::Kit::Namespaces::XMLENC}aes128-cbc",
        "#{::Xml::Kit::Namespaces::XMLENC}aes192-cbc",
        "#{::Xml::Kit::Namespaces::XMLENC}aes256-cbc",
      ].each do |symmetric_algorithm|
        context symmetric_algorithm do
          subject do
            described_class.new(xml, id: id, symmetric_cipher: symmetric_cipher, asymmetric_cipher: asymmetric_cipher)
          end

          let(:id) { ::Xml::Kit::Id.generate }

          specify { expect(decryptor.decrypt_xml(subject.to_xml)).to eql(xml) }
        end
      end
    end
  end
end