summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/asn1-ber.v1/suite_test.go
blob: ace8e6705e15df5c05c0bbf46a4056cb3b618b5c (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package ber

import (
	"bytes"
	"io"
	"io/ioutil"
	"testing"
)

var errEOF = io.ErrUnexpectedEOF.Error()

// Tests from http://www.strozhevsky.com/free_docs/free_asn1_testsuite_descr.pdf
// Source files and descriptions at http://www.strozhevsky.com/free_docs/TEST_SUITE.zip
var testcases = []struct {
	// File contains the path to the BER-encoded file
	File string
	// Error indicates whether a decoding error is expected
	Error string
	// AbnormalEncoding indicates whether a normalized re-encoding is expected to differ from the original source
	AbnormalEncoding bool
	// IndefiniteEncoding indicates the source file used indefinite-length encoding, so the re-encoding is expected to differ (since the length is known)
	IndefiniteEncoding bool
}{
	// Common blocks
	{File: "tests/tc1.ber", Error: "high-tag-number tag overflow"},
	{File: "tests/tc2.ber", Error: errEOF},
	{File: "tests/tc3.ber", Error: errEOF},
	{File: "tests/tc4.ber", Error: "invalid length byte 0xff"},
	{File: "tests/tc5.ber", Error: "", AbnormalEncoding: true},
	// Real numbers (some expected failures are disabled until support is added)
	{File: "tests/tc6.ber", Error: ""}, // Error: "REAL value +0 must be encoded with zero-length value block"},
	{File: "tests/tc7.ber", Error: ""}, // Error: "REAL value -0 must be encoded as a special value"},
	{File: "tests/tc8.ber", Error: ""},
	{File: "tests/tc9.ber", Error: ""}, // Error: "Bits 6 and 5 of information octet for REAL are equal to 11"
	{File: "tests/tc10.ber", Error: ""},
	{File: "tests/tc11.ber", Error: ""}, // Error: "Incorrect NR form"
	{File: "tests/tc12.ber", Error: ""}, // Error: "Encoding of "special value" not from ASN.1 standard"
	{File: "tests/tc13.ber", Error: errEOF},
	{File: "tests/tc14.ber", Error: errEOF},
	{File: "tests/tc15.ber", Error: ""}, // Error: "Too big value of exponent"
	{File: "tests/tc16.ber", Error: ""}, // Error: "Too big value of mantissa"
	{File: "tests/tc17.ber", Error: ""}, // Error: "Too big values for exponent and mantissa + using of "scaling factor" value"
	// Integers
	{File: "tests/tc18.ber", Error: ""},
	{File: "tests/tc19.ber", Error: errEOF},
	{File: "tests/tc20.ber", Error: ""},
	// Object identifiers
	{File: "tests/tc21.ber", Error: ""},
	{File: "tests/tc22.ber", Error: ""},
	{File: "tests/tc23.ber", Error: errEOF},
	{File: "tests/tc24.ber", Error: ""},
	// Booleans
	{File: "tests/tc25.ber", Error: ""},
	{File: "tests/tc26.ber", Error: ""},
	{File: "tests/tc27.ber", Error: errEOF},
	{File: "tests/tc28.ber", Error: ""},
	{File: "tests/tc29.ber", Error: ""},
	// Null
	{File: "tests/tc30.ber", Error: ""},
	{File: "tests/tc31.ber", Error: errEOF},
	{File: "tests/tc32.ber", Error: ""},
	// Bitstring (some expected failures are disabled until support is added)
	{File: "tests/tc33.ber", Error: ""}, // Error: "Too big value for "unused bits""
	{File: "tests/tc34.ber", Error: errEOF},
	{File: "tests/tc35.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of different from BIT STRING types as internal types for constructive encoding"
	{File: "tests/tc36.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of "unused bits" in internal BIT STRINGs with constructive form of encoding"
	{File: "tests/tc37.ber", Error: ""},
	{File: "tests/tc38.ber", Error: "", IndefiniteEncoding: true},
	{File: "tests/tc39.ber", Error: ""},
	{File: "tests/tc40.ber", Error: ""},
	// Octet string (some expected failures are disabled until support is added)
	{File: "tests/tc41.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of different from OCTET STRING types as internal types for constructive encoding"
	{File: "tests/tc42.ber", Error: errEOF},
	{File: "tests/tc43.ber", Error: errEOF},
	{File: "tests/tc44.ber", Error: ""},
	{File: "tests/tc45.ber", Error: ""},
	// Bitstring
	{File: "tests/tc46.ber", Error: "indefinite length used with primitive type"},
	{File: "tests/tc47.ber", Error: "eoc child not allowed with definite length"},
	{File: "tests/tc48.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of more than 7 "unused bits" in BIT STRING with constrictive encoding form"
}

func TestSuiteDecodePacket(t *testing.T) {
	// Debug = true
	for _, tc := range testcases {
		file := tc.File

		dataIn, err := ioutil.ReadFile(file)
		if err != nil {
			t.Errorf("%s: %v", file, err)
			continue
		}

		// fmt.Printf("%s: decode %d\n", file, len(dataIn))
		packet, err := DecodePacketErr(dataIn)
		if err != nil {
			if tc.Error == "" {
				t.Errorf("%s: unexpected error during DecodePacket: %v", file, err)
			} else if tc.Error != err.Error() {
				t.Errorf("%s: expected error %q during DecodePacket, got %q", file, tc.Error, err)
			}
			continue
		}
		if tc.Error != "" {
			t.Errorf("%s: expected error %q, got none", file, tc.Error)
			continue
		}

		dataOut := packet.Bytes()
		if tc.AbnormalEncoding || tc.IndefiniteEncoding {
			// Abnormal encodings and encodings that used indefinite length should re-encode differently
			if bytes.Equal(dataOut, dataIn) {
				t.Errorf("%s: data should have been re-encoded differently", file)
			}
		} else if !bytes.Equal(dataOut, dataIn) {
			// Make sure the serialized data matches the source
			t.Errorf("%s: data should be the same", file)
		}

		packet, err = DecodePacketErr(dataOut)
		if err != nil {
			t.Errorf("%s: unexpected error: %v", file, err)
			continue
		}

		// Make sure the re-serialized data matches our original serialization
		dataOut2 := packet.Bytes()
		if !bytes.Equal(dataOut, dataOut2) {
			t.Errorf("%s: data should be the same", file)
		}
	}
}

func TestSuiteReadPacket(t *testing.T) {
	for _, tc := range testcases {
		file := tc.File

		dataIn, err := ioutil.ReadFile(file)
		if err != nil {
			t.Errorf("%s: %v", file, err)
			continue
		}

		buffer := bytes.NewBuffer(dataIn)
		packet, err := ReadPacket(buffer)
		if err != nil {
			if tc.Error == "" {
				t.Errorf("%s: unexpected error during ReadPacket: %v", file, err)
			} else if tc.Error != err.Error() {
				t.Errorf("%s: expected error %q during ReadPacket, got %q", file, tc.Error, err)
			}
			continue
		}
		if tc.Error != "" {
			t.Errorf("%s: expected error %q, got none", file, tc.Error)
			continue
		}

		dataOut := packet.Bytes()
		if tc.AbnormalEncoding || tc.IndefiniteEncoding {
			// Abnormal encodings and encodings that used indefinite length should re-encode differently
			if bytes.Equal(dataOut, dataIn) {
				t.Errorf("%s: data should have been re-encoded differently", file)
			}
		} else if !bytes.Equal(dataOut, dataIn) {
			// Make sure the serialized data matches the source
			t.Errorf("%s: data should be the same", file)
		}

		packet, err = DecodePacketErr(dataOut)
		if err != nil {
			t.Errorf("%s: unexpected error: %v", file, err)
			continue
		}

		// Make sure the re-serialized data matches our original serialization
		dataOut2 := packet.Bytes()
		if !bytes.Equal(dataOut, dataOut2) {
			t.Errorf("%s: data should be the same", file)
		}
	}
}