Skip to content

Commit 6dc57df

Browse files
committedApr 17, 2024
the rest of mri asn.1 tests
1 parent f137183 commit 6dc57df

File tree

1 file changed

+407
-1
lines changed

1 file changed

+407
-1
lines changed
 

‎src/test/ruby/test_asn1.rb

Lines changed: 407 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ def test_encode_nested_set_to_der
247247
def test_null
248248
# TODO: Import Issue -- Is this related to the comment below in test_encode_all?
249249
# TypeError: nil value
250-
# src/test/ruby/test_asn1.rb:851:in `encode_test'
251250
#encode_decode_test B(%w{ 05 00 }), OpenSSL::ASN1::Null.new(nil)
252251
assert_raise(OpenSSL::ASN1::ASN1Error) {
253252
OpenSSL::ASN1.decode(B(%w{ 05 01 00 }))
@@ -359,6 +358,199 @@ def test_simple_to_der
359358
assert_equal "1\x00", OpenSSL::ASN1::Set.new(nil).to_der
360359
end
361360

361+
def test_sequence
362+
encode_decode_test B(%w{ 30 00 }), OpenSSL::ASN1::Sequence.new([])
363+
# TODO: Import Issue
364+
# TypeError: nil value
365+
#encode_decode_test B(%w{ 30 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Sequence.new([
366+
# OpenSSL::ASN1::Null.new(nil),
367+
# OpenSSL::ASN1::Sequence.new([]),
368+
# OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
369+
#])
370+
371+
expected = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))])
372+
expected.indefinite_length = true
373+
encode_decode_test B(%w{ 30 80 04 01 00 00 00 }), expected
374+
375+
# OpenSSL::ASN1::EndOfContent can only be at the end
376+
obj = OpenSSL::ASN1::Sequence.new([
377+
OpenSSL::ASN1::EndOfContent.new,
378+
OpenSSL::ASN1::OctetString.new(B(%w{ 00 })),
379+
OpenSSL::ASN1::EndOfContent.new,
380+
])
381+
obj.indefinite_length = true
382+
# TODO: Import Issue
383+
# <OpenSSL::ASN1::ASN1Error> exception was expected but none was thrown.
384+
#assert_raise(OpenSSL::ASN1::ASN1Error) { obj.to_der }
385+
386+
# The last EOC in value is ignored if indefinite length form is used
387+
expected = OpenSSL::ASN1::Sequence.new([
388+
OpenSSL::ASN1::OctetString.new(B(%w{ 00 })),
389+
OpenSSL::ASN1::EndOfContent.new
390+
])
391+
expected.indefinite_length = true
392+
encode_test B(%w{ 30 80 04 01 00 00 00 }), expected
393+
end
394+
395+
def test_set
396+
encode_decode_test B(%w{ 31 00 }), OpenSSL::ASN1::Set.new([])
397+
# TODO: Import Issue
398+
# <"1\a\x05\x000\x00\x04\x01\x00"> expected but was <"1\a\x04\x01\x00\x05\x000\x00">
399+
#encode_decode_test B(%w{ 31 07 05 00 30 00 04 01 00 }), OpenSSL::ASN1::Set.new([
400+
# OpenSSL::ASN1::Null.new(nil),
401+
# OpenSSL::ASN1::Sequence.new([]),
402+
# OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))
403+
#])
404+
expected = OpenSSL::ASN1::Set.new([OpenSSL::ASN1::OctetString.new(B(%w{ 00 }))])
405+
expected.indefinite_length = true
406+
encode_decode_test B(%w{ 31 80 04 01 00 00 00 }), expected
407+
end
408+
409+
def test_utctime
410+
encode_decode_test B(%w{ 17 0D }) + "160908234339Z".b,
411+
OpenSSL::ASN1::UTCTime.new(Time.utc(2016, 9, 8, 23, 43, 39))
412+
begin
413+
# possible range of UTCTime is 1969-2068 currently
414+
encode_decode_test B(%w{ 17 0D }) + "690908234339Z".b,
415+
OpenSSL::ASN1::UTCTime.new(Time.utc(1969, 9, 8, 23, 43, 39))
416+
rescue OpenSSL::ASN1::ASN1Error
417+
pend "No negative time_t support?"
418+
end
419+
# not implemented
420+
# decode_test B(%w{ 17 11 }) + "500908234339+0930".b,
421+
# OpenSSL::ASN1::UTCTime.new(Time.new(1950, 9, 8, 23, 43, 39, "+09:30"))
422+
# decode_test B(%w{ 17 0F }) + "5009082343-0930".b,
423+
# OpenSSL::ASN1::UTCTime.new(Time.new(1950, 9, 8, 23, 43, 0, "-09:30"))
424+
# assert_raise(OpenSSL::ASN1::ASN1Error) {
425+
# OpenSSL::ASN1.decode(B(%w{ 17 0C }) + "500908234339".b)
426+
# }
427+
# assert_raise(OpenSSL::ASN1::ASN1Error) {
428+
# OpenSSL::ASN1.decode(B(%w{ 17 0D }) + "500908234339Y".b)
429+
# }
430+
end
431+
432+
def test_generalizedtime
433+
encode_decode_test B(%w{ 18 0F }) + "20161208193429Z".b,
434+
OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 34, 29))
435+
encode_decode_test B(%w{ 18 0F }) + "99990908234339Z".b,
436+
OpenSSL::ASN1::GeneralizedTime.new(Time.utc(9999, 9, 8, 23, 43, 39))
437+
# not implemented
438+
# decode_test B(%w{ 18 13 }) + "20161208193439+0930".b,
439+
# OpenSSL::ASN1::GeneralizedTime.new(Time.new(2016, 12, 8, 19, 34, 39, "+09:30"))
440+
# decode_test B(%w{ 18 11 }) + "201612081934-0930".b,
441+
# OpenSSL::ASN1::GeneralizedTime.new(Time.new(2016, 12, 8, 19, 34, 0, "-09:30"))
442+
# decode_test B(%w{ 18 11 }) + "201612081934-09".b,
443+
# OpenSSL::ASN1::GeneralizedTime.new(Time.new(2016, 12, 8, 19, 34, 0, "-09:00"))
444+
# decode_test B(%w{ 18 0D }) + "2016120819.5Z".b,
445+
# OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 30, 0))
446+
# decode_test B(%w{ 18 0D }) + "2016120819,5Z".b,
447+
# OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 30, 0))
448+
# decode_test B(%w{ 18 0F }) + "201612081934.5Z".b,
449+
# OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 34, 30))
450+
# decode_test B(%w{ 18 11 }) + "20161208193439.5Z".b,
451+
# OpenSSL::ASN1::GeneralizedTime.new(Time.utc(2016, 12, 8, 19, 34, 39.5))
452+
# assert_raise(OpenSSL::ASN1::ASN1Error) {
453+
# OpenSSL::ASN1.decode(B(%w{ 18 0D }) + "201612081934Y".b)
454+
# }
455+
end
456+
457+
def test_basic_asn1data
458+
# TODO: Import Issue
459+
# Java::JavaLang::ClassCastException:
460+
# class org.jruby.RubyString cannot be cast to class org.jruby.ext.openssl.ASN1$ASN1Data
461+
# org.jruby.ext.openssl.ASN1$ASN1Data.toASN1TaggedObject(ASN1.java:1408)
462+
# org.jruby.ext.openssl.ASN1$ASN1Data.toASN1(ASN1.java:1383)
463+
# org.jruby.ext.openssl.ASN1$ASN1Data.toDER(ASN1.java:1424)
464+
# org.jruby.ext.openssl.ASN1$ASN1Data.to_der(ASN1.java:1414)
465+
#encode_test B(%w{ 00 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 0, :UNIVERSAL)
466+
#encode_test B(%w{ 01 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 1, :UNIVERSAL)
467+
#encode_decode_test B(%w{ 41 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 1, :APPLICATION)
468+
#encode_decode_test B(%w{ 81 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 1, :CONTEXT_SPECIFIC)
469+
#encode_decode_test B(%w{ C1 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 1, :PRIVATE)
470+
# TODO: Import Issue
471+
# OpenSSL::ASN1::ASN1Error: tag number for :UNIVERSAL too large
472+
# org/jruby/RubyClass.java:942:in `new'
473+
#encode_decode_test B(%w{ 1F 20 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 32, :UNIVERSAL)
474+
#encode_decode_test B(%w{ 1F C0 20 00 }), OpenSSL::ASN1::ASN1Data.new(B(%w{}), 8224, :UNIVERSAL)
475+
# TODO: Import Issue (same as start of this test)
476+
# Java::JavaLang::ClassCastException:
477+
# class org.jruby.RubyString cannot be cast to class org.jruby.ext.openssl.ASN1$ASN1Data
478+
# org.jruby.ext.openssl.ASN1$ASN1Data.toASN1TaggedObject(ASN1.java:1408)
479+
# org.jruby.ext.openssl.ASN1$ASN1Data.toASN1(ASN1.java:1383)
480+
# org.jruby.ext.openssl.ASN1$ASN1Data.toDER(ASN1.java:1424)
481+
# org.jruby.ext.openssl.ASN1$ASN1Data.to_der(ASN1.java:1414)
482+
#encode_decode_test B(%w{ 41 02 AB CD }), OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD }), 1, :APPLICATION)
483+
#encode_decode_test B(%w{ 41 81 80 } + %w{ AB CD } * 64), OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD } * 64), 1, :APPLICATION)
484+
#encode_decode_test B(%w{ 41 82 01 00 } + %w{ AB CD } * 128), OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD } * 128), 1, :APPLICATION)
485+
#encode_decode_test B(%w{ 61 00 }), OpenSSL::ASN1::ASN1Data.new([], 1, :APPLICATION)
486+
#obj = OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD }), 2, :PRIVATE)], 1, :APPLICATION)
487+
#obj.indefinite_length = true
488+
#encode_decode_test B(%w{ 61 80 C2 02 AB CD 00 00 }), obj
489+
#obj = OpenSSL::ASN1::ASN1Data.new([
490+
# OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD }), 2, :PRIVATE),
491+
# OpenSSL::ASN1::EndOfContent.new
492+
#], 1, :APPLICATION)
493+
#obj.indefinite_length = true
494+
#encode_test B(%w{ 61 80 C2 02 AB CD 00 00 }), obj
495+
#obj = OpenSSL::ASN1::ASN1Data.new(B(%w{ AB CD }), 1, :UNIVERSAL)
496+
#obj.indefinite_length = true
497+
# TODO: Import Issue
498+
# <OpenSSL::ASN1::ASN1Error> expected but was <#<Java::JavaLang::ClassCastException: class org.jruby.RubyString cannot be cast to class org.jruby.ext.openssl.ASN1$ASN1Data
499+
#assert_raise(OpenSSL::ASN1::ASN1Error) { obj.to_der }
500+
end
501+
502+
def test_basic_primitive
503+
# TODO: Import Issue
504+
# Java::JavaLang::NullPointerException:
505+
# org.jruby.ext.openssl.ASN1$Primitive.toDER(ASN1.java:1610)
506+
# org.jruby.ext.openssl.ASN1$ASN1Data.to_der(ASN1.java:1414)
507+
# org.jruby.ext.openssl.ASN1$Primitive.to_der(ASN1.java:1522)
508+
#encode_test B(%w{ 00 00 }), OpenSSL::ASN1::Primitive.new(B(%w{}), 0)
509+
# TODO: Import Issue
510+
# <"\x01\x00"> expected but was <"\x01\x01\xFF">
511+
#encode_test B(%w{ 01 00 }), OpenSSL::ASN1::Primitive.new(B(%w{}), 1, nil, :UNIVERSAL)
512+
# <"\x81\x00"> expected but was <"\x01\x01\xFF">
513+
#encode_test B(%w{ 81 00 }), OpenSSL::ASN1::Primitive.new(B(%w{}), 1, nil, :CONTEXT_SPECIFIC)
514+
# <"\x01\x02\xAB\xCD"> expected but was <"\x01\x01\xFF">
515+
#encode_test B(%w{ 01 02 AB CD }), OpenSSL::ASN1::Primitive.new(B(%w{ AB CD }), 1)
516+
# <TypeError> exception was expected but none was thrown.
517+
#assert_raise(TypeError) { OpenSSL::ASN1::Primitive.new([], 1).to_der }
518+
519+
prim = OpenSSL::ASN1::Integer.new(50)
520+
assert_equal false, prim.indefinite_length
521+
assert_not_respond_to prim, :indefinite_length=
522+
end
523+
524+
#
525+
# MRI seems to have some different tests for similar things (constructive
526+
# vs constructed) There may be some duplicated test cases within these but
527+
# for completeness' sake I'm going to include all of MRI's tests below
528+
#
529+
530+
def test_basic_constructed
531+
#octet_string = OpenSSL::ASN1::OctetString.new(B(%w{ AB CD }))
532+
# TODO: Import Issue
533+
# OpenSSL::ASN1::ASN1Error: Constructive shall only be used with indefinite length
534+
#encode_test B(%w{ 20 00 }), OpenSSL::ASN1::Constructive.new([], 0)
535+
#encode_test B(%w{ 21 00 }), OpenSSL::ASN1::Constructive.new([], 1, nil, :UNIVERSAL)
536+
#encode_test B(%w{ A1 00 }), OpenSSL::ASN1::Constructive.new([], 1, nil, :CONTEXT_SPECIFIC)
537+
#encode_test B(%w{ 21 04 04 02 AB CD }), OpenSSL::ASN1::Constructive.new([octet_string], 1)
538+
# Java::JavaLang::UnsupportedOperationException:
539+
# #<OpenSSL::ASN1::Constructive:0x4fc256ec @tag=1, @value=[#<OpenSSL::ASN1::OctetString:0x7fc56d61
540+
# @tag=4, @value="\xAB\xCD", @tag_class=:UNIVERSAL, @tagging=nil, @indefinite_length=false>],
541+
# @tag_class=:CONTEXT_SPECIFIC, @tagging=:EXPLICIT, @indefinite_length=true>
542+
# org.jruby.ext.openssl.ASN1$Constructive.toDER(ASN1.java:1881)
543+
# org.jruby.ext.openssl.ASN1$ASN1Data.to_der(ASN1.java:1414)
544+
# org.jruby.ext.openssl.ASN1$Constructive.to_der(ASN1.java:1858)
545+
#obj = OpenSSL::ASN1::Constructive.new([octet_string], 1)
546+
#obj.indefinite_length = true
547+
#encode_decode_test B(%w{ 21 80 04 02 AB CD 00 00 }), obj
548+
# (see above) Java::JavaLang::UnsupportedOperationException
549+
#obj = OpenSSL::ASN1::Constructive.new([octet_string, OpenSSL::ASN1::EndOfContent.new], 1)
550+
#obj.indefinite_length = true
551+
#encode_test B(%w{ 21 80 04 02 AB CD 00 00 }), obj
552+
end
553+
362554
def test_constructive
363555
oct = OpenSSL::ASN1::OctetString.new("")
364556
assert_equal "\x04\x00", oct.to_der
@@ -391,6 +583,213 @@ def test_constructive
391583
assert_equal "0\x800\x80\x02\x01\x01\x00\x00\x00\x00", outer.to_der
392584
end
393585

586+
def test_prim_explicit_tagging
587+
# TODO: Import Issue
588+
# <"\xA0\x03\x04\x01a"> expected but was <"\x04\x01a">
589+
#oct_str = OpenSSL::ASN1::OctetString.new("a", 0, :EXPLICIT)
590+
#encode_test B(%w{ A0 03 04 01 61 }), oct_str
591+
# <"a\x03\x04\x01a"> expected but was <"\x04\x01a">
592+
oct_str2 = OpenSSL::ASN1::OctetString.new("a", 1, :EXPLICIT, :APPLICATION)
593+
#encode_test B(%w{ 61 03 04 01 61 }), oct_str2
594+
595+
decoded = OpenSSL::ASN1.decode(oct_str2.to_der)
596+
# <:APPLICATION> expected but was <:UNIVERSAL>
597+
#assert_equal :APPLICATION, decoded.tag_class
598+
# <1> expected but was <4>
599+
#assert_equal 1, decoded.tag
600+
assert_equal 1, decoded.value.size
601+
#inner = decoded.value[0]
602+
# <OpenSSL::ASN1::OctetString> expected but was <String>
603+
#assert_equal OpenSSL::ASN1::OctetString, inner.class
604+
# NoMethodError: undefined method `value' for "a":String
605+
#assert_equal B(%w{ 61 }), inner.value
606+
end
607+
608+
def test_prim_implicit_tagging
609+
#int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT)
610+
# TODO: Import Issue
611+
# <"\x80\x01\x01"> expected but was <"\x02\x01\x01">
612+
#encode_test B(%w{ 80 01 01 }), int
613+
#int2 = OpenSSL::ASN1::Integer.new(1, 1, :IMPLICIT, :APPLICATION)
614+
# <"A\x01\x01"> expected but was <"\x02\x01\x01">
615+
#encode_test B(%w{ 41 01 01 }), int2
616+
#decoded = OpenSSL::ASN1.decode(int2.to_der)
617+
# <:APPLICATION> expected but was <:UNIVERSAL>
618+
#assert_equal :APPLICATION, decoded.tag_class
619+
# <1> expected but was <2>
620+
#assert_equal 1, decoded.tag
621+
# <"\x01"> expected but was <#<OpenSSL::BN 1>>
622+
#assert_equal B(%w{ 01 }), decoded.value
623+
624+
# Special behavior: Encoding universal types with non-default 'tag'
625+
# attribute and nil tagging method.
626+
#int3 = OpenSSL::ASN1::Integer.new(1, 1)
627+
# <"\x01\x01\x01"> expected but was <"\x02\x01\x01">
628+
#encode_test B(%w{ 01 01 01 }), int3
629+
end
630+
631+
def test_cons_explicit_tagging
632+
#content = [ OpenSSL::ASN1::PrintableString.new('abc') ]
633+
#seq = OpenSSL::ASN1::Sequence.new(content, 2, :EXPLICIT)
634+
# TODO: Import Issue
635+
# RuntimeError: No message available
636+
#encode_test B(%w{ A2 07 30 05 13 03 61 62 63 }), seq
637+
#seq2 = OpenSSL::ASN1::Sequence.new(content, 3, :EXPLICIT, :APPLICATION)
638+
# RuntimeError: No message available
639+
#encode_test B(%w{ 63 07 30 05 13 03 61 62 63 }), seq2
640+
641+
#content3 = [ OpenSSL::ASN1::PrintableString.new('abc'),
642+
# OpenSSL::ASN1::EndOfContent.new() ]
643+
#seq3 = OpenSSL::ASN1::Sequence.new(content3, 2, :EXPLICIT)
644+
#seq3.indefinite_length = true
645+
# RuntimeError: No message available
646+
#encode_test B(%w{ A2 80 30 80 13 03 61 62 63 00 00 00 00 }), seq3
647+
end
648+
649+
def test_cons_implicit_tagging
650+
#content = [ OpenSSL::ASN1::Null.new(nil) ]
651+
#seq = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT)
652+
# TODO: Import Issue
653+
# <"\xA1\x02\x05\x00"> expected but was <"0\x02\x05\x00">
654+
#encode_test B(%w{ A1 02 05 00 }), seq
655+
#seq2 = OpenSSL::ASN1::Sequence.new(content, 1, :IMPLICIT, :APPLICATION)
656+
# <"a\x02\x05\x00"> expected but was <"0\x02\x05\x00">
657+
#encode_test B(%w{ 61 02 05 00 }), seq2
658+
659+
#content3 = [ OpenSSL::ASN1::Null.new(nil),
660+
# OpenSSL::ASN1::EndOfContent.new() ]
661+
#seq3 = OpenSSL::ASN1::Sequence.new(content3, 1, :IMPLICIT)
662+
#seq3.indefinite_length = true
663+
# <"\xA1\x80\x05\x00\x00\x00"> expected but was <"0\x80\x05\x00\x00\x00">
664+
#encode_test B(%w{ A1 80 05 00 00 00 }), seq3
665+
666+
# Special behavior: Encoding universal types with non-default 'tag'
667+
# attribute and nil tagging method.
668+
#seq4 = OpenSSL::ASN1::Sequence.new([], 1)
669+
# <"!\x00"> expected but was <"0\x00">
670+
#encode_test B(%w{ 21 00 }), seq4
671+
end
672+
673+
def test_octet_string_constructed_tagging
674+
#octets = [ OpenSSL::ASN1::OctetString.new('aaa') ]
675+
#cons = OpenSSL::ASN1::Constructive.new(octets, 0, :IMPLICIT)
676+
# TODO: Import Issues
677+
# OpenSSL::ASN1::ASN1Error: Constructive shall only be used with indefinite length
678+
#encode_test B(%w{ A0 05 04 03 61 61 61 }), cons
679+
680+
#octets = [ OpenSSL::ASN1::OctetString.new('aaa'),
681+
# OpenSSL::ASN1::EndOfContent.new() ]
682+
#cons = OpenSSL::ASN1::Constructive.new(octets, 0, :IMPLICIT)
683+
#cons.indefinite_length = true
684+
# Java::JavaLang::UnsupportedOperationException:
685+
# #<OpenSSL::ASN1::Constructive:0x4a86be01 @tag=0,
686+
# @value=[#<OpenSSL::ASN1::OctetString:0x53b907d9 @tag=4, @value="aaa",
687+
# @tag_class=:UNIVERSAL, @tagging=nil, @indefinite_length=false>,
688+
# #<OpenSSL::ASN1::EndOfContent:0x2c3b0cc8 @tag=0, @value="",
689+
# @tag_class=:UNIVERSAL>], @tag_class=:CONTEXT_SPECIFIC, @tagging=:IMPLICIT, @indefinite_length=true>
690+
#encode_test B(%w{ A0 80 04 03 61 61 61 00 00 }), cons
691+
end
692+
693+
def test_recursive_octet_string_indefinite_length
694+
#octets_sub1 = [ OpenSSL::ASN1::OctetString.new("\x01"),
695+
# OpenSSL::ASN1::EndOfContent.new() ]
696+
#octets_sub2 = [ OpenSSL::ASN1::OctetString.new("\x02"),
697+
# OpenSSL::ASN1::EndOfContent.new() ]
698+
#container1 = OpenSSL::ASN1::Constructive.new(octets_sub1, OpenSSL::ASN1::OCTET_STRING, nil, :UNIVERSAL)
699+
#container1.indefinite_length = true
700+
#container2 = OpenSSL::ASN1::Constructive.new(octets_sub2, OpenSSL::ASN1::OCTET_STRING, nil, :UNIVERSAL)
701+
#container2.indefinite_length = true
702+
#octets3 = OpenSSL::ASN1::OctetString.new("\x03")
703+
704+
#octets = [ container1, container2, octets3,
705+
# OpenSSL::ASN1::EndOfContent.new() ]
706+
#cons = OpenSSL::ASN1::Constructive.new(octets, OpenSSL::ASN1::OCTET_STRING, nil, :UNIVERSAL)
707+
#cons.indefinite_length = true
708+
#raw = B(%w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 })
709+
# TODO: Implict Issue
710+
# Java::JavaLang::UnsupportedOperationException
711+
# org.jruby.ext.openssl.ASN1$Constructive$InternalEncodable.toASN1Primitive(ASN1.java:1961)
712+
# org.jruby.ext.openssl.ASN1$Constructive.octetStringToDER(ASN1.java:1904)
713+
# org.jruby.ext.openssl.ASN1$Constructive.toDER(ASN1.java:1873)
714+
# org.jruby.ext.openssl.ASN1$ASN1Data.to_der(ASN1.java:1414)
715+
#assert_equal(raw, cons.to_der)
716+
# <"$\x80$\x80\x04\x01\x01\x00\x00$\x80\x04\x01\x02\x00\x00\x04\x01\x03\x00\x00"> expected but was <"$\x80\x04\x03\x01\x02\x03\x00\x00">
717+
#assert_equal(raw, OpenSSL::ASN1.decode(raw).to_der)
718+
end
719+
720+
def test_recursive_octet_string_parse
721+
raw = B(%w{ 24 80 24 80 04 01 01 00 00 24 80 04 01 02 00 00 04 01 03 00 00 })
722+
asn1 = OpenSSL::ASN1.decode(raw)
723+
assert_equal(OpenSSL::ASN1::Constructive, asn1.class)
724+
# TODO: Import Issue
725+
# <false> expected but was <true>
726+
#assert_universal(OpenSSL::ASN1::OCTET_STRING, asn1)
727+
assert_equal(true, asn1.indefinite_length)
728+
# <3> expected but was <2>
729+
#assert_equal(3, asn1.value.size)
730+
nested1 = asn1.value[0]
731+
# <OpenSSL::ASN1::Constructive> expected but was <OpenSSL::ASN1::OctetString>
732+
#assert_equal(OpenSSL::ASN1::Constructive, nested1.class)
733+
assert_universal(OpenSSL::ASN1::OCTET_STRING, nested1)
734+
# <true> expected but was <false>
735+
#assert_equal(true, nested1.indefinite_length)
736+
# <1> expected but was <3>
737+
#assert_equal(1, nested1.value.size)
738+
#oct1 = nested1.value[0]
739+
# NoMethodError: undefined method `tag' for "\x01":String
740+
# Did you mean? tap
741+
# src/test/ruby/test_asn1.rb:1286:in `assert_universal'
742+
#assert_universal(OpenSSL::ASN1::OCTET_STRING, oct1)
743+
# NoMethodError: undefined method `indefinite_length' for "\x01":String
744+
#assert_equal(false, oct1.indefinite_length)
745+
#nested2 = asn1.value[1]
746+
# <OpenSSL::ASN1::Constructive> expected but was <OpenSSL::ASN1::EndOfContent>
747+
#assert_equal(OpenSSL::ASN1::Constructive, nested2.class)
748+
# <4> expected but was <0>
749+
#assert_universal(OpenSSL::ASN1::OCTET_STRING, nested2)
750+
# <true> expected but was <nil>
751+
#assert_equal(true, nested2.indefinite_length)
752+
# <1> expected but was <0>
753+
#assert_equal(1, nested2.value.size)
754+
#oct2 = nested2.value[0]
755+
# NoMethodError: undefined method `tag' for nil:NilClass
756+
#assert_universal(OpenSSL::ASN1::OCTET_STRING, oct2)
757+
# NoMethodError: undefined method `indefinite_length' for nil:NilClass
758+
#assert_equal(false, oct2.indefinite_length)
759+
#oct3 = asn1.value[2]
760+
# NoMethodError: undefined method `tag' for nil:NilClass
761+
#assert_universal(OpenSSL::ASN1::OCTET_STRING, oct3)
762+
# NoMethodError: undefined method `indefinite_length' for nil:NilClass
763+
#assert_equal(false, oct3.indefinite_length)
764+
end
765+
766+
def test_decode_constructed_overread
767+
#test = %w{ 31 06 31 02 30 02 05 00 }
768+
## ^ <- invalid
769+
#raw = [test.join].pack("H*")
770+
#ret = []
771+
# <OpenSSL::ASN1::ASN1Error> exception was expected but none was thrown.
772+
#assert_raise(OpenSSL::ASN1::ASN1Error) {
773+
# OpenSSL::ASN1.traverse(raw) { |x| ret << x }
774+
#}
775+
# <2> expected but was <0>
776+
#assert_equal 2, ret.size
777+
# NoMethodError: undefined method `[]' for nil:NilClass
778+
#assert_equal 17, ret[0][6]
779+
#assert_equal 17, ret[1][6]
780+
781+
#test = %w{ 31 80 30 03 00 00 }
782+
## ^ <- invalid
783+
#raw = [test.join].pack("H*")
784+
#ret = []
785+
# <OpenSSL::ASN1::ASN1Error> exception was expected but none was thrown.
786+
#assert_raise(OpenSSL::ASN1::ASN1Error) {
787+
# OpenSSL::ASN1.traverse(raw) { |x| ret << x }
788+
#}
789+
#assert_equal 1, ret.size
790+
#assert_equal 17, ret[0][6]
791+
end
792+
394793
def test_constructive_nesting
395794
seq = OpenSSL::ASN1::Sequence.new([
396795
OpenSSL::ASN1::ObjectId.new('DC'),
@@ -824,6 +1223,13 @@ def test_string_basic
8241223
#test.(30, OpenSSL::ASN1::BMPString)
8251224
end
8261225

1226+
def test_constructive_each
1227+
data = [OpenSSL::ASN1::Integer.new(0), OpenSSL::ASN1::Integer.new(1)]
1228+
seq = OpenSSL::ASN1::Sequence.new data
1229+
1230+
assert_equal data, seq.entries
1231+
end
1232+
8271233
def test_decode_all
8281234
expected = %w{ 02 01 01 02 01 02 02 01 03 }
8291235
raw = [expected.join('')].pack('H*')

0 commit comments

Comments
 (0)
Please sign in to comment.