scan.TestParseYumUpdateinfoListAvailable   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 34
dl 0
loc 59
rs 8.5973
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, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
package scan
17
18
import (
19
	"reflect"
20
	"sort"
21
	"strings"
22
	"testing"
23
	"time"
24
25
	"github.com/future-architect/vuls/config"
26
	"github.com/future-architect/vuls/models"
27
	"github.com/k0kubun/pp"
28
)
29
30
//  func unixtimeNoerr(s string) time.Time {
31
//      t, _ := unixtime(s)
32
//      return t
33
//  }
34
35
func TestParseInstalledPackagesLinesRedhat(t *testing.T) {
36
	r := newRHEL(config.ServerInfo{})
37
	r.Distro = config.Distro{Family: config.RedHat}
38
39
	var packagetests = []struct {
40
		in       string
41
		kernel   models.Kernel
42
		packages models.Packages
43
	}{
44
		{
45
			in: `openssl	0	1.0.1e	30.el6.11 x86_64
46
                 Percona-Server-shared-56	1	5.6.19	rel67.0.el6 x84_64
47
                 kernel 0 2.6.32 696.20.1.el6 x86_64
48
                 kernel 0 2.6.32 696.20.3.el6 x86_64
49
				 kernel 0 2.6.32 695.20.3.el6 x86_64`,
50
			kernel: models.Kernel{},
51
			packages: models.Packages{
52
				"openssl": models.Package{
53
					Name:    "openssl",
54
					Version: "1.0.1e",
55
					Release: "30.el6.11",
56
				},
57
				"Percona-Server-shared-56": models.Package{
58
					Name:    "Percona-Server-shared-56",
59
					Version: "1:5.6.19",
60
					Release: "rel67.0.el6",
61
				},
62
				"kernel": models.Package{
63
					Name:    "kernel",
64
					Version: "2.6.32",
65
					Release: "696.20.3.el6",
66
				},
67
			},
68
		},
69
		{
70
			in: `openssl	0	1.0.1e	30.el6.11 x86_64
71
                 Percona-Server-shared-56	1	5.6.19	rel67.0.el6 x84_64
72
                 kernel 0 2.6.32 696.20.1.el6 x86_64
73
                 kernel 0 2.6.32 696.20.3.el6 x86_64
74
				 kernel 0 2.6.32 695.20.3.el6 x86_64`,
75
			kernel: models.Kernel{Release: "2.6.32-695.20.3.el6.x86_64"},
76
			packages: models.Packages{
77
				"openssl": models.Package{
78
					Name:    "openssl",
79
					Version: "1.0.1e",
80
					Release: "30.el6.11",
81
				},
82
				"Percona-Server-shared-56": models.Package{
83
					Name:    "Percona-Server-shared-56",
84
					Version: "1:5.6.19",
85
					Release: "rel67.0.el6",
86
				},
87
				"kernel": models.Package{
88
					Name:    "kernel",
89
					Version: "2.6.32",
90
					Release: "695.20.3.el6",
91
				},
92
			},
93
		},
94
	}
95
96
	for _, tt := range packagetests {
97
		r.Kernel = tt.kernel
98
		packages, _, err := r.parseInstalledPackages(tt.in)
99
		if err != nil {
100
			t.Errorf("Unexpected error: %s", err)
101
		}
102
		for name, expectedPack := range tt.packages {
103
			pack := packages[name]
104
			if pack.Name != expectedPack.Name {
105
				t.Errorf("name: expected %s, actual %s", expectedPack.Name, pack.Name)
106
			}
107
			if pack.Version != expectedPack.Version {
108
				t.Errorf("version: expected %s, actual %s", expectedPack.Version, pack.Version)
109
			}
110
			if pack.Release != expectedPack.Release {
111
				t.Errorf("release: expected %s, actual %s", expectedPack.Release, pack.Release)
112
			}
113
		}
114
	}
115
116
}
117
func TestParseScanedPackagesLineRedhat(t *testing.T) {
118
	r := newRHEL(config.ServerInfo{})
119
120
	var packagetests = []struct {
121
		in   string
122
		pack models.Package
123
	}{
124
		{
125
			"openssl	0	1.0.1e	30.el6.11 x86_64",
126
			models.Package{
127
				Name:    "openssl",
128
				Version: "1.0.1e",
129
				Release: "30.el6.11",
130
			},
131
		},
132
		{
133
			"Percona-Server-shared-56	1	5.6.19	rel67.0.el6 x84_64",
134
			models.Package{
135
				Name:    "Percona-Server-shared-56",
136
				Version: "1:5.6.19",
137
				Release: "rel67.0.el6",
138
			},
139
		},
140
	}
141
142
	for _, tt := range packagetests {
143
		p, _ := r.parseInstalledPackagesLine(tt.in)
144
		if p.Name != tt.pack.Name {
145
			t.Errorf("name: expected %s, actual %s", tt.pack.Name, p.Name)
146
		}
147
		if p.Version != tt.pack.Version {
148
			t.Errorf("version: expected %s, actual %s", tt.pack.Version, p.Version)
149
		}
150
		if p.Release != tt.pack.Release {
151
			t.Errorf("release: expected %s, actual %s", tt.pack.Release, p.Release)
152
		}
153
	}
154
155
}
156
157
func TestChangeSectionState(t *testing.T) {
158
	r := newRHEL(config.ServerInfo{})
159
	var tests = []struct {
160
		oldState int
161
		newState int
162
	}{
163
		{Outside, Header},
164
		{Header, Content},
165
		{Content, Header},
166
	}
167
168
	for _, tt := range tests {
169
		if n := r.changeSectionState(tt.oldState); n != tt.newState {
170
			t.Errorf("expected %d, actual %d", tt.newState, n)
171
		}
172
	}
173
}
174
175
func TestParseYumUpdateinfoLineToGetCveIDs(t *testing.T) {
176
	r := newRHEL(config.ServerInfo{})
177
	var tests = []struct {
178
		in  string
179
		out []string
180
	}{
181
		{
182
			"Bugs : 1194651 - CVE-2015-0278 libuv:",
183
			[]string{"CVE-2015-0278"},
184
		},
185
		{
186
			": 1195457 - nodejs-0.10.35 causes undefined symbolsCVE-2015-0278, CVE-2015-0278, CVE-2015-02770000000 ",
187
			[]string{
188
				"CVE-2015-0278",
189
				"CVE-2015-0278",
190
				"CVE-2015-02770000000",
191
			},
192
		},
193
	}
194
195
	for _, tt := range tests {
196
		act := r.parseYumUpdateinfoLineToGetCveIDs(tt.in)
197
		for i, s := range act {
198
			if s != tt.out[i] {
199
				t.Errorf("expected %s, actual %s", tt.out[i], s)
200
			}
201
		}
202
	}
203
}
204
205
func TestParseYumUpdateinfoToGetAdvisoryID(t *testing.T) {
206
	r := newRHEL(config.ServerInfo{})
207
	var tests = []struct {
208
		in    string
209
		out   string
210
		found bool
211
	}{
212
		{
213
			"Update ID : RHSA-2015:2315",
214
			"RHSA-2015:2315",
215
			true,
216
		},
217
		{
218
			"Update ID : ALAS-2015-620",
219
			"ALAS-2015-620",
220
			true,
221
		},
222
		{
223
			"Issued : 2015-11-19 00:00:00",
224
			"",
225
			false,
226
		},
227
	}
228
229
	for _, tt := range tests {
230
		advisoryID, found := r.parseYumUpdateinfoToGetAdvisoryID(tt.in)
231
		if tt.out != advisoryID {
232
			t.Errorf("expected %s, actual %s", tt.out, advisoryID)
233
		}
234
		if tt.found != found {
235
			t.Errorf("expected %t, actual %t", tt.found, found)
236
		}
237
	}
238
}
239
240
func TestParseYumUpdateinfoLineToGetIssued(t *testing.T) {
241
242
	date, _ := time.Parse("2006-01-02", "2015-12-15")
243
244
	r := newRHEL(config.ServerInfo{})
245
	var tests = []struct {
246
		in    string
247
		out   time.Time
248
		found bool
249
	}{
250
		{
251
			"Issued : 2015-12-15 00:00:00",
252
			date,
253
			true,
254
		},
255
		{
256
			"        Issued : 2015-12-15 00:00:00     ",
257
			date,
258
			true,
259
		},
260
		{
261
			"Type : security",
262
			time.Time{},
263
			false,
264
		},
265
	}
266
267
	for i, tt := range tests {
268
		d, found := r.parseYumUpdateinfoLineToGetIssued(tt.in)
269
		if tt.found != found {
270
			t.Errorf("[%d] line: %s, expected %t, actual %t", i, tt.in, tt.found, found)
271
		}
272
		if tt.out != d {
273
			t.Errorf("[%d] line: %s, expected %v, actual %v", i, tt.in, tt.out, d)
274
		}
275
	}
276
}
277
278
func TestParseYumUpdateinfoLineToGetUpdated(t *testing.T) {
279
	date, _ := time.Parse("2006-01-02", "2015-12-15")
280
281
	r := newRHEL(config.ServerInfo{})
282
	var tests = []struct {
283
		in    string
284
		out   time.Time
285
		found bool
286
	}{
287
		{
288
			"Updated : 2015-12-15 00:00:00       Bugs : 1286966 - CVE-2015-8370 grub2: buffer overflow when checking password entered during bootup",
289
			date,
290
			true,
291
		},
292
		{
293
294
			"Updated : 2015-12-15 14:16       CVEs : CVE-2015-7981",
295
			date,
296
			true,
297
		},
298
		{
299
			"Type : security",
300
			time.Time{},
301
			false,
302
		},
303
	}
304
305
	for i, tt := range tests {
306
		d, found := r.parseYumUpdateinfoLineToGetUpdated(tt.in)
307
		if tt.found != found {
308
			t.Errorf("[%d] line: %s, expected %t, actual %t", i, tt.in, tt.found, found)
309
		}
310
		if tt.out != d {
311
			t.Errorf("[%d] line: %s, expected %v, actual %v", i, tt.in, tt.out, d)
312
		}
313
	}
314
}
315
316
func TestIsDescriptionLine(t *testing.T) {
317
	r := newRHEL(config.ServerInfo{})
318
	var tests = []struct {
319
		in    string
320
		found bool
321
	}{
322
		{
323
			"Description : Package updates are available for Amazon Linux AMI that fix the",
324
			true,
325
		},
326
		{
327
			"  Description : Package updates are available for Amazon Linux AMI that fix the",
328
			true,
329
		},
330
		{
331
			"Status : final",
332
			false,
333
		},
334
	}
335
336
	for i, tt := range tests {
337
		found := r.isDescriptionLine(tt.in)
338
		if tt.found != found {
339
			t.Errorf("[%d] line: %s, expected %t, actual %t", i, tt.in, tt.found, found)
340
		}
341
	}
342
}
343
344
func TestParseYumUpdateinfoToGetSeverity(t *testing.T) {
345
	r := newRHEL(config.ServerInfo{})
346
	var tests = []struct {
347
		in    string
348
		out   string
349
		found bool
350
	}{
351
		{
352
			"Severity : Moderate",
353
			"Moderate",
354
			true,
355
		},
356
		{
357
			"   Severity : medium",
358
			"medium",
359
			true,
360
		},
361
		{
362
			"Status : final",
363
			"",
364
			false,
365
		},
366
	}
367
368
	for i, tt := range tests {
369
		out, found := r.parseYumUpdateinfoToGetSeverity(tt.in)
370
		if tt.found != found {
371
			t.Errorf("[%d] line: %s, expected %t, actual %t", i, tt.in, tt.found, found)
372
		}
373
		if tt.out != out {
374
			t.Errorf("[%d] line: %s, expected %v, actual %v", i, tt.in, tt.out, out)
375
		}
376
	}
377
}
378
379
func TestParseYumUpdateinfoOL(t *testing.T) {
380
	stdout := `===============================================================================
381
   bind security update
382
===============================================================================
383
  Update ID : ELSA-2017-0276
384
    Release : Oracle Linux 7
385
       Type : security
386
     Status : final
387
     Issued : 2017-02-15
388
       CVEs : CVE-2017-3135
389
Description : [32:9.9.4-38.2]
390
   Severity : Moderate
391
392
===============================================================================
393
   openssl security update
394
===============================================================================
395
  Update ID : ELSA-2017-0286
396
    Release : Oracle Linux 7
397
       Type : security
398
     Status : final
399
     Issued : 2017-02-15
400
       CVEs : CVE-2016-8610
401
	    : CVE-2017-3731
402
Description : [1.0.1e-48.4]
403
   Severity : Moderate
404
405
===============================================================================
406
   Unbreakable Enterprise kernel security update
407
===============================================================================
408
  Update ID : ELSA-2017-3520
409
    Release : Oracle Linux 7
410
       Type : security
411
     Status : final
412
     Issued : 2017-02-15
413
       CVEs : CVE-2017-6074
414
Description : kernel-uek
415
   Severity : Important
416
417
	`
418
	issued, _ := time.Parse("2006-01-02", "2017-02-15")
419
420
	r := newRHEL(config.ServerInfo{})
421
	r.Distro = config.Distro{Family: "oraclelinux"}
422
423
	var tests = []struct {
424
		in  string
425
		out []distroAdvisoryCveIDs
426
	}{
427
		{
428
			stdout,
429
			[]distroAdvisoryCveIDs{
430
				{
431
					DistroAdvisory: models.DistroAdvisory{
432
						AdvisoryID:  "ELSA-2017-0276",
433
						Severity:    "Moderate",
434
						Issued:      issued,
435
						Description: "[32:9.9.4-38.2]\n",
436
					},
437
					CveIDs: []string{"CVE-2017-3135"},
438
				},
439
				{
440
					DistroAdvisory: models.DistroAdvisory{
441
						AdvisoryID:  "ELSA-2017-0286",
442
						Severity:    "Moderate",
443
						Issued:      issued,
444
						Description: "[1.0.1e-48.4]\n",
445
					},
446
					CveIDs: []string{
447
						"CVE-2016-8610",
448
						"CVE-2017-3731",
449
					},
450
				},
451
				{
452
					DistroAdvisory: models.DistroAdvisory{
453
						AdvisoryID:  "ELSA-2017-3520",
454
						Severity:    "Important",
455
						Issued:      issued,
456
						Description: "kernel-uek\n",
457
					},
458
					CveIDs: []string{"CVE-2017-6074"},
459
				},
460
			},
461
		},
462
	}
463
	for _, tt := range tests {
464
		actual, _ := r.parseYumUpdateinfo(tt.in)
465
		for i, advisoryCveIDs := range actual {
466
			if tt.out[i].DistroAdvisory != advisoryCveIDs.DistroAdvisory {
467
				t.Errorf("[%d] Alas is not same. \nexpected: %s\nactual: %s",
468
					i, tt.out[i].DistroAdvisory, advisoryCveIDs.DistroAdvisory)
0 ignored issues
show
introduced by
printf argument tt.out[i].DistroAdvisory has invalid or unknown type
Loading history...
introduced by
printf argument advisoryCveIDs.DistroAdvisory has invalid or unknown type
Loading history...
469
			}
470
			sort.Strings(tt.out[i].CveIDs)
471
			sort.Strings(advisoryCveIDs.CveIDs)
472
			if !reflect.DeepEqual(tt.out[i].CveIDs, advisoryCveIDs.CveIDs) {
473
				t.Errorf("[%d] Alas is not same. \nexpected: %s\nactual: %s",
474
					i, tt.out[i].CveIDs, advisoryCveIDs.CveIDs)
475
			}
476
		}
477
	}
478
}
479
480
func TestParseYumUpdateinfoRHEL(t *testing.T) {
481
482
	stdout := `===============================================================================
483
  Important: bind security update
484
===============================================================================
485
  Update ID : RHSA-2015:1705
486
    Release :
487
       Type : security
488
     Status : final
489
     Issued : 2015-09-03 00:00:00
490
       Bugs : 1259087 - CVE-2015-5722 bind: malformed DNSSEC key failed assertion denial of service
491
       CVEs : CVE-2015-5722
492
Description : The Berkeley Internet Name Domain (BIND) is an implementation of
493
   Severity : Important
494
495
===============================================================================
496
  Important: bind security update
497
===============================================================================
498
  Update ID : RHSA-2015:2655
499
    Release :
500
       Type : security
501
     Status : final
502
     Issued : 2015-09-03 01:00:00
503
    Updated : 2015-09-04 00:00:00
504
       Bugs : 1291176 - CVE-2015-8000 bind: responses with a malformed class attribute can trigger an assertion failure in db.c
505
       CVEs : CVE-2015-8000
506
            : CVE-2015-8001
507
Description : The Berkeley Internet Name Domain (BIND) is an implementation of
508
   Severity : Low
509
510
===============================================================================
511
  Moderate: bind security update
512
===============================================================================
513
  Update ID : RHSA-2016:0073
514
    Release :
515
       Type : security
516
     Status : final
517
     Issued : 2015-09-03 02:00:00
518
       Bugs : 1299364 - CVE-2015-8704 bind: specific APL data could trigger an INSIST in apl_42.c
519
	   CVEs : CVE-2015-8704
520
	        : CVE-2015-8705
521
Description : The Berkeley Internet Name Domain (BIND) is an implementation of
522
	        : CVE-2015-10000
523
   Severity : Moderate
524
525
===============================================================================
526
  Moderate: sudo security update
527
===============================================================================
528
  Update ID : RHSA-2017:1574
529
    Release : 0
530
       Type : security
531
     Status : final
532
     Issued : 2015-09-03 02:00:00
533
       Bugs : 1459152 - CVE-2017-1000368 sudo: Privilege escalation via improper get_process_ttyname() parsing (insufficient fix for CVE-2017-1000367)       CVEs : CVE-2017-1000368
534
Description : The sudo packages contain the sudo utility which allows system
535
            : administrators to provide certain users with the
536
   Severity : Moderate
537
	`
538
	issued, _ := time.Parse("2006-01-02", "2015-09-03")
539
	updated, _ := time.Parse("2006-01-02", "2015-09-04")
540
541
	r := newRHEL(config.ServerInfo{})
542
	r.Distro = config.Distro{Family: "redhat"}
543
544
	var tests = []struct {
545
		in  string
546
		out []distroAdvisoryCveIDs
547
	}{
548
		{
549
			stdout,
550
			[]distroAdvisoryCveIDs{
551
				{
552
					DistroAdvisory: models.DistroAdvisory{
553
						AdvisoryID:  "RHSA-2015:1705",
554
						Severity:    "Important",
555
						Issued:      issued,
556
						Description: "The Berkeley Internet Name Domain (BIND) is an implementation of\n",
557
					},
558
					CveIDs: []string{"CVE-2015-5722"},
559
				},
560
				{
561
					DistroAdvisory: models.DistroAdvisory{
562
						AdvisoryID:  "RHSA-2015:2655",
563
						Severity:    "Low",
564
						Issued:      issued,
565
						Updated:     updated,
566
						Description: "The Berkeley Internet Name Domain (BIND) is an implementation of\n",
567
					},
568
					CveIDs: []string{
569
						"CVE-2015-8000",
570
						"CVE-2015-8001",
571
					},
572
				},
573
				{
574
					DistroAdvisory: models.DistroAdvisory{
575
						AdvisoryID:  "RHSA-2016:0073",
576
						Severity:    "Moderate",
577
						Issued:      issued,
578
						Description: "The Berkeley Internet Name Domain (BIND) is an implementation of\nCVE-2015-10000\n",
579
					},
580
					CveIDs: []string{
581
						"CVE-2015-8704",
582
						"CVE-2015-8705",
583
					},
584
				},
585
				{
586
					DistroAdvisory: models.DistroAdvisory{
587
						AdvisoryID:  "RHSA-2017:1574",
588
						Severity:    "Moderate",
589
						Issued:      issued,
590
						Description: "The sudo packages contain the sudo utility which allows system\nadministrators to provide certain users with the\n",
591
					},
592
					CveIDs: []string{
593
						"CVE-2017-1000368",
594
					},
595
				},
596
			},
597
		},
598
	}
599
	for _, tt := range tests {
600
		actual, _ := r.parseYumUpdateinfo(tt.in)
601
		for i, advisoryCveIDs := range actual {
602
			sort.Strings(tt.out[i].CveIDs)
603
			sort.Strings(advisoryCveIDs.CveIDs)
604
			if !reflect.DeepEqual(tt.out[i], advisoryCveIDs) {
605
				e := pp.Sprintf("%v", tt.out[i])
606
				a := pp.Sprintf("%v", advisoryCveIDs)
607
				t.Errorf("[%d] not same. \nexpected: %s\nactual: %s",
608
					i, e, a)
609
			}
610
		}
611
	}
612
}
613
614
func TestParseYumUpdateinfoAmazon(t *testing.T) {
615
	r := newAmazon(config.ServerInfo{})
616
	r.Distro = config.Distro{Family: "redhat"}
617
618
	issued, _ := time.Parse("2006-01-02", "2015-12-15")
619
	// updated, _ := time.Parse("2006-01-02", "2015-12-16")
620
621
	var tests = []struct {
622
		in  string
623
		out []distroAdvisoryCveIDs
624
	}{
625
		{
626
			`===============================================================================
627
  Amazon Linux AMI 2014.03 - ALAS-2016-644: medium priority package update for python-rsa
628
===============================================================================
629
  Update ID : ALAS-2016-644
630
    Release :
631
       Type : security
632
     Status : final
633
     Issued : 2015-12-15 13:30
634
       CVEs : CVE-2016-1494
635
Description : Package updates are available for Amazon Linux AMI that fix the
636
            : CVE-20160-1111
637
            : hogehoge
638
   Severity : medium
639
640
===============================================================================
641
  Amazon Linux AMI 2014.03 - ALAS-2015-614: medium priority package update for openssl
642
===============================================================================
643
  Update ID : ALAS-2015-614
644
    Release :
645
       Type : security
646
     Status : final
647
     Issued : 2015-12-15 10:00
648
    Updated : 2015-12-16 14:15       CVEs : CVE-2015-3194
649
            : CVE-2015-3195
650
            : CVE-2015-3196
651
Description : Package updates are available for Amazon Linux AMI that fix the
652
            : foo bar baz
653
            : hoge fuga hega
654
   Severity : medium`,
655
656
			[]distroAdvisoryCveIDs{
657
				{
658
					DistroAdvisory: models.DistroAdvisory{
659
						AdvisoryID:  "ALAS-2016-644",
660
						Severity:    "medium",
661
						Issued:      issued,
662
						Description: "Package updates are available for Amazon Linux AMI that fix the\nCVE-20160-1111\nhogehoge\n",
663
					},
664
					CveIDs: []string{"CVE-2016-1494"},
665
				},
666
				{
667
					DistroAdvisory: models.DistroAdvisory{
668
						AdvisoryID:  "ALAS-2015-614",
669
						Severity:    "medium",
670
						Issued:      issued,
671
						Description: "Package updates are available for Amazon Linux AMI that fix the\nfoo bar baz\nhoge fuga hega\n",
672
					},
673
					CveIDs: []string{
674
						"CVE-2015-3194",
675
						"CVE-2015-3195",
676
						"CVE-2015-3196",
677
					},
678
				},
679
			},
680
		},
681
	}
682
683
	for _, tt := range tests {
684
		actual, _ := r.parseYumUpdateinfo(tt.in)
685
		for i, advisoryCveIDs := range actual {
686
			sort.Strings(tt.out[i].CveIDs)
687
			sort.Strings(actual[i].CveIDs)
688
			if !reflect.DeepEqual(tt.out[i], advisoryCveIDs) {
689
				e := pp.Sprintf("%v", tt.out[i])
690
				a := pp.Sprintf("%v", advisoryCveIDs)
691
				t.Errorf("[%d] Alas is not same. expected %s, actual %s",
692
					i, e, a)
693
			}
694
		}
695
	}
696
}
697
698
func TestParseYumCheckUpdateLine(t *testing.T) {
699
	r := newCentOS(config.ServerInfo{})
700
	r.Distro = config.Distro{Family: "centos"}
701
	var tests = []struct {
702
		in  string
703
		out models.Package
704
	}{
705
		{
706
			"zlib 0 1.2.7 17.el7 rhui-REGION-rhel-server-releases",
707
			models.Package{
708
				Name:       "zlib",
709
				NewVersion: "1.2.7",
710
				NewRelease: "17.el7",
711
				Repository: "rhui-REGION-rhel-server-releases",
712
			},
713
		},
714
		{
715
			"shadow-utils 2 4.1.5.1 24.el7 rhui-REGION-rhel-server-releases",
716
			models.Package{
717
				Name:       "shadow-utils",
718
				NewVersion: "2:4.1.5.1",
719
				NewRelease: "24.el7",
720
				Repository: "rhui-REGION-rhel-server-releases",
721
			},
722
		},
723
	}
724
725
	for _, tt := range tests {
726
		aPack, err := r.parseUpdatablePacksLine(tt.in)
727
		if err != nil {
728
			t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
729
			return
730
		}
731
		if !reflect.DeepEqual(tt.out, aPack) {
732
			e := pp.Sprintf("%v", tt.out)
733
			a := pp.Sprintf("%v", aPack)
734
			t.Errorf("expected %s, actual %s", e, a)
735
		}
736
	}
737
}
738
739
func TestParseYumCheckUpdateLines(t *testing.T) {
740
	r := newCentOS(config.ServerInfo{})
741
	r.Distro = config.Distro{Family: "centos"}
742
	stdout := `audit-libs 0 2.3.7 5.el6 base
743
bash 0 4.1.2 33.el6_7.1 updates
744
python-libs 0 2.6.6 64.el6 rhui-REGION-rhel-server-releases
745
python-ordereddict 0 1.1 3.el6ev installed
746
bind-utils 30 9.3.6 25.P1.el5_11.8 updates
747
pytalloc 0 2.0.7 2.el6 @CentOS 6.5/6.5`
748
749
	r.Packages = models.NewPackages(
750
		models.Package{Name: "audit-libs"},
751
		models.Package{Name: "bash"},
752
		models.Package{Name: "python-libs"},
753
		models.Package{Name: "python-ordereddict"},
754
		models.Package{Name: "bind-utils"},
755
		models.Package{Name: "pytalloc"},
756
	)
757
	var tests = []struct {
758
		in  string
759
		out models.Packages
760
	}{
761
		{
762
			stdout,
763
			models.NewPackages(
764
				models.Package{
765
					Name:       "audit-libs",
766
					NewVersion: "2.3.7",
767
					NewRelease: "5.el6",
768
					Repository: "base",
769
				},
770
				models.Package{
771
					Name:       "bash",
772
					NewVersion: "4.1.2",
773
					NewRelease: "33.el6_7.1",
774
					Repository: "updates",
775
				},
776
				models.Package{
777
					Name:       "python-libs",
778
					NewVersion: "2.6.6",
779
					NewRelease: "64.el6",
780
					Repository: "rhui-REGION-rhel-server-releases",
781
				},
782
				models.Package{
783
					Name:       "python-ordereddict",
784
					NewVersion: "1.1",
785
					NewRelease: "3.el6ev",
786
					Repository: "installed",
787
				},
788
				models.Package{
789
					Name:       "bind-utils",
790
					NewVersion: "30:9.3.6",
791
					NewRelease: "25.P1.el5_11.8",
792
					Repository: "updates",
793
				},
794
				models.Package{
795
					Name:       "pytalloc",
796
					NewVersion: "2.0.7",
797
					NewRelease: "2.el6",
798
					Repository: "@CentOS 6.5/6.5",
799
				},
800
			),
801
		},
802
	}
803
804
	for _, tt := range tests {
805
		packages, err := r.parseUpdatablePacksLines(tt.in)
806
		if err != nil {
807
			t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
808
			return
809
		}
810
		for name, ePack := range tt.out {
811
			if !reflect.DeepEqual(ePack, packages[name]) {
812
				e := pp.Sprintf("%v", ePack)
813
				a := pp.Sprintf("%v", packages[name])
814
				t.Errorf("expected %s, actual %s", e, a)
815
			}
816
		}
817
	}
818
}
819
820
func TestParseYumCheckUpdateLinesAmazon(t *testing.T) {
821
	r := newAmazon(config.ServerInfo{})
822
	r.Distro = config.Distro{Family: "amazon"}
823
	stdout := `bind-libs 32 9.8.2 0.37.rc1.45.amzn1 amzn-main
824
java-1.7.0-openjdk  0 1.7.0.95 2.6.4.0.65.amzn1 amzn-main
825
if-not-architecture 0 100 200 amzn-main`
826
	r.Packages = models.NewPackages(
827
		models.Package{Name: "bind-libs"},
828
		models.Package{Name: "java-1.7.0-openjdk"},
829
		models.Package{Name: "if-not-architecture"},
830
	)
831
	var tests = []struct {
832
		in  string
833
		out models.Packages
834
	}{
835
		{
836
			stdout,
837
			models.NewPackages(
838
				models.Package{
839
					Name:       "bind-libs",
840
					NewVersion: "32:9.8.2",
841
					NewRelease: "0.37.rc1.45.amzn1",
842
					Repository: "amzn-main",
843
				},
844
				models.Package{
845
					Name:       "java-1.7.0-openjdk",
846
					NewVersion: "1.7.0.95",
847
					NewRelease: "2.6.4.0.65.amzn1",
848
					Repository: "amzn-main",
849
				},
850
				models.Package{
851
					Name:       "if-not-architecture",
852
					NewVersion: "100",
853
					NewRelease: "200",
854
					Repository: "amzn-main",
855
				},
856
			),
857
		},
858
	}
859
860
	for _, tt := range tests {
861
		packages, err := r.parseUpdatablePacksLines(tt.in)
862
		if err != nil {
863
			t.Errorf("Error has occurred, err: %s\ntt.in: %v", err, tt.in)
864
			return
865
		}
866
		for name, ePack := range tt.out {
867
			if !reflect.DeepEqual(ePack, packages[name]) {
868
				e := pp.Sprintf("%v", ePack)
869
				a := pp.Sprintf("%v", packages[name])
870
				t.Errorf("[%s] expected %s, actual %s", name, e, a)
871
			}
872
		}
873
	}
874
}
875
876
func TestParseYumUpdateinfoListAvailable(t *testing.T) {
877
	r := newRHEL(config.ServerInfo{})
878
	rhelStdout := `RHSA-2015:2315 Moderate/Sec.  NetworkManager-1:1.0.6-27.el7.x86_64
879
RHSA-2015:2315 Moderate/Sec.  NetworkManager-config-server-1:1.0.6-27.el7.x86_64
880
RHSA-2015:1705 Important/Sec. bind-libs-lite-32:9.9.4-18.el7_1.5.x86_64
881
RHSA-2016:0176 Critical/Sec.  glibc-2.17-106.el7_2.4.x86_64
882
RHSA-2015:2401 Low/Sec.       grub2-1:2.02-0.29.el7.x86_64
883
RHSA-2015:2401 Low/Sec.       grub2-tools-1:2.02-0.29.el7.x86_64
884
updateinfo list done`
885
886
	var tests = []struct {
887
		in  string
888
		out []advisoryIDPacks
889
	}{
890
		{
891
			rhelStdout,
892
			[]advisoryIDPacks{
893
				{
894
					AdvisoryID: "RHSA-2015:2315",
895
					PackNames: []string{
896
						"NetworkManager",
897
						"NetworkManager-config-server",
898
					},
899
				},
900
				{
901
					AdvisoryID: "RHSA-2015:1705",
902
					PackNames: []string{
903
						"bind-libs-lite",
904
					},
905
				},
906
				{
907
					AdvisoryID: "RHSA-2016:0176",
908
					PackNames: []string{
909
						"glibc",
910
					},
911
				},
912
				{
913
					AdvisoryID: "RHSA-2015:2401",
914
					PackNames: []string{
915
						"grub2",
916
						"grub2-tools",
917
					},
918
				},
919
			},
920
		},
921
	}
922
923
	for _, tt := range tests {
924
		actual, err := r.parseYumUpdateinfoListAvailable(tt.in)
925
		if err != nil {
926
			t.Errorf("Error has occurred: %s", err)
927
			return
928
		}
929
930
		for i := range actual {
931
			if !reflect.DeepEqual(actual[i], tt.out[i]) {
932
				e := pp.Sprintf("%v", tt.out)
933
				a := pp.Sprintf("%v", actual)
934
				t.Errorf("[%d] expected: %s\nactual: %s", i, e, a)
935
			}
936
		}
937
	}
938
}
939
940
func TestExtractPackNameVerRel(t *testing.T) {
941
	r := newAmazon(config.ServerInfo{})
942
	var tests = []struct {
943
		in  string
944
		out []string
945
	}{
946
		{
947
			"openssh-server-6.2p2-8.45.amzn1.x86_64",
948
			[]string{"openssh-server", "6.2p2", "8.45.amzn1"},
949
		},
950
		{
951
			"bind-libs-lite-32:9.9.4-29.el7_2.1.x86_64",
952
			[]string{"bind-libs-lite", "32:9.9.4", "29.el7_2.1"},
953
		},
954
		{
955
			"glibc-2.17-106.el7_2.1.x86_64",
956
			[]string{"glibc", "2.17", "106.el7_2.1"},
957
		},
958
	}
959
960
	for _, tt := range tests {
961
		name, ver, rel := r.extractPackNameVerRel(tt.in)
962
		if tt.out[0] != name {
963
			t.Errorf("name: expected %s, actual %s", tt.out[0], name)
964
		}
965
		if tt.out[1] != ver {
966
			t.Errorf("ver: expected %s, actual %s", tt.out[1], ver)
967
		}
968
		if tt.out[2] != rel {
969
			t.Errorf("ver: expected %s, actual %s", tt.out[2], rel)
970
		}
971
	}
972
973
}
974
975
func TestGetDiffChangelog(t *testing.T) {
976
	r := newCentOS(config.ServerInfo{})
977
	type in struct {
978
		pack      models.Package
979
		changelog string
980
	}
981
982
	var tests = []struct {
983
		in  in
984
		out string
985
	}{
986
		// 0
987
		{
988
			in: in{
989
				pack: models.Package{
990
					Version: "2017a",
991
					Release: "1",
992
				},
993
				changelog: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> - 2017b-1
994
- Rebase to tzdata-2017b.
995
  - Haiti resumed DST on March 12, 2017.
996
997
* Thu Mar  2 12:00:00 2017 Patsy Franklin <[email protected]> - 2017a-1
998
- Rebase to tzdata-2017a
999
  - Mongolia no longer observes DST. (BZ #1425222)
1000
  - Add upstream patch to fix over-runing of POSIX limit on zone abbreviations.
1001
- Add zone1970.tab file to the install list. (BZ #1427694)
1002
1003
* Wed Nov 23 12:00:00 2016 Patsy Franklin <[email protected]> - 2016j-1
1004
- Rebase to tzdata-2016ij
1005
  - Saratov region of Russia is moving from +03 offset to +04 offset
1006
    on 2016-12-04.`,
1007
			},
1008
			out: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> - 2017b-1
1009
- Rebase to tzdata-2017b.
1010
  - Haiti resumed DST on March 12, 2017.`,
1011
		},
1012
		// 1
1013
		{
1014
			in: in{
1015
				pack: models.Package{
1016
					Version: "2004e",
1017
					Release: "2",
1018
				},
1019
				changelog: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> - 2017b-1
1020
- Rebase to tzdata-2017b.
1021
  - Haiti resumed DST on March 12, 2017.
1022
1023
* Wed Nov 23 12:00:00 2016 Patsy Franklin <[email protected]> - 2016j-1
1024
- Rebase to tzdata-2016ij
1025
  - Saratov region of Russia is moving from +03 offset to +04 offset
1026
    on 2016-12-04.
1027
1028
* Mon Nov 29 12:00:00 2004 Jakub Jelinek <[email protected]> 2004g-1
1029
- 2004g (#141107)
1030
- updates for Cuba
1031
1032
* Mon Oct 11 12:00:00 2004 Jakub Jelinek <[email protected]> 2004e-2
1033
- 2004e (#135194)
1034
- updates for Brazil, Uruguay and Argentina`,
1035
			},
1036
			out: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> - 2017b-1
1037
- Rebase to tzdata-2017b.
1038
  - Haiti resumed DST on March 12, 2017.
1039
1040
* Wed Nov 23 12:00:00 2016 Patsy Franklin <[email protected]> - 2016j-1
1041
- Rebase to tzdata-2016ij
1042
  - Saratov region of Russia is moving from +03 offset to +04 offset
1043
    on 2016-12-04.
1044
1045
* Mon Nov 29 12:00:00 2004 Jakub Jelinek <[email protected]> 2004g-1
1046
- 2004g (#141107)
1047
- updates for Cuba`,
1048
		},
1049
		// 2
1050
		{
1051
			in: in{
1052
				pack: models.Package{
1053
					Version: "2016j",
1054
					Release: "1",
1055
				},
1056
				changelog: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> -2017b-1
1057
- Rebase to tzdata-2017b.
1058
  - Haiti resumed DST on March 12, 2017.
1059
1060
* Wed Nov 23 12:00:00 2016 Patsy Franklin <[email protected]> -2016j-1
1061
- Rebase to tzdata-2016ij
1062
  - Saratov region of Russia is moving from +03 offset to +04 offset
1063
    on 2016-12-04.`,
1064
			},
1065
			out: `* Mon Mar 20 12:00:00 2017 Patsy Franklin <[email protected]> -2017b-1
1066
- Rebase to tzdata-2017b.
1067
  - Haiti resumed DST on March 12, 2017.`,
1068
		},
1069
		// 3
1070
		{
1071
			in: in{
1072
				pack: models.Package{
1073
					Version: "3.10.0",
1074
					Release: "327.22.1.el7",
1075
				},
1076
				changelog: `* Thu Jun  9 21:00:00 2016 Alexander Gordeev <[email protected]> [3.10.0-327.22.2.el7]
1077
- [infiniband] security: Restrict use of the write() interface (Don Dutile) [1332553 1316685] {CVE-2016-4565}
1078
1079
* Mon May 16 21:00:00 2016 Alexander Gordeev <[email protected]> [3.10.0-327.22.1.el7]
1080
- [mm] mmu_notifier: fix memory corruption (Jerome Glisse) [1335727 1307042]
1081
- [misc] cxl: Increase timeout for detection of AFU mmio hang (Steve Best) [1335419 1329682]
1082
- [misc] cxl: Configure the PSL for two CAPI ports on POWER8NVL (Steve Best) [1336389 1278793]`,
1083
			},
1084
			out: `* Thu Jun  9 21:00:00 2016 Alexander Gordeev <[email protected]> [3.10.0-327.22.2.el7]
1085
- [infiniband] security: Restrict use of the write() interface (Don Dutile) [1332553 1316685] {CVE-2016-4565}`,
1086
		},
1087
		// 4
1088
		{
1089
			in: in{
1090
				pack: models.Package{
1091
					Version: "6.6.1p1",
1092
					Release: "34",
1093
				},
1094
1095
				changelog: `* Wed Mar  1 21:00:00 2017 Jakub Jelen <[email protected]> - 6.6.1p1-35 + 0.9.3-9
1096
- Do not send SD_NOTIFY from forked childern (#1381997)
1097
1098
* Fri Feb 24 21:00:00 2017 Jakub Jelen <[email protected]> - 6.6.1p1-34 + 0.9.3-9
1099
- Add SD_NOTIFY code to help systemd to track running service (#1381997)`,
1100
			},
1101
			out: `* Wed Mar  1 21:00:00 2017 Jakub Jelen <[email protected]> - 6.6.1p1-35
1102
- Do not send SD_NOTIFY from forked childern (#1381997)`,
1103
		},
1104
		// 5
1105
		{
1106
			in: in{
1107
				pack: models.Package{
1108
					Version: "2.1.23",
1109
					Release: "15.el6",
1110
				},
1111
				changelog: `* Fri Feb 27 12:00:00 2015 Jakub Jelen <[email protected]> 2.1.23-15.2
1112
- Support AIX SASL GSSAPI (#1174315)
1113
1114
* Tue Nov 18 12:00:00 2014 Petr Lautrbach <[email protected]> 2.1.23-15.1
1115
- check a context value in sasl_gss_encode() (#1087221)
1116
1117
* Mon Jun 23 12:00:00 2014 Petr Lautrbach <[email protected]> 2.1.23-15
1118
- don't use " for saslauth user's description (#1081445)
1119
- backport the ad_compat option (#994242)
1120
- fixed a memory leak in the client side DIGEST-MD5 code (#838628)`,
1121
			},
1122
			out: `* Fri Feb 27 12:00:00 2015 Jakub Jelen <[email protected]> 2.1.23-15.2
1123
- Support AIX SASL GSSAPI (#1174315)
1124
1125
* Tue Nov 18 12:00:00 2014 Petr Lautrbach <[email protected]> 2.1.23-15.1
1126
- check a context value in sasl_gss_encode() (#1087221)`,
1127
		},
1128
		// 6
1129
		{
1130
			in: in{
1131
				pack: models.Package{
1132
					Version: "3.6.20",
1133
					Release: "1.el6",
1134
				},
1135
				changelog: `* Wed Jul 29 12:00:00 2015 Jan Stanek <[email protected]> - 3.6.20-1.2
1136
- Add patch for compiler warnings highlighted by rpmdiff.
1137
  Related: rhbz#1244727
1138
1139
* Wed Jul 22 12:00:00 2015 Viktor Jancik <[email protected]> - 3.6.20-1.el6_7.1
1140
- fix for CVE-2015-3416
1141
  Resolves: #1244727
1142
1143
* Tue Nov 17 12:00:00 2009 Panu Matilainen <[email protected]> - 3.6.20-1
1144
- update to 3.6.20 (http://www.sqlite.org/releaselog/3_6_20.html)
1145
1146
* Tue Oct  6 12:00:00 2009 Panu Matilainen <[email protected]> - 3.6.18-1
1147
- update to 3.6.18 (http://www.sqlite.org/releaselog/3_6_18.html)
1148
- drop no longer needed test-disabler patches`,
1149
			},
1150
			out: `* Wed Jul 29 12:00:00 2015 Jan Stanek <[email protected]> - 3.6.20-1.2
1151
- Add patch for compiler warnings highlighted by rpmdiff.
1152
  Related: rhbz#1244727
1153
1154
* Wed Jul 22 12:00:00 2015 Viktor Jancik <[email protected]> - 3.6.20-1.el6_7.1
1155
- fix for CVE-2015-3416
1156
  Resolves: #1244727`,
1157
		},
1158
		/*
1159
					// 7
1160
					{
1161
						in: in{
1162
							pack: models.Package{
1163
								Version: "2:7.4.160",
1164
								Release: "1.el7",
1165
							},
1166
							changelog: `* Mon Dec 12 21:00:00 2016 Karsten Hopp <[email protected]> 7.4.160-1.1
1167
			- add fix for CVE-2016-1248
1168
1169
			* Wed Jan 29 21:00:00 2014 Karsten Hopp <[email protected]> 7.4.160-1
1170
			- patchlevel 160
1171
			- Resolves: rhbz#1059321`,
1172
						},
1173
						out: `* Mon Dec 12 21:00:00 2016 Karsten Hopp <[email protected]> 7.4.160-1.1
1174
			- add fix for CVE-2016-1248`,
1175
					},
1176
					// 8
1177
					{
1178
						in: in{
1179
							pack: models.Package{
1180
								Version: "2:1.26",
1181
								Release: "29.el7",
1182
							},
1183
							changelog: `* Mon Jun 20 21:00:00 2016 Pavel Raiskup <[email protected]> - 1.26-31
1184
			- avoid double free in selinux code (rhbz#1347396)
1185
1186
			* Thu Jun  4 21:00:00 2015 Pavel Raiskup <[email protected]> - 1.26-30
1187
			- don't mistakenly set default ACLs (#1220890)
1188
1189
			* Fri Jan 24 21:00:00 2014 Daniel Mach <[email protected]> - 2:1.26-29
1190
			- Mass rebuild 2014-01-24`,
1191
						},
1192
						out: `* Mon Jun 20 21:00:00 2016 Pavel Raiskup <[email protected]> - 1.26-31
1193
			- avoid double free in selinux code (rhbz#1347396)
1194
1195
			* Thu Jun  4 21:00:00 2015 Pavel Raiskup <[email protected]> - 1.26-30
1196
			- don't mistakenly set default ACLs (#1220890)`,
1197
					},
1198
					// 9
1199
					{
1200
						in: in{
1201
							pack: models.Package{
1202
								Version: "1:1.0.1e",
1203
								Release: "51.el7_2.5",
1204
							},
1205
							changelog: `* Mon Feb  6 21:00:00 2017 Tomáš Mráz <[email protected]> 1.0.1e-60.1
1206
			- fix CVE-2017-3731 - DoS via truncated packets with RC4-MD5 cipher
1207
			- fix CVE-2016-8610 - DoS of single-threaded servers via excessive alerts
1208
1209
			* Fri Dec  4 21:00:00 2015 Tomáš Mráz <[email protected]> 1.0.1e-52
1210
			- fix CVE-2015-3194 - certificate verify crash with missing PSS parameter
1211
			- fix CVE-2015-3195 - X509_ATTRIBUTE memory leak
1212
			- fix CVE-2015-3196 - race condition when handling PSK identity hint
1213
1214
			* Tue Jun 23 21:00:00 2015 Tomáš Mráz <[email protected]> 1.0.1e-51
1215
			- fix the CVE-2015-1791 fix (broken server side renegotiation)`,
1216
						},
1217
						out: `* Mon Feb  6 21:00:00 2017 Tomáš Mráz <[email protected]> 1.0.1e-60.1
1218
			- fix CVE-2017-3731 - DoS via truncated packets with RC4-MD5 cipher
1219
			- fix CVE-2016-8610 - DoS of single-threaded servers via excessive alerts
1220
1221
			* Fri Dec  4 21:00:00 2015 Tomáš Mráz <[email protected]> 1.0.1e-52
1222
			- fix CVE-2015-3194 - certificate verify crash with missing PSS parameter
1223
			- fix CVE-2015-3195 - X509_ATTRIBUTE memory leak
1224
			- fix CVE-2015-3196 - race condition when handling PSK identity hint`,
1225
					},
1226
					// 10
1227
					{
1228
						in: in{
1229
							pack: models.Package{
1230
								Version: "1:5.5.47",
1231
								Release: "1.el7_2",
1232
							},
1233
							changelog: `* Wed Sep 21 21:00:00 2016 Honza Horak <[email protected]> - 5.5.52-1
1234
			- Rebase to 5.5.52, that also include fix for CVE-2016-6662
1235
			  Resolves: #1377974
1236
1237
			* Thu Feb 18 21:00:00 2016 Jakub Dorňák <[email protected]> - 1:5.5.47-2
1238
			- Add warning to /usr/lib/tmpfiles.d/mariadb.conf
1239
			  Resolves: #1241623
1240
1241
			* Wed Feb  3 21:00:00 2016 Jakub Dorňák <[email protected]> - 1:5.5.47-1
1242
			- Rebase to 5.5.47
1243
			  Also fixes: CVE-2015-4792 CVE-2015-4802 CVE-2015-4815 CVE-2015-4816
1244
			  CVE-2015-4819 CVE-2015-4826 CVE-2015-4830 CVE-2015-4836 CVE-2015-4858
1245
			  CVE-2015-4861 CVE-2015-4870 CVE-2015-4879 CVE-2015-4913 CVE-2015-7744
1246
			  CVE-2016-0505 CVE-2016-0546 CVE-2016-0596 CVE-2016-0597 CVE-2016-0598
1247
			  CVE-2016-0600 CVE-2016-0606 CVE-2016-0608 CVE-2016-0609 CVE-2016-0616
1248
			  CVE-2016-2047
1249
			  Resolves: #1300621`,
1250
						},
1251
						out: `* Wed Sep 21 21:00:00 2016 Honza Horak <[email protected]> - 5.5.52-1
1252
			- Rebase to 5.5.52, that also include fix for CVE-2016-6662
1253
			  Resolves: #1377974
1254
1255
			* Thu Feb 18 21:00:00 2016 Jakub Dorňák <[email protected]> - 1:5.5.47-2
1256
			- Add warning to /usr/lib/tmpfiles.d/mariadb.conf
1257
			  Resolves: #1241623`,
1258
					},
1259
		*/
1260
		// 11
1261
		{
1262
			in: in{
1263
				pack: models.Package{
1264
					Version: "0.252",
1265
					Release: "8.1.el7",
1266
				},
1267
				changelog: `* Thu Sep 29 21:00:00 2016 Vitezslav Crhonek <[email protected]> - 0.252-8.4
1268
- Remove wrong entry from usb ids.
1269
  Resolves: #1380159
1270
1271
* Mon Sep 26 21:00:00 2016 Vitezslav Crhonek <[email protected]> - 0.252-8.3
1272
- Updated pci, usb and vendor ids.
1273
- Resolves: rhbz#1292382
1274
1275
* Tue Jun 28 21:00:00 2016 Michal Minar <[email protected]> 0.252-8.2
1276
- Updated pci, usb and vendor ids.
1277
- Resolves: rhbz#1292382
1278
- Resolves: rhbz#1291614
1279
- Resolves: rhbz#1324198
1280
1281
* Fri Oct 23 21:00:00 2015 Michal Minar <[email protected]> 0.252-8.1
1282
- Updated pci, usb and vendor ids.`,
1283
			},
1284
			out: `* Thu Sep 29 21:00:00 2016 Vitezslav Crhonek <[email protected]> - 0.252-8.4
1285
- Remove wrong entry from usb ids.
1286
  Resolves: #1380159
1287
1288
* Mon Sep 26 21:00:00 2016 Vitezslav Crhonek <[email protected]> - 0.252-8.3
1289
- Updated pci, usb and vendor ids.
1290
- Resolves: rhbz#1292382
1291
1292
* Tue Jun 28 21:00:00 2016 Michal Minar <[email protected]> 0.252-8.2
1293
- Updated pci, usb and vendor ids.
1294
- Resolves: rhbz#1292382
1295
- Resolves: rhbz#1291614
1296
- Resolves: rhbz#1324198`,
1297
		},
1298
		// 12
1299
		{
1300
			in: in{
1301
				pack: models.Package{
1302
					Version: "1:2.02",
1303
					Release: "0.34.el7_2",
1304
				},
1305
				changelog: `* Mon Aug 29 21:00:00 2016 Peter Jones <[email protected]> - 2.02-0.44
1306
- Work around tftp servers that don't work with multiple consecutive slashes in
1307
  file paths.
1308
  Resolves: rhbz#1217243`,
1309
			},
1310
			out: `* Mon Aug 29 21:00:00 2016 Peter Jones <[email protected]> - 2.02-0.44
1311
- Work around tftp servers that don't work with multiple consecutive slashes in
1312
  file paths.
1313
  Resolves: rhbz#1217243`,
1314
		},
1315
	}
1316
1317
	for i, tt := range tests {
1318
		diff, _ := r.getDiffChangelog(tt.in.pack, tt.in.changelog)
1319
		if tt.out != diff {
1320
			t.Errorf("[%d] name: expected \n%s\nactual \n%s", i, tt.out, diff)
1321
		}
1322
	}
1323
1324
}
1325
1326
func TestDivideChangelogsIntoEachPackages(t *testing.T) {
1327
	r := newRHEL(config.ServerInfo{})
1328
	type in struct {
1329
		pack      models.Package
1330
		changelog string
1331
	}
1332
1333
	var tests = []struct {
1334
		in  string
1335
		out map[string]string
1336
	}{
1337
		{
1338
			in: `==================== Updated Packages ====================
1339
1:NetworkManager-1.4.0-20.el7_3.x86_64   rhui-rhel-7-server-rhui-rpms
1340
* Mon Apr 24 21:00:00 2017 Beniamino Galvani <[email protected]> - 1:1.4.0-20
1341
- vlan: use parent interface mtu as default (rh#1414186)
1342
1343
* Wed Mar 29 21:00:00 2017 Beniamino Galvani <[email protected]> - 1:1.4.0-19
1344
- core: alyways force a sync of the default route (rh#1431268)
1345
1346
1347
1:NetworkManager-0.9.9.1-25.git20140326. rhui-rhel-7-server-rhui-optional-rpms
1348
* Tue Jul  1 21:00:00 2014 Jiří Klimeš <[email protected]> - 1:0.9.9.1-25.git20140326
1349
- core: fix MTU handling while merging/subtracting IP configs (rh #1093231)
1350
1351
* Mon Jun 23 21:00:00 2014 Thomas Haller <[email protected]> - 1:0.9.9.1-24.git20140326
1352
- core: fix crash on failure of reading bridge sysctl values (rh #1112020)`,
1353
			out: map[string]string{
1354
				"1:NetworkManager-1.4.0-20.el7_3.x86_64": `* Mon Apr 24 21:00:00 2017 Beniamino Galvani <[email protected]> - 1:1.4.0-20
1355
- vlan: use parent interface mtu as default (rh#1414186)
1356
1357
* Wed Mar 29 21:00:00 2017 Beniamino Galvani <[email protected]> - 1:1.4.0-19
1358
- core: alyways force a sync of the default route (rh#1431268)`,
1359
1360
				"1:NetworkManager-0.9.9.1-25.git20140326.": `* Tue Jul  1 21:00:00 2014 Jiří Klimeš <[email protected]> - 1:0.9.9.1-25.git20140326
1361
- core: fix MTU handling while merging/subtracting IP configs (rh #1093231)
1362
1363
* Mon Jun 23 21:00:00 2014 Thomas Haller <[email protected]> - 1:0.9.9.1-24.git20140326
1364
- core: fix crash on failure of reading bridge sysctl values (rh #1112020)`,
1365
			},
1366
		},
1367
	}
1368
1369
	for _, tt := range tests {
1370
		changelogs := r.divideChangelogsIntoEachPackages(tt.in)
1371
		for k, v := range tt.out {
1372
			if strings.TrimSpace(v) != strings.TrimSpace(changelogs[k]) {
1373
				t.Errorf("expected: %v\nactual: %v", pp.Sprint(tt.out), pp.Sprint(changelogs))
1374
			}
1375
		}
1376
	}
1377
1378
}
1379
1380
func TestCheckYumPsInstalled(t *testing.T) {
1381
	r := newCentOS(config.ServerInfo{})
1382
	var tests = []struct {
1383
		in  string
1384
		out bool
1385
	}{
1386
		{
1387
			in: `Loaded plugins: changelog, fastestmirror, ps, remove-with-leaves, show-leaves
1388
Loading mirror speeds from cached hostfile
1389
 * base: ftp.tsukuba.wide.ad.jp
1390
 * extras: ftp.tsukuba.wide.ad.jp
1391
 * updates: ftp.tsukuba.wide.ad.jp
1392
Installed Packages
1393
Name        : yum
1394
Arch        : noarch
1395
Version     : 3.4.3
1396
Release     : 150.el7.centos
1397
Size        : 5.5 M
1398
Repo        : installed
1399
From repo   : anaconda
1400
Summary     : RPM package installer/updater/manager
1401
URL         : http://yum.baseurl.org/
1402
License     : GPLv2+
1403
Description : Yum is a utility that can check for and automatically download and
1404
            : install updated RPM packages. Dependencies are obtained and downloaded
1405
            : automatically, prompting the user for permission as necessary.
1406
1407
Available Packages
1408
Name        : yum
1409
Arch        : noarch
1410
Version     : 3.4.3
1411
Release     : 154.el7.centos.1
1412
Size        : 1.2 M
1413
Repo        : updates/7/x86_64
1414
Summary     : RPM package installer/updater/manager
1415
URL         : http://yum.baseurl.org/
1416
License     : GPLv2+
1417
Description : Yum is a utility that can check for and automatically download and
1418
            : install updated RPM packages. Dependencies are obtained and downloaded
1419
            : automatically, prompting the user for permission as necessary.`,
1420
			out: true,
1421
		},
1422
		{
1423
			in: `Failed to set locale, defaulting to C
1424
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
1425
Installed Packages
1426
Name        : yum
1427
Arch        : noarch
1428
Version     : 3.4.3
1429
Release     : 154.el7
1430
Size        : 5.5 M
1431
Repo        : installed
1432
From repo   : rhui-REGION-rhel-server-releases
1433
Summary     : RPM package installer/updater/manager
1434
URL         : http://yum.baseurl.org/
1435
License     : GPLv2+
1436
Description : Yum is a utility that can check for and automatically download and
1437
            : install updated RPM packages. Dependencies are obtained and downloaded
1438
            : automatically, prompting the user for permission as necessary.`,
1439
			out: false,
1440
		},
1441
	}
1442
1443
	for _, tt := range tests {
1444
		ok := r.checkYumPsInstalled(tt.in)
1445
		if ok != tt.out {
1446
			t.Errorf("expected: %v\nactual: %v", tt.out, ok)
1447
		}
1448
	}
1449
}
1450
1451
func TestParseYumPS(t *testing.T) {
1452
	r := newCentOS(config.ServerInfo{})
1453
	r.Distro = config.Distro{Family: "centos"}
1454
	r.Packages = models.NewPackages(
1455
		models.Package{
1456
			Name:    "python",
1457
			Version: "2.7.5",
1458
			Release: "34.el7",
1459
			Arch:    "x86_64",
1460
		},
1461
		models.Package{
1462
			Name:    "util-linux",
1463
			Version: "2.23.2",
1464
			Release: "26.el7",
1465
			Arch:    "x86_64",
1466
		},
1467
		models.Package{
1468
			Name:    "wpa_supplicant",
1469
			Version: "1:2.0",
1470
			Release: "17.el7_1",
1471
			Arch:    "x86_64",
1472
		},
1473
		models.Package{
1474
			Name:    "yum",
1475
			Version: "3.4.3",
1476
			Release: "150.el7.centos",
1477
			Arch:    "noarch",
1478
		},
1479
	)
1480
1481
	var tests = []struct {
1482
		in  string
1483
		out models.Packages
1484
	}{
1485
		{
1486
			`       pid proc                  CPU      RSS      State uptime
1487
python-2.7.5-34.el7.x86_64 Upgrade 2.7.5-48.el7.x86_64
1488
       741 tuned                1:54    16 MB   Sleeping:  14 day(s) 21:52:32
1489
     38755 yum                  0:00    42 MB    Running:  00:00
1490
util-linux-2.23.2-26.el7.x86_64 Upgrade 2.23.2-33.el7_3.2.x86_64
1491
       626 agetty               0:00   848 kB   Sleeping:  14 day(s) 21:52:37
1492
       628 agetty               0:00   848 kB   Sleeping:  14 day(s) 21:52:37
1493
1:wpa_supplicant-2.0-17.el7_1.x86_64 Upgrade 1:2.0-21.el7_3.x86_64
1494
       638 wpa_supplicant       0:00   2.6 MB   Sleeping:  14 day(s) 21:52:37
1495
yum-3.4.3-150.el7.centos.noarch
1496
     38755 yum                  0:00    42 MB    Running:  00:00
1497
ps
1498
	 `,
1499
			models.NewPackages(
1500
				models.Package{
1501
					Name:    "python",
1502
					Version: "2.7.5",
1503
					Release: "34.el7",
1504
					Arch:    "x86_64",
1505
					// NewVersion: "2.7.5-",
1506
					// NewRelease: "48.el7.x86_64",
1507
					AffectedProcs: []models.AffectedProcess{
1508
						{
1509
							PID:  "741",
1510
							Name: "tuned",
1511
						},
1512
						{
1513
							PID:  "38755",
1514
							Name: "yum",
1515
						},
1516
					},
1517
				},
1518
				models.Package{
1519
					Name:    "util-linux",
1520
					Version: "2.23.2",
1521
					Release: "26.el7",
1522
					Arch:    "x86_64",
1523
					// NewVersion: "2.7.5",
1524
					// NewRelease: "48.el7.x86_64",
1525
					AffectedProcs: []models.AffectedProcess{
1526
						{
1527
							PID:  "626",
1528
							Name: "agetty",
1529
						},
1530
						{
1531
							PID:  "628",
1532
							Name: "agetty",
1533
						},
1534
					},
1535
				},
1536
				models.Package{
1537
					Name:    "wpa_supplicant",
1538
					Version: "1:2.0",
1539
					Release: "17.el7_1",
1540
					Arch:    "x86_64",
1541
					// NewVersion: "1:2.0",
1542
					// NewRelease: "21.el7_3.x86_64",
1543
					AffectedProcs: []models.AffectedProcess{
1544
						{
1545
							PID:  "638",
1546
							Name: "wpa_supplicant",
1547
						},
1548
					},
1549
				},
1550
			),
1551
		},
1552
		{
1553
			`    pid proc                  CPU      RSS      State uptime
1554
acpid-2.0.19-6.7.amzn1.x86_64
1555
      2388 acpid                0:00   1.4 MB   Sleeping:  21:08
1556
at-3.1.10-48.15.amzn1.x86_64
1557
      2546 atd                  0:00   164 kB   Sleeping:  21:06
1558
cronie-anacron-1.4.4-15.8.amzn1.x86_64
1559
      2637 anacron              0:00   1.5 MB   Sleeping:  13:14
1560
12:dhclient-4.1.1-51.P1.26.amzn1.x86_64
1561
      2061 dhclient             0:00   1.4 MB   Sleeping:  21:10
1562
      2193 dhclient             0:00   2.1 MB   Sleeping:  21:08
1563
mingetty-1.08-5.9.amzn1.x86_64
1564
      2572 mingetty             0:00   1.4 MB   Sleeping:  21:06
1565
      2575 mingetty             0:00   1.4 MB   Sleeping:  21:06
1566
      2578 mingetty             0:00   1.5 MB   Sleeping:  21:06
1567
      2580 mingetty             0:00   1.4 MB   Sleeping:  21:06
1568
      2582 mingetty             0:00   1.4 MB   Sleeping:  21:06
1569
      2584 mingetty             0:00   1.4 MB   Sleeping:  21:06
1570
openssh-server-6.6.1p1-33.66.amzn1.x86_64
1571
      2481 sshd                 0:00   2.6 MB   Sleeping:  21:07
1572
python27-2.7.12-2.120.amzn1.x86_64
1573
      2649 yum                  0:00    35 MB    Running:  00:01
1574
rsyslog-5.8.10-9.26.amzn1.x86_64
1575
      2261 rsyslogd             0:00   2.6 MB   Sleeping:  21:08
1576
udev-173-4.13.amzn1.x86_64
1577
      1528 udevd                0:00   2.5 MB   Sleeping:  21:12
1578
      1652 udevd                0:00   2.1 MB   Sleeping:  21:12
1579
      1653 udevd                0:00   2.0 MB   Sleeping:  21:12
1580
upstart-0.6.5-13.3.13.amzn1.x86_64
1581
         1 init                 0:00   2.5 MB   Sleeping:  21:13
1582
util-linux-2.23.2-33.28.amzn1.x86_64
1583
      2569 agetty               0:00   1.6 MB   Sleeping:  21:06
1584
yum-3.4.3-150.70.amzn1.noarch
1585
      2649 yum                  0:00    35 MB    Running:  00:01
1586
`,
1587
			models.Packages{},
1588
		},
1589
	}
1590
1591
	for _, tt := range tests {
1592
		packages := r.parseYumPS(tt.in)
1593
		for name, ePack := range tt.out {
1594
			if !reflect.DeepEqual(ePack, packages[name]) {
1595
				e := pp.Sprintf("%v", ePack)
1596
				a := pp.Sprintf("%v", packages[name])
1597
				t.Errorf("expected %s, actual %s", e, a)
1598
			}
1599
		}
1600
	}
1601
}
1602
1603
func TestParseNeedsRestarting(t *testing.T) {
1604
	r := newCentOS(config.ServerInfo{})
1605
	r.Distro = config.Distro{Family: "centos"}
1606
1607
	var tests = []struct {
1608
		in  string
1609
		out []models.NeedRestartProcess
1610
	}{
1611
		{
1612
			`1 : /usr/lib/systemd/systemd --switched-root --system --deserialize 21
1613
437 : /usr/sbin/NetworkManager --no-daemon`,
1614
			[]models.NeedRestartProcess{
1615
				{
1616
					PID:     "437",
1617
					Path:    "/usr/sbin/NetworkManager --no-daemon",
1618
					HasInit: true,
1619
				},
1620
			},
1621
		},
1622
	}
1623
1624
	for _, tt := range tests {
1625
		procs := r.parseNeedsRestarting(tt.in)
1626
		if !reflect.DeepEqual(tt.out, procs) {
1627
			t.Errorf("expected %#v, actual %#v", tt.out, procs)
1628
		}
1629
	}
1630
}
1631
1632
func TestIsExecScanUsingYum(t *testing.T) {
1633
	r := newRHEL(config.ServerInfo{})
1634
	var tests = []struct {
1635
		modes  []byte
1636
		family string
1637
		out    bool
1638
	}{
1639
		{
1640
			modes: []byte{config.Offline},
1641
			out:   false,
1642
		},
1643
		{
1644
			modes:  []byte{},
1645
			family: config.CentOS,
1646
			out:    false,
1647
		},
1648
		{
1649
			family: config.Amazon,
1650
			modes:  []byte{config.FastRoot},
1651
			out:    true,
1652
		},
1653
		{
1654
			family: config.Amazon,
1655
			modes:  []byte{config.Deep},
1656
			out:    true,
1657
		},
1658
	}
1659
1660
	for i, tt := range tests {
1661
		r.Distro.Family = tt.family
1662
		mode := config.ScanMode{}
1663
		for _, m := range tt.modes {
1664
			mode.Set(m)
1665
		}
1666
1667
		si := r.getServerInfo()
1668
		si.Mode = mode
1669
		r.setServerInfo(si)
1670
1671
		out := r.isExecScanUsingYum()
1672
		if out != tt.out {
1673
			t.Errorf("[%d] expected %#v, actual %#v", i, tt.out, out)
1674
		}
1675
	}
1676
}
1677
1678
func TestIsExecFillChangelogs(t *testing.T) {
1679
	r := newRHEL(config.ServerInfo{})
1680
	var tests = []struct {
1681
		modes  []byte
1682
		family string
1683
		out    bool
1684
	}{
1685
		{
1686
			modes: []byte{config.Offline},
1687
			out:   false,
1688
		},
1689
		{
1690
			modes:  []byte{config.Deep},
1691
			family: config.CentOS,
1692
			out:    true,
1693
		},
1694
		{
1695
			family: config.Amazon,
1696
			modes:  []byte{config.Deep},
1697
			out:    false,
1698
		},
1699
		{
1700
			modes:  []byte{config.Deep},
1701
			family: config.RedHat,
1702
			out:    true,
1703
		},
1704
	}
1705
1706
	for i, tt := range tests {
1707
		r.Distro.Family = tt.family
1708
		mode := config.ScanMode{}
1709
		for _, m := range tt.modes {
1710
			mode.Set(m)
1711
		}
1712
1713
		si := r.getServerInfo()
1714
		si.Mode = mode
1715
		r.setServerInfo(si)
1716
		out := r.isExecFillChangelogs()
1717
		if out != tt.out {
1718
			t.Errorf("[%d] expected %#v, actual %#v", i, tt.out, out)
1719
		}
1720
	}
1721
}
1722
1723
func TestIsScanChangelogs(t *testing.T) {
1724
	r := newCentOS(config.ServerInfo{})
1725
	var tests = []struct {
1726
		modes  []byte
1727
		family string
1728
		out    bool
1729
	}{
1730
		{
1731
			modes: []byte{config.Offline},
1732
			out:   false,
1733
		},
1734
		{
1735
			modes: []byte{config.Fast},
1736
			out:   false,
1737
		},
1738
		{
1739
			modes: []byte{config.FastRoot},
1740
			out:   false,
1741
		},
1742
		{
1743
			modes:  []byte{config.Deep},
1744
			family: config.RedHat,
1745
			out:    true,
1746
		},
1747
	}
1748
1749
	for i, tt := range tests {
1750
		r.Distro.Family = tt.family
1751
		mode := config.ScanMode{}
1752
		for _, m := range tt.modes {
1753
			mode.Set(m)
1754
		}
1755
1756
		si := r.getServerInfo()
1757
		si.Mode = mode
1758
		r.setServerInfo(si)
1759
		out := r.isExecScanChangelogs()
1760
		if out != tt.out {
1761
			t.Errorf("[%d] expected %#v, actual %#v", i, tt.out, out)
1762
		}
1763
	}
1764
}
1765