| 1 |
package common |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/pkg/errors" |
| 8 |
) |
| 9 |
|
| 10 |
var ( |
| 11 |
any, _ = NewLogicalValue("ANY") |
| 12 |
na, _ = NewLogicalValue("NA") |
| 13 |
) |
| 14 |
|
| 15 |
func TestNewWellFormedName(t *testing.T) { |
| 16 |
vectors := []struct { |
| 17 |
expected WellFormedName |
| 18 |
}{{ |
| 19 |
expected: WellFormedName{ |
| 20 |
"vendor": any, |
| 21 |
"product": any, |
| 22 |
"version": any, |
| 23 |
"update": any, |
| 24 |
"edition": any, |
| 25 |
"language": any, |
| 26 |
"sw_edition": any, |
| 27 |
"target_sw": any, |
| 28 |
"target_hw": any, |
| 29 |
"other": any, |
| 30 |
}, |
| 31 |
}, |
| 32 |
} |
| 33 |
|
| 34 |
for i, v := range vectors { |
| 35 |
actual := NewWellFormedName() |
| 36 |
if !reflect.DeepEqual(actual, v.expected) { |
| 37 |
t.Errorf("test %d, Result: %v, want %v", i, actual, v.expected) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestWellFormedNameInitialize(t *testing.T) { |
| 43 |
vectors := []struct { |
| 44 |
part interface{} |
| 45 |
vendor interface{} |
| 46 |
product interface{} |
| 47 |
version interface{} |
| 48 |
update interface{} |
| 49 |
edition interface{} |
| 50 |
language interface{} |
| 51 |
swEdition interface{} |
| 52 |
targetSw interface{} |
| 53 |
targetHw interface{} |
| 54 |
other interface{} |
| 55 |
expected WellFormedName |
| 56 |
}{{ |
| 57 |
part: "a", |
| 58 |
vendor: "microsoft", |
| 59 |
product: "windows_7", |
| 60 |
version: na, |
| 61 |
update: any, |
| 62 |
edition: any, |
| 63 |
language: any, |
| 64 |
swEdition: any, |
| 65 |
targetSw: any, |
| 66 |
targetHw: any, |
| 67 |
other: any, |
| 68 |
expected: WellFormedName{ |
| 69 |
"part": "a", |
| 70 |
"vendor": "microsoft", |
| 71 |
"product": "windows_7", |
| 72 |
"version": na, |
| 73 |
"update": any, |
| 74 |
"edition": any, |
| 75 |
"language": any, |
| 76 |
"sw_edition": any, |
| 77 |
"target_sw": any, |
| 78 |
"target_hw": any, |
| 79 |
"other": any, |
| 80 |
}, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
for i, v := range vectors { |
| 85 |
wfn := WellFormedName{} |
| 86 |
wfn.Initialize(v.part, v.vendor, v.product, v.version, v.update, v.edition, v.language, |
| 87 |
v.swEdition, v.targetSw, v.targetHw, v.other) |
| 88 |
if !reflect.DeepEqual(wfn, v.expected) { |
| 89 |
t.Errorf("test %d, Result: %v, want %v", i, wfn, v.expected) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestWellFormedNameGet(t *testing.T) { |
| 95 |
vectors := []struct { |
| 96 |
wfn WellFormedName |
| 97 |
attribute string |
| 98 |
expected interface{} |
| 99 |
}{{ |
| 100 |
wfn: WellFormedName{ |
| 101 |
"part": "a", |
| 102 |
}, |
| 103 |
attribute: "part", |
| 104 |
expected: "a", |
| 105 |
}, { |
| 106 |
wfn: WellFormedName{ |
| 107 |
"vendor": "microsoft", |
| 108 |
}, |
| 109 |
attribute: "vendor", |
| 110 |
expected: "microsoft", |
| 111 |
}, { |
| 112 |
wfn: WellFormedName{ |
| 113 |
"product": any, |
| 114 |
}, |
| 115 |
attribute: "product", |
| 116 |
expected: any, |
| 117 |
}, { |
| 118 |
wfn: WellFormedName{ |
| 119 |
"product": any, |
| 120 |
}, |
| 121 |
attribute: "version", |
| 122 |
expected: any, |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
for i, v := range vectors { |
| 127 |
actual := v.wfn.Get(v.attribute) |
| 128 |
if !reflect.DeepEqual(actual, v.expected) { |
| 129 |
t.Errorf("test %d, Result: %v, want %v", i, actual, v.expected) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestWellFormedNameGetString(t *testing.T) { |
| 135 |
vectors := []struct { |
| 136 |
wfn WellFormedName |
| 137 |
attribute string |
| 138 |
expected interface{} |
| 139 |
}{{ |
| 140 |
wfn: WellFormedName{ |
| 141 |
"part": "a", |
| 142 |
}, |
| 143 |
attribute: "part", |
| 144 |
expected: "a", |
| 145 |
}, { |
| 146 |
wfn: WellFormedName{ |
| 147 |
"vendor": "microsoft", |
| 148 |
}, |
| 149 |
attribute: "vendor", |
| 150 |
expected: "microsoft", |
| 151 |
}, { |
| 152 |
wfn: WellFormedName{ |
| 153 |
"product": any, |
| 154 |
}, |
| 155 |
attribute: "product", |
| 156 |
expected: "ANY", |
| 157 |
}, { |
| 158 |
wfn: WellFormedName{ |
| 159 |
"product": any, |
| 160 |
}, |
| 161 |
attribute: "version", |
| 162 |
expected: "ANY", |
| 163 |
}, |
| 164 |
} |
| 165 |
|
| 166 |
for i, v := range vectors { |
| 167 |
actual := v.wfn.GetString(v.attribute) |
| 168 |
if !reflect.DeepEqual(actual, v.expected) { |
| 169 |
t.Errorf("test %d, Result: %v, want %v", i, actual, v.expected) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestWellFormedNameSet(t *testing.T) { |
| 175 |
vectors := []struct { |
| 176 |
wfn WellFormedName |
| 177 |
attribute string |
| 178 |
value interface{} |
| 179 |
expected WellFormedName |
| 180 |
wantErr error |
| 181 |
}{{ |
| 182 |
wfn: WellFormedName{}, |
| 183 |
attribute: "part", |
| 184 |
value: "a", |
| 185 |
expected: WellFormedName{ |
| 186 |
"part": "a", |
| 187 |
}, |
| 188 |
}, { |
| 189 |
wfn: WellFormedName{ |
| 190 |
"part": "a", |
| 191 |
}, |
| 192 |
attribute: "vendor", |
| 193 |
value: any, |
| 194 |
expected: WellFormedName{ |
| 195 |
"part": "a", |
| 196 |
"vendor": any, |
| 197 |
}, |
| 198 |
}, { |
| 199 |
wfn: WellFormedName{}, |
| 200 |
attribute: "vendor", |
| 201 |
value: nil, |
| 202 |
expected: WellFormedName{ |
| 203 |
"vendor": any, |
| 204 |
}, |
| 205 |
}, { |
| 206 |
wfn: WellFormedName{}, |
| 207 |
attribute: "part", |
| 208 |
value: na, // part component cannot be a logical value |
| 209 |
wantErr: ErrIllegalAttribute, |
| 210 |
}, { |
| 211 |
wfn: WellFormedName{}, |
| 212 |
attribute: "part", |
| 213 |
value: "i", // part component must be one of the following: 'a', 'o', 'h' |
| 214 |
wantErr: ErrParse, |
| 215 |
}, { |
| 216 |
wfn: WellFormedName{}, |
| 217 |
attribute: "vendor", |
| 218 |
value: 1, // value must be a logical value or string |
| 219 |
wantErr: ErrIllegalAttribute, |
| 220 |
}, { |
| 221 |
wfn: WellFormedName{}, |
| 222 |
attribute: "invalid", // invalid attribute name |
| 223 |
value: "invalid", |
| 224 |
wantErr: ErrIllegalAttribute, |
| 225 |
}, { |
| 226 |
wfn: WellFormedName{}, |
| 227 |
attribute: "version", |
| 228 |
value: "**1.2.3", |
| 229 |
wantErr: ErrParse, |
| 230 |
}, |
| 231 |
} |
| 232 |
|
| 233 |
for i, v := range vectors { |
| 234 |
err := v.wfn.Set(v.attribute, v.value) |
| 235 |
if errors.Cause(err) != v.wantErr { |
| 236 |
t.Errorf("test %d, Error: %v, want %v", i, errors.Cause(err), v.wantErr) |
| 237 |
} |
| 238 |
if err != nil { |
| 239 |
continue |
| 240 |
} |
| 241 |
if !reflect.DeepEqual(v.wfn, v.expected) { |
| 242 |
t.Errorf("test %d, Result: %v, want %v", i, v.wfn, v.expected) |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func TestWellFormedNameString(t *testing.T) { |
| 248 |
vectors := []struct { |
| 249 |
wfn WellFormedName |
| 250 |
expected string |
| 251 |
}{{ |
| 252 |
wfn: WellFormedName{}, |
| 253 |
expected: `wfn:[part=ANY, vendor=ANY, product=ANY, version=ANY, update=ANY, edition=ANY, language=ANY, sw_edition=ANY, target_sw=ANY, target_hw=ANY, other=ANY]`, |
| 254 |
}, { |
| 255 |
wfn: WellFormedName{ |
| 256 |
"part": "a", |
| 257 |
"vendor": "microsoft", |
| 258 |
"product": "windows_7", |
| 259 |
"version": na, |
| 260 |
"update": any, |
| 261 |
"edition": any, |
| 262 |
"language": any, |
| 263 |
"sw_edition": any, |
| 264 |
"target_sw": any, |
| 265 |
"target_hw": any, |
| 266 |
"other": any, |
| 267 |
}, |
| 268 |
expected: `wfn:[part="a", vendor="microsoft", product="windows_7", version=NA, update=ANY, edition=ANY, language=ANY, sw_edition=ANY, target_sw=ANY, target_hw=ANY, other=ANY]`, |
| 269 |
}, |
| 270 |
} |
| 271 |
|
| 272 |
for i, v := range vectors { |
| 273 |
actual := v.wfn.String() |
| 274 |
if actual != v.expected { |
| 275 |
t.Errorf("test %d, Result: %v, want %v", i, actual, v.expected) |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
func TestValidateStringValue(t *testing.T) { |
| 281 |
vectors := []struct { |
| 282 |
svalue string |
| 283 |
wantErr error |
| 284 |
}{{ |
| 285 |
svalue: "foo", |
| 286 |
}, { |
| 287 |
svalue: "?", |
| 288 |
}, { |
| 289 |
svalue: "??", |
| 290 |
}, { |
| 291 |
svalue: "**foo", |
| 292 |
wantErr: ErrParse, |
| 293 |
}, { |
| 294 |
svalue: "foo**", |
| 295 |
wantErr: ErrParse, |
| 296 |
}, { |
| 297 |
svalue: "foo*bar", |
| 298 |
wantErr: ErrParse, |
| 299 |
}, { |
| 300 |
svalue: "*foo*", |
| 301 |
}, { |
| 302 |
svalue: "foo!bar", |
| 303 |
wantErr: ErrParse, |
| 304 |
}, { |
| 305 |
svalue: "foo/bar", |
| 306 |
wantErr: ErrParse, |
| 307 |
}, { |
| 308 |
svalue: "foo\x07bar", |
| 309 |
wantErr: ErrParse, |
| 310 |
}, { |
| 311 |
svalue: "foo bar", |
| 312 |
wantErr: ErrParse, |
| 313 |
}, { |
| 314 |
svalue: "???foo??", |
| 315 |
}, { |
| 316 |
svalue: "??foo?bar??", |
| 317 |
wantErr: ErrParse, |
| 318 |
}, { |
| 319 |
svalue: "*", |
| 320 |
wantErr: ErrParse, |
| 321 |
}, { |
| 322 |
svalue: `\-`, |
| 323 |
wantErr: ErrParse, |
| 324 |
}, |
| 325 |
} |
| 326 |
|
| 327 |
for i, v := range vectors { |
| 328 |
err := ValidateStringValue(v.svalue) |
| 329 |
if errors.Cause(err) != v.wantErr { |
| 330 |
t.Errorf("test %d, Error: %v, want %v", i, errors.Cause(err), v.wantErr) |
| 331 |
} |
| 332 |
} |
| 333 |
} |