MCPcopy Create free account
hub / github.com/astercloud/aster / TestInMemoryAuditLog_GetEventsByTimeRange

Function TestInMemoryAuditLog_GetEventsByTimeRange

pkg/security/audit_test.go:181–217  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

179}
180
181func TestInMemoryAuditLog_GetEventsByTimeRange(t *testing.T) {
182 auditLog := NewInMemoryAuditLog(nil)
183 defer func() { _ = auditLog.Close() }()
184
185 now := time.Now()
186 yesterday := now.Add(-24 * time.Hour)
187 tomorrow := now.Add(24 * time.Hour)
188
189 // 添加不同时间的事件
190 _ = auditLog.LogEvent(AuditEvent{
191 Type: AuditTypeUserLogin,
192 UserID: "user1",
193 Message: "Old login",
194 Timestamp: yesterday.Add(-1 * time.Hour),
195 })
196 _ = auditLog.LogEvent(AuditEvent{
197 Type: AuditTypeUserLogin,
198 UserID: "user2",
199 Message: "Recent login",
200 Timestamp: now,
201 })
202
203 // 获取昨天到明天之间的事件
204 events, err := auditLog.GetEventsByTimeRange(yesterday, tomorrow, 10)
205 if err != nil {
206 t.Fatalf("GetEventsByTimeRange failed: %v", err)
207 }
208
209 // 应该只有一个在时间范围内的事件
210 if len(events) != 1 {
211 t.Errorf("expected 1 event in time range, got %d", len(events))
212 }
213
214 if len(events) > 0 && events[0].UserID != "user2" {
215 t.Errorf("expected user2, got %s", events[0].UserID)
216 }
217}
218
219func TestInMemoryAuditLog_PurgeEvents(t *testing.T) {
220 auditLog := NewInMemoryAuditLog(nil)

Callers

nothing calls this directly

Calls 5

CloseMethod · 0.95
LogEventMethod · 0.95
GetEventsByTimeRangeMethod · 0.95
NewInMemoryAuditLogFunction · 0.85
AddMethod · 0.65

Tested by

no test coverage detected