| 1 |
package naming |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/knqyf263/go-cpe/common" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
any, _ = common.NewLogicalValue("ANY") |
| 11 |
na, _ = common.NewLogicalValue("NA") |
| 12 |
) |
| 13 |
|
| 14 |
func TestBindToURI(t *testing.T) { |
| 15 |
vectors := []struct { |
| 16 |
w common.WellFormedName |
| 17 |
expected string |
| 18 |
}{{ |
| 19 |
w: common.WellFormedName{ |
| 20 |
"part": "a", |
| 21 |
}, |
| 22 |
expected: "cpe:/a", |
| 23 |
}, { |
| 24 |
w: common.WellFormedName{ |
| 25 |
"part": "a", |
| 26 |
"vendor": "microsoft", |
| 27 |
"product": "internet_explorer", |
| 28 |
"version": "8\\.0\\.6001", |
| 29 |
"update": "beta", |
| 30 |
"edition": any, |
| 31 |
"language": "sp2", |
| 32 |
}, |
| 33 |
expected: "cpe:/a:microsoft:internet_explorer:8.0.6001:beta::sp2", |
| 34 |
}, { |
| 35 |
w: common.WellFormedName{ |
| 36 |
"part": "a", |
| 37 |
"vendor": "foo\\$bar", |
| 38 |
"product": "insight", |
| 39 |
"version": "7\\.4\\.0\\.1570", |
| 40 |
"update": na, |
| 41 |
"sw_edition": "online", |
| 42 |
"target_sw": "win2003", |
| 43 |
"target_hw": "x64", |
| 44 |
}, |
| 45 |
expected: "cpe:/a:foo%24bar:insight:7.4.0.1570:-:~~online~win2003~x64~", |
| 46 |
}, { |
| 47 |
w: common.WellFormedName{ |
| 48 |
"part": "a", |
| 49 |
"vendor": 1, |
| 50 |
}, |
| 51 |
expected: "cpe:/a", |
| 52 |
}, { |
| 53 |
w: common.WellFormedName{ |
| 54 |
"part": "a", |
| 55 |
"vendor": "micro??", |
| 56 |
"product": "internet*", |
| 57 |
"version": `\!\"\#\$\%\&\'\(\)\*\+\,`, |
| 58 |
"update": `beta\-\.`, |
| 59 |
"language": `online\/\:\;\<\=\>\?\@\[\\\]\^`, |
| 60 |
}, |
| 61 |
expected: "cpe:/a:micro%01%01:internet%02:%21%22%23%24%25%26%27%28%29%2a%2b%2c:beta-.::online%2f%3a%3b%3c%3d%3e%3f%40%5b%5c%5d%5e", |
| 62 |
}, { |
| 63 |
w: common.WellFormedName{ |
| 64 |
"part": "a", |
| 65 |
"vendor": "\\`", |
| 66 |
"product": `\{\|\}\~\a`, |
| 67 |
}, |
| 68 |
expected: "cpe:/a:%60:%7b%7c%7d%7ea", |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
for i, v := range vectors { |
| 73 |
actual := BindToURI(v.w) |
| 74 |
if actual != v.expected { |
| 75 |
t.Errorf("test %d, Result: got %v, want %v", i, actual, v.expected) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestBindToFS(t *testing.T) { |
| 81 |
vectors := []struct { |
| 82 |
w common.WellFormedName |
| 83 |
expected string |
| 84 |
}{{ |
| 85 |
w: common.WellFormedName{ |
| 86 |
"part": "a", |
| 87 |
}, |
| 88 |
expected: "cpe:2.3:a:*:*:*:*:*:*:*:*:*:*", |
| 89 |
}, { |
| 90 |
w: common.WellFormedName{ |
| 91 |
"part": "a", |
| 92 |
"vendor": "microsoft", |
| 93 |
"product": "internet_explorer", |
| 94 |
"version": "8\\.0\\.6001", |
| 95 |
"update": "beta", |
| 96 |
"edition": any, |
| 97 |
"language": "sp2", |
| 98 |
}, |
| 99 |
expected: "cpe:2.3:a:microsoft:internet_explorer:8.0.6001:beta:*:sp2:*:*:*:*", |
| 100 |
}, { |
| 101 |
w: common.WellFormedName{ |
| 102 |
"part": "a", |
| 103 |
"vendor": "foo\\$bar", |
| 104 |
"product": "insight", |
| 105 |
"version": "7\\.4\\.0\\.1570", |
| 106 |
"update": na, |
| 107 |
"sw_edition": "online", |
| 108 |
"target_sw": "win2003", |
| 109 |
"target_hw": "x64", |
| 110 |
}, |
| 111 |
expected: "cpe:2.3:a:foo\\$bar:insight:7.4.0.1570:-:*:*:online:win2003:x64:*", |
| 112 |
}, { |
| 113 |
w: common.WellFormedName{ |
| 114 |
"part": "a", |
| 115 |
"vendor": 1, |
| 116 |
}, |
| 117 |
expected: "cpe:2.3:a::*:*:*:*:*:*:*:*:*", |
| 118 |
}, |
| 119 |
} |
| 120 |
|
| 121 |
for i, v := range vectors { |
| 122 |
actual := BindToFS(v.w) |
| 123 |
if actual != v.expected { |
| 124 |
t.Errorf("test %d, Result: got %v, want %v", i, actual, v.expected) |
| 125 |
} |
| 126 |
} |
| 127 |
} |