104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
package packages
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseTabbed(t *testing.T) {
|
|
out := "zeromq\t4.3.5-22.fc43\nspice-server\t0.16.0-2.fc43\n\nbad-no-tab\n"
|
|
got := parseTabbed(out)
|
|
want := []Package{{"zeromq", "4.3.5-22.fc43"}, {"spice-server", "0.16.0-2.fc43"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParseSpaced(t *testing.T) {
|
|
got := parseSpaced("linux 6.9.1\nhtop 3.3.0\n")
|
|
want := []Package{{"linux", "6.9.1"}, {"htop", "3.3.0"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParseDnf(t *testing.T) {
|
|
// dnf5 emits a section header ("Upgrades") that must be skipped.
|
|
out := "Upgrades\n" +
|
|
"code.x86_64 1.125.1-1781859648.el8 code\n" +
|
|
"containerd.io.x86_64 2.2.5-1.fc44 docker-ce-stable\n" +
|
|
"\nObsoleting Packages\n"
|
|
got := parseDnf(out)
|
|
want := []Package{{"code", "1.125.1-1781859648.el8"}, {"containerd.io", "2.2.5-1.fc44"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParseApt(t *testing.T) {
|
|
out := "Listing...\n" +
|
|
"vim/jammy-updates 2:8.2.3995-1ubuntu2.15 amd64 [upgradable from: 2:8.2.3995-1ubuntu2.1]\n"
|
|
got := parseApt(out)
|
|
want := []Package{{"vim", "2:8.2.3995-1ubuntu2.15"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParsePacmanUpdates(t *testing.T) {
|
|
got := parsePacmanUpdates("linux 6.9.1-1 -> 6.9.2-1\nfoo 1.0 1.0\n")
|
|
want := []Package{{"linux", "6.9.2-1"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStripArch(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{name: "x86_64 arch", in: "code.x86_64", want: "code"},
|
|
{name: "noarch suffix", in: "python3.11.noarch", want: "python3.11"},
|
|
{name: "no arch segment", in: "noarchhere", want: "noarchhere"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := stripArch(tt.in); got != tt.want {
|
|
t.Errorf("stripArch(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateName(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
value string
|
|
valid bool
|
|
}{
|
|
{name: "htop", value: "htop", valid: true},
|
|
{name: "openssh-server", value: "openssh-server", valid: true},
|
|
{name: "lib32-glibc", value: "lib32-glibc", valid: true},
|
|
{name: "g++", value: "g++", valid: true},
|
|
{name: "python3.11", value: "python3.11", valid: true},
|
|
{name: "empty", value: "", valid: false},
|
|
{name: "flag injection", value: "-rf", valid: false},
|
|
{name: "shell metachar", value: "foo;rm", valid: false},
|
|
{name: "space", value: "foo bar", valid: false},
|
|
{name: "equals", value: "pkg=1.0", valid: false},
|
|
{name: "path sep", value: "a/b", valid: false},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validateName(tt.value)
|
|
if tt.valid && err != nil {
|
|
t.Errorf("validateName(%q) = %v, want nil", tt.value, err)
|
|
}
|
|
if !tt.valid && err == nil {
|
|
t.Errorf("validateName(%q) = nil, want error", tt.value)
|
|
}
|
|
})
|
|
}
|
|
}
|