File: oid_test.go

package info (click to toggle)
golang-github-protonmail-go-crypto 1.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,932 kB
  • sloc: makefile: 10
file content (86 lines) | stat: -rw-r--r-- 1,946 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package encoding

import (
	"bytes"
	"io"
	"testing"

	"github.com/ProtonMail/go-crypto/openpgp/errors"
)

var oidTests = []struct {
	encoded   []byte
	bytes     []byte
	bitLength uint16
	err       error
}{
	{
		encoded:   []byte{0x1, 0x1},
		bytes:     []byte{0x1},
		bitLength: 8,
	},
	{
		encoded:   []byte{0x2, 0x1, 0x2},
		bytes:     []byte{0x1, 0x2},
		bitLength: 16,
	},
	{
		encoded:   []byte{0xa, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa},
		bytes:     []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa},
		bitLength: 80,
	},
	// extension overlap errors
	{
		encoded: []byte{0x0},
		err:     errors.UnsupportedError("reserved for future extensions"),
	},
	{
		encoded: append([]byte{0xff}, make([]byte, 0xff)...),
		err:     errors.UnsupportedError("reserved for future extensions"),
	},
	// EOF error,
	{
		encoded: []byte{},
		err:     io.ErrUnexpectedEOF,
	},
	{
		encoded: []byte{0xa},
		err:     io.ErrUnexpectedEOF,
	},
}

func TestOID(t *testing.T) {
	for i, test := range oidTests {
		oid := new(OID)
		if _, err := oid.ReadFrom(bytes.NewBuffer(test.encoded)); err != nil {
			if !sameError(err, test.err) {
				t.Errorf("#%d: ReadFrom error got:%q", i, err)
			}
			continue
		}
		if b := oid.Bytes(); !bytes.Equal(b, test.bytes) {
			t.Errorf("#%d: bad creation got:%x want:%x", i, b, test.bytes)
		}
		if bl := oid.BitLength(); bl != test.bitLength {
			t.Errorf("#%d: bad BitLength got:%d want:%d", i, bl, test.bitLength)
		}
		if b := oid.EncodedBytes(); !bytes.Equal(b, test.encoded) {
			t.Errorf("#%d: bad encoding got:%x want:%x", i, b, test.encoded)
		}
	}
}

func sameError(err1, err2 error) bool {
	switch {
	case err1 == err2:
		return true
	case err1 == nil, err2 == nil:
		return false
	default:
		return err1.Error() == err2.Error()
	}
}