File size: 6,199 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package contentsafety

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// ENHANCED_CASE_1: configured non-placeholder words are blocked in all required content types.
func TestEnhancedDistinctWordsAcrossAllContentTypes(t *testing.T) {
	words := []string{"question-risk-731", "回答风险词", "comment-risk-419"}
	filter := NewFilter(words)
	cases := []struct{ contentType, text, word string }{
		{"question", "prefix question-risk-731 suffix", words[0]},
		{"answer", "回答中包含回答风险词需要阻止", words[1]},
		{"comment", "prefix comment-risk-419 suffix", words[2]},
	}
	for _, tc := range cases {
		err := filter.Validate(tc.contentType, tc.text)
		if err == nil {
			t.Fatalf("%s containing configured word %q was not blocked", tc.contentType, tc.word)
		}
		if strings.TrimSpace(err.Error()) == "" {
			t.Errorf("%s block should return a non-empty error", tc.contentType)
		}
	}
}

// ENHANCED_CASE_2: administrator-style repeated configuration updates take effect immediately.
func TestEnhancedRepeatedConfigurationReplacement(t *testing.T) {
	filter := NewFilter([]string{"retired-risk-101"})
	filter.SetWords([]string{"current-risk-202"})
	if err := filter.Validate("question", "retired-risk-101 is now clean"); err != nil {
		t.Fatalf("replaced word remained blocked: %v", err)
	}
	if err := filter.Validate("answer", "contains current-risk-202"); err == nil {
		t.Fatal("newly configured word was not blocked")
	}
	filter.SetWords([]string{"final-risk-303"})
	if err := filter.Validate("comment", "current-risk-202 is now clean"); err != nil {
		t.Fatalf("second replacement did not remove prior configuration: %v", err)
	}
	if err := filter.Validate("comment", "contains final-risk-303"); err == nil {
		t.Fatal("second replacement did not add new configuration")
	}
}

// ENHANCED_CASE_3: every blocked attempt is auditable with its type, text, and matched word.
func TestEnhancedAuditTrailForMultipleAttempts(t *testing.T) {
	filter := NewFilter([]string{"audit-risk-551"})
	attempts := []struct{ contentType, text string }{
		{"question", "question audit-risk-551 payload"},
		{"answer", "answer audit-risk-551 payload"},
		{"comment", "comment audit-risk-551 payload"},
	}
	for _, attempt := range attempts {
		if err := filter.Validate(attempt.contentType, attempt.text); err == nil {
			t.Fatalf("blocked attempt for %s unexpectedly passed", attempt.contentType)
		}
	}
	if err := filter.Validate("comment", "a clean control message"); err != nil {
		t.Fatalf("clean control message was blocked: %v", err)
	}
	log := filter.AuditLog()
	if len(log) != len(attempts) {
		t.Fatalf("want %d audit entries, got %d", len(attempts), len(log))
	}
	remaining := make(map[string]bool, len(attempts))
	for _, attempt := range attempts {
		remaining[attempt.contentType+"\x00"+attempt.text] = true
	}
	for _, entry := range log {
		key := entry.ContentType + "\x00" + entry.Text
		if !remaining[key] || entry.Word != "audit-risk-551" {
			t.Errorf("audit entry does not preserve a blocked attempt: %+v", entry)
		}
		delete(remaining, key)
	}
	if len(remaining) != 0 {
		t.Fatalf("audit log omitted %d blocked attempts", len(remaining))
	}
}

func TestEnhancedFileConfigAndSubmission(t *testing.T) {
	f, err := NewFilterFromFile("sensitive_words.txt")
	if err != nil {
		t.Fatal(err)
	}
	if err := f.Submit("question", "A SPAM-LINK should not be accepted"); err == nil {
		t.Fatal("bundled word should block")
	}
	if len(f.Accepted()) != 0 {
		t.Fatal("blocked submission entered accepted list")
	}
	if err := f.Submit("answer", "This is a clean answer"); err != nil {
		t.Fatal(err)
	}
	if got := f.Accepted(); len(got) != 1 || got[0].ContentType != "answer" {
		t.Fatalf("accepted=%+v", got)
	}
	if got := f.AuditLog(); len(got) != 1 || got[0].Word != "spam-link" {
		t.Fatalf("audit=%+v", got)
	}
	path := filepath.Join(t.TempDir(), "words.txt")
	if err := os.WriteFile(path, []byte("# comment\n\n  alpha  \nBETA\n"), 0600); err != nil {
		t.Fatal(err)
	}
	words, err := LoadWordsFile(path)
	if err != nil || len(words) != 2 || words[0] != "alpha" || words[1] != "BETA" {
		t.Fatalf("loaded=%v err=%v", words, err)
	}
	f.SetWords(words)
	if err := f.Submit("comment", "beta mention"); err == nil {
		t.Fatal("updated file words did not block")
	}
	if err := f.Submit("comment", "spam-link is retired"); err != nil {
		t.Fatal("old word still active:", err)
	}
	if _, err := NewFilterFromFile(filepath.Join(t.TempDir(), "missing")); err == nil {
		t.Fatal("missing config must return error")
	}
}

func TestEnhancedInvalidTypeAndDefensiveCopies(t *testing.T) {
	f := NewFilter([]string{" ", "Risk", "risk"})
	if err := f.Submit("question", "clean"); err != nil {
		t.Fatal("blank word blocked all text:", err)
	}
	if err := f.Submit("post", "risk"); err == nil {
		t.Fatal("unsupported type accepted")
	}
	if len(f.Accepted()) != 1 || len(f.AuditLog()) != 0 {
		t.Fatal("invalid type altered records")
	}
	if err := f.Submit("comment", "RISK"); err == nil {
		t.Fatal("case fold failed")
	}
	accepted := f.Accepted()
	accepted[0].Text = "mutated"
	audit := f.AuditLog()
	audit[0].Text = "mutated"
	if f.Accepted()[0].Text != "clean" || f.AuditLog()[0].Text != "RISK" {
		t.Fatal("record snapshots expose internal state")
	}
}

func TestEnhancedWordOwnershipAndEmptyReplacement(t *testing.T) {
	words := []string{"original"}
	f := NewFilter(words)
	words[0] = "outside-change"
	if f.Validate("question", "original") == nil {
		t.Fatal("constructor must own its word snapshot")
	}
	updated := []string{"replacement"}
	f.SetWords(updated)
	updated[0] = "outside-change"
	if f.Validate("answer", "replacement") == nil {
		t.Fatal("SetWords must own its word snapshot")
	}
	f.SetWords(nil)
	before := len(f.AuditLog())
	if err := f.Submit("comment", "original replacement outside-change"); err != nil {
		t.Fatal("empty replacement must clear the word list:", err)
	}
	if len(f.AuditLog()) != before {
		t.Fatal("empty configuration created a block audit")
	}
	if err := f.Validate("post", "anything"); err == nil {
		t.Fatal("Validate must reject unsupported content types too")
	}
	if len(f.AuditLog()) != before {
		t.Fatal("invalid Validate call altered audit")
	}
}