mirror of
https://github.com/google/santa.git
synced 2026-04-24 03:00:12 -04:00
santad: Correctly delete corrupt events. Add test for this.
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
|
||||
///
|
||||
/// Add event to the database.
|
||||
///
|
||||
/// @param event the event to store.
|
||||
/// @return YES if event was successfully stored.
|
||||
///
|
||||
@@ -31,18 +32,23 @@
|
||||
|
||||
///
|
||||
/// Retrieves all events in the database
|
||||
///
|
||||
/// @return NSArray of SNTStoredEvent's
|
||||
///
|
||||
- (NSArray *)pendingEvents;
|
||||
|
||||
///
|
||||
/// Retrieves number of events in database without fetching every event.
|
||||
///
|
||||
/// @return Number of events in database.
|
||||
///
|
||||
- (NSUInteger)pendingEventsCount;
|
||||
|
||||
///
|
||||
/// Retrieve an event from the database.
|
||||
/// Retrieve an event from the database with a given SHA-256. If multiple events
|
||||
/// exist for the same SHA-256, just the first is returned.
|
||||
///
|
||||
/// @param sha256 a SHA-256 of the binary to return an event for.
|
||||
/// @return a single SNTStoredEvent.
|
||||
///
|
||||
- (SNTStoredEvent *)pendingEventForSHA256:(NSString *)sha256;
|
||||
@@ -50,12 +56,15 @@
|
||||
///
|
||||
/// Delete a single event from the database using its index.
|
||||
///
|
||||
- (void)deleteEventWithId:(NSNumber *)id;
|
||||
/// @param index the event ID.
|
||||
///
|
||||
- (void)deleteEventWithId:(NSNumber *)index;
|
||||
|
||||
///
|
||||
/// Delete multiple events from the database with an array of IDs.
|
||||
/// @param ids an array of event IDs.
|
||||
///
|
||||
- (void)deleteEventsWithIds:(NSArray *)ids;
|
||||
/// @param indexes an array of event IDs.
|
||||
///
|
||||
- (void)deleteEventsWithIds:(NSArray *)indexes;
|
||||
|
||||
@end
|
||||
|
||||
@@ -55,16 +55,6 @@
|
||||
return success;
|
||||
}
|
||||
|
||||
- (SNTStoredEvent *)eventFromResultSet:(FMResultSet *)rs {
|
||||
NSData *eventData = [rs dataForColumn:@"eventdata"];
|
||||
if (!eventData) return nil;
|
||||
|
||||
SNTStoredEvent *event = [NSKeyedUnarchiver unarchiveObjectWithData:eventData];
|
||||
event.idx = @([rs intForColumn:@"idx"]);
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
#pragma mark Querying/Retreiving
|
||||
|
||||
- (NSUInteger)pendingEventsCount {
|
||||
@@ -103,7 +93,8 @@
|
||||
if (obj) {
|
||||
[pendingEvents addObject:obj];
|
||||
} else {
|
||||
[db executeUpdate:@"DELETE FROM events WHERE idx=?", [rs intForColumn:@"idx"]];
|
||||
NSNumber *idx = [rs objectForColumnName:@"idx"];
|
||||
[db executeUpdate:@"DELETE FROM events WHERE idx=?", idx];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +104,16 @@
|
||||
return pendingEvents;
|
||||
}
|
||||
|
||||
- (SNTStoredEvent *)eventFromResultSet:(FMResultSet *)rs {
|
||||
NSData *eventData = [rs dataForColumn:@"eventdata"];
|
||||
if (!eventData) return nil;
|
||||
|
||||
SNTStoredEvent *event = [NSKeyedUnarchiver unarchiveObjectWithData:eventData];
|
||||
event.idx = @([rs intForColumn:@"idx"]);
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
#pragma mark Deleting
|
||||
|
||||
- (void)deleteEventWithId:(NSNumber *)index {
|
||||
|
||||
@@ -113,4 +113,23 @@
|
||||
XCTAssertEqual(self.sut.pendingEventsCount, 0);
|
||||
}
|
||||
|
||||
- (void)testDeleteCorruptEvent {
|
||||
[self.dbq inDatabase:^(FMDatabase *db) {
|
||||
[db executeUpdate:@"INSERT INTO events (filesha256) VALUES ('deadbeef')"];
|
||||
}];
|
||||
|
||||
NSArray *events = [self.sut pendingEvents];
|
||||
for (SNTStoredEvent *event in events) {
|
||||
if ([event.fileSHA256 isEqual:@"deadbeef"]) XCTFail("Received bad event");
|
||||
}
|
||||
|
||||
[self.dbq inDatabase:^(FMDatabase *db) {
|
||||
FMResultSet *rs = [db executeQuery:@"SELECT * FROM events WHERE filesha256='deadbeef'"];
|
||||
if ([rs next]) {
|
||||
XCTFail("Bad event was not deleted.");
|
||||
}
|
||||
[rs close];
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user