Files
ARTEX/knowledge/knowledge_test.go
T
BaaRai 9cd000dbd0 feat(agent): add versioned knowledge pack and knowledge_lookup tool
Embed MITRE ATT&CK, OWASP WSTG and MITRE CWE ids, names, categories and
capped descriptions generated by scripts/knowledge/build.py. The pack records
the source URL, license, retrieval date and SHA-256; the license texts and
attribution live in knowledge/NOTICE and knowledge/licenses.

Expose a read-only knowledge_lookup tool to the planner and the workers so an
agent can resolve an entry by id or keyword and cite the id in findings and
facts. The data files keep their own licenses; the repository's AGPL-3.0
covers the code.

Refs #521.
2026-11-24 01:13:15 +01:00

139 lines
3.4 KiB
Go

package knowledge
import (
"strings"
"testing"
)
func load(t *testing.T) *Pack {
t.Helper()
p, err := Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
return p
}
func TestLoadSources(t *testing.T) {
p := load(t)
names := p.SourceNames()
if len(names) != 3 {
t.Fatalf("sources = %v, want attack, cwe, wstg", names)
}
want := map[string]int{"attack": 600, "wstg": 100, "cwe": 900}
for _, name := range names {
info, ok := p.SourceInfo(name)
if !ok {
t.Fatalf("SourceInfo(%q) missing", name)
}
if info.Entries < want[name] {
t.Errorf("%s entries = %d, want at least %d", name, info.Entries, want[name])
}
if info.License == "" || info.SourceURL == "" || info.RetrievedAt == "" {
t.Errorf("%s metadata incomplete: %+v", name, info)
}
}
}
func TestLookupExact(t *testing.T) {
p := load(t)
cases := []struct {
source, id, name string
}{
{"attack", "T1190", "Exploit Public-Facing Application"},
{"wstg", "WSTG-ATHN-01", "Default Credentials"},
{"cwe", "CWE-79", "Improper Neutralization of Input During Web Page Generation"},
}
for _, c := range cases {
e, ok := p.Lookup(c.source, c.id)
if !ok {
t.Fatalf("%s %s not found", c.source, c.id)
}
if e.Name != c.name {
t.Errorf("%s name = %q, want %q", c.id, e.Name, c.name)
}
if e.URL == "" {
t.Errorf("%s has no url", c.id)
}
}
}
func TestLookupIsCaseInsensitiveAndTrimmed(t *testing.T) {
p := load(t)
for _, id := range []string{"t1190", " T1190 ", "T1190"} {
if _, ok := p.Lookup("ATTACK", id); !ok {
t.Errorf("Lookup(ATTACK, %q) not found", id)
}
}
}
func TestSearchKeyword(t *testing.T) {
p := load(t)
matches, total, err := p.Search("wstg", "default credentials", 5)
if err != nil {
t.Fatalf("Search: %v", err)
}
if total == 0 || len(matches) == 0 {
t.Fatal("no matches for default credentials")
}
if matches[0].ID != "WSTG-ATHN-01" {
t.Errorf("first match = %s, want WSTG-ATHN-01", matches[0].ID)
}
}
func TestSearchExactIDWins(t *testing.T) {
p := load(t)
matches, total, err := p.Search("cwe", "cwe-79", 5)
if err != nil {
t.Fatalf("Search: %v", err)
}
if total != 1 || len(matches) != 1 || matches[0].ID != "CWE-79" {
t.Fatalf("matches=%v total=%d, want one CWE-79", matches, total)
}
}
func TestSearchLimit(t *testing.T) {
p := load(t)
matches, total, err := p.Search("cwe", "data", 2)
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(matches) != 2 {
t.Fatalf("len(matches) = %d, want 2", len(matches))
}
if total <= 2 {
t.Fatalf("total = %d, want more than the limit", total)
}
matches, _, err = p.Search("cwe", "data", 100)
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(matches) > 20 {
t.Fatalf("limit was not clamped: %d", len(matches))
}
}
func TestSearchErrors(t *testing.T) {
p := load(t)
if _, _, err := p.Search("nope", "x", 5); err == nil || !strings.Contains(err.Error(), "unknown knowledge source") {
t.Fatalf("unknown source error = %v", err)
}
if _, _, err := p.Search("attack", " ", 5); err == nil || !strings.Contains(err.Error(), "query is required") {
t.Fatalf("empty query error = %v", err)
}
}
func TestEveryEntryHasNameAndURL(t *testing.T) {
p := load(t)
for _, name := range p.SourceNames() {
for _, e := range p.sources[name].entries {
if strings.TrimSpace(e.ID) == "" || strings.TrimSpace(e.Name) == "" {
t.Fatalf("%s has an entry without id or name: %+v", name, e)
}
if strings.TrimSpace(e.URL) == "" {
t.Fatalf("%s %s has no url", name, e.ID)
}
}
}
}