Skip to content

Commit

Permalink
Incorporated review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarkowsky committed Sep 22, 2021
1 parent 6a9be5d commit 3498893
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
13 changes: 7 additions & 6 deletions Source/santametricservice/Formats/SNTMetricRawJSONFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ - (NSDictionary *)normalize:(NSDictionary *)metrics {

if (![NSJSONSerialization isValidJSONObject:normalizedMetrics]) {
if (err != nil) {
*err = [[NSError alloc] initWithDomain:@"SNTMetricRawJSONFileWriter"
code:EINVAL
userInfo:@{
NSLocalizedDescriptionKey : @"unable to convert metrics to JSON: invalid metrics"
}];
*err = [[NSError alloc]
initWithDomain:@"SNTMetricRawJSONFileWriter"
code:EINVAL
userInfo:@{
NSLocalizedDescriptionKey : @"unable to convert metrics to JSON: invalid metrics"
}];
}
return nil;
}

NSData *json = [NSJSONSerialization dataWithJSONObject:normalizedMetrics
options:NSJSONWritingPrettyPrinted
error:err];
if (json == nil && *err != nil) {
if (json == nil || (err != nil && *err != nil)) {
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ - (void)testMetricsConversionToJSON {
NSError *err = nil;
NSArray<NSData *> *output = [formatter convert:validMetricsDict error:&err];

XCTAssertEqual(1, [output count]);
XCTAssertEqual(1, output.count);
XCTAssertNotNil(output[0]);
XCTAssertNil(err);

Expand Down
27 changes: 13 additions & 14 deletions Source/santametricservice/SNTMetricServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (void)initializeValidMetricsDict {
@"root_labels" : @{@"hostname" : @"testHost", @"username" : @"testUser"},
@"metrics" : @{
@"/build/label" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeConstantString],
@"type" : @((int)SNTMetricTypeConstantString),
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -39,7 +39,7 @@ - (void)initializeValidMetricsDict {
}
},
@"/santa/events" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeCounter],
@"type" : @((int)SNTMetricTypeCounter),
@"fields" : @{
@"rule_type" : @[
@{
Expand All @@ -58,7 +58,7 @@ - (void)initializeValidMetricsDict {
},
},
@"/santa/rules" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"type" : @((int)SNTMetricTypeGaugeInt64),
@"fields" : @{
@"rule_type" : @[
@{
Expand All @@ -77,7 +77,7 @@ - (void)initializeValidMetricsDict {
},
},
@"/santa/using_endpoint_security_framework" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeConstantBool],
@"type" : @((int)SNTMetricTypeConstantBool),
@"fields" : @{
@"" : @[ @{
@"value" : @"",
Expand All @@ -88,13 +88,13 @@ - (void)initializeValidMetricsDict {
}
},
@"/proc/birth_timestamp" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeConstantInt64],
@"type" : @((int)SNTMetricTypeConstantInt64),
@"fields" : @{
@"" : @[ @{
@"value" : @"",
@"created" : fixedDate,
@"last_updated" : fixedDate,
@"data" : [NSNumber numberWithLong:1250999830800]
@"data" : @1250999830800L,
} ]
},
},
Expand All @@ -105,18 +105,18 @@ - (void)initializeValidMetricsDict {
@"value" : @"",
@"created" : fixedDate,
@"last_updated" : fixedDate,
@"data" : [NSNumber numberWithInt:987654321]
@"data" : @987654321,
} ]
}
},
@"/proc/memory/resident_size" : @{
@"type" : [NSNumber numberWithInt:(int)SNTMetricTypeGaugeInt64],
@"type" : @((int)SNTMetricTypeGaugeInt64),
@"fields" : @{
@"" : @[ @{
@"value" : @"",
@"created" : fixedDate,
@"last_updated" : fixedDate,
@"data" : [NSNumber numberWithInt:123456789]
@"data" : @123456789,
} ]
},
},
Expand All @@ -142,8 +142,7 @@ - (void)setUp {
self.tempDir =
[[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempPath
length:strlen(tempPath)];
self.jsonURL =
[NSURL URLWithString:[NSString pathWithComponents:@[ @"file://", self.tempDir, @"test.json" ]]];
self.jsonURL = [NSURL fileURLWithPathComponents:@[ self.tempDir, @"test.json" ]];
}

- (void)tearDown {
Expand Down Expand Up @@ -171,7 +170,7 @@ - (NSDictionary *)convertJSONDateStringsToNSDateWithJson:(NSDictionary *)jsonDat
for (NSString *field in metric[@"fields"]) {
NSMutableArray<NSMutableDictionary *> *values = metric[@"fields"][field];

for (int i = 0; i < [values count]; i++) {
for (int i = 0; i < values.count; ++i) {
values[i][@"created"] = [self createNSDateFromDateString:values[i][@"created"]];
values[i][@"last_updated"] = [self createNSDateFromDateString:values[i][@"last_updated"]];
}
Expand All @@ -189,7 +188,7 @@ - (void)testDefaultConfigOptionsDoNotExport {
// Check the temp dir
NSArray *items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.tempDir
error:NULL];
XCTAssertEqual(0, [items count]);
XCTAssertEqual(0, items.count, @"found unexpected files in %@", self.tempDir);
}

- (void)testWritingRawJSONFile {
Expand All @@ -203,7 +202,7 @@ - (void)testWritingRawJSONFile {
// Ensure that this has written 1 file that is well formed.
NSArray *items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.tempDir
error:NULL];
XCTAssertEqual(1, [items count], @"failed to create JSON metrics file");
XCTAssertEqual(1, items.count, @"failed to create JSON metrics file");

NSData *jsonData = [NSData dataWithContentsOfFile:self.jsonURL.path
options:NSDataReadingUncached
Expand Down
8 changes: 3 additions & 5 deletions Source/santametricservice/Writers/SNTMetricFileWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ @implementation SNTMetricFileWriter
/*
* Open a file for appending.
*/
- (NSFileHandle *)fileHandleForAppendingAtPath:(NSString *)path createMode:(mode_t)mode {
- (NSFileHandle *)fileHandleForNewFileAtPath:(NSString *)path createMode:(mode_t)mode {
int fd;
if (!path) {
return nil;
Expand All @@ -44,7 +44,7 @@ - (BOOL)write:(NSArray<NSData *> *)metrics toURL:(NSURL *)url error:(NSError **)
return NO;
}

NSFileHandle *file = [self fileHandleForAppendingAtPath:url.path createMode:0600];
NSFileHandle *file = [self fileHandleForNewFileAtPath:url.path createMode:0600];
const char newline[1] = {'\n'};

if (file == nil) {
Expand All @@ -60,9 +60,7 @@ - (BOOL)write:(NSArray<NSData *> *)metrics toURL:(NSURL *)url error:(NSError **)
[entryData appendBytes:newline length:1];

if (@available(macos 10.15, *)) {
[file writeData:entryData error:error];

if (error != nil && *error != nil) {
if (![file writeData:entryData error:error]) {
return NO;
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions Source/santametricservice/Writers/SNTMetricFileWriterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ - (void)testWritingToNonFileURLFails {
}

- (void)testWritingDataToFileWorks {
NSString *testFile = [NSString pathWithComponents:@[ @"file://", self.tempDir, @"test.data" ]];
NSURL *url = [NSURL URLWithString:testFile];
NSURL *url = [NSURL fileURLWithPathComponents:@[ self.tempDir, @"test.data" ]];

SNTMetricFileWriter *fileWriter = [[SNTMetricFileWriter alloc] init];

Expand Down

0 comments on commit 3498893

Please sign in to comment.