OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package asn1 | 5 package asn1 |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "math/big" | 10 "math/big" |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 | 385 |
386 type TestContextSpecificTags struct { | 386 type TestContextSpecificTags struct { |
387 A int `asn1:"tag:1"` | 387 A int `asn1:"tag:1"` |
388 } | 388 } |
389 | 389 |
390 type TestContextSpecificTags2 struct { | 390 type TestContextSpecificTags2 struct { |
391 A int `asn1:"explicit,tag:1"` | 391 A int `asn1:"explicit,tag:1"` |
392 B int | 392 B int |
393 } | 393 } |
394 | 394 |
| 395 type TestContextSpecificTags3 struct { |
| 396 S string `asn1:"tag:1,utf8"` |
| 397 } |
| 398 |
395 type TestElementsAfterString struct { | 399 type TestElementsAfterString struct { |
396 S string | 400 S string |
397 A, B int | 401 A, B int |
398 } | 402 } |
399 | 403 |
400 type TestBigInt struct { | 404 type TestBigInt struct { |
401 X *big.Int | 405 X *big.Int |
402 } | 406 } |
403 | 407 |
404 type TestSet struct { | 408 type TestSet struct { |
405 Ints []int `asn1:"set"` | 409 Ints []int `asn1:"set"` |
406 } | 410 } |
407 | 411 |
408 var unmarshalTestData = []struct { | 412 var unmarshalTestData = []struct { |
409 in []byte | 413 in []byte |
410 out interface{} | 414 out interface{} |
411 }{ | 415 }{ |
412 {[]byte{0x02, 0x01, 0x42}, newInt(0x42)}, | 416 {[]byte{0x02, 0x01, 0x42}, newInt(0x42)}, |
413 {[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &Te
stObjectIdentifierStruct{[]int{1, 2, 840, 113549}}}, | 417 {[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &Te
stObjectIdentifierStruct{[]int{1, 2, 840, 113549}}}, |
414 {[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93,
192}, 18}}, | 418 {[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93,
192}, 18}}, |
415 {[]byte{0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03
}, &[]int{1, 2, 3}}, | 419 {[]byte{0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03
}, &[]int{1, 2, 3}}, |
416 {[]byte{0x02, 0x01, 0x10}, newInt(16)}, | 420 {[]byte{0x02, 0x01, 0x10}, newInt(16)}, |
417 {[]byte{0x13, 0x04, 't', 'e', 's', 't'}, newString("test")}, | 421 {[]byte{0x13, 0x04, 't', 'e', 's', 't'}, newString("test")}, |
418 {[]byte{0x16, 0x04, 't', 'e', 's', 't'}, newString("test")}, | 422 {[]byte{0x16, 0x04, 't', 'e', 's', 't'}, newString("test")}, |
419 {[]byte{0x16, 0x04, 't', 'e', 's', 't'}, &RawValue{0, 22, false, []byte(
"test"), []byte("\x16\x04test")}}, | 423 {[]byte{0x16, 0x04, 't', 'e', 's', 't'}, &RawValue{0, 22, false, []byte(
"test"), []byte("\x16\x04test")}}, |
420 {[]byte{0x04, 0x04, 1, 2, 3, 4}, &RawValue{0, 4, false, []byte{1, 2, 3,
4}, []byte{4, 4, 1, 2, 3, 4}}}, | 424 {[]byte{0x04, 0x04, 1, 2, 3, 4}, &RawValue{0, 4, false, []byte{1, 2, 3,
4}, []byte{4, 4, 1, 2, 3, 4}}}, |
421 {[]byte{0x30, 0x03, 0x81, 0x01, 0x01}, &TestContextSpecificTags{1}}, | 425 {[]byte{0x30, 0x03, 0x81, 0x01, 0x01}, &TestContextSpecificTags{1}}, |
422 {[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &Te
stContextSpecificTags2{1, 2}}, | 426 {[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &Te
stContextSpecificTags2{1, 2}}, |
| 427 {[]byte{0x30, 0x03, 0x81, 0x01, '@'}, &TestContextSpecificTags3{"@"}}, |
423 {[]byte{0x01, 0x01, 0x00}, newBool(false)}, | 428 {[]byte{0x01, 0x01, 0x00}, newBool(false)}, |
424 {[]byte{0x01, 0x01, 0xff}, newBool(true)}, | 429 {[]byte{0x01, 0x01, 0xff}, newBool(true)}, |
425 {[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02
, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}}, | 430 {[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02
, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}}, |
426 {[]byte{0x30, 0x05, 0x02, 0x03, 0x12, 0x34, 0x56}, &TestBigInt{big.NewIn
t(0x123456)}}, | 431 {[]byte{0x30, 0x05, 0x02, 0x03, 0x12, 0x34, 0x56}, &TestBigInt{big.NewIn
t(0x123456)}}, |
427 {[]byte{0x30, 0x0b, 0x31, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02
, 0x01, 0x03}, &TestSet{Ints: []int{1, 2, 3}}}, | 432 {[]byte{0x30, 0x0b, 0x31, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02
, 0x01, 0x03}, &TestSet{Ints: []int{1, 2, 3}}}, |
428 } | 433 } |
429 | 434 |
430 func TestUnmarshal(t *testing.T) { | 435 func TestUnmarshal(t *testing.T) { |
431 for i, test := range unmarshalTestData { | 436 for i, test := range unmarshalTestData { |
432 pv := reflect.New(reflect.TypeOf(test.out).Elem()) | 437 pv := reflect.New(reflect.TypeOf(test.out).Elem()) |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 _, err = Unmarshal(bs, &res) | 810 _, err = Unmarshal(bs, &res) |
806 if err != nil { | 811 if err != nil { |
807 t.Error(err) | 812 t.Error(err) |
808 } | 813 } |
809 | 814 |
810 if fmt.Sprintf("%v", res) != fmt.Sprintf("%v", test) { | 815 if fmt.Sprintf("%v", res) != fmt.Sprintf("%v", test) { |
811 t.Errorf("incorrect marshal/unmarshal; %v != %v", res, t
est) | 816 t.Errorf("incorrect marshal/unmarshal; %v != %v", res, t
est) |
812 } | 817 } |
813 } | 818 } |
814 } | 819 } |
| 820 |
| 821 type explicitTaggedTimeTest struct { |
| 822 Time time.Time `asn1:"explicit,tag:0"` |
| 823 } |
| 824 |
| 825 var explicitTaggedTimeTestData = []struct { |
| 826 in []byte |
| 827 out explicitTaggedTimeTest |
| 828 }{ |
| 829 {[]byte{0x30, 0x11, 0xa0, 0xf, 0x17, 0xd, '9', '1', '0', '5', '0', '6',
'1', '6', '4', '5', '4', '0', 'Z'}, |
| 830 explicitTaggedTimeTest{time.Date(1991, 05, 06, 16, 45, 40, 0, ti
me.UTC)}}, |
| 831 {[]byte{0x30, 0x17, 0xa0, 0xf, 0x18, 0x13, '2', '0', '1', '0', '0', '1',
'0', '2', '0', '3', '0', '4', '0', '5', '+', '0', '6', '0', '7'}, |
| 832 explicitTaggedTimeTest{time.Date(2010, 01, 02, 03, 04, 05, 0, ti
me.FixedZone("", 6*60*60+7*60))}}, |
| 833 } |
| 834 |
| 835 func TestExplicitTaggedTime(t *testing.T) { |
| 836 // Test that a time.Time will match either tagUTCTime or |
| 837 // tagGeneralizedTime. |
| 838 for i, test := range explicitTaggedTimeTestData { |
| 839 var got explicitTaggedTimeTest |
| 840 _, err := Unmarshal(test.in, &got) |
| 841 if err != nil { |
| 842 t.Errorf("Unmarshal failed at index %d %v", i, err) |
| 843 } |
| 844 if !got.Time.Equal(test.out.Time) { |
| 845 t.Errorf("#%d: got %v, want %v", i, got.Time, test.out.T
ime) |
| 846 } |
| 847 } |
| 848 } |
| 849 |
| 850 type implicitTaggedTimeTest struct { |
| 851 Time time.Time `asn1:"tag:24"` |
| 852 } |
| 853 |
| 854 func TestImplicitTaggedTime(t *testing.T) { |
| 855 // An implicitly tagged time value, that happens to have an implicit |
| 856 // tag equal to a GENERALIZEDTIME, should still be parsed as a UTCTime. |
| 857 // (There's no "timeType" in fieldParameters to determine what type of |
| 858 // time should be expected when implicitly tagged.) |
| 859 der := []byte{0x30, 0x0f, 0x80 | 24, 0xd, '9', '1', '0', '5', '0', '6',
'1', '6', '4', '5', '4', '0', 'Z'} |
| 860 var result implicitTaggedTimeTest |
| 861 if _, err := Unmarshal(der, &result); err != nil { |
| 862 t.Fatalf("Error while parsing: %s", err) |
| 863 } |
| 864 if expected := time.Date(1991, 05, 06, 16, 45, 40, 0, time.UTC); !result
.Time.Equal(expected) { |
| 865 t.Errorf("Wrong result. Got %v, want %v", result.Time, expected) |
| 866 } |
| 867 } |
OLD | NEW |