| 1 | package ber |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "io/ioutil" |
| 8 | "math" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | var errEOF = io.ErrUnexpectedEOF.Error() |
| 13 | |
| 14 | // Tests from http://www.strozhevsky.com/free_docs/free_asn1_testsuite_descr.pdf |
| 15 | // Source files and descriptions at http://www.strozhevsky.com/free_docs/TEST_SUITE.zip |
| 16 | var testCases = []struct { |
| 17 | // File contains the path to the BER-encoded file |
| 18 | File string |
| 19 | // Error indicates whether a decoding error is expected |
| 20 | Error string |
| 21 | // AbnormalEncoding indicates whether a normalized re-encoding is expected to differ from the original source |
| 22 | AbnormalEncoding bool |
| 23 | // IndefiniteEncoding indicates the source file used indefinite-length encoding, so the re-encoding is expected to differ (since the length is known) |
| 24 | IndefiniteEncoding bool |
| 25 | }{ |
| 26 | // Common blocks |
| 27 | {File: "tests/tc1.ber", Error: "high-tag-number tag overflow"}, |
| 28 | {File: "tests/tc2.ber", Error: errEOF}, |
| 29 | {File: "tests/tc3.ber", Error: errEOF}, |
| 30 | {File: "tests/tc4.ber", Error: "invalid length byte 0xff"}, |
| 31 | {File: "tests/tc5.ber", Error: "", AbnormalEncoding: true}, |
| 32 | // Real numbers (some expected failures are disabled until support is added) |
| 33 | {File: "tests/tc6.ber", Error: "REAL value +0 must be encoded with zero-length value block"}, |
| 34 | {File: "tests/tc7.ber", Error: "REAL value -0 must be encoded as a special value"}, |
| 35 | {File: "tests/tc8.ber", Error: `encoding of "special value" must not contain exponent and mantissa`}, |
| 36 | {File: "tests/tc9.ber", Error: "bits 6 and 5 of information octet for REAL are equal to 11"}, |
| 37 | {File: "tests/tc10.ber", Error: ""}, |
| 38 | {File: "tests/tc11.ber", Error: "incorrect NR form"}, |
| 39 | {File: "tests/tc12.ber", Error: `encoding of "special value" not from ASN.1 standard`}, |
| 40 | {File: "tests/tc13.ber", Error: errEOF}, |
| 41 | {File: "tests/tc14.ber", Error: errEOF}, |
| 42 | {File: "tests/tc15.ber", Error: "too big value of exponent"}, |
| 43 | {File: "tests/tc16.ber", Error: "too big value of mantissa"}, |
| 44 | {File: "tests/tc17.ber", Error: "too big value of exponent"}, // Error: "Too big values for exponent and mantissa + using of "scaling factor" value" |
| 45 | // Integers |
| 46 | {File: "tests/tc18.ber", Error: ""}, |
| 47 | {File: "tests/tc19.ber", Error: errEOF}, |
| 48 | {File: "tests/tc20.ber", Error: ""}, |
| 49 | // Object identifiers |
| 50 | {File: "tests/tc21.ber", Error: ""}, |
| 51 | {File: "tests/tc22.ber", Error: ""}, |
| 52 | {File: "tests/tc23.ber", Error: errEOF}, |
| 53 | {File: "tests/tc24.ber", Error: ""}, |
| 54 | // Booleans |
| 55 | {File: "tests/tc25.ber", Error: ""}, |
| 56 | {File: "tests/tc26.ber", Error: ""}, |
| 57 | {File: "tests/tc27.ber", Error: errEOF}, |
| 58 | {File: "tests/tc28.ber", Error: ""}, |
| 59 | {File: "tests/tc29.ber", Error: ""}, |
| 60 | // Null |
| 61 | {File: "tests/tc30.ber", Error: ""}, |
| 62 | {File: "tests/tc31.ber", Error: errEOF}, |
| 63 | {File: "tests/tc32.ber", Error: ""}, |
| 64 | // Bit string (some expected failures are disabled until support is added) |
| 65 | {File: "tests/tc33.ber", Error: ""}, // Error: "Too big value for "unused bits"" |
| 66 | {File: "tests/tc34.ber", Error: errEOF}, |
| 67 | {File: "tests/tc35.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of different from BIT STRING types as internal types for constructive encoding" |
| 68 | {File: "tests/tc36.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of "unused bits" in internal BIT STRINGs with constructive form of encoding" |
| 69 | {File: "tests/tc37.ber", Error: ""}, |
| 70 | {File: "tests/tc38.ber", Error: "", IndefiniteEncoding: true}, |
| 71 | {File: "tests/tc39.ber", Error: ""}, |
| 72 | {File: "tests/tc40.ber", Error: ""}, |
| 73 | // Octet string (some expected failures are disabled until support is added) |
| 74 | {File: "tests/tc41.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of different from OCTET STRING types as internal types for constructive encoding" |
| 75 | {File: "tests/tc42.ber", Error: errEOF}, |
| 76 | {File: "tests/tc43.ber", Error: errEOF}, |
| 77 | {File: "tests/tc44.ber", Error: ""}, |
| 78 | {File: "tests/tc45.ber", Error: ""}, |
| 79 | // Bit string |
| 80 | {File: "tests/tc46.ber", Error: "indefinite length used with primitive type"}, |
| 81 | {File: "tests/tc47.ber", Error: "eoc child not allowed with definite length"}, |
| 82 | {File: "tests/tc48.ber", Error: "", IndefiniteEncoding: true}, // Error: "Using of more than 7 "unused bits" in BIT STRING with constrictive encoding form" |
| 83 | {File: "tests/tc49.ber", Error: ""}, |
| 84 | {File: "tests/tc50.ber", Error: is64bit("length cannot be less than -1", "long-form length overflow")}, |
| 85 | {File: "tests/tc51.ber", Error: is64bit(fmt.Sprintf("length 206966894640 greater than maximum %v", MaxPacketLengthBytes), "long-form length overflow")}, |
| 86 | } |
| 87 | |
| 88 | func is64bit(a, b string) string { |
| 89 | maxInt64 := int64(math.MaxInt64) |
| 90 | length := int(maxInt64) |
| 91 | if int64(length) != maxInt64 { |
| 92 | return b |
| 93 | } |
| 94 | return a |
| 95 | } |
| 96 | |
| 97 | func TestSuiteDecodePacket(t *testing.T) { |
| 98 | // Debug = true |
| 99 | for _, tc := range testCases { |
| 100 | file := tc.File |
| 101 | |
| 102 | dataIn, err := ioutil.ReadFile(file) |
| 103 | if err != nil { |
| 104 | t.Errorf("%s: %v", file, err) |
| 105 | continue |
| 106 | } |
| 107 | |
| 108 | // fmt.Printf("%s: decode %d\n", file, len(dataIn)) |
| 109 | packet, err := DecodePacketErr(dataIn) |
| 110 | if err != nil { |
| 111 | if tc.Error == "" { |
| 112 | t.Errorf("%s: unexpected error during DecodePacket: %v", file, err) |
| 113 | } else if tc.Error != err.Error() { |
| 114 | t.Errorf("%s: expected error %q during DecodePacket, got %q", file, tc.Error, err) |
| 115 | } |
| 116 | continue |
| 117 | } |
| 118 | if tc.Error != "" { |
| 119 | t.Errorf("%s: expected error %q, got none", file, tc.Error) |
| 120 | continue |
| 121 | } |
| 122 | |
| 123 | dataOut := packet.Bytes() |
| 124 | if tc.AbnormalEncoding || tc.IndefiniteEncoding { |
| 125 | // Abnormal encodings and encodings that used indefinite length should re-encode differently |
| 126 | if bytes.Equal(dataOut, dataIn) { |
| 127 | t.Errorf("%s: data should have been re-encoded differently", file) |
| 128 | } |
| 129 | } else if !bytes.Equal(dataOut, dataIn) { |
| 130 | // Make sure the serialized data matches the source |
| 131 | t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataIn, dataOut) |
| 132 | } |
| 133 | |
| 134 | packet, err = DecodePacketErr(dataOut) |
| 135 | if err != nil { |
| 136 | t.Errorf("%s: unexpected error: %v", file, err) |
| 137 | continue |
| 138 | } |
| 139 | |
| 140 | // Make sure the re-serialized data matches our original serialization |
| 141 | dataOut2 := packet.Bytes() |
| 142 | if !bytes.Equal(dataOut, dataOut2) { |
| 143 | t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataOut, dataOut2) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestSuiteReadPacket(t *testing.T) { |
| 149 | for _, tc := range testCases { |
| 150 | file := tc.File |
| 151 | |
| 152 | dataIn, err := ioutil.ReadFile(file) |
| 153 | if err != nil { |
| 154 | t.Errorf("%s: %v", file, err) |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | buffer := bytes.NewBuffer(dataIn) |
| 159 | packet, err := ReadPacket(buffer) |
| 160 | if err != nil { |
| 161 | if tc.Error == "" { |
| 162 | t.Errorf("%s: unexpected error during ReadPacket: %v", file, err) |
| 163 | } else if tc.Error != err.Error() { |
| 164 | t.Errorf("%s: expected error %q during ReadPacket, got %q", file, tc.Error, err) |
| 165 | } |
| 166 | continue |
| 167 | } |
| 168 | if tc.Error != "" { |
| 169 | t.Errorf("%s: expected error %q, got none", file, tc.Error) |
| 170 | continue |
| 171 | } |
| 172 | |
| 173 | dataOut := packet.Bytes() |
| 174 | if tc.AbnormalEncoding || tc.IndefiniteEncoding { |
| 175 | // Abnormal encodings and encodings that used indefinite length should re-encode differently |
| 176 | if bytes.Equal(dataOut, dataIn) { |
| 177 | t.Errorf("%s: data should have been re-encoded differently", file) |
| 178 | } |
| 179 | } else if !bytes.Equal(dataOut, dataIn) { |
| 180 | // Make sure the serialized data matches the source |
| 181 | t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataIn, dataOut) |
| 182 | } |
| 183 | |
| 184 | packet, err = DecodePacketErr(dataOut) |
| 185 | if err != nil { |
| 186 | t.Errorf("%s: unexpected error: %v", file, err) |
| 187 | continue |
| 188 | } |
| 189 | |
| 190 | // Make sure the re-serialized data matches our original serialization |
| 191 | dataOut2 := packet.Bytes() |
| 192 | if !bytes.Equal(dataOut, dataOut2) { |
| 193 | t.Errorf("%s: data should be the same\nwant: %#v\ngot: %#v", file, dataOut, dataOut2) |
| 194 | } |
| 195 | } |
| 196 | } |