fix: nil-guard _Plugin wrapper, add adversarial tests
Two bugs found by adversarial tests: 1. _Plugin wrapper panicked on nil WName/WVersion — the daemon calls Name()/Version() at startup to log loaded plugins, so a plugin with nil func fields would crash the daemon. Fixed with nil guards returning empty strings. 2. LoadDir returned nil for empty directories instead of an empty slice — inconsistent with the contract (nil means 'no plugin dir configured', empty means 'no plugins found'). Added 9 adversarial tests that attempt to break LoadDir: - Nil function fields (must not panic on load or call) - Empty name/version (valid per interface contract) - Wrong export type (int instead of Plugin — must skip) - Partial interface implementation (missing method — must skip) - Empty directory (returns empty slice, not nil) - Nested subdirectories (must not be traversed) - Panic in Name() function (deferred to call time, loader survives) - Unused import (must not crash eval) - Go build tags (must be ignored in Yaegi) Why: per test-first-build skill, every build/test failure results in a test that would have caught it. Adversarial tests alongside confirmation tests prevent silent breakage.
This commit is contained in:
1 parent
f38ba5d515
commit
9bd61e61ac
3 files changed
+244
-1
No files matched your search
@@ -63,7 +63,7 @@ func LoadDir(dir string, extraSymbols ...map[string]map[string]reflect.Value) ([
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
var plugins []Plugin
|
||||
plugins := make([]Plugin, 0)
|
||||
for _, name := range names {
|
||||
path := filepath.Join(dir, name)
|
||||
src, err := os.ReadFile(path)
|
||||
|
||||
@@ -178,3 +178,240 @@ func TestLoadDirNonexistentDirReturnsNil(t *testing.T) {
|
||||
t.Errorf("LoadDir returned %v, want nil for nonexistent directory", plugins)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Adversarial tests ────────────────────────────────────────────────
|
||||
//
|
||||
// These tests try to BREAK LoadDir. A correct implementation must
|
||||
// degrade gracefully — skip the bad plugin, load the rest, never panic.
|
||||
|
||||
func TestLoadDirNilFunctionFieldsLoadsWithoutPanic(t *testing.T) {
|
||||
// A _Plugin with nil WName/WVersion must LOAD without panicking
|
||||
// AND must not panic when Name()/Version() is called — the wrapper
|
||||
// must nil-guard. Otherwise logging plugin names at startup crashes
|
||||
// the daemon.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "nilfuncs.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: nil,
|
||||
WVersion: nil,
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 1 (nil funcs should not prevent loading)", len(plugins))
|
||||
}
|
||||
|
||||
// These must not panic — the _Plugin wrapper must nil-guard.
|
||||
// The daemon calls Name()/Version() at startup to log loaded plugins.
|
||||
name := plugins[0].Name()
|
||||
version := plugins[0].Version()
|
||||
if name != "" {
|
||||
t.Errorf("Name() = %q, want empty string from nil WName", name)
|
||||
}
|
||||
if version != "" {
|
||||
t.Errorf("Version() = %q, want empty string from nil WVersion", version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirPluginReturningEmptyNameVersion(t *testing.T) {
|
||||
// Plugins with empty name/version are still valid — the interface
|
||||
// doesn't forbid empty strings. LoadDir must not reject them.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "empty.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "" },
|
||||
WVersion: func() string { return "" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 1 (empty name is still valid)", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSkipsPluginWithWrongExportType(t *testing.T) {
|
||||
// Plugin var exists but is an int, not a Plugin. Must be skipped.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "wrongtype.go"), []byte(`package main
|
||||
|
||||
var Plugin = 42
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins from wrong-type export, want 0", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSkipsPluginWithOnlySomeInterfaceMethods(t *testing.T) {
|
||||
// A struct that has Name() but not Version() — doesn't satisfy Plugin.
|
||||
// type assertion must fail and the plugin must be skipped.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "partial.go"), []byte(`package main
|
||||
|
||||
type partial struct{}
|
||||
func (p partial) Name() string { return "partial" }
|
||||
// No Version() method — deliberately incomplete.
|
||||
|
||||
var Plugin partial
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins from partial impl, want 0", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirEmptyDirReturnsEmptySlice(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins from empty dir, want 0", len(plugins))
|
||||
}
|
||||
if plugins == nil {
|
||||
t.Error("LoadDir returned nil from empty dir, want empty slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirSubdirectoriesAreIgnored(t *testing.T) {
|
||||
// Only top-level .go files are loaded. Subdirectories are not traversed.
|
||||
dir := t.TempDir()
|
||||
subDir := filepath.Join(dir, "nested")
|
||||
if err := os.MkdirAll(subDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(subDir, "nested.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "nested" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 0 {
|
||||
t.Errorf("LoadDir returned %d plugins, want 0 — subdirectories must not be traversed", len(plugins))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDirPluginWithPanicInNameFunc(t *testing.T) {
|
||||
// A plugin whose Name() function panics must still be loadable —
|
||||
// the panic only fires on call, not on load.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "panicky.go"), []byte(`package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { panic("boom") },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Fatalf("LoadDir returned %d plugins, want 1 (panics are deferred to call time)", len(plugins))
|
||||
}
|
||||
|
||||
// Verify calling Name() actually panics — the plugin loaded, the panic
|
||||
// is the caller's responsibility.
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Error("Name() did not panic — expected panic from panicky plugin")
|
||||
}
|
||||
}()
|
||||
plugins[0].Name()
|
||||
}()
|
||||
}
|
||||
|
||||
func TestLoadDirPluginWithUnusedImport(t *testing.T) {
|
||||
// Yaegi should still eval a file that imports a package but doesn't
|
||||
// use it directly (the import is needed for the _Plugin type).
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "unusedimp.go"), []byte(`package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var Plugin = struct{}{}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// This must not crash — just skip.
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal("LoadDir errored on unused import:", err)
|
||||
}
|
||||
_ = plugins
|
||||
}
|
||||
|
||||
func TestLoadDirPluginWithGoBuildTag(t *testing.T) {
|
||||
// Build tags in Yaegi-interpreted code are just comments — they
|
||||
// don't gate evaluation. The file loads normally.
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "tagged.go"), []byte(`//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "tagged" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
plugins, err := plugin.LoadDir(dir, symbols)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(plugins) != 1 {
|
||||
t.Errorf("LoadDir returned %d plugins from tagged file, want 1 (build tags are ignored in Yaegi)", len(plugins))
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,14 @@ type _github_com_samcharles93_archie_core_internal_plugin_Plugin struct {
|
||||
}
|
||||
|
||||
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Name() string {
|
||||
if W.WName == nil {
|
||||
return ""
|
||||
}
|
||||
return W.WName()
|
||||
}
|
||||
func (W _github_com_samcharles93_archie_core_internal_plugin_Plugin) Version() string {
|
||||
if W.WVersion == nil {
|
||||
return ""
|
||||
}
|
||||
return W.WVersion()
|
||||
}
|
||||
Reference in new issue
Block a user