Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename type aliases #999

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Source/santad/DataLayer/WatchItems.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@
using santa::santad::data_layer::WatchItemPathType;
using santa::santad::data_layer::WatchItemPolicy;

// Type aliases
using ValidatorBlock = bool (^)(id, NSError **);
using PathAndTypePair = std::pair<std::string, WatchItemPathType>;
using PathList = std::vector<PathAndTypePair>;
using ProcessList = std::vector<WatchItemPolicy::Process>;

NSString *const kWatchItemConfigKeyVersion = @"Version";
NSString *const kWatchItemConfigKeyWatchItems = @"WatchItems";
NSString *const kWatchItemConfigKeyPaths = @"Paths";
Expand Down Expand Up @@ -76,6 +70,12 @@

namespace santa::santad::data_layer {

// Type aliases
using ValidatorBlock = bool (^)(id, NSError **);
using PathAndTypePair = std::pair<std::string, WatchItemPathType>;
using PathList = std::vector<PathAndTypePair>;
using ProcessList = std::vector<WatchItemPolicy::Process>;

static void PopulateError(NSError **err, NSString *msg) {
if (err) {
*err = [NSError errorWithDomain:@"com.google.santa.watchitems"
Expand Down
74 changes: 39 additions & 35 deletions Source/santad/DataLayer/WatchItemsTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
using santa::santad::data_layer::WatchItemPolicy;
using santa::santad::data_layer::WatchItems;

namespace santatest {
using PathAndTypePair = std::pair<std::string, WatchItemPathType>;
using PathList = std::vector<PathAndTypePair>;
using ProcessList = std::vector<WatchItemPolicy::Process>;
} // namespace santatest

namespace santa::santad::data_layer {

Expand All @@ -51,9 +53,10 @@ extern bool ParseConfig(NSDictionary *config,
extern bool ParseConfigSingleWatchItem(NSString *name, NSDictionary *watch_item,
std::vector<std::shared_ptr<WatchItemPolicy>> &policies,
NSError **err);
extern std::variant<Unit, PathList> VerifyConfigWatchItemPaths(NSArray<id> *paths, NSError **err);
extern std::variant<Unit, ProcessList> VerifyConfigWatchItemProcesses(NSDictionary *watch_item,
NSError **err);
extern std::variant<Unit, santatest::PathList> VerifyConfigWatchItemPaths(NSArray<id> *paths,
NSError **err);
extern std::variant<Unit, santatest::ProcessList> VerifyConfigWatchItemProcesses(
NSDictionary *watch_item, NSError **err);
class WatchItemsPeer : public WatchItems {
public:
using WatchItems::ReloadConfig;
Expand Down Expand Up @@ -431,7 +434,7 @@ - (void)testPolicyLookup {
}

- (void)testVerifyConfigWatchItemPaths {
std::variant<Unit, PathList> path_list;
std::variant<Unit, santatest::PathList> path_list;
NSError *err;

// Test no paths specified
Expand All @@ -457,28 +460,29 @@ - (void)testVerifyConfigWatchItemPaths {

// Test path array dictionary with default path type
path_list = VerifyConfigWatchItemPaths(@[ @{kWatchItemConfigKeyPathsPath : @"A"} ], &err);
XCTAssertTrue(std::holds_alternative<PathList>(path_list));
XCTAssertEqual(std::get<PathList>(path_list).size(), 1);
XCTAssertCStringEqual(std::get<PathList>(path_list)[0].first.c_str(), "A");
XCTAssertEqual(std::get<PathList>(path_list)[0].second, kWatchItemPolicyDefaultPathType);
XCTAssertTrue(std::holds_alternative<santatest::PathList>(path_list));
XCTAssertEqual(std::get<santatest::PathList>(path_list).size(), 1);
XCTAssertCStringEqual(std::get<santatest::PathList>(path_list)[0].first.c_str(), "A");
XCTAssertEqual(std::get<santatest::PathList>(path_list)[0].second,
kWatchItemPolicyDefaultPathType);

// Test path array dictionary with custom path type
path_list = VerifyConfigWatchItemPaths(
@[ @{kWatchItemConfigKeyPathsPath : @"A", kWatchItemConfigKeyPathsIsPrefix : @(YES)} ], &err);
XCTAssertTrue(std::holds_alternative<PathList>(path_list));
XCTAssertEqual(std::get<PathList>(path_list).size(), 1);
XCTAssertCStringEqual(std::get<PathList>(path_list)[0].first.c_str(), "A");
XCTAssertEqual(std::get<PathList>(path_list)[0].second, WatchItemPathType::kPrefix);
XCTAssertTrue(std::holds_alternative<santatest::PathList>(path_list));
XCTAssertEqual(std::get<santatest::PathList>(path_list).size(), 1);
XCTAssertCStringEqual(std::get<santatest::PathList>(path_list)[0].first.c_str(), "A");
XCTAssertEqual(std::get<santatest::PathList>(path_list)[0].second, WatchItemPathType::kPrefix);
}

- (void)testVerifyConfigWatchItemProcesses {
std::variant<Unit, ProcessList> proc_list;
std::variant<Unit, santatest::ProcessList> proc_list;
NSError *err;

// Non-existent process list parses successfully, but has no items
proc_list = VerifyConfigWatchItemProcesses(@{}, &err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 0);
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 0);

// Process list fails to parse if contains non-array type
proc_list = VerifyConfigWatchItemProcesses(@{kWatchItemConfigKeyProcesses : @""}, &err);
Expand All @@ -488,7 +492,7 @@ - (void)testVerifyConfigWatchItemProcesses {
proc_list = VerifyConfigWatchItemProcesses(@{kWatchItemConfigKeyProcesses : @{}}, &err);
XCTAssertTrue(std::holds_alternative<Unit>(proc_list));
proc_list = VerifyConfigWatchItemProcesses(@{kWatchItemConfigKeyProcesses : @[]}, &err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));

// Test a process dictionary with no valid attributes set
proc_list = VerifyConfigWatchItemProcesses(@{kWatchItemConfigKeyProcesses : @[ @{} ]}, &err);
Expand All @@ -506,9 +510,9 @@ - (void)testVerifyConfigWatchItemProcesses {
proc_list = VerifyConfigWatchItemProcesses(
@{kWatchItemConfigKeyProcesses : @[ @{kWatchItemConfigKeyProcessesBinaryPath : @"mypath"} ]},
&err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("mypath", "", "", {}, ""));

// Test SigningID length limits
Expand All @@ -525,9 +529,9 @@ - (void)testVerifyConfigWatchItemProcesses {
@[ @{kWatchItemConfigKeyProcessesSigningID : @"com.google.test"} ]
},
&err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("", "com.google.test", "", {}, ""));

// Test TeamID length limits
Expand All @@ -542,9 +546,9 @@ - (void)testVerifyConfigWatchItemProcesses {
proc_list = VerifyConfigWatchItemProcesses(
@{kWatchItemConfigKeyProcesses : @[ @{kWatchItemConfigKeyProcessesTeamID : @"myvalidtid"} ]},
&err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("", "", "myvalidtid", {}, ""));

// Test CDHash length limits
Expand All @@ -569,9 +573,9 @@ - (void)testVerifyConfigWatchItemProcesses {
std::fill(cdhashBytes.begin(), cdhashBytes.end(), 0xAA);
proc_list = VerifyConfigWatchItemProcesses(
@{kWatchItemConfigKeyProcesses : @[ @{kWatchItemConfigKeyProcessesCDHash : cdhash} ]}, &err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("", "", "", cdhashBytes, ""));

// Test Cert Hash length limits
Expand Down Expand Up @@ -600,9 +604,9 @@ - (void)testVerifyConfigWatchItemProcesses {
kWatchItemConfigKeyProcesses : @[ @{kWatchItemConfigKeyProcessesCertificateSha256 : certHash} ]
},
&err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 1);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("", "", "", {}, [certHash UTF8String]));

// Test valid multiple attributes, multiple procs
Expand All @@ -625,12 +629,12 @@ - (void)testVerifyConfigWatchItemProcesses {
]
},
&err);
XCTAssertTrue(std::holds_alternative<ProcessList>(proc_list));
XCTAssertEqual(std::get<ProcessList>(proc_list).size(), 2);
XCTAssertEqual(std::get<ProcessList>(proc_list)[0],
XCTAssertTrue(std::holds_alternative<santatest::ProcessList>(proc_list));
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list).size(), 2);
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[0],
WatchItemPolicy::Process("mypath1", "com.google.test1", "validtid_1", cdhashBytes,
[certHash UTF8String]));
XCTAssertEqual(std::get<ProcessList>(proc_list)[1],
XCTAssertEqual(std::get<santatest::ProcessList>(proc_list)[1],
WatchItemPolicy::Process("mypath2", "com.google.test2", "validtid_2", cdhashBytes,
[certHash UTF8String]));
}
Expand Down