-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathidentity_test.go
More file actions
99 lines (87 loc) · 3.74 KB
/
identity_test.go
File metadata and controls
99 lines (87 loc) · 3.74 KB
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
// Copyright (c) 2024 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package mtls_test
import (
"bytes"
"testing"
"aead.dev/mtls"
)
func TestParseIdentity(t *testing.T) {
t.Parallel()
for i, test := range parseIdentityTests {
id, err := mtls.ParseIdentity(test.Identity)
if err != nil && !test.ShouldFail {
t.Fatalf("Test %d: failed to parse identity %s: %v", i, test.Identity, err)
}
if err == nil {
if test.ShouldFail {
t.Fatalf("Test %d: parsing %s should have failed", i, test.Identity)
}
if s := id.String(); s != test.Identity {
t.Fatalf("Test %d: identity mismatch: %s != %s", i, s, test.Identity)
}
}
}
}
func TestIdentity_UnmarshalBinary(t *testing.T) {
t.Parallel()
for i, test := range identityUnmarshalBinaryTests {
var id mtls.Identity
if err := id.UnmarshalBinary(test.Identity); err != nil {
if !test.ShouldFail {
t.Fatalf("Test %d: failed to parse identity: %v", i, err)
}
} else {
if test.ShouldFail {
t.Fatalf("Test %d: parsing should have failed", i)
}
b, err := id.MarshalBinary()
if err != nil {
t.Fatalf("Test %d: failed to marshal identity %s: %v", i, id, err)
}
if !bytes.Equal(b, test.Identity) {
t.Fatalf("Test %d: identity mismatch: '%v' != '%v'", i, b, test.Identity)
}
}
}
}
var identityUnmarshalBinaryTests = []struct {
Identity []byte
ShouldFail bool
}{
{Identity: []byte{0x68, 0x31, 0x3a, 0x8, 0xcb, 0xcc, 0x47, 0x25, 0xcc, 0x5f, 0x59, 0xf5, 0xdd, 0x83, 0x8d, 0x96, 0x88, 0x74, 0xd3, 0x19, 0xa6, 0xba, 0x79, 0xb0, 0xfb, 0xf1, 0x44, 0x58, 0x81, 0x50, 0xc0, 0x45, 0xce, 0xf2, 0x28}}, // 0
{Identity: []byte{0x68, 0x31, 0x3a, 0x9e, 0x17, 0xe6, 0xd5, 0x73, 0x1e, 0x64, 0xe8, 0xd3, 0x18, 0xff, 0x32, 0x26, 0x11, 0x74, 0x11, 0xd4, 0x4c, 0xef, 0xc4, 0x23, 0x8e, 0x11, 0x3, 0xf2, 0xbe, 0xf1, 0xd, 0xbb, 0x5f, 0x3d, 0xce}}, // 1
{
Identity: []byte{0x69, 0x31, 0x3a, 0x5a, 0x8a, 0x97, 0x57, 0x49, 0x63, 0xfa, 0x25, 0x2e, 0x1f, 0xa6, 0xc0, 0x50, 0xb1, 0x2e, 0xc9, 0x2b, 0xfa, 0xc8, 0xe5, 0xef, 0x32, 0xa1, 0xab, 0x93, 0x31, 0x29, 0x71, 0x31, 0xd, 0x6f, 0x66},
ShouldFail: true,
}, // 2
{
Identity: []byte{0x68, 0x30, 0x3a, 0x2e, 0x72, 0x18, 0xd6, 0x7d, 0x6b, 0xe8, 0x4e, 0xbc, 0x12, 0x9d, 0x72, 0xa8, 0x5, 0xb4, 0xb7, 0x7c, 0x11, 0xd8, 0xe3, 0x68, 0xa7, 0x7d, 0x57, 0x96, 0x9e, 0x14, 0xd4, 0x5a, 0x97, 0xed, 0x44},
ShouldFail: true,
}, // 3
{
Identity: []byte{0x68, 0x31, 0x3b, 0x58, 0x8e, 0x8d, 0x3a, 0x23, 0x13, 0x2f, 0x44, 0x92, 0xb6, 0xf0, 0x18, 0x44, 0xff, 0x82, 0x66, 0xe7, 0x48, 0xf2, 0x85, 0xc0, 0xc0, 0xa2, 0xe, 0x32, 0xc2, 0x59, 0x48, 0xb7, 0xb9, 0xbb, 0xbe},
ShouldFail: true,
}, // 4
{
Identity: []byte{0x68, 0x31, 0x3a, 0x2f, 0x5b, 0x5, 0xf1, 0x65, 0xcb, 0x3f, 0x9a, 0xe2, 0xb2, 0x53, 0x6c, 0xa6, 0x6, 0x26, 0x8e, 0x16, 0x78, 0xfa, 0x45, 0xa6, 0x65, 0x9e, 0x5f, 0x8f, 0xd4, 0x7d, 0xfc, 0x97, 0x8b, 0x12, 0xdc, 0x57},
ShouldFail: true,
}, // 5
{
Identity: []byte{0x68, 0x31, 0x3a, 0xf, 0x48, 0x81, 0x6c, 0x29, 0xd2, 0x68, 0xe3, 0xbd, 0x71, 0x90, 0x21, 0x67, 0x6c, 0x6b, 0xd8, 0xdb, 0x3, 0x17, 0x84, 0xf5, 0x60, 0xe5, 0x67, 0xb6, 0xb6, 0x4e, 0x9d, 0x45, 0x8e, 0xf7},
ShouldFail: true,
}, // 6
}
var parseIdentityTests = []struct {
Identity string
ShouldFail bool
}{
{Identity: ""}, // 0
{Identity: "h1:01Wao7cTy0ZGRsOmZzjtnrIliF2ozLlwkGa4adRMAYI"}, // 1
{Identity: "h1:9edVjiHvgN6h5L1AmEjUgyapyTdCcrO54-INxKHeDdw"}, // 2
{Identity: "h1:Ks-Q7OFe1W_ktXYdoBmHxLWKICLyGRj1P4wFQoZ2JKA"}, // 3
{Identity: "k1:rEvhGSEz3JBPamsG4x09DQ6qHOXElhGvXJeqRl4wD-c", ShouldFail: true}, // 4
{Identity: "h1:XCYpGV3rHlfQH10EQ85UbomqW08Chx5Z0ZBTFKpfGqQs", ShouldFail: true}, // 5
{Identity: "h1:rakZkIp7uZSoIo1zXl52o75m3GWBK20EntBW4zd-oQ", ShouldFail: true}, // 6
}