-
Notifications
You must be signed in to change notification settings - Fork 59
/
inputs_test.go
72 lines (66 loc) · 2.47 KB
/
inputs_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
package alterx
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestInput(t *testing.T) {
testcases := []string{"scanme.co.uk", "https://scanme.co.uk", "scanme.co.uk:443", "https://scanme.co.uk:443"}
expected := &Input{
TLD: "uk",
ETLD: "co.uk",
SLD: "scanme",
Root: "scanme.co.uk",
Suffix: "scanme.co.uk",
Sub: "",
}
for _, v := range testcases {
got, err := NewInput(v)
require.Nilf(t, err, "failed to parse url %v", v)
require.Equal(t, expected, got)
}
}
func TestInputSub(t *testing.T) {
testcases := []struct {
url string
expected *Input
}{
{url: "something.scanme.sh", expected: &Input{TLD: "sh", ETLD: "", SLD: "scanme", Root: "scanme.sh", Sub: "something", Suffix: "scanme.sh"}},
{url: "nested.something.scanme.sh", expected: &Input{TLD: "sh", ETLD: "", SLD: "scanme", Root: "scanme.sh", Sub: "nested", Suffix: "something.scanme.sh", MultiLevel: []string{"something"}}},
{url: "nested.multilevel.scanme.co.uk", expected: &Input{TLD: "uk", ETLD: "co.uk", SLD: "scanme", Root: "scanme.co.uk", Sub: "nested", Suffix: "multilevel.scanme.co.uk", MultiLevel: []string{"multilevel"}}},
{url: "sub.level1.level2.scanme.sh", expected: &Input{TLD: "sh", ETLD: "", SLD: "scanme", Root: "scanme.sh", Sub: "sub", Suffix: "level1.level2.scanme.sh", MultiLevel: []string{"level1", "level2"}}},
{url: "scanme.sh", expected: &Input{TLD: "sh", ETLD: "", Sub: "", Suffix: "scanme.sh", SLD: "scanme", Root: "scanme.sh"}},
}
for _, v := range testcases {
got, err := NewInput(v.url)
require.Nilf(t, err, "failed to parse url %v", v.url)
require.Equal(t, v.expected, got, *v.expected)
}
}
func TestVarCount(t *testing.T) {
testcases := []struct {
statement string
count int
}{
{statement: "{{sub}}.something.{{tld}}", count: 2},
{statement: "{{sub}}.{{sub1}}.{{sub2}}.{{root}}", count: 4},
{statement: "no variables", count: 0},
}
for _, v := range testcases {
require.EqualValues(t, v.count, getVarCount(v.statement), "variable count mismatch")
}
}
func TestExtractVar(t *testing.T) {
// extract all variables from statement
testcases := []struct {
statement string
expected []string
}{
{statement: "{{sub}}.something.{{tld}}", expected: []string{"sub", "tld"}},
{statement: "{{sub}}.{{sub1}}.{{sub2}}.{{root}}", expected: []string{"sub", "sub1", "sub2", "root"}},
{statement: "no variables", expected: nil},
}
for _, v := range testcases {
actual := getAllVars(v.statement)
require.Equal(t, v.expected, actual)
}
}