File: any_set_test.go

package info (click to toggle)
golang-github-github-smimesign 0.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 600 kB
  • sloc: makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
package protocol

import (
	"bytes"
	"encoding/asn1"
	"encoding/hex"
	"testing"
)

func TestAnySet(t *testing.T) {
	// OpenSSL::ASN1::Set.new([
	//   OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(5)),
	//   OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(6))
	// ])
	der := []byte{49, 6, 2, 1, 5, 2, 1, 6}

	var rv asn1.RawValue
	if rest, err := asn1.Unmarshal(der, &rv); err != nil {
		t.Fatal(err)
	} else if len(rest) > 0 {
		t.Fatal("trailing data")
	}

	as, err := DecodeAnySet(rv)
	if err != nil {
		t.Fatal(err)
	}

	if len(as.Elements) != 2 {
		t.Fatal("bad decoded values")
	}

	var i int
	if rest, err := asn1.Unmarshal(as.Elements[0].FullBytes, &i); err != nil {
		t.Fatal(err)
	} else if len(rest) > 0 {
		t.Fatal("trailing data")
	}
	if i != 5 {
		t.Fatalf("bad decoded value: %d", i)
	}

	if rest, err := asn1.Unmarshal(as.Elements[1].FullBytes, &i); err != nil {
		t.Fatal(err)
	} else if len(rest) > 0 {
		t.Fatal("trailing data")
	}
	if i != 6 {
		t.Fatalf("bad decoded value: %d", i)
	}

	var rv2 asn1.RawValue
	if err := as.Encode(&rv2); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(rv.FullBytes, rv2.FullBytes) {
		t.Fatal(hex.EncodeToString(rv2.FullBytes), " != ", hex.EncodeToString(rv.FullBytes))
	}
}