Skip to content

Commit

Permalink
Tests: Update unit tests to use structured_resources
Browse files Browse the repository at this point in the history
Passing test data via the data parameter no longer works, using structured_resources and accessing via the NSBundle still works well.
  • Loading branch information
russellhancox committed Apr 11, 2024
1 parent 1c8c940 commit d45dd77
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Source/santad/BUILD
Expand Up @@ -913,7 +913,7 @@ santa_unit_test(
santa_unit_test(
name = "SantadTest",
srcs = ["SantadTest.mm"],
data = [
structured_resources = [
"//Source/santad/testdata:binaryrules_testdata",
],
minimum_os_version = "11.0",
Expand Down Expand Up @@ -1015,7 +1015,7 @@ santa_unit_test(
santa_unit_test(
name = "EndpointSecuritySerializerProtobufTest",
srcs = ["Logs/EndpointSecurity/Serializers/ProtobufTest.mm"],
data = [
structured_resources = [
"//Source/santad/testdata:protobuf_json_testdata",
],
deps = [
Expand Down
37 changes: 16 additions & 21 deletions Source/santad/Logs/EndpointSecurity/Serializers/ProtobufTest.mm
Expand Up @@ -78,6 +78,12 @@
using santa::santad::logs::endpoint_security::serializers::GetPolicyDecision;
using santa::santad::logs::endpoint_security::serializers::GetReasonEnum;

@interface ProtobufTest : XCTestCase
@property id mockConfigurator;
@property id mockDecisionCache;
@property SNTCachedDecision *testCachedDecision;
@end

JsonPrintOptions DefaultJsonPrintOptions() {
JsonPrintOptions options;
options.always_print_enums_as_ints = false;
Expand All @@ -87,21 +93,6 @@ JsonPrintOptions DefaultJsonPrintOptions() {
return options;
}

NSString *TestJsonPath(NSString *jsonFileName, uint32_t version) {
static dispatch_once_t onceToken;
static NSString *testPath;
static NSString *testDataRepoPath = @"santa/Source/santad/testdata/protobuf";
NSString *testDataRepoVersionPath = [NSString stringWithFormat:@"v%u", version];

dispatch_once(&onceToken, ^{
testPath = [NSString pathWithComponents:@[
[[[NSProcessInfo processInfo] environment] objectForKey:@"TEST_SRCDIR"], testDataRepoPath
]];
});

return [NSString pathWithComponents:@[ testPath, testDataRepoVersionPath, jsonFileName ]];
}

NSString *EventTypeToFilename(es_event_type_t eventType) {
switch (eventType) {
case ES_EVENT_TYPE_NOTIFY_CLOSE: return @"close.json";
Expand All @@ -117,6 +108,16 @@ JsonPrintOptions DefaultJsonPrintOptions() {
}
}

NSString *TestJsonPath(NSString *jsonFileName, uint32_t version) {
NSString *p = [NSString pathWithComponents:@[
[[NSBundle bundleForClass:[ProtobufTest class]] resourcePath],
@"protobuf",
[NSString stringWithFormat:@"v%u", version],
jsonFileName,
]];
return p;
}

NSString *LoadTestJson(NSString *jsonFileName, uint32_t version) {
NSError *err = nil;
NSString *jsonData = [NSString stringWithContentsOfFile:TestJsonPath(jsonFileName, version)
Expand Down Expand Up @@ -325,12 +326,6 @@ void SerializeAndCheckNonESEvents(
XCTBubbleMockVerifyAndClearExpectations(mockESApi.get());
}

@interface ProtobufTest : XCTestCase
@property id mockConfigurator;
@property id mockDecisionCache;
@property SNTCachedDecision *testCachedDecision;
@end

@implementation ProtobufTest

- (void)setUp {
Expand Down
4 changes: 2 additions & 2 deletions Source/santad/SantadTest.mm
Expand Up @@ -71,7 +71,6 @@ static void SetBinaryDataFromHexString(const char *hexStr, uint8_t *buf, size_t
}
}

static NSString *const testBinariesPath = @"santa/Source/santad/testdata/binaryrules";
static const char *kAllowedSigningID = "com.google.allowed_signing_id";
static const char *kBlockedSigningID = "com.google.blocked_signing_id";
static const char *kNoRuleMatchSigningID = "com.google.no_rule_match_signing_id";
Expand Down Expand Up @@ -127,7 +126,8 @@ - (BOOL)checkBinaryExecution:(NSString *)binaryName
OCMStub([mockConfigurator fileAccessPolicyUpdateIntervalSec]).andReturn(600);

NSString *testPath = [NSString pathWithComponents:@[
[[[NSProcessInfo processInfo] environment] objectForKey:@"TEST_SRCDIR"], testBinariesPath
[[NSBundle bundleForClass:[self class]] resourcePath],
@"binaryrules",
]];

OCMStub([self.mockSNTDatabaseController databasePath]).andReturn(testPath);
Expand Down
2 changes: 0 additions & 2 deletions helper.bzl
Expand Up @@ -26,7 +26,6 @@ def santa_unit_test(
resources = [],
structured_resources = [],
copts = [],
data = [],
**kwargs):
apple_resource_group(
name = "%s_resources" % name,
Expand All @@ -50,6 +49,5 @@ def santa_unit_test(
minimum_os_version = minimum_os_version,
deps = [":%s_lib" % name],
size = size,
data = data,
visibility = ["//:__subpackages__"],
)

0 comments on commit d45dd77

Please sign in to comment.