Skip to content

Commit

Permalink
Add regular execution integration tests (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnek committed Oct 1, 2021
1 parent 2611b55 commit a22e3ea
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Testing/integration/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@build_bazel_rules_apple//apple:macos.bzl", "macos_bundle")
load("//:helper.bzl", "santa_unit_test")

santa_unit_test(
name = "SNTExecTest",
data = [
"//Source/santad/testdata:binaryrules_testdata",
],
srcs = [
"SNTExecTest.m"
],
deps = [],
minimum_os_version = "10.15",
)

test_suite(
name = "integration_tests",
tests = [
":SNTExecTest",
],
)
69 changes: 69 additions & 0 deletions Testing/integration/SNTExecTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// Copyright 2021 Google Inc. All rights reserved.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>

#include <unistd.h>

@interface SNTExecTest : XCTestCase
@end

@implementation SNTExecTest : XCTestCase
- (void)setUp {
[super setUp];
fclose(stdout);
}

- (void)checkExecution:(NSString *)path shouldExec:(BOOL)shouldExec {
__block int status;

XCTestExpectation *expectation =
[self expectationWithDescription:@"Wait for test binary to execute"];

__block NSTask *task = [[NSTask alloc] init];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
task.launchPath = path;
[task launch];
[task waitUntilExit];
status = [task terminationStatus];
[expectation fulfill];
});

[self waitForExpectationsWithTimeout:20.0
handler:^(NSError *error) {
XCTAssertFalse([task isRunning], @"Test timed out: %@", error);
[task terminate];
}];

BOOL didExec = (status == 0);
XCTAssertEqual(didExec, shouldExec);
}

- (void)testShouldExecute {
NSString *testPath = @"santa/Source/santad/testdata/binaryrules";
NSString *fullTestPath = [NSString pathWithComponents:@[
[[[NSProcessInfo processInfo] environment] objectForKey:@"TEST_SRCDIR"], testPath
]];
NSDictionary *testCases = @{
@"goodbinary" : @YES,
@"badbinary" : @NO,
@"noop" : @YES,
};
for (NSString *binary in testCases) {
[self checkExecution:[NSString pathWithComponents:@[ fullTestPath, binary ]]
shouldExec:[testCases[binary] boolValue]];
}
}

@end
22 changes: 22 additions & 0 deletions Testing/integration_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -e
set -x


run_tests() {
local -a TEST_FLAGS=( --strategy=TestRunner=standalone --test_output=all )
cd ./integration
time bazel test "${TEST_FLAGS[@]}" -- ...
}

setup() {
./start_env.sh
sudo santactl sync --debug
}

main() {
setup
run_tests
}

main "$@"
1 change: 0 additions & 1 deletion Testing/start_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function install_profile() {
}

function build_install_santa() {
sudo systemextensionsctl developer on
echo "> Building and signing Santa"
$GIT_ROOT/Testing/build_and_sign.sh
systemextensionsctl list
Expand Down

0 comments on commit a22e3ea

Please sign in to comment.