aboutsummaryrefslogtreecommitdiff
path: root/v0/utils/verification_test.go
blob: f9dd161ab9445b0dbd628a0439400df0ae09d869 (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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package utils

import (
	"testing"

	"github.com/spdx/tools-golang/v0/spdx"
)

// ===== Verification code functionality tests =====

func TestPackage2_1CanGetVerificationCode(t *testing.T) {
	files := []*spdx.File2_1{
		&spdx.File2_1{
			FileName:         "file2.txt",
			FileChecksumSHA1: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file1.txt",
			FileChecksumSHA1: "3333333333bbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file3.txt",
			FileChecksumSHA1: "8888888888bbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file5.txt",
			FileChecksumSHA1: "2222222222bbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file4.txt",
			FileChecksumSHA1: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa",
		},
	}

	wantCode := "ac924b375119c81c1f08c3e2722044bfbbdcd3dc"

	gotCode, err := GetVerificationCode2_1(files, "")
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}
	if wantCode != gotCode {
		t.Errorf("expected %v, got %v", wantCode, gotCode)
	}

}

func TestPackage2_1CanGetVerificationCodeIgnoringExcludesFile(t *testing.T) {
	files := []*spdx.File2_1{
		&spdx.File2_1{
			FileName:         "file1.txt",
			FileChecksumSHA1: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file2.txt",
			FileChecksumSHA1: "3333333333bbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "thisfile.spdx",
			FileChecksumSHA1: "bbbbbbbbbbccccccccccddddddddddaaaaaaaaaa",
		},
		&spdx.File2_1{
			FileName:         "file3.txt",
			FileChecksumSHA1: "8888888888bbbbbbbbbbccccccccccdddddddddd",
		},
		&spdx.File2_1{
			FileName:         "file4.txt",
			FileChecksumSHA1: "2222222222bbbbbbbbbbccccccccccdddddddddd",
		},
	}

	wantCode := "17fab1bd18fe5c13b5d3983f1c17e5f88b8ff266"

	gotCode, err := GetVerificationCode2_1(files, "thisfile.spdx")
	if err != nil {
		t.Fatalf("expected nil error, got %v", err)
	}
	if wantCode != gotCode {
		t.Errorf("expected %v, got %v", wantCode, gotCode)
	}

}

func TestPackage2_1GetVerificationCodeFailsIfNilFileInSlice(t *testing.T) {
	files := []*spdx.File2_1{
		&spdx.File2_1{
			FileName:         "file2.txt",
			FileChecksumSHA1: "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd",
		},
		nil,
		&spdx.File2_1{
			FileName:         "file3.txt",
			FileChecksumSHA1: "8888888888bbbbbbbbbbccccccccccdddddddddd",
		},
	}

	_, err := GetVerificationCode2_1(files, "")
	if err == nil {
		t.Fatalf("expected non-nil error, got nil")
	}
}