1
|
|
|
/* Vuls - Vulnerability Scanner |
2
|
|
|
Copyright (C) 2016 Future Corporation , Japan. |
3
|
|
|
|
4
|
|
|
This program is free software: you can redistribute it and/or modify |
5
|
|
|
it under the terms of the GNU General Public License as published by |
6
|
|
|
the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
(at your option) any later version. |
8
|
|
|
|
9
|
|
|
This program is distributed in the hope that it will be useful, |
10
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
GNU General Public License for more details. |
13
|
|
|
|
14
|
|
|
You should have received a copy of the GNU General Public License |
15
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
package scan |
19
|
|
|
|
20
|
|
|
import ( |
21
|
|
|
"reflect" |
22
|
|
|
"testing" |
23
|
|
|
|
24
|
|
|
"github.com/future-architect/vuls/config" |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
func TestParseDockerPs(t *testing.T) { |
28
|
|
|
var test = struct { |
29
|
|
|
in string |
30
|
|
|
expected []config.Container |
31
|
|
|
}{ |
32
|
|
|
`c7ca0992415a romantic_goldberg ubuntu:14.04.5 |
33
|
|
|
f570ae647edc agitated_lovelace centos:latest`, |
34
|
|
|
[]config.Container{ |
35
|
|
|
{ |
36
|
|
|
ContainerID: "c7ca0992415a", |
37
|
|
|
Name: "romantic_goldberg", |
38
|
|
|
Image: "ubuntu:14.04.5", |
39
|
|
|
}, |
40
|
|
|
{ |
41
|
|
|
ContainerID: "f570ae647edc", |
42
|
|
|
Name: "agitated_lovelace", |
43
|
|
|
Image: "centos:latest", |
44
|
|
|
}, |
45
|
|
|
}, |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
r := newRHEL(config.ServerInfo{}) |
49
|
|
|
actual, err := r.parseDockerPs(test.in) |
50
|
|
|
if err != nil { |
51
|
|
|
t.Errorf("Error occurred. in: %s, err: %s", test.in, err) |
52
|
|
|
return |
53
|
|
|
} |
54
|
|
|
for i, e := range test.expected { |
55
|
|
|
if !reflect.DeepEqual(e, actual[i]) { |
56
|
|
|
t.Errorf("expected %v, actual %v", e, actual[i]) |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
func TestParseLxdPs(t *testing.T) { |
62
|
|
|
var test = struct { |
63
|
|
|
in string |
64
|
|
|
expected []config.Container |
65
|
|
|
}{ |
66
|
|
|
`+-------+ |
67
|
|
|
| NAME | |
68
|
|
|
+-------+ |
69
|
|
|
| test1 | |
70
|
|
|
+-------+ |
71
|
|
|
| test2 | |
72
|
|
|
+-------+`, |
73
|
|
|
[]config.Container{ |
74
|
|
|
{ |
75
|
|
|
ContainerID: "test1", |
76
|
|
|
Name: "test1", |
77
|
|
|
}, |
78
|
|
|
{ |
79
|
|
|
ContainerID: "test2", |
80
|
|
|
Name: "test2", |
81
|
|
|
}, |
82
|
|
|
}, |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
r := newRHEL(config.ServerInfo{}) |
86
|
|
|
actual, err := r.parseLxdPs(test.in) |
87
|
|
|
if err != nil { |
88
|
|
|
t.Errorf("Error occurred. in: %s, err: %s", test.in, err) |
89
|
|
|
return |
90
|
|
|
} |
91
|
|
|
for i, e := range test.expected { |
92
|
|
|
if !reflect.DeepEqual(e, actual[i]) { |
93
|
|
|
t.Errorf("expected %v, actual %v", e, actual[i]) |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
func TestParseIp(t *testing.T) { |
99
|
|
|
|
100
|
|
|
var test = struct { |
101
|
|
|
in string |
102
|
|
|
expected4 []string |
103
|
|
|
expected6 []string |
104
|
|
|
}{ |
105
|
|
|
in: `1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN \ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
106
|
|
|
1: lo inet 127.0.0.1/8 scope host lo |
107
|
|
|
1: lo inet6 ::1/128 scope host \ valid_lft forever preferred_lft forever |
108
|
|
|
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\ link/ether 52:54:00:2a:86:4c brd ff:ff:ff:ff:ff:ff |
109
|
|
|
2: eth0 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 |
110
|
|
|
2: eth0 inet6 fe80::5054:ff:fe2a:864c/64 scope link \ valid_lft forever preferred_lft forever |
111
|
|
|
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\ link/ether 08:00:27:36:76:60 brd ff:ff:ff:ff:ff:ff |
112
|
|
|
3: eth1 inet 192.168.33.11/24 brd 192.168.33.255 scope global eth1 |
113
|
|
|
3: eth1 inet6 2001:db8::68/64 scope link \ valid_lft forever preferred_lft forever `, |
114
|
|
|
expected4: []string{"10.0.2.15", "192.168.33.11"}, |
115
|
|
|
expected6: []string{"2001:db8::68"}, |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
r := newRHEL(config.ServerInfo{}) |
119
|
|
|
actual4, actual6 := r.parseIP(test.in) |
120
|
|
|
if !reflect.DeepEqual(test.expected4, actual4) { |
121
|
|
|
t.Errorf("expected %v, actual %v", test.expected4, actual4) |
122
|
|
|
} |
123
|
|
|
if !reflect.DeepEqual(test.expected6, actual6) { |
124
|
|
|
t.Errorf("expected %v, actual %v", test.expected6, actual6) |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
func TestIsAwsInstanceID(t *testing.T) { |
129
|
|
|
var tests = []struct { |
130
|
|
|
in string |
131
|
|
|
expected bool |
132
|
|
|
}{ |
133
|
|
|
{"i-1234567a", true}, |
134
|
|
|
{"i-1234567890abcdef0", true}, |
135
|
|
|
{"i-1234567890abcdef0000000", true}, |
136
|
|
|
{"e-1234567890abcdef0", false}, |
137
|
|
|
{"i-1234567890abcdef0 foo bar", false}, |
138
|
|
|
{"no data", false}, |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
r := newAmazon(config.ServerInfo{}) |
142
|
|
|
for _, tt := range tests { |
143
|
|
|
actual := r.isAwsInstanceID(tt.in) |
144
|
|
|
if tt.expected != actual { |
145
|
|
|
t.Errorf("expected %t, actual %t, str: %s", tt.expected, actual, tt.in) |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
func TestParseSystemctlStatus(t *testing.T) { |
151
|
|
|
var tests = []struct { |
152
|
|
|
in string |
153
|
|
|
out string |
154
|
|
|
}{ |
155
|
|
|
{ |
156
|
|
|
in: `● NetworkManager.service - Network Manager |
157
|
|
|
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled) |
158
|
|
|
Active: active (running) since Wed 2018-01-10 17:15:39 JST; 2 months 10 days ago |
159
|
|
|
Docs: man:NetworkManager(8) |
160
|
|
|
Main PID: 437 (NetworkManager) |
161
|
|
|
Memory: 424.0K |
162
|
|
|
CGroup: /system.slice/NetworkManager.service |
163
|
|
|
├─437 /usr/sbin/NetworkManager --no-daemon |
164
|
|
|
└─572 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-ens160.pid -lf /var/lib/NetworkManager/dhclient-241ed966-e1c7-4d5c-a6a0-8a6dba457277-ens160.lease -cf /var/lib/NetworkManager/dhclient-ens160.conf ens160`, |
165
|
|
|
out: "NetworkManager.service", |
166
|
|
|
}, |
167
|
|
|
{ |
168
|
|
|
in: `Failed to get unit for PID 700: PID 700 does not belong to any loaded unit.`, |
169
|
|
|
out: "", |
170
|
|
|
}, |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
r := newCentOS(config.ServerInfo{}) |
174
|
|
|
for _, tt := range tests { |
175
|
|
|
actual := r.parseSystemctlStatus(tt.in) |
176
|
|
|
if tt.out != actual { |
177
|
|
|
t.Errorf("expected %v, actual %v", tt.out, actual) |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
func TestUnset(t *testing.T) { |
183
|
|
|
|
184
|
|
|
var test = struct { |
185
|
|
|
in1 []string |
186
|
|
|
in2 int |
187
|
|
|
expected []string |
188
|
|
|
}{ |
189
|
|
|
in1: []string{"0番目","1番目","2番目","3番目","4番目"}, |
190
|
|
|
in2: 0, |
191
|
|
|
expected: []string{"1番目","2番目","3番目","4番目"}, |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
actual := unset(test.in1, test.in2) |
195
|
|
|
if !reflect.DeepEqual(test.expected, actual) { |
196
|
|
|
t.Errorf("expected %v, actual %v", test.expected, actual) |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/* |
202
|
|
|
func TestParseStatus(t *testing.T) { |
203
|
|
|
|
204
|
|
|
var test = struct { |
205
|
|
|
in string |
206
|
|
|
expected []WpStatus |
207
|
|
|
}{ |
208
|
|
|
in: `"+-----------------+----------+-----------+---------+\r\n |
209
|
|
|
| name | status | update | version |\r\n+----- |
210
|
|
|
------------+----------+-----------+---------+\r\n| twentyfift |
211
|
|
|
een | inactive | available | 1.9 |\r\n| twentyseventeen |
212
|
|
|
| active | available | 1.4 |\r\n| twentysixteen | inac |
213
|
|
|
tive | available | 1.4 |\r\n+-----------------+----------+ |
214
|
|
|
-----------+---------+\r\n"`, |
215
|
|
|
expected: []WpStatus{ |
216
|
|
|
WpStatus{ |
217
|
|
|
Name: "twentyfifteen", |
218
|
|
|
Status: "inactive", |
219
|
|
|
Update: "available", |
220
|
|
|
Version: "1.9", |
221
|
|
|
}, |
222
|
|
|
WpStatus{ |
223
|
|
|
Name: "twentyseventeen", |
224
|
|
|
Status: "active", |
225
|
|
|
Update: "available", |
226
|
|
|
Version: "1.4", |
227
|
|
|
}, |
228
|
|
|
WpStatus{ |
229
|
|
|
Name: "twentysixteen", |
230
|
|
|
Status: "inactive", |
231
|
|
|
Update: "available", |
232
|
|
|
Version: "1.4", |
233
|
|
|
}, |
234
|
|
|
}, |
235
|
|
|
} |
236
|
|
|
actual := parseStatus(test.in) |
237
|
|
|
if !reflect.DeepEqual(test.expected, actual) { |
238
|
|
|
t.Errorf("expected %v, actual %v", test.expected, actual) |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
*/ |
243
|
|
|
|