Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/reporting/exporters/es/elasticsearch_test.go
4538 views
1
package es
2
3
import (
4
"testing"
5
6
"github.com/go-playground/validator/v10"
7
)
8
9
func TestOptionsValidation(t *testing.T) {
10
validate := validator.New()
11
12
tests := []struct {
13
name string
14
opts Options
15
wantErr bool
16
}{
17
{
18
name: "host only",
19
opts: Options{
20
Host: "elasticsearch.local",
21
Port: 9200,
22
Username: "admin",
23
Password: "secret",
24
IndexName: "nuclei",
25
},
26
wantErr: false,
27
},
28
{
29
name: "ip only",
30
opts: Options{
31
IP: "192.168.1.1",
32
Port: 9200,
33
Username: "admin",
34
Password: "secret",
35
IndexName: "nuclei",
36
},
37
wantErr: false,
38
},
39
{
40
name: "both host and ip",
41
opts: Options{
42
Host: "elasticsearch.local",
43
IP: "192.168.1.1",
44
Port: 9200,
45
Username: "admin",
46
Password: "secret",
47
IndexName: "nuclei",
48
},
49
wantErr: false,
50
},
51
{
52
name: "neither host nor ip",
53
opts: Options{
54
Port: 9200,
55
Username: "admin",
56
Password: "secret",
57
IndexName: "nuclei",
58
},
59
wantErr: true,
60
},
61
{
62
name: "ip with invalid format",
63
opts: Options{
64
IP: "not-an-ip",
65
Port: 9200,
66
Username: "admin",
67
Password: "secret",
68
IndexName: "nuclei",
69
},
70
wantErr: true,
71
},
72
{
73
name: "ipv6 address",
74
opts: Options{
75
IP: "::1",
76
Port: 9200,
77
Username: "admin",
78
Password: "secret",
79
IndexName: "nuclei",
80
},
81
wantErr: false,
82
},
83
{
84
name: "missing username",
85
opts: Options{
86
Host: "elasticsearch.local",
87
Port: 9200,
88
Password: "secret",
89
IndexName: "nuclei",
90
},
91
wantErr: true,
92
},
93
{
94
name: "missing password",
95
opts: Options{
96
Host: "elasticsearch.local",
97
Port: 9200,
98
Username: "admin",
99
IndexName: "nuclei",
100
},
101
wantErr: true,
102
},
103
{
104
name: "missing index name",
105
opts: Options{
106
Host: "elasticsearch.local",
107
Port: 9200,
108
Username: "admin",
109
Password: "secret",
110
},
111
wantErr: true,
112
},
113
{
114
name: "port out of range",
115
opts: Options{
116
Host: "elasticsearch.local",
117
Port: 70000,
118
Username: "admin",
119
Password: "secret",
120
IndexName: "nuclei",
121
},
122
wantErr: true,
123
},
124
{
125
name: "zero port is valid",
126
opts: Options{
127
Host: "elasticsearch.local",
128
Port: 0,
129
Username: "admin",
130
Password: "secret",
131
IndexName: "nuclei",
132
},
133
wantErr: false,
134
},
135
}
136
137
for _, tt := range tests {
138
t.Run(tt.name, func(t *testing.T) {
139
err := validate.Struct(tt.opts)
140
if (err != nil) != tt.wantErr {
141
t.Errorf("validate.Struct() error = %v, wantErr %v", err, tt.wantErr)
142
}
143
})
144
}
145
}
146
147