| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package pkix |
| 15 |
|
| 16 |
import "testing" |
| 17 |
|
| 18 |
func TestParseDistinguishedName(t *testing.T) { |
| 19 |
// Test cases |
| 20 |
tests := []struct { |
| 21 |
name string |
| 22 |
input string |
| 23 |
wantErr bool |
| 24 |
}{ |
| 25 |
{ |
| 26 |
name: "valid DN", |
| 27 |
input: "C=US,ST=California,O=Notary Project", |
| 28 |
wantErr: false, |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "valid DN with State alias", |
| 32 |
input: "C=US,S=California,O=Notary Project", |
| 33 |
wantErr: false, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "invalid DN", |
| 37 |
input: "C=US,ST=California", |
| 38 |
wantErr: true, |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "invalid DN without State", |
| 42 |
input: "C=US,O=Notary Project", |
| 43 |
wantErr: true, |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "invalid DN without State", |
| 47 |
input: "invalid", |
| 48 |
wantErr: true, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "duplicate RDN attribute", |
| 52 |
input: "C=US,ST=California,O=Notary Project,S=California", |
| 53 |
wantErr: true, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "unsupported DN =#", |
| 57 |
input: "C=US,ST=California,O=Notary Project=#", |
| 58 |
wantErr: true, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "multi-valued RDN attributes", |
| 62 |
input: "OU=Sales+CN=J. Smith,DC=example,DC=net", |
| 63 |
wantErr: true, |
| 64 |
}, |
| 65 |
} |
| 66 |
|
| 67 |
// Run tests |
| 68 |
for _, tt := range tests { |
| 69 |
t.Run(tt.name, func(t *testing.T) { |
| 70 |
_, err := ParseDistinguishedName(tt.input) |
| 71 |
if tt.wantErr != (err != nil) { |
| 72 |
t.Errorf("ParseDistinguishedName() error = %v, wantErr %v", err, tt.wantErr) |
| 73 |
} |
| 74 |
}) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestIsSubsetDN(t *testing.T) { |
| 79 |
// Test cases |
| 80 |
tests := []struct { |
| 81 |
name string |
| 82 |
dn1 map[string]string |
| 83 |
dn2 map[string]string |
| 84 |
want bool |
| 85 |
}{ |
| 86 |
{ |
| 87 |
name: "subset DN", |
| 88 |
dn1: map[string]string{ |
| 89 |
"C": "US", |
| 90 |
"ST": "California", |
| 91 |
"O": "Notary Project", |
| 92 |
}, |
| 93 |
dn2: map[string]string{ |
| 94 |
"C": "US", |
| 95 |
"ST": "California", |
| 96 |
"O": "Notary Project", |
| 97 |
"L": "Los Angeles", |
| 98 |
}, |
| 99 |
want: true, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "not subset DN", |
| 103 |
dn1: map[string]string{ |
| 104 |
"C": "US", |
| 105 |
"ST": "California", |
| 106 |
"O": "Notary Project", |
| 107 |
}, |
| 108 |
dn2: map[string]string{ |
| 109 |
"C": "US", |
| 110 |
"ST": "California", |
| 111 |
"O": "Notary Project 2", |
| 112 |
"L": "Los Angeles", |
| 113 |
"CN": "Notary", |
| 114 |
}, |
| 115 |
want: false, |
| 116 |
}, |
| 117 |
{ |
| 118 |
name: "not subset DN 2", |
| 119 |
dn1: map[string]string{ |
| 120 |
"C": "US", |
| 121 |
"ST": "California", |
| 122 |
"O": "Notary Project", |
| 123 |
"CN": "Notary", |
| 124 |
}, |
| 125 |
dn2: map[string]string{ |
| 126 |
"C": "US", |
| 127 |
"ST": "California", |
| 128 |
"O": "Notary Project", |
| 129 |
"L": "Los Angeles", |
| 130 |
}, |
| 131 |
want: false, |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
// Run tests |
| 136 |
for _, tt := range tests { |
| 137 |
t.Run(tt.name, func(t *testing.T) { |
| 138 |
if got := IsSubsetDN(tt.dn1, tt.dn2); got != tt.want { |
| 139 |
t.Errorf("IsSubsetDN() = %v, want %v", got, tt.want) |
| 140 |
} |
| 141 |
}) |
| 142 |
} |
| 143 |
} |