Issues (121)

scan/debian_test.go (1 issue)

Severity
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
	"os"
22
	"reflect"
23
	"testing"
24
25
	"github.com/future-architect/vuls/cache"
26
	"github.com/future-architect/vuls/config"
27
	"github.com/future-architect/vuls/models"
28
	"github.com/k0kubun/pp"
29
	"github.com/sirupsen/logrus"
30
)
31
32
func TestGetCveIDsFromChangelog(t *testing.T) {
33
34
	var tests = []struct {
35
		in        []string
36
		cveIDs    []DetectedCveID
37
		changelog models.Changelog
38
	}{
39
		{
40
			//0 verubuntu1
41
			[]string{
42
				"systemd",
43
				"228-4ubuntu1",
44
				`systemd (229-2) unstable; urgency=medium
45
systemd (229-1) unstable; urgency=medium
46
systemd (228-6) unstable; urgency=medium
47
CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
48
CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
49
CVE-2015-3210: heap buffer overflow in pcre_compile2() /
50
systemd (228-5) unstable; urgency=medium
51
systemd (228-4) unstable; urgency=medium
52
systemd (228-3) unstable; urgency=medium`,
53
			},
54
			[]DetectedCveID{
55
				{"CVE-2015-2325", models.ChangelogExactMatch},
56
				{"CVE-2015-2326", models.ChangelogExactMatch},
57
				{"CVE-2015-3210", models.ChangelogExactMatch},
58
			},
59
			models.Changelog{
60
				Contents: `systemd (229-2) unstable; urgency=medium
61
systemd (229-1) unstable; urgency=medium
62
systemd (228-6) unstable; urgency=medium
63
CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
64
CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
65
CVE-2015-3210: heap buffer overflow in pcre_compile2() /
66
systemd (228-5) unstable; urgency=medium`,
67
				Method: models.ChangelogExactMatchStr,
68
			},
69
		},
70
		{
71
			//1 ver
72
			[]string{
73
				"libpcre3",
74
				"2:8.35-7.1ubuntu1",
75
				`pcre3 (2:8.38-2) unstable; urgency=low
76
		 pcre3 (2:8.38-1) unstable; urgency=low
77
		 pcre3 (2:8.35-8) unstable; urgency=low
78
		 pcre3 (2:8.35-7.4) unstable; urgency=medium
79
		 pcre3 (2:8.35-7.3) unstable; urgency=medium
80
		 pcre3 (2:8.35-7.2) unstable; urgency=low
81
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
82
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
83
		 CVE-2015-3210: heap buffer overflow in pcre_compile2() /
84
		 pcre3 (2:8.35-7.1) unstable; urgency=medium
85
		 pcre3 (2:8.35-7) unstable; urgency=medium`,
86
			},
87
			[]DetectedCveID{
88
				{"CVE-2015-2325", models.ChangelogExactMatch},
89
				{"CVE-2015-2326", models.ChangelogExactMatch},
90
				{"CVE-2015-3210", models.ChangelogExactMatch},
91
			},
92
			models.Changelog{
93
				Contents: `pcre3 (2:8.38-2) unstable; urgency=low
94
		 pcre3 (2:8.38-1) unstable; urgency=low
95
		 pcre3 (2:8.35-8) unstable; urgency=low
96
		 pcre3 (2:8.35-7.4) unstable; urgency=medium
97
		 pcre3 (2:8.35-7.3) unstable; urgency=medium
98
		 pcre3 (2:8.35-7.2) unstable; urgency=low
99
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
100
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
101
		 CVE-2015-3210: heap buffer overflow in pcre_compile2() /`,
102
				Method: models.ChangelogExactMatchStr,
103
			},
104
		},
105
		{
106
			//2 ver-ubuntu3
107
			[]string{
108
				"sysvinit",
109
				"2.88dsf-59.2ubuntu3",
110
				`sysvinit (2.88dsf-59.3ubuntu1) xenial; urgency=low
111
		 sysvinit (2.88dsf-59.3) unstable; urgency=medium
112
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
113
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
114
		 CVE-2015-3210: heap buffer overflow in pcre_compile2() /
115
		 sysvinit (2.88dsf-59.2ubuntu3) xenial; urgency=medium
116
		 sysvinit (2.88dsf-59.2ubuntu2) wily; urgency=medium
117
		 sysvinit (2.88dsf-59.2ubuntu1) wily; urgency=medium
118
		 CVE-2015-2321: heap buffer overflow in pcre_compile2(). (Closes: #783285)
119
		 sysvinit (2.88dsf-59.2) unstable; urgency=medium
120
		 sysvinit (2.88dsf-59.1ubuntu3) wily; urgency=medium
121
		 CVE-2015-2322: heap buffer overflow in pcre_compile2(). (Closes: #783285)
122
		 sysvinit (2.88dsf-59.1ubuntu2) wily; urgency=medium
123
		 sysvinit (2.88dsf-59.1ubuntu1) wily; urgency=medium
124
		 sysvinit (2.88dsf-59.1) unstable; urgency=medium
125
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
126
		 sysvinit (2.88dsf-59) unstable; urgency=medium
127
		 sysvinit (2.88dsf-58) unstable; urgency=low
128
		 sysvinit (2.88dsf-57) unstable; urgency=low`,
129
			},
130
			[]DetectedCveID{
131
				{"CVE-2015-2325", models.ChangelogExactMatch},
132
				{"CVE-2015-2326", models.ChangelogExactMatch},
133
				{"CVE-2015-3210", models.ChangelogExactMatch},
134
			},
135
			models.Changelog{
136
				Contents: `sysvinit (2.88dsf-59.3ubuntu1) xenial; urgency=low
137
		 sysvinit (2.88dsf-59.3) unstable; urgency=medium
138
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
139
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
140
		 CVE-2015-3210: heap buffer overflow in pcre_compile2() /`,
141
				Method: models.ChangelogExactMatchStr,
142
			},
143
		},
144
		{
145
			//3  1:ver-ubuntu3
146
			[]string{
147
				"bsdutils",
148
				"1:2.27.1-1ubuntu3",
149
				`util-linux (2.27.1-3ubuntu1) xenial; urgency=medium
150
		 util-linux (2.27.1-3) unstable; urgency=medium
151
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
152
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
153
		 CVE-2015-3210: CVE-2016-1000000heap buffer overflow in pcre_compile2() /
154
		 util-linux (2.27.1-2) unstable; urgency=medium
155
		 util-linux (2.27.1-1ubuntu4) xenial; urgency=medium
156
		 util-linux (2.27.1-1ubuntu3) xenial; urgency=medium
157
		 util-linux (2.27.1-1ubuntu2) xenial; urgency=medium
158
		 util-linux (2.27.1-1ubuntu1) xenial; urgency=medium
159
		 util-linux (2.27.1-1) unstable; urgency=medium
160
		 util-linux (2.27-3ubuntu1) xenial; urgency=medium`,
161
			},
162
			[]DetectedCveID{
163
				// {"CVE-2015-2325", models.ChangelogLenientMatch},
164
				// {"CVE-2015-2326", models.ChangelogLenientMatch},
165
				// {"CVE-2015-3210", models.ChangelogLenientMatch},
166
				// {"CVE-2016-1000000", models.ChangelogLenientMatch},
167
			},
168
			models.Changelog{
169
				// Contents: `util-linux (2.27.1-3ubuntu1) xenial; urgency=medium
170
				// util-linux (2.27.1-3) unstable; urgency=medium
171
				// CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
172
				// CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
173
				// CVE-2015-3210: CVE-2016-1000000heap buffer overflow in pcre_compile2() /
174
				// util-linux (2.27.1-2) unstable; urgency=medium
175
				// util-linux (2.27.1-1ubuntu4) xenial; urgency=medium
176
				// util-linux (2.27.1-1ubuntu3) xenial; urgency=medium`,
177
				Method: models.ChangelogExactMatchStr,
178
			},
179
		},
180
		{
181
			//4 1:ver-ubuntu3
182
			[]string{
183
				"bsdutils",
184
				"1:2.27-3ubuntu3",
185
				`util-linux (2.27.1-3ubuntu1) xenial; urgency=medium
186
		 util-linux (2.27.1-3) unstable; urgency=medium
187
		 CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
188
		 CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
189
		 CVE-2015-3210: CVE-2016-1000000heap buffer overflow in pcre_compile2() /
190
		 util-linux (2.27.1-2) unstable; urgency=medium
191
		 util-linux (2.27.1-1ubuntu4) xenial; urgency=medium
192
		 util-linux (2.27.1-1ubuntu3) xenial; urgency=medium
193
		 util-linux (2.27.1-1ubuntu2) xenial; urgency=medium
194
		 util-linux (2.27.1-1ubuntu1) xenial; urgency=medium
195
		 util-linux (2.27.1-1) unstable; urgency=medium
196
		 util-linux (2.27-3) xenial; urgency=medium`,
197
			},
198
			[]DetectedCveID{
199
				// {"CVE-2015-2325", models.ChangelogLenientMatch},
200
				// {"CVE-2015-2326", models.ChangelogLenientMatch},
201
				// {"CVE-2015-3210", models.ChangelogLenientMatch},
202
				// {"CVE-2016-1000000", models.ChangelogLenientMatch},
203
			},
204
			models.Changelog{
205
				// Contents: `util-linux (2.27.1-3ubuntu1) xenial; urgency=medium
206
				// util-linux (2.27.1-3) unstable; urgency=medium
207
				// CVE-2015-2325: heap buffer overflow in compile_branch(). (Closes: #781795)
208
				// CVE-2015-2326: heap buffer overflow in pcre_compile2(). (Closes: #783285)
209
				// CVE-2015-3210: CVE-2016-1000000heap buffer overflow in pcre_compile2() /
210
				// util-linux (2.27.1-2) unstable; urgency=medium
211
				// util-linux (2.27.1-1ubuntu4) xenial; urgency=medium
212
				// util-linux (2.27.1-1ubuntu3) xenial; urgency=medium
213
				// util-linux (2.27.1-1ubuntu2) xenial; urgency=medium
214
				// util-linux (2.27.1-1ubuntu1) xenial; urgency=medium
215
				// util-linux (2.27.1-1) unstable; urgency=medium`,
216
				Method: models.ChangelogExactMatchStr,
217
			},
218
		},
219
		{
220
			//5 https://github.com/future-architect/vuls/pull/350
221
			[]string{
222
				"tar",
223
				"1.27.1-2+b1",
224
				`tar (1.27.1-2+deb8u1) jessie-security; urgency=high
225
		   * CVE-2016-6321: Bypassing the extract path name.
226
		 tar (1.27.1-2) unstable; urgency=low`,
227
			},
228
			[]DetectedCveID{
229
				{"CVE-2016-6321", models.ChangelogExactMatch},
230
			},
231
			models.Changelog{
232
				Contents: `tar (1.27.1-2+deb8u1) jessie-security; urgency=high
233
		   * CVE-2016-6321: Bypassing the extract path name.`,
234
				Method: models.ChangelogExactMatchStr,
235
			},
236
		},
237
	}
238
239
	d := newDebian(config.ServerInfo{})
240
	d.Distro.Family = "ubuntu"
241
	for i, tt := range tests {
242
		aCveIDs, aPack := d.getCveIDsFromChangelog(tt.in[2], tt.in[0], tt.in[1])
243
		if len(aCveIDs) != len(tt.cveIDs) {
244
			t.Errorf("[%d] Len of return array are'nt same. expected %#v, actual %#v", i, tt.cveIDs, aCveIDs)
245
			t.Errorf(pp.Sprintf("%s", tt.in))
0 ignored issues
show
can't check non-constant format in call to Errorf
Loading history...
246
			continue
247
		}
248
		for j := range tt.cveIDs {
249
			if !reflect.DeepEqual(tt.cveIDs[j], aCveIDs[j]) {
250
				t.Errorf("[%d] expected %v, actual %v", i, tt.cveIDs[j], aCveIDs[j])
251
			}
252
		}
253
254
		if aPack.Changelog.Contents != tt.changelog.Contents {
255
			t.Error(pp.Sprintf("[%d] expected: %s, actual: %s", i, tt.changelog.Contents, aPack.Changelog.Contents))
256
		}
257
258
		if aPack.Changelog.Method != tt.changelog.Method {
259
			t.Error(pp.Sprintf("[%d] expected: %s, actual: %s", i, tt.changelog.Method, aPack.Changelog.Method))
260
		}
261
	}
262
}
263
264
func TestGetUpdatablePackNames(t *testing.T) {
265
266
	var tests = []struct {
267
		in       string
268
		expected []string
269
	}{
270
		{ // Ubuntu 12.04
271
			`Reading package lists... Done
272
Building dependency tree
273
Reading state information... Done
274
The following packages will be upgraded:
275
  apt ca-certificates cpio dpkg e2fslibs e2fsprogs gnupg gpgv libc-bin libc6 libcomerr2 libpcre3
276
  libpng12-0 libss2 libssl1.0.0 libudev0 multiarch-support openssl tzdata udev upstart
277
21 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.`,
278
			[]string{
279
				"apt",
280
				"ca-certificates",
281
				"cpio",
282
				"dpkg",
283
				"e2fslibs",
284
				"e2fsprogs",
285
				"gnupg",
286
				"gpgv",
287
				"libc-bin",
288
				"libc6",
289
				"libcomerr2",
290
				"libpcre3",
291
				"libpng12-0",
292
				"libss2",
293
				"libssl1.0.0",
294
				"libudev0",
295
				"multiarch-support",
296
				"openssl",
297
				"tzdata",
298
				"udev",
299
				"upstart",
300
			},
301
		},
302
		{ // Ubuntu 14.04
303
			`Reading package lists... Done
304
Building dependency tree
305
Reading state information... Done
306
Calculating upgrade... Done
307
The following packages will be upgraded:
308
  apt apt-utils base-files bsdutils coreutils cpio dh-python dpkg e2fslibs
309
  e2fsprogs gcc-4.8-base gcc-4.9-base gnupg gpgv ifupdown initscripts iproute2
310
  isc-dhcp-client isc-dhcp-common libapt-inst1.5 libapt-pkg4.12 libblkid1
311
  libc-bin libc6 libcgmanager0 libcomerr2 libdrm2 libexpat1 libffi6 libgcc1
312
  libgcrypt11 libgnutls-openssl27 libgnutls26 libmount1 libpcre3 libpng12-0
313
  libpython3.4-minimal libpython3.4-stdlib libsqlite3-0 libss2 libssl1.0.0
314
  libstdc++6 libtasn1-6 libudev1 libuuid1 login mount multiarch-support
315
  ntpdate passwd python3.4 python3.4-minimal rsyslog sudo sysv-rc
316
  sysvinit-utils tzdata udev util-linux
317
59 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
318
`,
319
			[]string{
320
				"apt",
321
				"apt-utils",
322
				"base-files",
323
				"bsdutils",
324
				"coreutils",
325
				"cpio",
326
				"dh-python",
327
				"dpkg",
328
				"e2fslibs",
329
				"e2fsprogs",
330
				"gcc-4.8-base",
331
				"gcc-4.9-base",
332
				"gnupg",
333
				"gpgv",
334
				"ifupdown",
335
				"initscripts",
336
				"iproute2",
337
				"isc-dhcp-client",
338
				"isc-dhcp-common",
339
				"libapt-inst1.5",
340
				"libapt-pkg4.12",
341
				"libblkid1",
342
				"libc-bin",
343
				"libc6",
344
				"libcgmanager0",
345
				"libcomerr2",
346
				"libdrm2",
347
				"libexpat1",
348
				"libffi6",
349
				"libgcc1",
350
				"libgcrypt11",
351
				"libgnutls-openssl27",
352
				"libgnutls26",
353
				"libmount1",
354
				"libpcre3",
355
				"libpng12-0",
356
				"libpython3.4-minimal",
357
				"libpython3.4-stdlib",
358
				"libsqlite3-0",
359
				"libss2",
360
				"libssl1.0.0",
361
				"libstdc++6",
362
				"libtasn1-6",
363
				"libudev1",
364
				"libuuid1",
365
				"login",
366
				"mount",
367
				"multiarch-support",
368
				"ntpdate",
369
				"passwd",
370
				"python3.4",
371
				"python3.4-minimal",
372
				"rsyslog",
373
				"sudo",
374
				"sysv-rc",
375
				"sysvinit-utils",
376
				"tzdata",
377
				"udev",
378
				"util-linux",
379
			},
380
		},
381
		{
382
			//Ubuntu12.04
383
			`Reading package lists... Done
384
Building dependency tree
385
Reading state information... Done
386
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.`,
387
			[]string{},
388
		},
389
		{
390
			//Ubuntu14.04
391
			`Reading package lists... Done
392
Building dependency tree
393
Reading state information... Done
394
Calculating upgrade... Done
395
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.`,
396
			[]string{},
397
		},
398
	}
399
400
	d := newDebian(config.ServerInfo{})
401
	for _, tt := range tests {
402
		actual, err := d.parseAptGetUpgrade(tt.in)
403
		if err != nil {
404
			t.Errorf("Returning error is unexpected")
405
		}
406
		if len(tt.expected) != len(actual) {
407
			t.Errorf("Result length is not as same as expected. expected: %d, actual: %d", len(tt.expected), len(actual))
408
			pp.Println(tt.expected)
409
			pp.Println(actual)
410
			return
411
		}
412
		for i := range tt.expected {
413
			if tt.expected[i] != actual[i] {
414
				t.Errorf("[%d] expected %s, actual %s", i, tt.expected[i], actual[i])
415
			}
416
		}
417
	}
418
}
419
420
func TestGetChangelogCache(t *testing.T) {
421
	const servername = "server1"
422
	pack := models.Package{
423
		Name:       "apt",
424
		Version:    "1.0.0",
425
		NewVersion: "1.0.1",
426
	}
427
	var meta = cache.Meta{
428
		Name: servername,
429
		Distro: config.Distro{
430
			Family:  "ubuntu",
431
			Release: "16.04",
432
		},
433
		Packs: models.Packages{
434
			"apt": pack,
435
		},
436
	}
437
438
	const path = "/tmp/vuls-test-cache-11111111.db"
439
	log := logrus.NewEntry(&logrus.Logger{})
440
	if err := cache.SetupBolt(path, log); err != nil {
441
		t.Errorf("Failed to setup bolt: %s", err)
442
	}
443
	defer os.Remove(path)
444
445
	if err := cache.DB.EnsureBuckets(meta); err != nil {
446
		t.Errorf("Failed to ensure buckets: %s", err)
447
	}
448
449
	d := newDebian(config.ServerInfo{})
450
	actual := d.getChangelogCache(&meta, pack)
451
	if actual != "" {
452
		t.Errorf("Failed to get empty stirng from cache:")
453
	}
454
455
	clog := "changelog-text"
456
	if err := cache.DB.PutChangelog(servername, "apt", clog); err != nil {
457
		t.Errorf("Failed to put changelog: %s", err)
458
	}
459
460
	actual = d.getChangelogCache(&meta, pack)
461
	if actual != clog {
462
		t.Errorf("Failed to get changelog from cache: %s", actual)
463
	}
464
465
	// increment a version of the pack
466
	pack.NewVersion = "1.0.2"
467
	actual = d.getChangelogCache(&meta, pack)
468
	if actual != "" {
469
		t.Errorf("The changelog is not invalidated: %s", actual)
470
	}
471
472
	// change a name of the pack
473
	pack.Name = "bash"
474
	actual = d.getChangelogCache(&meta, pack)
475
	if actual != "" {
476
		t.Errorf("The changelog is not invalidated: %s", actual)
477
	}
478
}
479
480
func TestSplitAptCachePolicy(t *testing.T) {
481
	var tests = []struct {
482
		stdout   string
483
		expected map[string]string
484
	}{
485
		// This function parse apt-cache policy by using Regexp multi-line mode.
486
		// So, test data includes "\r\n"
487
		{
488
			"apt:\r\n  Installed: 1.2.6\r\n  Candidate: 1.2.12~ubuntu16.04.1\r\n  Version table:\r\n     1.2.12~ubuntu16.04.1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     1.2.10ubuntu1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 1.2.6 100\r\n        100 /var/lib/dpkg/status\r\napt-utils:\r\n  Installed: 1.2.6\r\n  Candidate: 1.2.12~ubuntu16.04.1\r\n  Version table:\r\n     1.2.12~ubuntu16.04.1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     1.2.10ubuntu1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 1.2.6 100\r\n        100 /var/lib/dpkg/status\r\nbase-files:\r\n  Installed: 9.4ubuntu3\r\n  Candidate: 9.4ubuntu4.2\r\n  Version table:\r\n     9.4ubuntu4.2 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     9.4ubuntu4 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 9.4ubuntu3 100\r\n        100 /var/lib/dpkg/status\r\n",
489
490
			map[string]string{
491
				"apt": "apt:\r\n  Installed: 1.2.6\r\n  Candidate: 1.2.12~ubuntu16.04.1\r\n  Version table:\r\n     1.2.12~ubuntu16.04.1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     1.2.10ubuntu1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 1.2.6 100\r\n        100 /var/lib/dpkg/status\r\n",
492
493
				"apt-utils": "apt-utils:\r\n  Installed: 1.2.6\r\n  Candidate: 1.2.12~ubuntu16.04.1\r\n  Version table:\r\n     1.2.12~ubuntu16.04.1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     1.2.10ubuntu1 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 1.2.6 100\r\n        100 /var/lib/dpkg/status\r\n",
494
495
				"base-files": "base-files:\r\n  Installed: 9.4ubuntu3\r\n  Candidate: 9.4ubuntu4.2\r\n  Version table:\r\n     9.4ubuntu4.2 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages\r\n     9.4ubuntu4 500\r\n        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages\r\n *** 9.4ubuntu3 100\r\n        100 /var/lib/dpkg/status\r\n",
496
			},
497
		},
498
	}
499
500
	d := newDebian(config.ServerInfo{})
501
	for _, tt := range tests {
502
		actual := d.splitAptCachePolicy(tt.stdout)
503
		if !reflect.DeepEqual(tt.expected, actual) {
504
			e := pp.Sprintf("%v", tt.expected)
505
			a := pp.Sprintf("%v", actual)
506
			t.Errorf("expected %s, actual %s", e, a)
507
		}
508
	}
509
}
510
511
func TestParseAptCachePolicy(t *testing.T) {
512
513
	var tests = []struct {
514
		stdout   string
515
		name     string
516
		expected packCandidateVer
517
	}{
518
		{
519
			// Ubuntu 16.04
520
			`openssl:
521
  Installed: 1.0.2f-2ubuntu1
522
  Candidate: 1.0.2g-1ubuntu2
523
  Version table:
524
     1.0.2g-1ubuntu2 500
525
        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages
526
 *** 1.0.2f-2ubuntu1 100
527
        100 /var/lib/dpkg/status`,
528
			"openssl",
529
			packCandidateVer{
530
				Name:      "openssl",
531
				Installed: "1.0.2f-2ubuntu1",
532
				Candidate: "1.0.2g-1ubuntu2",
533
				Repo:      "xenial/main",
534
			},
535
		},
536
		{
537
			// Ubuntu 14.04
538
			`openssl:
539
  Installed: 1.0.1f-1ubuntu2.16
540
  Candidate: 1.0.1f-1ubuntu2.17
541
  Version table:
542
     1.0.1f-1ubuntu2.17 0
543
        500 http://archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
544
        500 http://archive.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
545
 *** 1.0.1f-1ubuntu2.16 0
546
        100 /var/lib/dpkg/status
547
     1.0.1f-1ubuntu2 0
548
        500 http://archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages`,
549
			"openssl",
550
			packCandidateVer{
551
				Name:      "openssl",
552
				Installed: "1.0.1f-1ubuntu2.16",
553
				Candidate: "1.0.1f-1ubuntu2.17",
554
				Repo:      "trusty-updates/main",
555
			},
556
		},
557
		{
558
			// Ubuntu 12.04
559
			`openssl:
560
  Installed: 1.0.1-4ubuntu5.33
561
  Candidate: 1.0.1-4ubuntu5.34
562
  Version table:
563
     1.0.1-4ubuntu5.34 0
564
        500 http://archive.ubuntu.com/ubuntu/ precise-updates/main amd64 Packages
565
        500 http://archive.ubuntu.com/ubuntu/ precise-security/main amd64 Packages
566
 *** 1.0.1-4ubuntu5.33 0
567
        100 /var/lib/dpkg/status
568
     1.0.1-4ubuntu3 0
569
        500 http://archive.ubuntu.com/ubuntu/ precise/main amd64 Packages`,
570
			"openssl",
571
			packCandidateVer{
572
				Name:      "openssl",
573
				Installed: "1.0.1-4ubuntu5.33",
574
				Candidate: "1.0.1-4ubuntu5.34",
575
				Repo:      "precise-updates/main",
576
			},
577
		},
578
	}
579
580
	d := newDebian(config.ServerInfo{})
581
	for _, tt := range tests {
582
		actual, err := d.parseAptCachePolicy(tt.stdout, tt.name)
583
		if err != nil {
584
			t.Errorf("Error has occurred: %s, actual: %#v", err, actual)
585
		}
586
		if !reflect.DeepEqual(tt.expected, actual) {
587
			e := pp.Sprintf("%v", tt.expected)
588
			a := pp.Sprintf("%v", actual)
589
			t.Errorf("expected %s, actual %s", e, a)
590
		}
591
	}
592
}
593
594
func TestParseCheckRestart(t *testing.T) {
595
	r := newDebian(config.ServerInfo{})
596
	r.Distro = config.Distro{Family: "debian"}
597
	var tests = []struct {
598
		in              string
599
		out             models.Packages
600
		unknownServices []string
601
	}{
602
		{
603
			in: `Found 27 processes using old versions of upgraded files
604
(19 distinct programs)
605
(15 distinct packages)
606
607
Of these, 14 seem to contain systemd service definitions or init scripts which can be used to restart them.
608
The following packages seem to have definitions that could be used
609
to restart their services:
610
varnish:
611
	3490	/usr/sbin/varnishd
612
	3704	/usr/sbin/varnishd
613
memcached:
614
	3636	/usr/bin/memcached
615
openssh-server:
616
	1252	/usr/sbin/sshd
617
	1184	/usr/sbin/sshd
618
accountsservice:
619
	462     /usr/lib/accountsservice/accounts-daemon
620
621
These are the systemd services:
622
systemctl restart accounts-daemon.service
623
624
These are the initd scripts:
625
service varnish restart
626
service memcached restart
627
service ssh restart
628
629
These processes (1) do not seem to have an associated init script to restart them:
630
util-linux:
631
	3650	/sbin/agetty
632
	3648	/sbin/agetty`,
633
			out: models.NewPackages(
634
				models.Package{
635
					Name: "varnish",
636
					NeedRestartProcs: []models.NeedRestartProcess{
637
						{
638
							PID:         "3490",
639
							Path:        "/usr/sbin/varnishd",
640
							ServiceName: "varnish",
641
							HasInit:     true,
642
						},
643
						{
644
							PID:         "3704",
645
							Path:        "/usr/sbin/varnishd",
646
							ServiceName: "varnish",
647
							HasInit:     true,
648
						},
649
					},
650
				},
651
				models.Package{
652
					Name: "memcached",
653
					NeedRestartProcs: []models.NeedRestartProcess{
654
						{
655
							PID:         "3636",
656
							Path:        "/usr/bin/memcached",
657
							ServiceName: "memcached",
658
							HasInit:     true,
659
						},
660
					},
661
				},
662
				models.Package{
663
					Name: "openssh-server",
664
					NeedRestartProcs: []models.NeedRestartProcess{
665
						{
666
							PID:         "1252",
667
							Path:        "/usr/sbin/sshd",
668
							ServiceName: "",
669
							HasInit:     true,
670
						},
671
						{
672
							PID:         "1184",
673
							Path:        "/usr/sbin/sshd",
674
							ServiceName: "",
675
							HasInit:     true,
676
						},
677
					},
678
				},
679
				models.Package{
680
					Name: "accountsservice",
681
					NeedRestartProcs: []models.NeedRestartProcess{
682
						{
683
							PID:         "462",
684
							Path:        "/usr/lib/accountsservice/accounts-daemon",
685
							ServiceName: "",
686
							HasInit:     true,
687
						},
688
					},
689
				},
690
				models.Package{
691
					Name: "util-linux",
692
					NeedRestartProcs: []models.NeedRestartProcess{
693
						{
694
							PID:     "3650",
695
							Path:    "/sbin/agetty",
696
							HasInit: false,
697
						},
698
						{
699
							PID:     "3648",
700
							Path:    "/sbin/agetty",
701
							HasInit: false,
702
						},
703
					},
704
				},
705
			),
706
			unknownServices: []string{"ssh"},
707
		},
708
		{
709
			in:              `Found 0 processes using old versions of upgraded files`,
710
			out:             models.Packages{},
711
			unknownServices: []string{},
712
		},
713
	}
714
715
	for _, tt := range tests {
716
		packages, services := r.parseCheckRestart(tt.in)
717
		for name, ePack := range tt.out {
718
			if !reflect.DeepEqual(ePack, packages[name]) {
719
				e := pp.Sprintf("%v", ePack)
720
				a := pp.Sprintf("%v", packages[name])
721
				t.Errorf("expected %s, actual %s", e, a)
722
			}
723
		}
724
		if !reflect.DeepEqual(tt.unknownServices, services) {
725
			t.Errorf("expected %s, actual %s", tt.unknownServices, services)
726
		}
727
	}
728
}
729