| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package encoding |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"io" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 13 |
) |
| 14 |
|
| 15 |
var oidTests = []struct { |
| 16 |
encoded []byte |
| 17 |
bytes []byte |
| 18 |
bitLength uint16 |
| 19 |
err error |
| 20 |
}{ |
| 21 |
{ |
| 22 |
encoded: []byte{0x1, 0x1}, |
| 23 |
bytes: []byte{0x1}, |
| 24 |
bitLength: 8, |
| 25 |
}, |
| 26 |
{ |
| 27 |
encoded: []byte{0x2, 0x1, 0x2}, |
| 28 |
bytes: []byte{0x1, 0x2}, |
| 29 |
bitLength: 16, |
| 30 |
}, |
| 31 |
{ |
| 32 |
encoded: []byte{0xa, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}, |
| 33 |
bytes: []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}, |
| 34 |
bitLength: 80, |
| 35 |
}, |
| 36 |
// extension overlap errors |
| 37 |
{ |
| 38 |
encoded: []byte{0x0}, |
| 39 |
err: errors.UnsupportedError("reserved for future extensions"), |
| 40 |
}, |
| 41 |
{ |
| 42 |
encoded: append([]byte{0xff}, make([]byte, 0xff)...), |
| 43 |
err: errors.UnsupportedError("reserved for future extensions"), |
| 44 |
}, |
| 45 |
// EOF error, |
| 46 |
{ |
| 47 |
encoded: []byte{}, |
| 48 |
err: io.ErrUnexpectedEOF, |
| 49 |
}, |
| 50 |
{ |
| 51 |
encoded: []byte{0xa}, |
| 52 |
err: io.ErrUnexpectedEOF, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
func TestOID(t *testing.T) { |
| 57 |
for i, test := range oidTests { |
| 58 |
oid := new(OID) |
| 59 |
if _, err := oid.ReadFrom(bytes.NewBuffer(test.encoded)); err != nil { |
| 60 |
if !sameError(err, test.err) { |
| 61 |
t.Errorf("#%d: ReadFrom error got:%q", i, err) |
| 62 |
} |
| 63 |
continue |
| 64 |
} |
| 65 |
if b := oid.Bytes(); !bytes.Equal(b, test.bytes) { |
| 66 |
t.Errorf("#%d: bad creation got:%x want:%x", i, b, test.bytes) |
| 67 |
} |
| 68 |
if bl := oid.BitLength(); bl != test.bitLength { |
| 69 |
t.Errorf("#%d: bad BitLength got:%d want:%d", i, bl, test.bitLength) |
| 70 |
} |
| 71 |
if b := oid.EncodedBytes(); !bytes.Equal(b, test.encoded) { |
| 72 |
t.Errorf("#%d: bad encoding got:%x want:%x", i, b, test.encoded) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func sameError(err1, err2 error) bool { |
| 78 |
switch { |
| 79 |
case err1 == err2: |
| 80 |
return true |
| 81 |
case err1 == nil, err2 == nil: |
| 82 |
return false |
| 83 |
default: |
| 84 |
return err1.Error() == err2.Error() |
| 85 |
} |
| 86 |
} |