Skip to content

Commit ebf058e

Browse files
committed
test: add unit tests for libsRegex
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 9ca08d8 commit ebf058e

1 file changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestLibsRegex(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input string
12+
wantLib string
13+
wantHash string
14+
match bool
15+
}{
16+
{
17+
name: "standard line with MD5Sum",
18+
input: "'http://deb.debian.org/debian/pool/main/liba/libaec/libaec0_1.0.6-1+b1_amd64.deb' libaec0_1.0.6-1+b1_amd64.deb 22052 MD5Sum:42611bf8032dad2d74c26d8dc084d322",
19+
wantLib: "libaec0_1.0.6-1+b1_amd64.deb",
20+
wantHash: "MD5Sum:42611bf8032dad2d74c26d8dc084d322",
21+
match: true,
22+
},
23+
{
24+
name: "line without MD5Sum",
25+
input: "'http://deb.debian.org/debian/pool/main/libn/libnss3/libnss3_3.87.1-1+deb12u1_amd64.deb' libnss3_3.87.1-1+deb12u1_amd64.deb 1378920",
26+
wantLib: "libnss3_3.87.1-1+deb12u1_amd64.deb",
27+
wantHash: "",
28+
match: true,
29+
},
30+
{
31+
name: "epoch in version (URL-encoded colon)",
32+
input: "'http://deb.debian.org/debian/pool/main/liba/libarmadillo/libarmadillo11_1%3a11.4.2+dfsg-1_amd64.deb' libarmadillo11_1%3a11.4.2+dfsg-1_amd64.deb 11340 MD5Sum:0ec736fe1888c654c32c3812add9d61d",
33+
wantLib: "libarmadillo11_1%3a11.4.2+dfsg-1_amd64.deb",
34+
wantHash: "MD5Sum:0ec736fe1888c654c32c3812add9d61d",
35+
match: true,
36+
},
37+
{
38+
name: "trailing whitespace after MD5Sum",
39+
input: "'http://example.com/libfoo_1.0_amd64.deb' libfoo_1.0_amd64.deb 4096 MD5Sum:abc123 ",
40+
wantLib: "libfoo_1.0_amd64.deb",
41+
wantHash: "MD5Sum:abc123",
42+
match: true,
43+
},
44+
{
45+
name: "trailing whitespace without MD5Sum",
46+
input: "'http://example.com/libfoo_1.0_amd64.deb' libfoo_1.0_amd64.deb 4096 ",
47+
wantLib: "libfoo_1.0_amd64.deb",
48+
wantHash: "",
49+
match: true,
50+
},
51+
{
52+
name: "non-lib package is excluded",
53+
input: "'http://deb.debian.org/debian/pool/main/p/proj/proj-data_9.1.1-1_all.deb' proj-data_9.1.1-1_all.deb 7891012 MD5Sum:deadbeef",
54+
match: false,
55+
},
56+
{
57+
name: "non-deb file is excluded",
58+
input: "'http://example.com/libfoo_1.0.tar.gz' libfoo_1.0.tar.gz 4096 MD5Sum:abc123",
59+
match: false,
60+
},
61+
{
62+
name: "empty line",
63+
input: "",
64+
match: false,
65+
},
66+
{
67+
name: "noise line from apt-get",
68+
input: "Reading package lists...",
69+
match: false,
70+
},
71+
}
72+
73+
for _, tt := range tests {
74+
t.Run(tt.name, func(t *testing.T) {
75+
matches := libsRegex.FindStringSubmatch(tt.input)
76+
if !tt.match {
77+
if matches != nil {
78+
t.Errorf("expected no match, got %v", matches)
79+
}
80+
return
81+
}
82+
if matches == nil {
83+
t.Fatal("expected a match, got nil")
84+
}
85+
if matches[1] != tt.wantLib {
86+
t.Errorf("library name: got %q, want %q", matches[1], tt.wantLib)
87+
}
88+
if matches[2] != tt.wantHash {
89+
t.Errorf("MD5Sum: got %q, want %q", matches[2], tt.wantHash)
90+
}
91+
})
92+
}
93+
}
94+
95+
func TestLibsRegexMultiline(t *testing.T) {
96+
input := `Reading package lists...
97+
Building dependency tree...
98+
'http://deb.debian.org/debian/pool/main/liba/libaec/libaec0_1.0.6-1+b1_amd64.deb' libaec0_1.0.6-1+b1_amd64.deb 22052 MD5Sum:42611bf8032dad2d74c26d8dc084d322
99+
'http://deb.debian.org/debian/pool/main/libn/libnss3/libnss3_3.87.1-1_amd64.deb' libnss3_3.87.1-1_amd64.deb 1378920
100+
'http://deb.debian.org/debian/pool/main/p/proj/proj-data_9.1.1-1_all.deb' proj-data_9.1.1-1_all.deb 7891012 MD5Sum:deadbeef`
101+
102+
matches := libsRegex.FindAllStringSubmatch(input, -1)
103+
if len(matches) != 2 {
104+
t.Fatalf("expected 2 matches, got %d", len(matches))
105+
}
106+
107+
if matches[0][1] != "libaec0_1.0.6-1+b1_amd64.deb" {
108+
t.Errorf("match 0 lib: got %q", matches[0][1])
109+
}
110+
if matches[0][2] != "MD5Sum:42611bf8032dad2d74c26d8dc084d322" {
111+
t.Errorf("match 0 hash: got %q", matches[0][2])
112+
}
113+
114+
if matches[1][1] != "libnss3_3.87.1-1_amd64.deb" {
115+
t.Errorf("match 1 lib: got %q", matches[1][1])
116+
}
117+
if matches[1][2] != "" {
118+
t.Errorf("match 1 hash: expected empty, got %q", matches[1][2])
119+
}
120+
}
121+
122+
func TestBuildResultFromMatches(t *testing.T) {
123+
tests := []struct {
124+
name string
125+
input string
126+
want string
127+
}{
128+
{
129+
name: "with MD5Sum",
130+
input: "'http://example.com/libfoo_1.0_amd64.deb' libfoo_1.0_amd64.deb 4096 MD5Sum:abc123",
131+
want: "libfoo_1.0_amd64.deb MD5Sum:abc123\n",
132+
},
133+
{
134+
name: "without MD5Sum",
135+
input: "'http://example.com/libfoo_1.0_amd64.deb' libfoo_1.0_amd64.deb 4096",
136+
want: "libfoo_1.0_amd64.deb\n",
137+
},
138+
{
139+
name: "mixed lines",
140+
input: `'http://example.com/libfoo_1.0_amd64.deb' libfoo_1.0_amd64.deb 4096 MD5Sum:abc123
141+
'http://example.com/libbar_2.0_amd64.deb' libbar_2.0_amd64.deb 8192`,
142+
want: "libfoo_1.0_amd64.deb MD5Sum:abc123\nlibbar_2.0_amd64.deb\n",
143+
},
144+
}
145+
146+
for _, tt := range tests {
147+
t.Run(tt.name, func(t *testing.T) {
148+
matches := libsRegex.FindAllStringSubmatch(tt.input, -1)
149+
var result string
150+
for _, m := range matches {
151+
line := strings.Join(m[1:], " ")
152+
result += strings.TrimSpace(line) + "\n"
153+
}
154+
if result != tt.want {
155+
t.Errorf("got %q, want %q", result, tt.want)
156+
}
157+
})
158+
}
159+
}

0 commit comments

Comments
 (0)