Completed
Pull Request — master (#769)
by
unknown
11:08
created

scan.TestContentConvertVinfos   B

Complexity

Conditions 2

Size

Total Lines 71
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 61
dl 0
loc 71
rs 8.2763
c 0
b 0
f 0
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 TestContentConvertVinfos(t *testing.T) {
186
187
	var test = struct {
188
		in1      *base
189
		in2      string
190
		in3      WpStatus
191
		expected []models.VulnInfo
192
	}{
193
		in1: &base{osPackages: osPackages{Packages: models.Packages{}, VulnInfos: models.VulnInfos{}}},
194
		in2: "{\"twentyfifteen\":{\"friendly_name\":\"Twenty Fifteen\"" +
195
			",\"latest_version\":\"2.3\",\"last_updated\":\"2019-" +
196
			"01-09T00:00:00.000Z\",\"popular\":true,\"vulnerabili" +
197
			"ties\":[{\"id\":7965,\"title\":\"Twenty Fifteen Them" +
198
			"e <= 1.1 - DOM Cross-Site Scripting (XSS)\",\"create" +
199
			"d_at\":\"2015-05-06T17:22:10.000Z\",\"updated_at\":\"" +
200
			"2015-05-15T13:49:28.000Z\",\"published_date\":\"2015" +
201
			"-05-06T00:00:00.000Z\",\"vuln_type\":\"XSS\",\"refer" +
202
			"ences\":{\"url\":[\"https://blog.sucuri.net/2015/05/" +
203
			"jetpack-and-twentyfifteen-vulnerable-to-dom-based-xs" +
204
			"s-millions-of-wordpress-websites-affected-millions-o" +
205
			"f-wordpress-websites-affected.html\",\"http://packet" +
206
			"stormsecurity.com/files/131802/\",\"http://seclists." +
207
			"org/fulldisclosure/2015/May/41\"],\"cve\":[\"2015-34" +
208
			"29\"]},\"fixed_in\":\"1.2\"}]}}",
209
		in3: WpStatus{Name: "twentyfifteen", Status: "inactive", Update: "available", Version: "1.1"},
210
		expected: []models.VulnInfo{
211
			{
212
				CveID:       "CVE-2015-3429",
213
				Confidences: models.Confidences{},
214
				AffectedPackages: models.PackageStatuses{
215
					models.PackageStatus{
216
						Name:        "",
217
						NotFixedYet: false,
218
						FixState:    "",
219
					},
220
				},
221
				DistroAdvisories: []models.DistroAdvisory{},
222
				CpeURIs:          []string{},
223
				CveContents: models.NewCveContents(
224
					models.CveContent{
225
						Type:          "",
226
						CveID:         "CVE-2015-3429",
227
						Title:         "Twenty Fifteen Theme <= 1.1 - DOM Cross-Site Scripting (XSS)",
228
						Summary:       "",
229
						Cvss2Score:    0.000000,
230
						Cvss2Vector:   "",
231
						Cvss2Severity: "",
232
						Cvss3Score:    0.000000,
233
						Cvss3Vector:   "",
234
						Cvss3Severity: "",
235
						SourceLink:    "",
236
						Cpes:          []models.Cpe{},
237
						References:    models.References{},
238
						CweIDs:        []string{},
239
						Published:     time.Time{},
240
						LastModified:  time.Time{},
241
						Mitigation:    "",
242
						Optional:      map[string]string{},
243
					},
244
				),
245
				Exploits: []models.Exploit{},
246
				AlertDict: models.AlertDict{
247
					Ja: []alert.Alert{},
248
					En: []alert.Alert{},
249
				},
250
			},
251
		},
252
	}
253
	actual, _ := contentConvertVinfos(test.in1, test.in2, test.in3)
254
	if reflect.ValueOf(test.expected).Pointer() == reflect.ValueOf(actual).Pointer() {
255
		t.Errorf("expected %v, actual %v", test.expected, actual)
256
	}
257
258
}
259
260
func TestCoreConvertVinfos(t *testing.T) {
261
262
	var test = struct {
263
		in1      *base
264
		in2      string
265
		expected []models.VulnInfo
266
	}{
267
		in1: &base{osPackages: osPackages{Packages: models.Packages{}, VulnInfos: models.VulnInfos{}}},
268
		in2: "{\"4.9.4\":{\"release_date\":\"2018-02-06\",\"changelog_url\"" +
269
			":\"https://codex.wordpress.org/Version_4.9.4\",\"status\"" +
270
			":\"insecure\",\"vulnerabilities\":[{\"id\":9021,\"title\"" +
271
			":\"WordPress <= 4.9.4 - Application Denial of Service (Do" +
272
			"S) (unpatched)\",\"created_at\":\"2018-02-05T16:50:40.000" +
273
			"Z\",\"updated_at\":\"2018-08-29T19:13:04.000Z\",\"publish" +
274
			"ed_date\":\"2018-02-05T00:00:00.000Z\",\"vuln_type\":\"DO" +
275
			"S\",\"references\":{\"url\":[\"https://baraktawily.blogsp" +
276
			"ot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html\"" +
277
			",\"https://github.com/quitten/doser.py\",\"https://thehac" +
278
			"kernews.com/2018/02/wordpress-dos-exploit.html\"],\"cve\"" +
279
			":[\"2018-6389\"]},\"fixed_in\":null}]}}",
280
		expected: []models.VulnInfo{
281
			{
282
				CveID:       "CVE-2018-6389",
283
				Confidences: models.Confidences{},
284
				AffectedPackages: models.PackageStatuses{
285
					models.PackageStatus{
286
						Name:        "",
287
						NotFixedYet: true,
288
						FixState:    "",
289
					},
290
				},
291
				DistroAdvisories: []models.DistroAdvisory{},
292
				CpeURIs:          []string{},
293
				CveContents: models.NewCveContents(
294
					models.CveContent{
295
						Type:          "",
296
						CveID:         "CVE-2018-6389",
297
						Title:         "WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)",
298
						Summary:       "",
299
						Cvss2Score:    0.000000,
300
						Cvss2Vector:   "",
301
						Cvss2Severity: "",
302
						Cvss3Score:    0.000000,
303
						Cvss3Vector:   "",
304
						Cvss3Severity: "",
305
						SourceLink:    "",
306
						Cpes:          []models.Cpe{},
307
						References:    models.References{},
308
						CweIDs:        []string{},
309
						Published:     time.Time{},
310
						LastModified:  time.Time{},
311
						Mitigation:    "",
312
						Optional:      map[string]string{},
313
					},
314
				),
315
				Exploits: []models.Exploit{},
316
				AlertDict: models.AlertDict{
317
					Ja: []alert.Alert{},
318
					En: []alert.Alert{},
319
				},
320
			},
321
		},
322
	}
323
	actual, _ := coreConvertVinfos(test.in1, test.in2)
324
	if reflect.ValueOf(test.expected).Pointer() == reflect.ValueOf(actual).Pointer()  {
325
		t.Errorf("expected %v, actual %v", test.expected, actual)
326
	}
327
328
}
329