feat: add diagnostics and safe reset maintenance
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package oplog_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"proxyctl/internal/app"
|
||||
"proxyctl/internal/oplog"
|
||||
)
|
||||
|
||||
func TestLoggerRotatesAtBoundAndNeverStoresAuthenticatedURL(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state", "proxyctl.log")
|
||||
logger := oplog.New(path, 180, 2)
|
||||
for i := 0; i < 20; i++ {
|
||||
err := logger.Write(app.LogEntry{Time: time.Unix(int64(i), 0), Operation: "reset", Target: "fake", Result: "failure", Detail: "request http://alice:secret@proxy.example:8080/private failed"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, candidate := range []string{path, path + ".1", path + ".2"} {
|
||||
data, err := os.ReadFile(candidate)
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data) > 180 {
|
||||
t.Errorf("%s has %d bytes, max 180", filepath.Base(candidate), len(data))
|
||||
}
|
||||
text := string(data)
|
||||
if strings.Contains(text, "secret") || strings.Contains(text, "proxy.example:8080/private") {
|
||||
t.Errorf("log leaked authenticated URL: %q", text)
|
||||
}
|
||||
if !strings.Contains(text, "operation=reset") || !strings.Contains(text, "target=fake") || !strings.Contains(text, "result=failure") {
|
||||
t.Errorf("log lacks operation fields: %q", text)
|
||||
}
|
||||
}
|
||||
matches, err := filepath.Glob(path + "*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(matches) > 3 {
|
||||
t.Fatalf("retained %d logs, want at most 3: %v", len(matches), matches)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user