123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
package plugin_test
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func TestPackDirectorySignAndInstallRoundTrip(t *testing.T) {
|
|
pub, priv, err := ed25519.GenerateKey(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srcDir := t.TempDir()
|
|
man, _ := json.Marshal(plugin.Manifest{
|
|
Schema: 1,
|
|
ID: "official.theme.packtest",
|
|
Version: "1.0.0",
|
|
Name: plugin.LocalizedText{ZhCN: "打包测试主题", EnUS: "Pack Test Theme"},
|
|
Description: plugin.LocalizedText{EnUS: "Clean theme for pack test"},
|
|
Publisher: "reims-go",
|
|
License: "Apache-2.0",
|
|
Contributes: plugin.Contributes{
|
|
Themes: []plugin.ThemeContribution{{
|
|
ID: "official.theme.packtest",
|
|
Name: plugin.LocalizedText{EnUS: "Pack Test"},
|
|
Stylesheet: "theme.css",
|
|
}},
|
|
},
|
|
})
|
|
_ = os.WriteFile(filepath.Join(srcDir, "plugin.json"), man, 0o644)
|
|
_ = os.WriteFile(filepath.Join(srcDir, "theme.css"), []byte(`.rg-p-sidebar { background: var(--surface-sidebar); }`), 0o644)
|
|
// Junk files that pack must ignore.
|
|
_ = os.MkdirAll(filepath.Join(srcDir, ".git"), 0o755)
|
|
_ = os.WriteFile(filepath.Join(srcDir, ".git", "HEAD"), []byte("ref: refs/heads/main\n"), 0o644)
|
|
_ = os.WriteFile(filepath.Join(srcDir, ".DS_Store"), []byte("junk"), 0o644)
|
|
|
|
outZip := filepath.Join(t.TempDir(), "official.theme.packtest-1.0.0.zip")
|
|
res, err := plugin.PackDirectory(srcDir, plugin.PackOptions{
|
|
HostVersion: "0.2.8",
|
|
OutputPath: outZip,
|
|
SignKey: priv,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("PackDirectory: %v", err)
|
|
}
|
|
if !res.Signed || len(res.SHA256) != 64 || res.FileCount != 2 {
|
|
t.Fatalf("unexpected PackResult: %+v", res)
|
|
}
|
|
|
|
// Verify round-trip install with keyring derived via EnvPluginExtraKeys.
|
|
extraJSON, _ := json.Marshal([]map[string]string{{
|
|
"key_id": "org-official",
|
|
"publisher": "reims-go",
|
|
"trust": "official",
|
|
"public_key": hex.EncodeToString(pub),
|
|
}})
|
|
t.Setenv(plugin.EnvPluginExtraKeys, string(extraJSON))
|
|
|
|
keyring := plugin.DefaultKeyring()
|
|
if len(keyring) == 0 {
|
|
t.Fatal("expected DefaultKeyring to load REIMS_GO_PLUGIN_EXTRA_KEYS")
|
|
}
|
|
|
|
layout := plugin.NewPluginsLayout(t.TempDir())
|
|
installed, err := plugin.InstallArchive(layout, outZip, plugin.PackageInstallOptions{
|
|
HostVersion: "0.2.8",
|
|
Keyring: keyring,
|
|
AllowUnsigned: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("InstallArchive packed zip: %v", err)
|
|
}
|
|
if installed.Trust != plugin.TrustOfficial {
|
|
t.Fatalf("trust = %s, want official", installed.Trust)
|
|
}
|
|
}
|
|
|
|
func TestPackDirectoryRefusesBlockedPackage(t *testing.T) {
|
|
srcDir := t.TempDir()
|
|
man, _ := json.Marshal(plugin.Manifest{
|
|
Schema: 1,
|
|
ID: "community.theme.badpack",
|
|
Version: "1.0.0",
|
|
Name: plugin.LocalizedText{EnUS: "Bad Pack"},
|
|
})
|
|
_ = os.WriteFile(filepath.Join(srcDir, "plugin.json"), man, 0o644)
|
|
_ = os.WriteFile(filepath.Join(srcDir, "sneak.ps1"), []byte(`Write-Host "nope"`), 0o644)
|
|
|
|
outZip := filepath.Join(t.TempDir(), "should-not-exist.zip")
|
|
res, err := plugin.PackDirectory(srcDir, plugin.PackOptions{
|
|
HostVersion: "0.2.8",
|
|
OutputPath: outZip,
|
|
})
|
|
if err == nil || !strings.Contains(err.Error(), "static security gate") || !res.Report.HardBlocked {
|
|
t.Fatalf("expected hard gate refusal during pack, got res=%+v err=%v", res, err)
|
|
}
|
|
if _, statErr := os.Stat(outZip); !os.IsNotExist(statErr) {
|
|
t.Fatalf("blocked pack must not create output zip")
|
|
}
|
|
}
|
|
|
|
func TestDefaultMarketURLsPointToReimsGoOrg(t *testing.T) {
|
|
t.Setenv(plugin.EnvPluginMarketURL, "")
|
|
urls := plugin.DefaultMarketURLs()
|
|
if len(urls) < 2 {
|
|
t.Fatalf("expected primary + mirror URLs, got %v", urls)
|
|
}
|
|
if !strings.Contains(urls[0], "reims-go/plugins") || !strings.Contains(urls[1], "reims-go/plugins") {
|
|
t.Fatalf("expected reims-go/plugins URLs, got %v", urls)
|
|
}
|
|
t.Setenv(plugin.EnvPluginMarketURL, "off")
|
|
if got := plugin.DefaultMarketURLs(); got != nil {
|
|
t.Fatalf("expected nil URLs in offline mode, got %v", got)
|
|
}
|
|
}
|