summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/distinfo_test.go
blob: 1231b8f381905a3847d14d0aabc474cee81a2603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package pkglint

import "gopkg.in/check.v1"

func (s *Suite) Test_CheckLinesDistinfo__parse_errors(c *check.C) {
	t := s.Init(c)

	t.Chdir("category/package")
	t.CreateFileLines("patches/patch-aa",
		RcsID+" line is ignored for computing the SHA1 hash",
		"patch contents")
	t.CreateFileLines("patches/patch-ab",
		"patch contents")
	lines := t.SetUpFileLines("distinfo",
		"should be the RCS ID",
		"should be empty",
		"MD5 (distfile.tar.gz) = 12345678901234567890123456789012",
		"SHA1 (distfile.tar.gz) = 1234567890123456789012345678901234567890",
		"SHA1 (patch-aa) = 6b98dd609f85a9eb9c4c1e4e7055a6aaa62b7cc7",
		"Size (patch-aa) = 104",
		"SHA1 (patch-ab) = 6b98dd609f85a9eb9c4c1e4e7055a6aaa62b7cc7",
		"Another invalid line",
		"SHA1 (patch-nonexistent) = 1234")
	G.Pkg = NewPackage(".")

	CheckLinesDistinfo(lines)

	t.CheckOutputLines(
		"ERROR: distinfo:1: Expected \"$"+"NetBSD$\".",
		"NOTE: distinfo:1: Empty line expected before this line.",
		"ERROR: distinfo:1: Invalid line: should be the RCS ID",
		"ERROR: distinfo:2: Invalid line: should be empty",
		"ERROR: distinfo:8: Invalid line: Another invalid line",
		"ERROR: distinfo:3: Expected SHA1, RMD160, SHA512, Size checksums for \"distfile.tar.gz\", got MD5, SHA1.",
		"ERROR: distinfo:5: Expected SHA1 hash for patch-aa, got SHA1, Size.",
		"WARN: distinfo:9: Patch file \"patch-nonexistent\" does not exist in directory \"patches\".")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__nonexistent_distfile_called_patch(c *check.C) {
	t := s.Init(c)

	t.Chdir("category/package")
	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"MD5 (patch-5.3.tar.gz) = 12345678901234567890123456789012",
		"SHA1 (patch-5.3.tar.gz) = 1234567890123456789012345678901234567890")
	G.Pkg = NewPackage(".")

	CheckLinesDistinfo(lines)

	// Even though the filename starts with "patch-" and therefore looks like
	// a patch, it is a normal distfile because it has other hash algorithms
	// than exactly SHA1.
	t.CheckOutputLines(
		"ERROR: distinfo:3: Expected SHA1, RMD160, SHA512, Size checksums " +
			"for \"patch-5.3.tar.gz\", got MD5, SHA1.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__wrong_distfile_algorithms(c *check.C) {
	t := s.Init(c)

	t.Chdir("category/package")
	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"MD5 (distfile.tar.gz) = 12345678901234567890123456789012",
		"SHA1 (distfile.tar.gz) = 1234567890123456789012345678901234567890")

	CheckLinesDistinfo(lines)

	t.CheckOutputLines(
		"ERROR: distinfo:3: Expected SHA1, RMD160, SHA512, Size checksums " +
			"for \"distfile.tar.gz\", got MD5, SHA1.")
}

// This case only happens when a distinfo file is checked on its own,
// without any reference to a pkgsrc package. Additionally the distfile
// must start with the patch- prefix and the algorithms must be wrong
// for both distfile or patch.
//
// This test only demonstrates the edge case.
func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__ambiguous_distfile(c *check.C) {
	t := s.Init(c)

	t.SetUpCommandLine("--explain")
	t.Chdir("category/package")
	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"MD5 (patch-4.2.tar.gz) = 12345678901234567890123456789012")

	CheckLinesDistinfo(lines)

	t.CheckOutputLines(
		"ERROR: distinfo:3: Wrong checksum algorithms MD5 for patch-4.2.tar.gz.",
		"",
		"\tDistfiles that are downloaded from external sources must have the",
		"\tchecksum algorithms SHA1, RMD160, SHA512, Size.",
		"",
		"\tPatch files from pkgsrc must have only the SHA1 hash.",
		"")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__wrong_patch_algorithms(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.CreateFileDummyPatch("patches/patch-aa")
	t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"MD5 (patch-aa) = 12345678901234567890123456789012",
		"SHA1 (patch-aa) = 1234567890123456789012345678901234567890")

	G.Check(".")

	t.CheckOutputLines(
		"ERROR: distinfo:3: Expected SHA1 hash for patch-aa, got MD5, SHA1.",
		"ERROR: distinfo:4: SHA1 hash of patches/patch-aa differs "+
			"(distinfo has 1234567890123456789012345678901234567890, "+
			"patch file has ebbf34b0641bcb508f17d5a27f2bf2a536d810ac).")
}

func (s *Suite) Test_distinfoLinesChecker_parse__empty(c *check.C) {
	t := s.Init(c)

	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"")

	CheckLinesDistinfo(lines)

	t.CheckOutputLines(
		"NOTE: ~/distinfo:2: Trailing empty lines.")
}

// When checking the complete pkgsrc tree, pkglint has all information it needs
// to check whether different packages use the same distfile but require
// different hashes for it.
//
// In such a case, typically one of the packages should put its distfiles into
// a DIST_SUBDIR.
func (s *Suite) Test_distinfoLinesChecker_checkGlobalDistfileMismatch(c *check.C) {
	t := s.Init(c)

	t.SetUpPkgsrc()
	t.SetUpPackage("category/package1")
	t.SetUpPackage("category/package2")
	t.CreateFileLines("category/package1/distinfo",
		RcsID,
		"",
		"SHA512 (distfile-1.0.tar.gz) = 1234567811111111",
		"SHA512 (distfile-1.1.tar.gz) = 1111111111111111",
		"SHA512 (patch-4.2.tar.gz) = 1234567812345678")
	t.CreateFileLines("category/package2/distinfo",
		RcsID,
		"",
		"SHA512 (distfile-1.0.tar.gz) = 1234567822222222",
		"SHA512 (distfile-1.1.tar.gz) = 1111111111111111",
		"SHA512 (encoding-error.tar.gz) = 12345678abcdefgh")
	t.CreateFileLines("Makefile",
		MkRcsID,
		"",
		"COMMENT=\tThis is pkgsrc",
		"",
		"SUBDIR+=\tcategory")
	t.CreateFileLines("category/Makefile",
		MkRcsID,
		"",
		"COMMENT=\tUseful programs",
		"",
		"SUBDIR+=\tpackage1",
		"SUBDIR+=\tpackage2",
		"",
		".include \"../mk/misc/category.mk\"")

	G.Main("pkglint", "-r", "-Wall", "-Call", t.File("."))

	t.CheckOutputLines(
		"ERROR: ~/category/package1/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"distfile-1.0.tar.gz\", got SHA512.",
		"ERROR: ~/category/package1/distinfo:4: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"distfile-1.1.tar.gz\", got SHA512.",
		"ERROR: ~/category/package1/distinfo:5: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"patch-4.2.tar.gz\", got SHA512.",

		"ERROR: ~/category/package2/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"distfile-1.0.tar.gz\", got SHA512.",
		"ERROR: ~/category/package2/distinfo:3: "+
			"The SHA512 hash for distfile-1.0.tar.gz is 1234567822222222, "+
			"which conflicts with 1234567811111111 in ../../category/package1/distinfo:3.",
		"ERROR: ~/category/package2/distinfo:4: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"distfile-1.1.tar.gz\", got SHA512.",
		"ERROR: ~/category/package2/distinfo:5: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"encoding-error.tar.gz\", got SHA512.",
		"ERROR: ~/category/package2/distinfo:5: "+
			"The SHA512 hash for encoding-error.tar.gz contains a non-hex character.",

		"WARN: ~/licenses/gnu-gpl-v2: This license seems to be unused.",
		"8 errors and 1 warning found.",
		"(Run \"pkglint -e\" to show explanations.)")

	// Ensure that hex.DecodeString does not waste memory here.
	t.Check(len(G.Hashes["SHA512:distfile-1.0.tar.gz"].hash), equals, 8)
	t.Check(cap(G.Hashes["SHA512:distfile-1.0.tar.gz"].hash), equals, 8)
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__missing_patch_with_distfile_checksums(c *check.C) {
	t := s.Init(c)

	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ...",
		"RMD160 (patch-aa) = ...",
		"SHA512 (patch-aa) = ...",
		"Size (patch-aa) = ... bytes")

	CheckLinesDistinfo(lines)

	// The file name certainly looks like a pkgsrc patch, but there
	// is no corresponding file in the file system, and there is no
	// current package to correctly determine the PATCHDIR. Therefore
	// pkglint doesn't know whether this is a distfile or a missing
	// patch file and doesn't warn at all.
	t.CheckOutputEmpty()
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__existing_patch_with_distfile_checksums(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ...",
		"RMD160 (patch-aa) = ...",
		"SHA512 (patch-aa) = ...",
		"Size (patch-aa) = ... bytes")
	t.CreateFileDummyPatch("category/package/patches/patch-aa")

	G.Check(t.File("category/package"))

	// Even though the checksums in the distinfo file look as if they
	// refer to a distfile, there is a patch file in the file system
	// that matches the distinfo lines. When checking a pkgsrc package
	// (as opposed to checking a distinfo file on its own), this means
	// that the distinfo lines clearly refer to that patch file and not
	// to a distfile.
	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1 hash for patch-aa, got SHA1, RMD160, SHA512, Size.",
		"ERROR: ~/category/package/distinfo:3: "+
			"SHA1 hash of patches/patch-aa differs (distinfo has ..., "+
			"patch file has ebbf34b0641bcb508f17d5a27f2bf2a536d810ac).")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithms__missing_patch_with_wrong_algorithms(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.SetUpFileLines("category/package/distinfo",
		RcsID,
		"",
		"RMD160 (patch-aa) = ...")

	G.Check(t.File("category/package"))

	// Patch files usually have the SHA1 hash or none at all if they are fresh.
	// In all other cases pkglint assumes that the file is a distfile,
	// therefore it requires the usual distfile checksum algorithms here.
	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: " +
			"Expected SHA1, RMD160, SHA512, Size checksums for \"patch-aa\", got RMD160.")
}

func (s *Suite) Test_distinfoLinesChecker_checkUncommittedPatch__bad(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.CreateFileDummyPatch("patches/patch-aa")
	t.CreateFileLines("CVS/Entries",
		"/distinfo/...")
	t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ebbf34b0641bcb508f17d5a27f2bf2a536d810ac")

	G.checkdirPackage(".")

	t.CheckOutputLines(
		"WARN: distinfo:3: patches/patch-aa is registered in distinfo but not added to CVS.")
}

func (s *Suite) Test_distinfoLinesChecker_checkUncommittedPatch__good(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.CreateFileDummyPatch("patches/patch-aa")
	t.CreateFileLines("CVS/Entries",
		"/distinfo/...")
	t.CreateFileLines("patches/CVS/Entries",
		"/patch-aa/...")
	t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ebbf34b0641bcb508f17d5a27f2bf2a536d810ac")

	G.checkdirPackage(".")

	t.CheckOutputEmpty()
}

func (s *Suite) Test_distinfoLinesChecker_checkUnrecordedPatches(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.CreateFileLines("patches/CVS/Entries")
	t.CreateFileDummyPatch("patches/patch-aa")
	t.CreateFileDummyPatch("patches/patch-src-Makefile")
	t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"SHA1 (distfile.tar.gz) = ...",
		"RMD160 (distfile.tar.gz) = ...",
		"SHA512 (distfile.tar.gz) = ...",
		"Size (distfile.tar.gz) = 1024 bytes")

	G.checkdirPackage(".")

	t.CheckOutputLines(
		"ERROR: distinfo: Patch \"patches/patch-aa\" is not recorded. Run \""+confMake+" makepatchsum\".",
		"ERROR: distinfo: Patch \"patches/patch-src-Makefile\" is not recorded. Run \""+confMake+" makepatchsum\".")
}

// The distinfo file and the patches are usually placed in the package
// directory. By defining PATCHDIR or DISTINFO_FILE, a package can define
// that they are somewhere else in pkgsrc.
func (s *Suite) Test_distinfoLinesChecker_checkPatchSha1__relative_path_in_distinfo(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package",
		"DISTINFO_FILE=\t../../other/common/distinfo",
		"PATCHDIR=\t../../devel/patches/patches")
	t.Remove("category/package/distinfo")
	t.CreateFileLines("devel/patches/patches/CVS/Entries")
	t.CreateFileDummyPatch("devel/patches/patches/patch-aa")
	t.CreateFileDummyPatch("devel/patches/patches/patch-only-in-patches")
	t.SetUpFileLines("other/common/distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ...",
		"SHA1 (patch-only-in-distinfo) = ...")
	t.Chdir("category/package")

	G.checkdirPackage(".")

	t.CheckOutputLines(
		"ERROR: ../../other/common/distinfo:3: SHA1 hash of ../../devel/patches/patches/patch-aa differs "+
			"(distinfo has ..., patch file has ebbf34b0641bcb508f17d5a27f2bf2a536d810ac).",
		"WARN: ../../other/common/distinfo:4: Patch file \"patch-only-in-distinfo\" "+
			"does not exist in directory \"../../devel/patches/patches\".",
		"ERROR: ../../other/common/distinfo: Patch \"../../devel/patches/patches/patch-only-in-patches\" "+
			"is not recorded. Run \""+confMake+" makepatchsum\".")
}

// When the distinfo file and the patches are placed in the same package,
// their diagnostics use short relative paths.
func (s *Suite) Test_CheckLinesDistinfo__distinfo_and_patches_in_separate_directory(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package",
		"DISTINFO_FILE=\t../../other/common/distinfo",
		"PATCHDIR=\t../../other/common/patches")
	t.Remove("category/package/distinfo")
	t.CreateFileLines("other/common/patches/CVS/Entries")
	t.CreateFileDummyPatch("other/common/patches/patch-aa")
	t.CreateFileDummyPatch("other/common/patches/patch-only-in-patches")
	t.SetUpFileLines("other/common/distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ...",
		"SHA1 (patch-only-in-distinfo) = ...")
	t.Chdir("category/package")

	G.checkdirPackage(".")

	t.CheckOutputLines(
		"ERROR: ../../other/common/distinfo:3: SHA1 hash of patches/patch-aa differs "+
			"(distinfo has ..., patch file has ebbf34b0641bcb508f17d5a27f2bf2a536d810ac).",
		"WARN: ../../other/common/distinfo:4: Patch file \"patch-only-in-distinfo\" "+
			"does not exist in directory \"patches\".",
		"ERROR: ../../other/common/distinfo: Patch \"patches/patch-only-in-patches\" "+
			"is not recorded. Run \""+confMake+" makepatchsum\".")
}

func (s *Suite) Test_CheckLinesDistinfo__manual_patches(c *check.C) {
	t := s.Init(c)

	t.Chdir("category/package")
	t.CreateFileLines("patches/manual-libtool.m4")
	lines := t.SetUpFileLines("distinfo",
		RcsID,
		"",
		"SHA1 (patch-aa) = ...")

	CheckLinesDistinfo(lines)

	// When a distinfo file is checked on its own, without belonging to a package,
	// the PATCHDIR is not known and therefore no diagnostics are logged.
	t.CheckOutputEmpty()

	G.Pkg = NewPackage(".")

	CheckLinesDistinfo(lines)

	// When a distinfo file is checked in the context of a package,
	// the PATCHDIR is known, therefore the check is active.
	t.CheckOutputLines(
		"WARN: distinfo:3: Patch file \"patch-aa\" does not exist in directory \"patches\".")
}

// PHP modules that are not PECL use the distinfo file from lang/php* but
// their own patches directory. Therefore the distinfo file refers to missing
// patches. Since this strange situation is caused by the pkgsrc
// infrastructure, there is nothing a package author can do about.
//
// XXX: Re-check the documentation for this test.
func (s *Suite) Test_CheckLinesDistinfo__missing_php_patches(c *check.C) {
	t := s.Init(c)

	t.SetUpPkgsrc()
	t.SetUpCommandLine("-Wall,no-space")
	t.CreateFileLines("licenses/unknown-license")
	t.CreateFileLines("lang/php/ext.mk",
		MkRcsID,
		"",
		"PHPEXT_MK=      # defined",
		"PHPPKGSRCDIR=   ../../lang/php72",
		"LICENSE?=       unknown-license",
		"COMMENT?=       Some PHP package",
		"GENERATE_PLIST+=# none",
		"",
		".if !defined(PECL_VERSION)",
		"DISTINFO_FILE=  ${.CURDIR}/${PHPPKGSRCDIR}/distinfo",
		".endif",
		".if defined(USE_PHP_EXT_PATCHES)",
		"PATCHDIR=       ${.CURDIR}/${PHPPKGSRCDIR}/patches",
		".endif")
	t.CreateFileDummyPatch("lang/php72/patches/patch-php72")
	t.CreateFileLines("lang/php72/distinfo",
		RcsID,
		"",
		"SHA1 (patch-php72) = ebbf34b0641bcb508f17d5a27f2bf2a536d810ac")

	t.CreateFileLines("archivers/php-bz2/Makefile",
		MkRcsID,
		"",
		"USE_PHP_EXT_PATCHES=    yes",
		"",
		".include \"../../lang/php/ext.mk\"",
		".include \"../../mk/bsd.pkg.mk\"")

	G.Check(t.File("archivers/php-bz2"))

	t.CreateFileLines("archivers/php-zlib/Makefile",
		MkRcsID,
		"",
		".include \"../../lang/php/ext.mk\"",
		".include \"../../mk/bsd.pkg.mk\"")

	G.Check(t.File("archivers/php-zlib"))

	t.CheckOutputEmpty()
}

func (s *Suite) Test_distinfoLinesChecker_checkPatchSha1(c *check.C) {
	t := s.Init(c)

	G.Pkg = NewPackage(t.File("category/package"))
	distinfoLine := t.NewLine(t.File("category/package/distinfo"), 5, "")

	checker := distinfoLinesChecker{}
	checker.checkPatchSha1(distinfoLine, "patch-nonexistent", "distinfo-sha1")

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:5: Patch patch-nonexistent does not exist.")
}

// When there is at least one correct hash for a distfile, running
// pkglint --autofix adds the missing hashes, provided the distfile has been
// downloaded to pkgsrc/distfiles, which is the standard distfiles location.
func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__add_missing_hashes(c *check.C) {
	t := s.Init(c)

	t.SetUpCommandLine("-Wall", "--explain")
	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"RMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a",
		"Size (package-1.0.txt) = 13 bytes",
		"CRC32 (package-1.0.txt) = asdf")
	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	// This run is only used to verify that the RMD160 hash is correct, and if
	// it should ever differ, the correct hash will appear in an error message.
	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", "+
			"got RMD160, Size, CRC32.",
		"",
		"\tTo add the missing lines to the distinfo file, run",
		"\t\t"+confMake+" distinfo",
		"\tfor each variant of the package until all distfiles are downloaded",
		"\tto \"${PKGSRCDIR}/distfiles\".",
		"",
		"\tThe variants are typically selected by setting EMUL_PLATFORM or",
		"\tsimilar variables in the command line.",
		"",
		"\tAfter that, run \"cvs update -C distinfo\" to revert the distinfo file",
		"\tto the previous state, since the above commands have removed some of",
		"\tthe entries.",
		"",
		"\tAfter downloading all possible distfiles, run \"pkglint --autofix\",",
		"\twhich will find the downloaded distfiles and add the missing hashes",
		"\tto the distinfo file.",
		"",
		"ERROR: ~/category/package/distinfo:3: Missing SHA1 hash for package-1.0.txt.",
		"ERROR: ~/category/package/distinfo:3: Missing SHA512 hash for package-1.0.txt.")

	t.SetUpCommandLine("-Wall", "--autofix", "--show-autofix", "--source")

	G.Check(t.File("category/package"))

	// Since the file exists in the distfiles directory, pkglint checks the
	// hash right away. It also adds the missing hashes since this file is
	// not a patch file.
	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: Missing SHA1 hash for package-1.0.txt.",
		"AUTOFIX: ~/category/package/distinfo:3: "+
			"Inserting a line \"SHA1 (package-1.0.txt) "+
			"= cd50d19784897085a8d0e3e413f8612b097c03f1\" "+
			"before this line.",
		"+\tSHA1 (package-1.0.txt) = cd50d19784897085a8d0e3e413f8612b097c03f1",
		">\tRMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a",
		"",
		"ERROR: ~/category/package/distinfo:3: Missing SHA512 hash for package-1.0.txt.",
		"AUTOFIX: ~/category/package/distinfo:3: "+
			"Inserting a line \"SHA512 (package-1.0.txt) "+
			"= f65f341b35981fda842b09b2c8af9bcdb7602a4c2e6fa1f7d41f0974d3e3122f"+
			"268fc79d5a4af66358f5133885cd1c165c916f80ab25e5d8d95db46f803c782c\" after this line.",
		"+\tSHA1 (package-1.0.txt) = cd50d19784897085a8d0e3e413f8612b097c03f1",
		">\tRMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a",
		"+\tSHA512 (package-1.0.txt) = f65f341b35981fda842b09b2c8af9bcdb7602a4c2e6fa1f7d41f0974d3e3122f"+
			"268fc79d5a4af66358f5133885cd1c165c916f80ab25e5d8d95db46f803c782c")

	t.SetUpCommandLine("-Wall")

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: " +
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", " +
			"got SHA1, RMD160, SHA512, Size, CRC32.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__wrong_distfile_hash(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"RMD160 (package-1.0.txt) = 1234wrongHash1234")
	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", "+
			"got RMD160.",
		"ERROR: ~/category/package/distinfo:3: "+
			"The RMD160 checksum for \"package-1.0.txt\" is 1234wrongHash1234 in distinfo, "+
			"1a88147a0344137404c63f3b695366eab869a98a in ../../distfiles/package-1.0.txt.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__no_usual_algorithm(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"MD5 (package-1.0.txt) = 1234wrongHash1234")
	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: " +
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", " +
			"got MD5.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__top_algorithms_missing(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"SHA512 (package-1.0.txt) = f65f341b35981fda842b09b2c8af9bcdb7602a4c2e6fa1f7"+
			"d41f0974d3e3122f268fc79d5a4af66358f5133885cd1c165c916f80ab25e5d8d95db46f803c782c",
		"Size (package-1.0.txt) = 13 bytes")
	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", "+
			"got SHA512, Size.",
		"ERROR: ~/category/package/distinfo:3: Missing SHA1 hash for package-1.0.txt.",
		"ERROR: ~/category/package/distinfo:3: Missing RMD160 hash for package-1.0.txt.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__bottom_algorithms_missing(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"SHA1 (package-1.0.txt) = cd50d19784897085a8d0e3e413f8612b097c03f1",
		"RMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a")
	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", "+
			"got SHA1, RMD160.",
		"ERROR: ~/category/package/distinfo:4: Missing SHA512 hash for package-1.0.txt.",
		"ERROR: ~/category/package/distinfo:4: Missing Size hash for package-1.0.txt.")

	t.SetUpCommandLine("-Wall", "--autofix")

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"AUTOFIX: ~/category/package/distinfo:4: "+
			"Inserting a line \"SHA512 (package-1.0.txt) = f65f341b35981fda842b"+
			"09b2c8af9bcdb7602a4c2e6fa1f7d41f0974d3e3122f268fc79d5a4af66358f513"+
			"3885cd1c165c916f80ab25e5d8d95db46f803c782c\" after this line.",
		"AUTOFIX: ~/category/package/distinfo:4: "+
			"Inserting a line \"Size (package-1.0.txt) = 13 bytes\" after this line.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__algorithms_in_wrong_order(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"RMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a",
		"SHA1 (package-1.0.txt) = cd50d19784897085a8d0e3e413f8612b097c03f1",
		"Size (package-1.0.txt) = 13 bytes",
		"SHA512 (package-1.0.txt) = f65f341b35981fda842b09b2c8af9bcdb7602a4c2e6fa1f7"+
			"d41f0974d3e3122f268fc79d5a4af66358f5133885cd1c165c916f80ab25e5d8d95db46f803c782c")

	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	// This case doesn't happen in practice, therefore there's no autofix for it.
	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: " +
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", " +
			"got RMD160, SHA1, Size, SHA512.")
}

func (s *Suite) Test_distinfoLinesChecker_checkAlgorithmsDistfile__some_algorithms_in_wrong_order(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/distinfo",
		RcsID,
		"",
		"RMD160 (package-1.0.txt) = 1a88147a0344137404c63f3b695366eab869a98a",
		"Size (package-1.0.txt) = 13 bytes",
		"SHA512 (package-1.0.txt) = f65f341b35981fda842b09b2c8af9bcdb7602a4c2e6fa1f7"+
			"d41f0974d3e3122f268fc79d5a4af66358f5133885cd1c165c916f80ab25e5d8d95db46f803c782c")

	t.CreateFileLines("distfiles/package-1.0.txt",
		"hello, world")
	G.Pkgsrc.LoadInfrastructure()

	G.Check(t.File("category/package"))

	// This case doesn't happen in practice, therefore there's no autofix for it.
	t.CheckOutputLines(
		"ERROR: ~/category/package/distinfo:3: "+
			"Expected SHA1, RMD160, SHA512, Size checksums for \"package-1.0.txt\", "+
			"got RMD160, Size, SHA512.",
		"ERROR: ~/category/package/distinfo:3: Missing SHA1 hash for package-1.0.txt.")
}