|
|
@@ -2,6 +2,7 @@
|
|
|
package runtime
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"crypto/sha256"
|
|
|
"encoding/hex"
|
|
|
"fmt"
|
|
|
@@ -9,11 +10,65 @@ import (
|
|
|
"net/http/httptest"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
-
|
|
|
- "github.com/gnacho/netpulse/agent/internal/tlspin"
|
|
|
)
|
|
|
|
|
|
+func TestValidateFirmwareTarget(t *testing.T) {
|
|
|
+ good := strings.Repeat("0", 64)
|
|
|
+ cases := []struct {
|
|
|
+ name string
|
|
|
+ url string
|
|
|
+ checksum string
|
|
|
+ wantErr bool
|
|
|
+ }{
|
|
|
+ {"https", "https://example.com/image.bin", good, false},
|
|
|
+ {"http", "http://example.com/image.bin", good, true},
|
|
|
+ {"sin host", "https://", good, true},
|
|
|
+ {"sin checksum", "https://example.com/image.bin", "", true},
|
|
|
+ {"checksum corto", "https://example.com/image.bin", "abc", true},
|
|
|
+ {"checksum no hex", "https://example.com/image.bin", strings.Repeat("z", 64), true},
|
|
|
+ }
|
|
|
+ for _, tc := range cases {
|
|
|
+ t.Run(tc.name, func(t *testing.T) {
|
|
|
+ err := validateFirmwareTarget(tc.url, tc.checksum)
|
|
|
+ if (err != nil) != tc.wantErr {
|
|
|
+ t.Fatalf("validateFirmwareTarget(%q, %q) err=%v wantErr=%v", tc.url, tc.checksum, err, tc.wantErr)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestDownloadImageRejectsEmptyChecksum(t *testing.T) {
|
|
|
+ dest := filepath.Join(t.TempDir(), "image.bin")
|
|
|
+ if err := downloadImage(context.Background(), nil, "https://example.com/image.bin", dest, "", nil); err == nil {
|
|
|
+ t.Fatal("esperaba error por checksum vacio")
|
|
|
+ }
|
|
|
+ if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
|
|
+ t.Fatalf("no deberia quedar el fichero: %v", err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestDownloadImageRejectsOversize(t *testing.T) {
|
|
|
+ orig := maxFirmwareImageBytes
|
|
|
+ maxFirmwareImageBytes = 1024
|
|
|
+ defer func() { maxFirmwareImageBytes = orig }()
|
|
|
+
|
|
|
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ _, _ = w.Write(make([]byte, 2048))
|
|
|
+ }))
|
|
|
+ defer srv.Close()
|
|
|
+
|
|
|
+ dest := filepath.Join(t.TempDir(), "image.bin")
|
|
|
+ err := downloadImage(context.Background(), srv.Client(), srv.URL, dest, strings.Repeat("0", 64), nil)
|
|
|
+ if err == nil || !strings.Contains(err.Error(), "mayor") {
|
|
|
+ t.Fatalf("esperaba error de tamano, got %v", err)
|
|
|
+ }
|
|
|
+ if _, statErr := os.Stat(dest); !os.IsNotExist(statErr) {
|
|
|
+ t.Fatalf("no deberia quedar el fichero: %v", statErr)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestVerifyImageSha256(t *testing.T) {
|
|
|
data := []byte("openwrt-image-stub")
|
|
|
want := sha256.Sum256(data)
|
|
|
@@ -62,7 +117,7 @@ func TestHandleFirmwareUpgradeInvokesSysupgrade(t *testing.T) {
|
|
|
image := []byte("openwrt-firmware-image")
|
|
|
wantHash := sha256.Sum256(image)
|
|
|
wantHex := hex.EncodeToString(wantHash[:])
|
|
|
- imgServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ imgServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
w.Write(image)
|
|
|
}))
|
|
|
defer imgServer.Close()
|
|
|
@@ -87,10 +142,7 @@ func TestHandleFirmwareUpgradeInvokesSysupgrade(t *testing.T) {
|
|
|
Slug: "rt-fw",
|
|
|
Token: "token",
|
|
|
}
|
|
|
- tr, err := tlspin.BuildTransport(opts.Server, opts.ServerFP)
|
|
|
- if err != nil {
|
|
|
- t.Fatalf("transport: %v", err)
|
|
|
- }
|
|
|
+ tr := imgServer.Client().Transport
|
|
|
cmd := fmt.Sprintf(`{"upgradeId":7,"targetUrl":"%s/image.bin","checksum":"%s","keepConfig":true}`, imgServer.URL, wantHex)
|
|
|
handleFirmwareUpgrade(opts, tr, cmd)
|
|
|
|