Completed
Pull Request — master (#769)
by
unknown
12:17 queued 01:04
created

scan/base_test.go   A

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 149
dl 0
loc 259
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A scan.TestParseIp 0 27 3
B scan.TestParseLxdPs 0 33 5
B scan.TestParseDockerPs 0 30 5
B scan.TestContentConvertVinfos 0 59 4
A scan.TestParseSystemctlStatus 0 28 4
A scan.TestIsAwsInstanceID 0 18 4
A scan.TestCoreConvertVinfos 0 29 2
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
	"github.com/future-architect/vuls/models"
26
	"github.com/k0kubun/pp"
27
)
28
29
func TestParseDockerPs(t *testing.T) {
30
	var test = struct {
31
		in       string
32
		expected []config.Container
33
	}{
34
		`c7ca0992415a romantic_goldberg ubuntu:14.04.5
35
f570ae647edc agitated_lovelace centos:latest`,
36
		[]config.Container{
37
			{
38
				ContainerID: "c7ca0992415a",
39
				Name:        "romantic_goldberg",
40
				Image:       "ubuntu:14.04.5",
41
			},
42
			{
43
				ContainerID: "f570ae647edc",
44
				Name:        "agitated_lovelace",
45
				Image:       "centos:latest",
46
			},
47
		},
48
	}
49
50
	r := newRHEL(config.ServerInfo{})
51
	actual, err := r.parseDockerPs(test.in)
52
	if err != nil {
53
		t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
54
		return
55
	}
56
	for i, e := range test.expected {
57
		if !reflect.DeepEqual(e, actual[i]) {
58
			t.Errorf("expected %v, actual %v", e, actual[i])
59
		}
60
	}
61
}
62
63
func TestParseLxdPs(t *testing.T) {
64
	var test = struct {
65
		in       string
66
		expected []config.Container
67
	}{
68
		`+-------+
69
| NAME  |
70
+-------+
71
| test1 |
72
+-------+
73
| test2 |
74
+-------+`,
75
		[]config.Container{
76
			{
77
				ContainerID: "test1",
78
				Name:        "test1",
79
			},
80
			{
81
				ContainerID: "test2",
82
				Name:        "test2",
83
			},
84
		},
85
	}
86
87
	r := newRHEL(config.ServerInfo{})
88
	actual, err := r.parseLxdPs(test.in)
89
	if err != nil {
90
		t.Errorf("Error occurred. in: %s, err: %s", test.in, err)
91
		return
92
	}
93
	for i, e := range test.expected {
94
		if !reflect.DeepEqual(e, actual[i]) {
95
			t.Errorf("expected %v, actual %v", e, actual[i])
96
		}
97
	}
98
}
99
100
func TestParseIp(t *testing.T) {
101
102
	var test = struct {
103
		in        string
104
		expected4 []string
105
		expected6 []string
106
	}{
107
		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
108
1: lo    inet 127.0.0.1/8 scope host lo
109
1: lo    inet6 ::1/128 scope host \       valid_lft forever preferred_lft forever
110
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
111
2: eth0    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
112
2: eth0    inet6 fe80::5054:ff:fe2a:864c/64 scope link \       valid_lft forever preferred_lft forever
113
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
114
3: eth1    inet 192.168.33.11/24 brd 192.168.33.255 scope global eth1
115
3: eth1    inet6 2001:db8::68/64 scope link \       valid_lft forever preferred_lft forever `,
116
		expected4: []string{"10.0.2.15", "192.168.33.11"},
117
		expected6: []string{"2001:db8::68"},
118
	}
119
120
	r := newRHEL(config.ServerInfo{})
121
	actual4, actual6 := r.parseIP(test.in)
122
	if !reflect.DeepEqual(test.expected4, actual4) {
123
		t.Errorf("expected %v, actual %v", test.expected4, actual4)
124
	}
125
	if !reflect.DeepEqual(test.expected6, actual6) {
126
		t.Errorf("expected %v, actual %v", test.expected6, actual6)
127
	}
128
}
129
130
func TestIsAwsInstanceID(t *testing.T) {
131
	var tests = []struct {
132
		in       string
133
		expected bool
134
	}{
135
		{"i-1234567a", true},
136
		{"i-1234567890abcdef0", true},
137
		{"i-1234567890abcdef0000000", true},
138
		{"e-1234567890abcdef0", false},
139
		{"i-1234567890abcdef0 foo bar", false},
140
		{"no data", false},
141
	}
142
143
	r := newAmazon(config.ServerInfo{})
144
	for _, tt := range tests {
145
		actual := r.isAwsInstanceID(tt.in)
146
		if tt.expected != actual {
147
			t.Errorf("expected %t, actual %t, str: %s", tt.expected, actual, tt.in)
148
		}
149
	}
150
}
151
152
func TestParseSystemctlStatus(t *testing.T) {
153
	var tests = []struct {
154
		in  string
155
		out string
156
	}{
157
		{
158
			in: `● NetworkManager.service - Network Manager
159
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
160
   Active: active (running) since Wed 2018-01-10 17:15:39 JST; 2 months 10 days ago
161
     Docs: man:NetworkManager(8)
162
 Main PID: 437 (NetworkManager)
163
   Memory: 424.0K
164
   CGroup: /system.slice/NetworkManager.service
165
           ├─437 /usr/sbin/NetworkManager --no-daemon
166
           └─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`,
167
			out: "NetworkManager.service",
168
		},
169
		{
170
			in:  `Failed to get unit for PID 700: PID 700 does not belong to any loaded unit.`,
171
			out: "",
172
		},
173
	}
174
175
	r := newCentOS(config.ServerInfo{})
176
	for _, tt := range tests {
177
		actual := r.parseSystemctlStatus(tt.in)
178
		if tt.out != actual {
179
			t.Errorf("expected %v, actual %v", tt.out, actual)
180
		}
181
	}
182
}
183
184
func TestContentConvertVinfos(t *testing.T) {
185
186
	var tests = []struct {
187
		in1      string
188
		in2      WpStatus
189
		expected []models.VulnInfo
190
	}{
191
		{
192
			in1: `{"4.9.4":{"release_date":"2018-02-06","changelog_url":"https://codex.wordpress.org/Version_4.9.4","status":"insecure","vulnerabilities":[{"id":9021,"title":"WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)","created_at":"2018-02-05T16:50:40.000Z","updated_at":"2018-08-29T19:13:04.000Z","published_date":"2018-02-05T00:00:00.000Z","vuln_type":"DOS","references":{"url":["https://baraktawily.blogspot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html","https://github.com/quitten/doser.py","https://thehackernews.com/2018/02/wordpress-dos-exploit.html"],"cve":["2018-6389"]},"fixed_in":null}]}}`,
193
			in2: WpStatus{Name: "twentyfifteen", Status: "inactive", Update: "available", Version: "1.1"},
194
			expected: []models.VulnInfo{
195
				{
196
					CveID: "CVE-2018-6389",
197
					AffectedPackages: models.PackageStatuses{
198
						models.PackageStatus{
199
							NotFixedYet: true,
200
						},
201
					},
202
					CveContents: models.NewCveContents(
203
						models.CveContent{
204
							CveID: "CVE-2018-6389",
205
							Title: "WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)",
206
						},
207
					),
208
				},
209
			},
210
		},
211
		{
212
			in1: `{"4.9.4":{"release_date":"2018-02-06","changelog_url":"https://codex.wordpress.org/Version_4.9.4","status":"insecure","vulnerabilities":[{"id":9021,"title":"WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)","created_at":"2018-02-05T16:50:40.000Z","updated_at":"2018-08-29T19:13:04.000Z","published_date":"2018-02-05T00:00:00.000Z","vuln_type":"DOS","references":{"url":["https://baraktawily.blogspot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html","https://github.com/quitten/doser.py","https://thehackernews.com/2018/02/wordpress-dos-exploit.html"],"cve":["2018-6389"]},"fixed_in":"1.0"}]}}`,
213
			in2:      WpStatus{Name: "twentyfifteen", Status: "inactive", Update: "available", Version: "1.1"},
214
			expected: nil,
215
		},
216
		{
217
			in1: `{"4.9.4":{"release_date":"2018-02-06","changelog_url":"https://codex.wordpress.org/Version_4.9.4","status":"insecure","vulnerabilities":[{"id":9021,"title":"WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)","created_at":"2018-02-05T16:50:40.000Z","updated_at":"2018-08-29T19:13:04.000Z","published_date":"2018-02-05T00:00:00.000Z","vuln_type":"DOS","references":{"url":["https://baraktawily.blogspot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html","https://github.com/quitten/doser.py","https://thehackernews.com/2018/02/wordpress-dos-exploit.html"],"cve":["2018-6389"]},"fixed_in":"1.2"}]}}`,
218
			in2: WpStatus{Name: "twentyfifteen", Status: "inactive", Update: "available", Version: "1.1"},
219
			expected: []models.VulnInfo{
220
				{
221
					CveID: "CVE-2018-6389",
222
					AffectedPackages: models.PackageStatuses{
223
						models.PackageStatus{
224
							NotFixedYet: false,
225
						},
226
					},
227
					CveContents: models.NewCveContents(
228
						models.CveContent{
229
							CveID: "CVE-2018-6389",
230
							Title: "WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)",
231
						},
232
					),
233
				},
234
			},
235
		},
236
	}
237
	for _, test := range tests {
238
		actual, _ := contentConvertVinfos(test.in1, test.in2)
239
		if !reflect.DeepEqual(test.expected, actual) {
240
			h := pp.Sprint(actual)
241
			k := pp.Sprint(test.expected)
242
			t.Errorf("expected %v, actual %v", k, h)
243
		}
244
	}
245
246
}
247
248
func TestCoreConvertVinfos(t *testing.T) {
249
250
	var test = struct {
251
		in1      string
252
		expected []models.VulnInfo
253
	}{
254
		in1: `{"4.9.4":{"release_date":"2018-02-06","changelog_url":"https://codex.wordpress.org/Version_4.9.4","status":"insecure","vulnerabilities":[{"id":9021,"title":"WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)","created_at":"2018-02-05T16:50:40.000Z","updated_at":"2018-08-29T19:13:04.000Z","published_date":"2018-02-05T00:00:00.000Z","vuln_type":"DOS","references":{"url":["https://baraktawily.blogspot.fr/2018/02/how-to-dos-29-of-world-wide-websites.html","https://github.com/quitten/doser.py","https://thehackernews.com/2018/02/wordpress-dos-exploit.html"],"cve":["2018-6389"]},"fixed_in":null}]}}`,
255
		expected: []models.VulnInfo{
256
			{
257
				CveID: "CVE-2018-6389",
258
				AffectedPackages: models.PackageStatuses{
259
					models.PackageStatus{
260
						NotFixedYet: true,
261
					},
262
				},
263
				CveContents: models.NewCveContents(
264
					models.CveContent{
265
						CveID: "CVE-2018-6389",
266
						Title: "WordPress <= 4.9.4 - Application Denial of Service (DoS) (unpatched)",
267
					},
268
				),
269
			},
270
		},
271
	}
272
	actual, _ := coreConvertVinfos(test.in1)
273
	if !reflect.DeepEqual(test.expected, actual) {
274
		h := pp.Sprint(actual)
275
		k := pp.Sprint(test.expected)
276
		t.Errorf("expected %v, actual %v", k, h)
277
	}
278
}
279