Sandboxed gopher-lua with time, memory and call-stack limits from [sandbox], a goquery-backed doc API, price parsing, and the 'check', 'plugin test' and 'fixtures' commands. Sellers are first_party, fulfilled (shipped by the shop) or third_party, and items accept fulfilled by default; used-only offers are not available. Plugin tests run on real pages that 'dibs fixtures' fetches into an untracked directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.5 KiB
Go
81 lines
2.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.security-command.org/Overlord/dibs/internal/config"
|
|
)
|
|
|
|
// sandboxed loads testdata/sandbox/<name>.lua.
|
|
func sandboxed(t *testing.T, name string, limits Limits) *Plugin {
|
|
t.Helper()
|
|
|
|
path := filepath.Join(testdata, "sandbox", name+".lua")
|
|
|
|
code, err := os.ReadFile(path)
|
|
if err != nil { t.Fatal(err) }
|
|
|
|
p, err := Load(name, path, code, limits, discardLogger())
|
|
if err != nil { t.Fatal(err) }
|
|
t.Cleanup(p.Close)
|
|
|
|
return p
|
|
}
|
|
|
|
func parse(p *Plugin, url string) (*Result, error) {
|
|
return p.Parse(context.Background(), []byte("<html></html>"), url)
|
|
}
|
|
|
|
func TestSandboxGlobals(t *testing.T) {
|
|
r, err := parse(sandboxed(t, "globals", DefaultLimits()), "")
|
|
if err != nil { t.Fatal(err) }
|
|
if !r.Available { t.Errorf("reachable from the sandbox: %s", r.Title) }
|
|
}
|
|
|
|
func TestSandboxTimeout(t *testing.T) {
|
|
limits := DefaultLimits()
|
|
limits.Timeout = 50 * time.Millisecond
|
|
|
|
p := sandboxed(t, "timeout", limits)
|
|
|
|
for _, url := range []string{"loop", "pcall"} {
|
|
start := time.Now()
|
|
if _, err := parse(p, url); !errors.Is(err, ErrTimeout) { t.Errorf("%s: got %v, want ErrTimeout", url, err) }
|
|
if d := time.Since(start); d > 2*time.Second { t.Errorf("%s: timeout took %s", url, d) }
|
|
}
|
|
|
|
// The plugin was reloaded and still works.
|
|
if r, err := parse(p, "fine"); err != nil || !r.Available { t.Errorf("after a timeout: %v, %+v", err, r) }
|
|
}
|
|
|
|
func TestSandboxMemoryLimit(t *testing.T) {
|
|
limits := DefaultLimits()
|
|
limits.Timeout = 20 * time.Second
|
|
limits.MemoryBytes = 16 << 20
|
|
|
|
if _, err := parse(sandboxed(t, "memory", limits), ""); !errors.Is(err, ErrMemoryLimit) { t.Errorf("got %v, want ErrMemoryLimit", err) }
|
|
if _, err := parse(sandboxed(t, "string_rep", limits), ""); err == nil || !strings.Contains(err.Error(), "memory limit") { t.Errorf("string.rep: got %v, want a memory limit error", err) }
|
|
}
|
|
|
|
func TestSandboxCallStack(t *testing.T) {
|
|
limits := DefaultLimits()
|
|
limits.CallStack = 50
|
|
|
|
if _, err := parse(sandboxed(t, "call_stack", limits), ""); err == nil || !strings.Contains(err.Error(), "stack overflow") { t.Errorf("got %v, want a stack overflow", err) }
|
|
}
|
|
|
|
// The config's seller policies rank these values by name, so a renamed
|
|
// constant would silently never pass a policy.
|
|
func TestSellersKnownToConfig(t *testing.T) {
|
|
anyone := config.Item{Seller: config.SellerAny}
|
|
for _, s := range []string{SellerFirstParty, SellerFulfilled, SellerThirdParty, SellerUnknown} {
|
|
if !anyone.AcceptsSeller(s) { t.Errorf("config does not know seller %q", s) }
|
|
}
|
|
}
|