Completed
Pull Request — master (#769)
by
unknown
10:56
created

scan.TestParseDockerPs   B

Complexity

Conditions 5

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
dl 0
loc 30
rs 8.9093
c 0
b 0
f 0
nop 1
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/alert"
25
	"github.com/future-architect/vuls/config"
26
	"github.com/future-architect/vuls/models"
27
	"time"
28
)
29
30
func TestParseDockerPs(t *testing.T) {
31
	var test = struct {
32
		in       string
33
		expected []config.Container
34
	}{
35
		`c7ca0992415a romantic_goldberg ubuntu:14.04.5
36
f570ae647edc agitated_lovelace centos:latest`,
37
		[]config.Container{
38
			{
39
				ContainerID: "c7ca0992415a",
40
				Name:        "romantic_goldberg",
41
				Image:       "ubuntu:14.04.5",
42
			},
43
			{
44
				ContainerID: "f570ae647edc",
45
				Name:        "agitated_lovelace",
46
				Image:       "centos:latest",
47
			},
48
		},
49
	}
50
51
	r := newRHEL(config.ServerInfo{})
52
	actual, err := r.parseDockerPs(test.in)
53
	if err != nil {
54
		t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
55
		return
56
	}
57
	for i, e := range test.expected {
58
		if !reflect.DeepEqual(e, actual[i]) {
59
			t.Errorf("expected %v, actual %v", e, actual[i])
60
		}
61
	}
62
}
63
64
func TestParseLxdPs(t *testing.T) {
65
	var test = struct {
66
		in       string
67
		expected []config.Container
68
	}{
69
		`+-------+
70
| NAME  |
71
+-------+
72
| test1 |
73
+-------+
74
| test2 |
75
+-------+`,
76
		[]config.Container{
77
			{
78
				ContainerID: "test1",
79
				Name:        "test1",
80
			},
81
			{
82
				ContainerID: "test2",
83
				Name:        "test2",
84
			},
85
		},
86
	}
87
88
	r := newRHEL(config.ServerInfo{})
89
	actual, err := r.parseLxdPs(test.in)
90
	if err != nil {
91
		t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
92
		return
93
	}
94
	for i, e := range test.expected {
95
		if !reflect.DeepEqual(e, actual[i]) {
96
			t.Errorf("expected %v, actual %v", e, actual[i])
97
		}
98
	}
99
}
100
101
func TestParseIp(t *testing.T) {
102
103
	var test = struct {
104
		in        string
105
		expected4 []string
106
		expected6 []string
107
	}{
108
		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
109
1: lo    inet 127.0.0.1/8 scope host lo
110
1: lo    inet6 ::1/128 scope host \       valid_lft forever preferred_lft forever
111
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
112
2: eth0    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
113
2: eth0    inet6 fe80::5054:ff:fe2a:864c/64 scope link \       valid_lft forever preferred_lft forever
114
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
115
3: eth1    inet 192.168.33.11/24 brd 192.168.33.255 scope global eth1
116
3: eth1    inet6 2001:db8::68/64 scope link \       valid_lft forever preferred_lft forever `,
117
		expected4: []string{"10.0.2.15", "192.168.33.11"},
118
		expected6: []string{"2001:db8::68"},
119
	}
120
121
	r := newRHEL(config.ServerInfo{})
122
	actual4, actual6 := r.parseIP(test.in)
123
	if !reflect.DeepEqual(test.expected4, actual4) {
124
		t.Errorf("expected %v, actual %v", test.expected4, actual4)
125
	}
126
	if !reflect.DeepEqual(test.expected6, actual6) {
127
		t.Errorf("expected %v, actual %v", test.expected6, actual6)
128
	}
129
}
130
131
func TestIsAwsInstanceID(t *testing.T) {
132
	var tests = []struct {
133
		in       string
134
		expected bool
135
	}{
136
		{"i-1234567a", true},
137
		{"i-1234567890abcdef0", true},
138
		{"i-1234567890abcdef0000000", true},
139
		{"e-1234567890abcdef0", false},
140
		{"i-1234567890abcdef0 foo bar", false},
141
		{"no data", false},
142
	}
143
144
	r := newAmazon(config.ServerInfo{})
145
	for _, tt := range tests {
146
		actual := r.isAwsInstanceID(tt.in)
147
		if tt.expected != actual {
148
			t.Errorf("expected %t, actual %t, str: %s", tt.expected, actual, tt.in)
149
		}
150
	}
151
}
152
153
func TestParseSystemctlStatus(t *testing.T) {
154
	var tests = []struct {
155
		in  string
156
		out string
157
	}{
158
		{
159
			in: `● NetworkManager.service - Network Manager
160
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
161
   Active: active (running) since Wed 2018-01-10 17:15:39 JST; 2 months 10 days ago
162
     Docs: man:NetworkManager(8)
163
 Main PID: 437 (NetworkManager)
164
   Memory: 424.0K
165
   CGroup: /system.slice/NetworkManager.service
166
           ├─437 /usr/sbin/NetworkManager --no-daemon
167
           └─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`,
168
			out: "NetworkManager.service",
169
		},
170
		{
171
			in:  `Failed to get unit for PID 700: PID 700 does not belong to any loaded unit.`,
172
			out: "",
173
		},
174
	}
175
176
	r := newCentOS(config.ServerInfo{})
177
	for _, tt := range tests {
178
		actual := r.parseSystemctlStatus(tt.in)
179
		if tt.out != actual {
180
			t.Errorf("expected %v, actual %v", tt.out, actual)
181
		}
182
	}
183
}
184
185
func TestUnset(t *testing.T) {
186
187
	var test = struct {
188
		in1      []string
189
		in2      int
190
		expected []string
191
	}{
192
		in1:      []string{"0", "1", "2", "3", "4"},
193
		in2:      0,
194
		expected: []string{"1", "2", "3", "4"},
195
	}
196
	actual := unset(test.in1, test.in2)
197
	if !reflect.DeepEqual(test.expected, actual) {
198
		t.Errorf("expected %v, actual %v", test.expected, actual)
199
	}
200
201
}
202
203
func TestContentConvertVinfo(t *testing.T) {
204
205
	var test = struct {
206
		in       string
207
		expected []models.VulnInfo
208
	}{
209
		in: "{\"4.9.4\":{\"release_date\":\"2018-02-06\",\"changelog_url\"" +
210
			":\"https://codex.wordpress.org/Version_4.9.4\",\"status\"" +
211
			":\"insecure\",\"vulnerabilities\":[{\"id\":9021,\"title\"" +
212
			":\"WordPress <= 4.9.4 - Application Denial of Service (Do" +
213
			"S) (unpatched)\",\"created_at\":\"2018-02-05T16:50:40.000" +
214
			"Z\",\"updated_at\":\"2018-08-29T19:13:04.000Z\",\"publish" +
215
			"ed_date\":\"2018-02-05T00:00:00.000Z\",\"vuln_type\":\"DO" +
216
			"S\",\"references\":{\"url\":[\"https://baraktawily.blogsp" +
217
			"ot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html\"" +
218
			",\"https://github.com/quitten/doser.py\",\"https://thehac" +
219
			"kernews.com/2018/02/wordpress-dos-exploit.html\"],\"cve\"" +
220
			":[\"2018-6389\"]},\"fixed_in\":null}]}}",
221
		expected: []models.VulnInfo{
222
			{
223
				CveID:       "CVE-2018-6389",
224
				Confidences: models.Confidences{},
225
				AffectedPackages: models.PackageStatuses{
226
					models.PackageStatus{
227
						Name:        "",
228
						NotFixedYet: true,
229
						FixState:    "",
230
					},
231
				},
232
				DistroAdvisories: []models.DistroAdvisory{},
233
				CpeURIs:          []string{},
234
				CveContents: models.NewCveContents(
235
					models.CveContent{
236
						Type:          "",
237
						CveID:         "CVE-2018-6389",
238
						Title:         "WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)",
239
						Summary:       "",
240
						Cvss2Score:    0.000000,
241
						Cvss2Vector:   "",
242
						Cvss2Severity: "",
243
						Cvss3Score:    0.000000,
244
						Cvss3Vector:   "",
245
						Cvss3Severity: "",
246
						SourceLink:    "",
247
						Cpes:          []models.Cpe{},
248
						References:    models.References{},
249
						CweIDs:        []string{},
250
						Published:     time.Time{},
251
						LastModified:  time.Time{},
252
						Mitigation:    "",
253
						Optional:      map[string]string{},
254
					},
255
				),
256
				Exploits: []models.Exploit{},
257
				AlertDict: models.AlertDict{
258
					Ja: []alert.Alert{},
259
					En: []alert.Alert{},
260
				},
261
			},
262
		},
263
	}
264
	actual, _ := coreConvertVinfo(test.in)
265
	if !reflect.DeepEqual(test.expected, actual) {
266
		t.Errorf("expected %v, actual %v", test.expected, actual)
267
	}
268
269
}
270
271
/*
272
func TestParseStatus(t *testing.T) {
273
274
	var test = struct {
275
		in        string
276
		expected []WpStatus
277
	}{
278
		in: `"+-----------------+----------+-----------+---------+\r\n
279
		| name            | status   | update    | version |\r\n+-----
280
		------------+----------+-----------+---------+\r\n| twentyfift
281
		een   | inactive | available | 1.9     |\r\n| twentyseventeen
282
		| active   | available | 1.4     |\r\n| twentysixteen   | inac
283
		tive | available | 1.4     |\r\n+-----------------+----------+
284
		-----------+---------+\r\n"`,
285
		expected: []WpStatus{
286
			WpStatus{
287
				Name:    "twentyfifteen",
288
				Status:  "inactive",
289
				Update:  "available",
290
				Version: "1.9",
291
			},
292
			WpStatus{
293
				Name:    "twentyseventeen",
294
				Status:  "active",
295
				Update:  "available",
296
				Version: "1.4",
297
			},
298
			WpStatus{
299
				Name:    "twentysixteen",
300
				Status:  "inactive",
301
				Update:  "available",
302
				Version: "1.4",
303
			},
304
		},
305
	}
306
	actual := parseStatus(test.in)
307
	if !reflect.DeepEqual(test.expected, actual) {
308
		t.Errorf("expected %v, actual %v", test.expected, actual)
309
	}
310
311
}
312
*/
313