This commit is contained in:
1 parent
9bdf866a87
commit
5faadcd6eb
41 files changed
+1994
-59
No files matched your search
@@ -0,0 +1,30 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAuditChangesRedactPluginArraysAndSettings(t *testing.T) {
|
||||
before := RuntimeConfig{}
|
||||
after := RuntimeConfig{
|
||||
Root: RootConfig{Database: DatabaseConfig{Password: StoredSecret{Ciphertext: "hidden-cipher"}}},
|
||||
Plugins: PluginsSectionConfig{Plugins: map[string]PluginInstance{
|
||||
"sample": {Settings: json.RawMessage(`{"items":[{"password":"hidden-password"}]}`)},
|
||||
}},
|
||||
}
|
||||
changes := AuditChanges(before, after)
|
||||
data, err := json.Marshal(changes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), "hidden-") {
|
||||
t.Fatalf("secret leaked: %s", data)
|
||||
}
|
||||
fields := map[string]any{}
|
||||
flattenAudit(fields, "array", []any{map[string]any{"password": "hidden-password"}})
|
||||
if !auditSensitive("array.0.password") || fields["array.0.password"] == nil {
|
||||
t.Fatal("array traversal failed")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package migration
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLogMigrationChecksum(t *testing.T) {
|
||||
items := DefaultMigrations()
|
||||
if got := items[len(items)-1].Checksum(); got != "1dad66acb5e7fecf6c39f4a78526b994748d90045220f446a2e80c1b5fd881e7" {
|
||||
t.Fatalf("unexpected log migration checksum: %s", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package logstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db/migration"
|
||||
"git.pcux.cn/xuans/Smail/utils/logpolicy"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func testStore(t *testing.T) *Store {
|
||||
t.Helper()
|
||||
database, err := sql.Open("sqlite", filepath.Join(t.TempDir(), "logs.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
items := migration.DefaultMigrations()
|
||||
for _, statement := range items[len(items)-1].StatementsFor(migration.DialectSQLite) {
|
||||
if _, err := database.Exec(statement); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
store, err := New(database, migration.DialectSQLite)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func TestLogRetentionBoundariesAndBusinessSafety(t *testing.T) {
|
||||
s := testStore(t)
|
||||
now := time.Date(2026, 9, 8, 9, 37, 0, 0, time.UTC)
|
||||
s.now = func() time.Time { return now.Add(-logpolicy.Retention) }
|
||||
cutoff := logpolicy.Cutoff(now)
|
||||
ctx := context.Background()
|
||||
if _, err := s.db.Exec("CREATE TABLE business_probe (value TEXT)"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.Exec("INSERT INTO business_probe VALUES ('keep')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, kind := range []string{"operation", "mail"} {
|
||||
for index := 0; index < 2005; index++ {
|
||||
at := cutoff.Add(-time.Nanosecond)
|
||||
if index >= 2002 {
|
||||
at = cutoff.Add(time.Duration(index - 2002))
|
||||
}
|
||||
if err := s.Write(ctx, Entry{ID: fmt.Sprintf("%s-%d", kind, index), Kind: kind, CreatedAt: at, Result: "success", Payload: []byte(`{"message":"test"}`)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
s.now = func() time.Time { return now }
|
||||
for _, kind := range []string{"operation", "mail"} {
|
||||
items, err := s.Query(ctx, Query{Kind: kind})
|
||||
if err != nil || len(items) != 3 {
|
||||
t.Fatalf("visible %s = %d, %v", kind, len(items), err)
|
||||
}
|
||||
}
|
||||
deleted, err := s.Cleanup(ctx, now)
|
||||
if err != nil || deleted != 4004 {
|
||||
t.Fatalf("cleanup = %d, %v", deleted, err)
|
||||
}
|
||||
deleted, err = s.Cleanup(ctx, now)
|
||||
if err != nil || deleted != 0 {
|
||||
t.Fatalf("repeated cleanup = %d, %v", deleted, err)
|
||||
}
|
||||
var count int
|
||||
if err := s.db.QueryRow("SELECT COUNT(*) FROM business_probe").Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("business data changed")
|
||||
}
|
||||
if err := s.Write(ctx, Entry{Kind: "mail", CreatedAt: cutoff.Add(-1), Payload: []byte(`{}`)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.db.QueryRow("SELECT COUNT(*) FROM mail_log").Scan(&count); err != nil || count != 3 {
|
||||
t.Fatal("expired replay was inserted")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err := s.Cleanup(ctx, now); err == nil {
|
||||
t.Fatal("cancelled cleanup succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogDialectStatementsAndAuditReplay(t *testing.T) {
|
||||
s := testStore(t)
|
||||
for _, dialect := range []migration.Dialect{migration.DialectSQLite, migration.DialectMySQL, migration.DialectPostgres} {
|
||||
s.dialect = dialect
|
||||
got := s.bind("SELECT id FROM mail_log WHERE created_at < ? LIMIT ?")
|
||||
if dialect == migration.DialectPostgres {
|
||||
if !strings.Contains(got, "$1 LIMIT $2") {
|
||||
t.Fatal(got)
|
||||
}
|
||||
} else if strings.Count(got, "?") != 2 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
s.dialect = migration.DialectSQLite
|
||||
audit := &AuditWriter{Store: s}
|
||||
event, err := config.NormalizeConfigAuditEvent(config.ConfigAuditEvent{
|
||||
Operation: config.ConfigOperationUpdate, Section: config.SectionRoot, ActorID: "1", ActorRole: "super_admin", ActorName: "tester",
|
||||
FromGeneration: 1, ToGeneration: 2, CreatedAt: time.Now(), Result: "success",
|
||||
Changes: []config.ConfigAuditChange{{Field: "root.logLevel", Before: "info", After: "debug"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := audit.WriteConfigAudit(context.Background(), event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
items, err := audit.Query(context.Background(), config.ConfigAuditQuery{})
|
||||
if err != nil || len(items) != 1 || items[0].Changes[0].After != "debug" {
|
||||
t.Fatalf("audit: %+v, %v", items, err)
|
||||
}
|
||||
data, err := audit.ExportCSV(context.Background(), config.ConfigAuditQuery{})
|
||||
if err != nil || !strings.Contains(string(data), "tester") || !strings.Contains(string(data), "debug") {
|
||||
t.Fatalf("csv: %s, %v", data, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package runtimelog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/logpolicy"
|
||||
)
|
||||
|
||||
func TestRuntimeLogCompressionAndExactRetention(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
w, err := New(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer w.Close()
|
||||
now := time.Date(2026, 9, 8, 12, 0, 0, 0, time.UTC)
|
||||
cutoff := logpolicy.Cutoff(now)
|
||||
w.now = func() time.Time { return cutoff }
|
||||
for _, at := range []time.Time{cutoff.Add(-time.Hour), cutoff, cutoff.Add(time.Hour), now.Add(-8 * 24 * time.Hour), now} {
|
||||
if err := w.WriteAt(at, "sample\nsecond line"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
w.now = func() time.Time { return now }
|
||||
ctx := context.Background()
|
||||
if err := w.Maintain(ctx, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files, err := w.Files()
|
||||
if err != nil || len(files) != 3 {
|
||||
t.Fatalf("files: %+v %v", files, err)
|
||||
}
|
||||
name := prefix + cutoff.Format("2006-01-02") + ".log.gz"
|
||||
page, err := w.Read(ctx, name, 0, 100)
|
||||
if err != nil || len(page.Lines) != 2 {
|
||||
t.Fatalf("boundary: %+v %v", page, err)
|
||||
}
|
||||
if !strings.Contains(page.Lines[0], `\n`) {
|
||||
t.Fatal("multiline event split")
|
||||
}
|
||||
// 压缩后仍须逐条裁剪,不能按整个归档日期删除有效记录。
|
||||
now = now.Add(30 * time.Minute)
|
||||
if err := w.Maintain(ctx, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page, err = w.Read(ctx, name, 0, 100)
|
||||
if err != nil || len(page.Lines) != 1 {
|
||||
t.Fatalf("compressed boundary: %+v %v", page, err)
|
||||
}
|
||||
now = now.Add(24 * time.Hour)
|
||||
if err := w.Maintain(ctx, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := w.root.Stat(name); !os.IsNotExist(err) {
|
||||
t.Fatalf("expired archive still exists: %v", err)
|
||||
}
|
||||
if _, err := w.Read(ctx, "../config/config.json", 0, 100); err == nil {
|
||||
t.Fatal("path traversal accepted")
|
||||
}
|
||||
if err := w.Maintain(ctx, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeLogCorruptionAndUnknownFilesArePreserved(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
w, err := New(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer w.Close()
|
||||
now := time.Now().UTC()
|
||||
w.now = func() time.Time { return now }
|
||||
directory := filepath.Join(root, "log", "运行日志")
|
||||
name := prefix + now.Add(-40*24*time.Hour).Format("2006-01-02") + ".log.gz"
|
||||
if err := os.WriteFile(filepath.Join(directory, name), []byte("broken"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directory, "manual.log"), []byte("keep"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := w.Maintain(context.Background(), now); err == nil {
|
||||
t.Fatal("corruption not reported")
|
||||
}
|
||||
for _, file := range []string{name, "manual.log"} {
|
||||
if _, err := os.Stat(filepath.Join(directory, file)); err != nil {
|
||||
t.Fatal("unmanaged or damaged evidence deleted")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,10 @@ if ((suite === 'all' || suite === 'backend') && result === 0) {
|
||||
const args = extra[0] === '--' ? extra.slice(1) : extra
|
||||
// 独立回归源码集中放在 test/backend,通过 overlay 保留同包测试语义。
|
||||
const replacements = {
|
||||
'backend/services/logstore/log_store_test.go': 'test/backend/log_store_test.go',
|
||||
'backend/services/runtimelog/runtime_logs_test.go': 'test/backend/runtime_logs_test.go',
|
||||
'backend/db/migration/log_migration_test.go': 'test/backend/log_migration_test.go',
|
||||
'backend/config/audit_changes_test.go': 'test/backend/audit_changes_test.go',
|
||||
'backend/config/json_format_test.go': 'test/backend/json_format_test.go',
|
||||
'backend/config/deployment_state_test.go': 'test/backend/deployment_state_test.go',
|
||||
'backend/config/ssl_retention_test.go': 'test/backend/ssl_retention_test.go',
|
||||
|
||||
Reference in new issue
Block a user