返回 DeepSeek-Reasonix
cli_test.go
根目录 / internal / releaseasset / cli_test.go
1 package releaseasset
2
3 import (
4 "archive/tar"
5 "bytes"
6 "compress/gzip"
7 "context"
8 "crypto/sha256"
9 "encoding/hex"
10 "fmt"
11 "net/http"
12 "net/http/httptest"
13 "testing"
14 )
15
16 func TestDownloadCLIFromBaseVerifiesAndExtracts(t *testing.T) {
17 binary := []byte("reasonix-binary")
18 archive := testCLIArchive(t, binary)
19 digest := sha256.Sum256(archive)
20 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21 switch r.URL.Path {
22 case "/v1.2.3/reasonix-linux-arm64.tar.gz":
23 _, _ = w.Write(archive)
24 case "/v1.2.3/SHA256SUMS":
25 _, _ = fmt.Fprintf(w, "%s reasonix-linux-arm64.tar.gz\n", hex.EncodeToString(digest[:]))
26 default:
27 http.NotFound(w, r)
28 }
29 }))
30 defer server.Close()
31
32 got, err := downloadCLIFromBase(context.Background(), server.Client(), server.URL, "v1.2.3", "linux", "arm64", false)
33 if err != nil {
34 t.Fatal(err)
35 }
36 if !bytes.Equal(got, binary) {
37 t.Fatalf("binary = %q, want %q", got, binary)
38 }
39 }
40
41 func TestDownloadCLIFromBaseRejectsChecksumMismatch(t *testing.T) {
42 archive := testCLIArchive(t, []byte("reasonix-binary"))
43 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44 if r.URL.Path == "/v1.2.3/reasonix-linux-amd64.tar.gz" {
45 _, _ = w.Write(archive)
46 return
47 }
48 _, _ = fmt.Fprintf(w, "%064d reasonix-linux-amd64.tar.gz\n", 0)
49 }))
50 defer server.Close()
51
52 if _, err := downloadCLIFromBase(context.Background(), server.Client(), server.URL, "v1.2.3", "linux", "amd64", false); err == nil {
53 t.Fatal("checksum mismatch was accepted")
54 }
55 }
56
57 func TestDownloadCLIRejectsDevelopmentAndUnsupportedTargets(t *testing.T) {
58 for _, test := range []struct{ version, goos, goarch string }{
59 {"dev", "linux", "amd64"},
60 {"v1.2.3", "windows", "amd64"},
61 {"v1.2.3", "linux", "riscv64"},
62 } {
63 if _, err := DownloadCLI(context.Background(), http.DefaultClient, test.version, test.goos, test.goarch); err == nil {
64 t.Fatalf("DownloadCLI(%q,%q,%q) unexpectedly succeeded", test.version, test.goos, test.goarch)
65 }
66 }
67 }
68
69 func testCLIArchive(t *testing.T, binary []byte) []byte {
70 t.Helper()
71 var buf bytes.Buffer
72 gz := gzip.NewWriter(&buf)
73 tw := tar.NewWriter(gz)
74 if err := tw.WriteHeader(&tar.Header{Name: "reasonix", Mode: 0o755, Size: int64(len(binary)), Typeflag: tar.TypeReg}); err != nil {
75 t.Fatal(err)
76 }
77 if _, err := tw.Write(binary); err != nil {
78 t.Fatal(err)
79 }
80 if err := tw.Close(); err != nil {
81 t.Fatal(err)
82 }
83 if err := gz.Close(); err != nil {
84 t.Fatal(err)
85 }
86 return buf.Bytes()
87 }
88
88 lines GO