54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package market_test
|
|
|
|
import (
|
|
"archive/zip"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func makePluginZip(t *testing.T, files map[string][]byte, priv ed25519.PrivateKey, corruptSum bool) string {
|
|
t.Helper()
|
|
zipPath := filepath.Join(t.TempDir(), "plugin.zip")
|
|
f, err := os.Create(zipPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zw := zip.NewWriter(f)
|
|
|
|
var sumLines []string
|
|
for rel, content := range files {
|
|
w, err := zw.Create(rel)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = w.Write(content)
|
|
h := sha256.Sum256(content)
|
|
hexSum := hex.EncodeToString(h[:])
|
|
if corruptSum {
|
|
hexSum = strings.Repeat("0", 64)
|
|
}
|
|
sumLines = append(sumLines, fmt.Sprintf("%s %s", hexSum, rel))
|
|
}
|
|
sumsBytes := []byte(strings.Join(sumLines, "\n") + "\n")
|
|
w, _ := zw.Create("SHA256SUMS")
|
|
_, _ = w.Write(sumsBytes)
|
|
|
|
if len(priv) > 0 {
|
|
sig := ed25519.Sign(priv, sumsBytes)
|
|
sw, _ := zw.Create("SHA256SUMS.sig")
|
|
_, _ = sw.Write([]byte(base64.StdEncoding.EncodeToString(sig)))
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = f.Close()
|
|
return zipPath
|
|
}
|