-
Notifications
You must be signed in to change notification settings - Fork 117
/
patterns_test.go
142 lines (137 loc) · 3.61 KB
/
patterns_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
136
137
138
139
140
141
142
package wappalyzer
import "testing"
func TestParsePattern(t *testing.T) {
tests := []struct {
name string
input string
expectedRegex string
expectedConf int
expectedVer string
expectError bool
}{
{
name: "Basic pattern",
input: "Mage.*",
expectedRegex: "(?i)Mage.{0,250}",
expectedConf: 100,
},
{
name: "With confidence",
input: "Mage.*\\;confidence:50",
expectedRegex: "(?i)Mage.{0,250}",
expectedConf: 50,
},
{
name: "With version",
input: "jquery-([0-9.]+)\\.js\\;version:\\1",
expectedRegex: "(?i)jquery-([0-9.]{1,250})\\.js",
expectedConf: 100,
expectedVer: "\\1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pattern, err := ParsePattern(tt.input)
if (err != nil) != tt.expectError {
t.Errorf("parsePattern() error = %v, expectError %v", err, tt.expectError)
return
}
if err == nil {
if pattern.regex.String() != tt.expectedRegex {
t.Errorf("Expected regex = %s, got %s", tt.expectedRegex, pattern.regex.String())
}
if pattern.Confidence != tt.expectedConf {
t.Errorf("Expected confidence = %d, got %d", tt.expectedConf, pattern.Confidence)
}
if pattern.Version != tt.expectedVer {
t.Errorf("Expected version = %s, got %s", tt.expectedVer, pattern.Version)
}
}
})
}
}
func TestExtractVersion(t *testing.T) {
tests := []struct {
name string
pattern string
target string
expectedVer string
expectError bool
}{
{
name: "Simple version extraction",
pattern: "Mage ([0-9.]+)\\;version:\\1",
target: "Mage 2.3",
expectedVer: "2.3",
expectError: false,
},
{
name: "Version with ternary - true",
pattern: "Mage ([0-9.]+)\\;version:\\1?found:",
target: "Mage 2.3",
expectedVer: "found",
expectError: false,
},
{
name: "Version with ternary - false",
pattern: "Mage\\;version:\\1?:not found",
target: "Mage",
expectedVer: "not found",
expectError: false,
},
{
name: "First version pattern",
pattern: "Mage ([0-9.]+)\\;version:\\1?a:",
target: "Mage 2.3",
expectedVer: "a",
expectError: false,
},
{
name: "Complex pattern",
pattern: "([\\d.]+)?/modernizr(?:\\.([\\d.]+))?.*\\.js\\;version:\\1?\\1:\\2",
target: "2.6.2/modernizr.js",
expectedVer: "2.6.2",
expectError: false,
},
{
name: "Complex pattern - 2",
pattern: "([\\d.]+)?/modernizr(?:\\.([\\d.]+))?.*\\.js\\;version:\\1?\\1:\\2",
target: "/modernizr.2.5.7.js",
expectedVer: "2.5.7",
expectError: false,
},
{
name: "Complex pattern - 3",
pattern: "(?:apache(?:$|/([\\d.]+)|[^/-])|(?:^|\\b)httpd)\\;version:\\1",
target: "apache",
expectError: false,
},
{
name: "Complex pattern - 4",
pattern: "(?:apache(?:$|/([\\d.]+)|[^/-])|(?:^|\\b)httpd)\\;version:\\1",
target: "apache/2.4.29",
expectedVer: "2.4.29",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := ParsePattern(tt.pattern)
if err != nil {
t.Fatal("Failed to parse pattern:", err)
}
match, ver := p.Evaluate(tt.target)
if !match {
t.Errorf("Failed to match pattern %s with target %s", tt.pattern, tt.target)
return
}
if (err != nil) != tt.expectError {
t.Errorf("extractVersion() error = %v, expectError %v", err, tt.expectError)
return
}
if ver != tt.expectedVer {
t.Errorf("Expected version = %s, got %s", tt.expectedVer, ver)
}
})
}
}