package apt import ( "strings" "testing" ) const plainRelease = `Origin: Debian Suite: bookworm Codename: bookworm Components: main contrib Architectures: amd64 arm64 SHA256: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890 12345 main/binary-amd64/Packages 0000111122223333000011112222333300001111222233330000111122223333 67890 main/binary-amd64/Packages.gz ` const pgpArmoredRelease = `-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Origin: Debian Suite: bookworm Codename: bookworm Components: main contrib Architectures: amd64 SHA256: abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890 12345 main/binary-amd64/Packages -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE2F...fake-signature... -----END PGP SIGNATURE----- ` func TestParseRelease_plain(t *testing.T) { rf, err := ParseRelease([]byte(plainRelease)) if err != nil { t.Fatalf("unexpected error: %v", err) } if rf.Suite != "bookworm" { t.Errorf("Suite: got %q, want bookworm", rf.Suite) } if rf.Codename != "bookworm" { t.Errorf("Codename: got %q, want bookworm", rf.Codename) } if rf.Components != "main contrib" { t.Errorf("Components: got %q, want %q", rf.Components, "main contrib") } if rf.Architectures != "amd64 arm64" { t.Errorf("Architectures: got %q, want %q", rf.Architectures, "amd64 arm64") } } func TestParseRelease_sha256entries(t *testing.T) { rf, err := ParseRelease([]byte(plainRelease)) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(rf.SHA256) != 2 { t.Fatalf("SHA256 entries: got %d, want 2", len(rf.SHA256)) } e := rf.SHA256[0] if e.Hash != "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" { t.Errorf("entry[0] Hash: got %q", e.Hash) } if e.Size != 12345 { t.Errorf("entry[0] Size: got %d, want 12345", e.Size) } if e.Path != "main/binary-amd64/Packages" { t.Errorf("entry[0] Path: got %q", e.Path) } } func TestParseRelease_pgpArmored(t *testing.T) { rf, err := ParseRelease([]byte(pgpArmoredRelease)) if err != nil { t.Fatalf("unexpected error: %v", err) } if rf.Suite != "bookworm" { t.Errorf("Suite: got %q, want bookworm", rf.Suite) } if len(rf.SHA256) != 1 { t.Errorf("SHA256 entries: got %d, want 1", len(rf.SHA256)) } // PGP signature must not bleed into entries. for _, e := range rf.SHA256 { if strings.Contains(e.Path, "PGP") || strings.Contains(e.Hash, "PGP") { t.Errorf("PGP artifact in entry: %+v", e) } } } func TestGenerateRelease_parseable(t *testing.T) { entries := []ReleaseEntry{ {Hash: "aabbccdd", Size: 100, Path: "main/binary-amd64/Packages"}, {Hash: "eeff0011", Size: 200, Path: "main/binary-amd64/Packages.gz"}, } data := GenerateRelease("bookworm", "bookworm", []string{"main", "contrib"}, []string{"amd64"}, entries) rf, err := ParseRelease(data) if err != nil { t.Fatalf("ParseRelease on generated output: %v", err) } if rf.Suite != "bookworm" { t.Errorf("Suite: got %q, want bookworm", rf.Suite) } if len(rf.SHA256) != 2 { t.Fatalf("SHA256 entries: got %d, want 2", len(rf.SHA256)) } if rf.SHA256[0].Size != 100 { t.Errorf("entry[0] Size: got %d, want 100", rf.SHA256[0].Size) } if rf.SHA256[1].Path != "main/binary-amd64/Packages.gz" { t.Errorf("entry[1] Path: got %q", rf.SHA256[1].Path) } } func TestGenerateRelease_emptyEntries(t *testing.T) { data := GenerateRelease("bookworm", "bookworm", []string{"main"}, []string{"amd64"}, nil) rf, err := ParseRelease(data) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(rf.SHA256) != 0 { t.Errorf("expected 0 SHA256 entries, got %d", len(rf.SHA256)) } }