Files
urapt/server/aptrepo/helpers_test.go
T
owen 981587e83d Initial release: self-hostable APT repository server and CLI
urapt is a self-hostable APT repository server with a companion CLI for
pushing and managing Debian .deb packages.

Server (urapt-server):
- REST API + APT endpoint, SQLite storage (pure-Go modernc driver, no CGO)
- .deb files stored content-addressed on disk, reference-counted for dedup
- Server-managed RSA-4096 OpenPGP signing key (ProtonMail/go-crypto)
- APT indices (Release/InRelease/Packages[.gz/.xz]) generated on demand
  from the DB, cached in memory, signed with the server key
- Full APT model: repositories -> distributions -> components -> architectures
- Bearer-token auth for REST; HTTP Basic auth for private-repo APT reads
- First registrant becomes admin; repo-scoped permissions
  (read/write/read-write/admin) plus owner and server-admin roles
- Multipart package push with control-field extraction, list/show/delete,
  pool serving, blob ref-count cleanup
- Audit log

CLI (urapt):
- register/login/logout/whoami, token management
- repo/distro/component/arch CRUD, member management
- push/pull/ls/show/rm for packages
- apt-config helper that emits apt setup commands (key, sources.list,
  auth.conf for private repos)

Packaging & docs:
- Dockerfile (multi-stage distroless), docker-compose.yml, sample config
- README quick start, architecture overview, config reference, security notes
- PLAN.md design blueprint, CHANGELOG.md, GPL-3.0 LICENSE
- GitHub Actions CI (test, lint, cross-build for linux/darwin amd64/arm64)
- Makefile release target producing static binaries + tarballs + checksums

Tests cover the data-access layer, auth/permission checks, APT index
generation, .deb parsing, GPG signing, the REST API, and the typed API
client. Verified end-to-end on a Raspberry Pi (arm64) pushing and installing
a real package.
2026-06-28 16:57:34 -05:00

127 lines
2.9 KiB
Go

package aptrepo_test
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func jsonMarshal(v any) []byte {
b, _ := json.Marshal(v)
return b
}
func readAll(r io.Reader) []byte {
b, _ := io.ReadAll(r)
return b
}
func mkdirAll(dir string) error { return os.MkdirAll(dir, 0o755) }
func str(body []byte, key string) string {
var m map[string]any
_ = json.Unmarshal(body, &m)
if v, ok := m[key].(string); ok {
return v
}
return ""
}
// buildDeb constructs a minimal valid .deb with the given package/version/arch.
func buildDeb(name, version, arch string) []byte {
control := "Package: " + name + "\nVersion: " + version + "\nArchitecture: " + arch + "\nMaintainer: t <t@e>\nDescription: short\n extended\n"
var ctrlBuf bytes.Buffer
gz := gzip.NewWriter(&ctrlBuf)
tw := tar.NewWriter(gz)
writeTw(tw, "control", control)
tw.Close()
gz.Close()
var dataBuf bytes.Buffer
gz2 := gzip.NewWriter(&dataBuf)
tw2 := tar.NewWriter(gz2)
writeTw(tw2, "usr/share/"+name, "x")
tw2.Close()
gz2.Close()
var out bytes.Buffer
out.WriteString("!<arch>\n")
writeAr(&out, "debian-binary", []byte("2.0\n"))
writeAr(&out, "control.tar.gz", ctrlBuf.Bytes())
writeAr(&out, "data.tar.gz", dataBuf.Bytes())
return out.Bytes()
}
func writeTw(tw *tar.Writer, name, body string) {
_ = tw.WriteHeader(&tar.Header{Name: name, Mode: 0o644, Size: int64(len(body)), Typeflag: tar.TypeReg})
_, _ = tw.Write([]byte(body))
}
func writeAr(buf *bytes.Buffer, name string, data []byte) {
header := make([]byte, 60)
for i := range header {
header[i] = ' '
}
copy(header[0:], name+"/")
ds := []byte(padNum(len(data), 10))
copy(header[48:], ds)
header[58] = '`'
header[59] = '\n'
buf.Write(header)
buf.Write(data)
if len(data)%2 == 1 {
buf.WriteByte('\n')
}
}
func padNum(n, width int) string {
s := make([]byte, width)
for i := range s {
s[i] = ' '
}
digits := []byte(itoa(n))
copy(s[len(s)-len(digits):], digits)
return string(s)
}
func itoa(n int) string {
if n == 0 {
return "0"
}
var b []byte
for n > 0 {
b = append([]byte{byte('0' + n%10)}, b...)
n /= 10
}
return string(b)
}
func upload(t *testing.T, srv *httptest.Server, token, repo, dist, component, filename string, deb []byte) {
t.Helper()
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
_ = mw.WriteField("component", component)
fw, _ := mw.CreateFormFile("file", filename)
_, _ = fw.Write(deb)
_ = mw.Close()
req, _ := http.NewRequest("POST", srv.URL+"/api/v1/repositories/"+repo+"/distributions/"+dist+"/packages", &buf)
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("upload: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 201 {
t.Fatalf("upload status %d", resp.StatusCode)
}
}