-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistinguished_test.go
135 lines (118 loc) · 3.75 KB
/
distinguished_test.go
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
package dn
import (
"strings"
"testing"
)
// Test basic functions
type basetest struct {
in string
shouldbe string
}
func Test1DN(t *testing.T) {
tvals := []basetest{
basetest{in: "CN=Ethel the Aardvark,OU=Australia,O=Marsupials,C=OZ",
shouldbe: "CN=Ethel the Aardvark|OU=Australia|O=Marsupials|C=OZ"},
basetest{in: " CN=Ethel the Aardvark; OU=Australia ,O=Marsupials;C=OZ",
shouldbe: "CN=Ethel the Aardvark|OU=Australia|O=Marsupials|C=OZ"},
basetest{in: "Ethel the Aardvark,OU=Australia,O=Marsupials,C=OZ",
shouldbe: "Ethel the Aardvark|OU=Australia|O=Marsupials|C=OZ"},
basetest{in: ",OU=Australia,O= ,C=OZ",
shouldbe: "OU=Australia|O=|C=OZ"},
basetest{in: ",;,;,,",
shouldbe: ""},
}
for i, test := range tvals {
out := strings.Join(Canonical2Strings(test.in), "|")
if out != test.shouldbe {
t.Errorf("%d: Basic split of %s failed\nProduced: %s\nShould have been: %s",
i, test.in, out, test.shouldbe)
}
}
}
func Test2DN(t *testing.T) {
tvals := []basetest{
basetest{in: "CN=Ethel the Aardvark,OU=Australia,O=Marsupials,C=OZ",
shouldbe: "CN=Ethel the Aardvark; OU=Australia; O=Marsupials; C=OZ"},
basetest{in: " CN= Ethel the Aardvark ; OU = Australia ,O= Marsupials,C=OZ",
shouldbe: "CN=Ethel the Aardvark; OU=Australia; O=Marsupials; C=OZ"},
basetest{in: "Ethel the Aardvark,OU=Australia,O=Marsupials,C=OZ",
shouldbe: "Ethel the Aardvark; OU=Australia; O=Marsupials; C=OZ"},
basetest{in: ",OU=Australia,O= ,C=OZ,=Sunny",
shouldbe: "OU=Australia; C=OZ"},
basetest{in: ",;,;,,",
shouldbe: ""},
basetest{in: "",
shouldbe: ""},
}
// This time test complete
for i, test := range tvals {
dn := New(test.in)
out := dn.String()
if out != test.shouldbe {
t.Errorf("%d: Complete parse of %s failed\nProduced: %s\nShould have been: %s",
i, test.in, out, test.shouldbe)
}
}
}
// Test GetValues
type gvtest struct {
in string
key string
shouldbe string
}
func TestGetValues(t *testing.T) {
tvals := []gvtest{
gvtest{in: "CN=Ethel the Aardvark,OU=Australia,O=Marsupials,C=OZ", key: "OU",
shouldbe: "Australia"},
gvtest{in: "CN=Ethel the Aardvark; OU=Four Feets; OU=Australia; OU=Cuteness; C=OZ", key: "OU",
shouldbe: "Four Feets|Australia|Marsupials|Cuteness"},
gvtest{in: "CN=Ethel the Aardvark,job=Quantity Surveying", key: "job",
shouldbe: "Quantity Surveying"},
gvtest{in: "CN=Ethel the Aardvark,job=Quantity Surveying,[email protected]", key: "email",
shouldbe: "[email protected]"},
}
for i, test := range tvals {
dn := New(test.in)
out := strings.Join(dn.GetValues(test.key), "|")
if out != test.shouldbe {
t.Errorf("%d: Complete parse of %s failed\nProduced: %s\nShould have been: %s",
i, test.in, out, test.shouldbe)
}
}
}
// Test CommonName
type cntest struct {
in string
shouldbe string
shouldbeerr bool
}
func TestCommonName(t *testing.T) {
tvals := []cntest{
cntest{in: "CN=Ethel the Aardvark; OU=Australia; O=Marsupials; C=OZ",
shouldbe: "Ethel the Aardvark", shouldbeerr: false},
cntest{in: "CN=Ethel the Aardvark; OU=Australia; O=Marsupials; C=OZ",
shouldbe: "Ethel the Aardvark", shouldbeerr: false},
}
for i, test := range tvals {
dn := New(test.in)
out, err := dn.CommonName()
if err != nil {
if !test.shouldbeerr {
t.Errorf("%d: CommonName of %s produced false error\nError: %s\n",
i, test.in, err.Error())
} else {
continue // Correctly produced an error
}
} else {
if test.shouldbeerr {
t.Errorf("%d: CommonName of %s didn't produce an error, when it should\nProduced: %s\n",
i, test.in, out)
} else {
if out != test.shouldbe {
t.Errorf("%d: CommonName of %s failed\nProduced: %s\nShould have been: %s",
i, test.in, out, test.shouldbe)
}
}
}
}
}