github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/smime/ber/decoder_test.go (about)

     1  package ber
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/asn1"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  type testcase struct {
    12  	in     []byte
    13  	expect []expectation
    14  }
    15  type testcases []testcase
    16  
    17  type expectation struct {
    18  	Kind  Kind
    19  	Class int
    20  	Tag   int
    21  	Value interface{}
    22  }
    23  
    24  func TestBitString(t *testing.T) {
    25  	runTestCases(t, testcases{
    26  		testcase{ // simple
    27  			in: []byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0},
    28  			expect: []expectation{
    29  				{Value, Universal, TagBitString, asn1.BitString{[]byte{0x6E, 0x5D, 0xC0}, 18}},
    30  			},
    31  		},
    32  		testcase{ // "100000" padding
    33  			in: []byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xe0},
    34  			expect: []expectation{
    35  				{Value, Universal, TagBitString, asn1.BitString{[]byte{0x6E, 0x5D, 0xC0}, 18}},
    36  			},
    37  		},
    38  		testcase{ // long form length octets
    39  			in: []byte{0x03, 0x81, 0x04, 0x06, 0x6e, 0x5d, 0xe0},
    40  			expect: []expectation{
    41  				{Value, Universal, TagBitString, asn1.BitString{[]byte{0x6E, 0x5D, 0xC0}, 18}},
    42  			},
    43  		},
    44  		testcase{ // constructed
    45  			in: []byte{0x23, 0x09,
    46  				0x03, 0x03, 0x00, 0x6e, 0x5d,
    47  				0x03, 0x02, 0x06, 0xc0},
    48  			expect: []expectation{
    49  				{Constructed, Universal, TagBitString, nil},
    50  				{Value, Universal, TagBitString, asn1.BitString{[]byte{0x6E, 0x5D}, 16}},
    51  				{Value, Universal, TagBitString, asn1.BitString{[]byte{0xC0}, 2}},
    52  				{EndConstructed, Universal, TagBitString, nil},
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestCMS(t *testing.T) {
    59  	runTestCases(t, testcases{
    60  		testcase{ // simple
    61  			in: []byte{0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04, 0x54,
    62  				0x68, 0x69, 0x73, 0x04, 0x18, 0x20, 0x69, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65,
    63  				0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    64  			expect: []expectation{
    65  				{Constructed, Universal, TagSequence, nil},
    66  				{Value, Universal, TagObjectIdentifier, asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}},
    67  				{Constructed, Context, 0, nil},
    68  				{Constructed, Universal, TagOctetString, nil},
    69  				{Value, Universal, TagOctetString, []byte("This")},
    70  				{Value, Universal, TagOctetString, []byte(" is some sample content.")},
    71  				{EndConstructed, Universal, TagOctetString, nil},
    72  				{EndConstructed, Context, 0, nil},
    73  				{EndConstructed, Universal, TagSequence, nil},
    74  			},
    75  		},
    76  		testcase{ // "100000" padding
    77  			in: []byte{0x30, 0x2b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x1e, 0x04, 0x1c, 0x54, 0x68, 0x69,
    78  				0x73, 0x20, 0x69, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x63, 0x6f, 0x6e,
    79  				0x74, 0x65, 0x6e, 0x74, 0x2e},
    80  			expect: []expectation{
    81  				{Constructed, Universal, TagSequence, nil},
    82  				{Value, Universal, TagObjectIdentifier, asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}},
    83  				{Constructed, Context, 0, nil},
    84  				{Value, Universal, TagOctetString, []byte("This is some sample content.")},
    85  				{EndConstructed, Context, 0, nil},
    86  				{EndConstructed, Universal, TagSequence, nil},
    87  			},
    88  		},
    89  	})
    90  }
    91  
    92  func TestEmptySet(t *testing.T) {
    93  	runTestCases(t, testcases{
    94  		testcase{
    95  			in: []byte{0x31, 0x00},
    96  			expect: []expectation{
    97  				{Constructed, Universal, TagSet, nil},
    98  				{EndConstructed, Universal, TagSet, nil},
    99  			},
   100  		},
   101  	})
   102  }
   103  
   104  func runTestCase(i int, test testcase, t *testing.T) {
   105  	d := NewDecoder(bytes.NewReader(test.in))
   106  	for j, expect := range test.expect {
   107  		token, err := d.Token()
   108  		if err != nil {
   109  			t.Fatalf("error while parsing: %v", err)
   110  		}
   111  
   112  		if token.Kind != expect.Kind ||
   113  			token.Class != expect.Class ||
   114  			token.Tag != expect.Tag {
   115  			t.Fatalf("wrong class/kind/tag at %d %d: got %#v expected %#v", i, j, token, expect)
   116  		}
   117  
   118  		if token.Kind == Value {
   119  			value, err := token.Universal()
   120  			if err != nil {
   121  				t.Fatalf("error at %d %d had %#v: %v", i, j, value, err)
   122  			}
   123  
   124  			if !reflect.DeepEqual(value, expect.Value) {
   125  				t.Fatalf("error at %d %d: got %#v expected %#v", i, j, value, expect.Value)
   126  			}
   127  		}
   128  	}
   129  
   130  	token, err := d.Token()
   131  	if err != io.EOF {
   132  		t.Fatalf("didn't get EOF, got %#v %#v", token, err)
   133  	}
   134  }
   135  
   136  func runTestCases(t *testing.T, cases testcases) {
   137  	for i, test := range cases {
   138  		runTestCase(i, test, t)
   139  	}
   140  }