* 2022-01-18 [ci skip]
[ruby-80x24.org.git] / test / openssl / test_x509attr.rb
blob2919d23d2d38c7a2778b0b0f781bae681dc61e57
1 # frozen_string_literal: true
2 require_relative "utils"
4 if defined?(OpenSSL)
6 class OpenSSL::TestX509Attribute < OpenSSL::TestCase
7   def test_new
8     ef = OpenSSL::X509::ExtensionFactory.new
9     val = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
10       ef.create_extension("keyUsage", "keyCertSign", true)
11     ])])
12     attr = OpenSSL::X509::Attribute.new("extReq", val)
13     assert_equal("extReq", attr.oid)
14     assert_equal(val.to_der, attr.value.to_der)
16     attr = OpenSSL::X509::Attribute.new("1.2.840.113549.1.9.14", val)
17     assert_equal("extReq", attr.oid)
18   end
20   def test_from_der
21     # oid: challengePassword, values: Set[UTF8String<"abc123">]
22     test_der = "\x30\x15\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x07\x31\x08" \
23       "\x0c\x06\x61\x62\x63\x31\x32\x33".b
24     attr = OpenSSL::X509::Attribute.new(test_der)
25     assert_equal(test_der, attr.to_der)
26     assert_equal("challengePassword", attr.oid)
27     assert_equal("abc123", attr.value.value[0].value)
28   end
30   def test_to_der
31     ef = OpenSSL::X509::ExtensionFactory.new
32     val = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
33       ef.create_extension("keyUsage", "keyCertSign", true)
34     ])])
35     attr = OpenSSL::X509::Attribute.new("extReq", val)
36     expected = OpenSSL::ASN1::Sequence.new([
37       OpenSSL::ASN1::ObjectId.new("extReq"),
38       val
39     ])
40     assert_equal(expected.to_der, attr.to_der)
41   end
43   def test_invalid_value
44     # should not change the original value
45     test_der = "\x30\x15\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x09\x07\x31\x08" \
46       "\x0c\x06\x61\x62\x63\x31\x32\x33".b
47     attr = OpenSSL::X509::Attribute.new(test_der)
48     assert_raise(TypeError) {
49       attr.value = "1234"
50     }
51     assert_equal(test_der, attr.to_der)
52     assert_raise(OpenSSL::X509::AttributeError) {
53       attr.oid = "abc123"
54     }
55     assert_equal(test_der, attr.to_der)
56   end
58   def test_dup
59     val = OpenSSL::ASN1::Set([
60       OpenSSL::ASN1::UTF8String("abc123")
61     ])
62     attr = OpenSSL::X509::Attribute.new("challengePassword", val)
63     assert_equal(attr.to_der, attr.dup.to_der)
64   end
66   def test_eq
67     val1 = OpenSSL::ASN1::Set([
68       OpenSSL::ASN1::UTF8String("abc123")
69     ])
70     attr1 = OpenSSL::X509::Attribute.new("challengePassword", val1)
71     attr2 = OpenSSL::X509::Attribute.new("challengePassword", val1)
72     ef = OpenSSL::X509::ExtensionFactory.new
73     val2 = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::Sequence.new([
74       ef.create_extension("keyUsage", "keyCertSign", true)
75     ])])
76     attr3 = OpenSSL::X509::Attribute.new("extReq", val2)
78     assert_equal false, attr1 == 12345
79     assert_equal true, attr1 == attr2
80     assert_equal false, attr1 == attr3
81   end
83   def test_marshal
84     val = OpenSSL::ASN1::Set([
85       OpenSSL::ASN1::UTF8String("abc123")
86     ])
87     attr = OpenSSL::X509::Attribute.new("challengePassword", val)
88     deserialized = Marshal.load(Marshal.dump(attr))
90     assert_equal attr.to_der, deserialized.to_der
91   end
92 end
94 end