Skip to content

Commit

Permalink
Fix/test reading configuration content in /usr/local/etc/wireguard
Browse files Browse the repository at this point in the history
closes 21
  • Loading branch information
Johan Bloemberg committed Jan 12, 2019
1 parent 92131a6 commit 3eafb59
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 25 deletions.
38 changes: 33 additions & 5 deletions IntegrationTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,44 @@ import XCTest

let testConfigFile = "test-localhost"
let testConfigFileInvalid = "test-invalid"
let testConfigFileUsrLocal = "test-usr-local"

class IntegrationTests: XCTestCase {
override func setUp() {
continueAfterFailure = false

// Verify the proper configuration file for testing is installed.
// Verify the proper configuration files for testing are installed.
let bundle = Bundle(for: type(of: self).self)
for configFile in [testConfigFile, testConfigFileInvalid] {
let testConfig = bundle.path(forResource: configFile, ofType: "conf")
if !FileManager.default.contentsEqual(atPath: testConfig!, andPath: "/etc/wireguard/\(configFile).conf") {
var requiredTestConfigFiles = [testConfigFile, testConfigFileInvalid].map { "/etc/wireguard/\($0).conf" }
requiredTestConfigFiles.append("/usr/local/etc/wireguard/\(testConfigFileUsrLocal).conf")
for configFilePath in requiredTestConfigFiles {
let configFileName = ((configFilePath as NSString).lastPathComponent as NSString).deletingPathExtension
let testConfig = bundle.path(forResource: configFileName, ofType: "conf")
if !FileManager.default.contentsEqual(atPath: testConfig!, andPath: configFilePath) {
XCTFail("Integration test environment not prepared. Please run `make prep-integration`.")
}
}
XCUIApplication().launch()
let app = XCUIApplication()
app.launchEnvironment = ["RESET_CONFIGURATION": "1"]
app.launch()

// gives a little more time before starting a test to enter the password to install the helper
addUIInterruptionMonitor(withDescription: "Wait for Helper install password dialog") { (alert) -> Bool in
if alert.buttons["Install Helper"].exists {
sleep(1_000_000_000)
return true
}
return false
}
}

// If configuration is loaded correctly the proper menu item should be available
func testLoadConfiguration() {
let menuBarsQuery = XCUIApplication().menuBars
menuBarsQuery.children(matching: .statusItem).element.click()
XCTAssertTrue(menuBarsQuery.menuItems[testConfigFile].exists)
XCTAssertTrue(menuBarsQuery.menuItems[testConfigFileInvalid].exists)
XCTAssertTrue(menuBarsQuery.menuItems[testConfigFileUsrLocal].exists)
}

// Tunnel should be checked and show details if it is enabled
Expand Down Expand Up @@ -92,4 +109,15 @@ class IntegrationTests: XCTestCase {

app.dialogs["alert"].buttons["OK"].click()
}

func testTunnelDetails() {
let menuBarsQuery = XCUIApplication().menuBars
XCUIElement.perform(withKeyModifiers: .option) {
menuBarsQuery.children(matching: .statusItem).element.click()
}

XCTAssertTrue(menuBarsQuery.menuItems["Address: 192.0.2.0/32"].exists)
// details for /usr/local/etc/wireguard config
XCTAssertTrue(menuBarsQuery.menuItems["Address: 192.0.3.0/32"].exists)
}
}
8 changes: 8 additions & 0 deletions IntegrationTests/test-usr-local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# A WireGuard config used for integration testing
[Interface]
Address = 192.0.3.0/32
PrivateKey = MIKtfK9lvhBbMU9xThDJ+fe7XXN009ljIKiVDxEMXn0=
[Peer]
PublicKey = ExO1PPLobAXSOCDFs7GpwJcG+5VMQZD9Pk73YqxXoS8=
Endpoint = 192.0.3.1:51820
AllowedIPs = 198.51.100.0/24
17 changes: 8 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ swift_sources=$(shell find * -name "*.swift"|grep -vE 'SKQueue|INIParse')
other_sources=$(shell find * -name "*.plist") WireGuardStatusbar.xcodeproj/project.pbxproj
sources=${swift_sources} ${other_sources}

configuration?=Release

version?=$(shell git describe --tags --always --abbrev=0)
next_version:=$(shell echo ${version} | ( IFS=".$$IFS" ; read major minor && echo $$major.$$((minor + 1)) ))
new_version?=${next_version}
Expand All @@ -30,7 +28,7 @@ all: test dist install
# run tests
test: .test-unit .test-integration
.test-unit: ${sources} .check | icons ${xcpretty}
set -o pipefail; xcodebuild -scheme WireGuardStatusbar -configuration ${configuration} test | ${xcpretty}
set -o pipefail; xcodebuild -scheme WireGuardStatusbar test | ${xcpretty}
@touch $@

# verify code quality
Expand All @@ -55,11 +53,11 @@ test-integration: .test-integration
.test-integration: ${sources} /etc/wireguard/test-localhost.conf | icons
# application running in Xcode will hang the test
-osascript -e 'tell application "Xcode" to set actionResult to stop workspace document 1'
set -o pipefail; xcodebuild -scheme IntegrationTests -configuration ${configuration} test | ${xcpretty}
set -o pipefail; xcodebuild -scheme IntegrationTests test | ${xcpretty}
@touch $@

prep-integration: /etc/wireguard/test-localhost.conf /etc/wireguard/test-invalid.conf
/etc/wireguard/test-%.conf: IntegrationTests/test-%.conf
prep-integration: /etc/wireguard/test-localhost.conf /etc/wireguard/test-invalid.conf /usr/local/etc/wireguard/test-usr-local.conf
/etc/wireguard/test-%.conf /usr/local/etc/wireguard/test-%.conf: IntegrationTests/test-%.conf
sudo chmod 0755 ${@D}
sudo cp $< $@

Expand Down Expand Up @@ -97,7 +95,7 @@ ${dist}/WireGuardStatusbar.app: ${build_dest}/WireGuardStatusbar.app Misc/Uninst

# Generate archive build (this excludes debug symbols (dSYM) which are in a release build)
${build_dest}/WireGuardStatusbar.app: ${sources} | icons ${xcpretty}
xcodebuild -scheme WireGuardStatusbar -archivePath "${archive}" -configuration ${configuration} archive | ${xcpretty}
xcodebuild -scheme WireGuardStatusbar -archivePath "${archive}" archive | ${xcpretty}

# install and run the App /Application using the distributable .dmg
install: /Applications/WireGuardStatusbar.app
Expand Down Expand Up @@ -128,11 +126,12 @@ bump:
git commit --amend --no-edit
git tag ${new_version}

release: test bump dist install
prep-release: test dist install
release: prep-release
git push
git push --tags
open .
open https://github.com/aequitas/macos-menubar-wireguard/releases/edit/${new_version}
open https://github.com/aequitas/macos-menubar-wireguard/releases/edit/${version}

## Icon/image generation

Expand Down
4 changes: 4 additions & 0 deletions WireGuardStatusbar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
0428EA0D21BB066E00178BA0 /* WireGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0428EA0C21BB066E00178BA0 /* WireGuard.swift */; };
0428EA1021BB081A00178BA0 /* INIParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0428EA0E21BB07DB00178BA0 /* INIParser.swift */; };
043C8C8121E7CDBF00069D5C /* test-invalid.conf in Resources */ = {isa = PBXBuildFile; fileRef = 043C8C8021E7CDBF00069D5C /* test-invalid.conf */; };
043C8C8621EA0E1C00069D5C /* test-usr-local.conf in Resources */ = {isa = PBXBuildFile; fileRef = 043C8C8521EA0E1C00069D5C /* test-usr-local.conf */; };
0441E7EF211F78590035E4EF /* WireGuardStatusbarHelper in Copy Helper */ = {isa = PBXBuildFile; fileRef = 0441E7EC211F76B60035E4EF /* WireGuardStatusbarHelper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
0444B862211F5CD700CA6ECA /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0444B861211F5CD700CA6ECA /* main.swift */; };
04554CBC21C0515900A08A17 /* test-localhost.conf in Resources */ = {isa = PBXBuildFile; fileRef = 04554CBB21C0515900A08A17 /* test-localhost.conf */; };
Expand Down Expand Up @@ -124,6 +125,7 @@
0428EA0C21BB066E00178BA0 /* WireGuard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuard.swift; sourceTree = "<group>"; };
0428EA0E21BB07DB00178BA0 /* INIParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = INIParser.swift; sourceTree = "<group>"; };
043C8C8021E7CDBF00069D5C /* test-invalid.conf */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "test-invalid.conf"; sourceTree = "<group>"; };
043C8C8521EA0E1C00069D5C /* test-usr-local.conf */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "test-usr-local.conf"; sourceTree = "<group>"; };
0441E7E9211F76B60035E4EF /* WireGuardStatusbar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WireGuardStatusbar.app; sourceTree = BUILT_PRODUCTS_DIR; };
0441E7EC211F76B60035E4EF /* WireGuardStatusbarHelper */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = WireGuardStatusbarHelper; sourceTree = BUILT_PRODUCTS_DIR; };
0444B861211F5CD700CA6ECA /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,6 +209,7 @@
0490167F21C04B3900299400 /* IntegrationTests.swift */,
043C8C8021E7CDBF00069D5C /* test-invalid.conf */,
04554CBB21C0515900A08A17 /* test-localhost.conf */,
043C8C8521EA0E1C00069D5C /* test-usr-local.conf */,
04B2BD3521BD654C00CCAE2F /* UITests.swift */,
);
path = IntegrationTests;
Expand Down Expand Up @@ -396,6 +399,7 @@
buildActionMask = 2147483647;
files = (
04554CBC21C0515900A08A17 /* test-localhost.conf in Resources */,
043C8C8621EA0E1C00069D5C /* test-usr-local.conf in Resources */,
043C8C8121E7CDBF00069D5C /* test-invalid.conf in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 4 additions & 2 deletions WireGuardStatusbar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSUserNotifi
defaults.register(defaults: DefaultSettings.App)

#if DEBUG
// reset preferences to defaults for development/(ui)testing
defaults.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
// reset preferences to defaults for UI testing
if ProcessInfo.processInfo.environment["RESET_CONFIGURATION"] == "1" {
defaults.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
}
#endif

// set a default icon at startup
Expand Down
2 changes: 1 addition & 1 deletion WireGuardStatusbar/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.22</string>
<key>CFBundleVersion</key>
<string>124</string>
<string>125</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
4 changes: 2 additions & 2 deletions WireGuardStatusbarHelper/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
<key>CFBundleShortVersionString</key>
<string>1.22</string>
<key>CFBundleVersion</key>
<string>34</string>
<string>35</string>
<key>Hash</key>
<string>303e3fef62e57d3b185e15d2fb15ab21</string>
<key>SMAuthorizedClients</key>
<array>
<string>anchor apple generic and identifier "WireGuardStatusbar"</string>
</array>
<key>SOURCE_HASH</key>
<string>0a2cf7044d6923ec4b35e7fe34b7b4cb</string>
<string>926b12e8d7b7687205e644a65496db4f</string>
</dict>
</plist>
7 changes: 1 addition & 6 deletions WireGuardStatusbarHelper/WireGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,10 @@ struct WireGuard {

// TODO: read configuration data from wg showconf as well
NSLog("Reading config file: \(configFile)")
var configData: String
if let configFileContents = try? String(contentsOfFile: configFile,
encoding: .utf8) {
configData = configFileContents
} else {
NSLog("Failed to read configuration file '\(configFile)'")
configData = ""
return WireGuard.censorConfigurationData(configFileContents)
}
return WireGuard.censorConfigurationData(configData)
}
NSLog("Could not find configuration file for tunnel '\(tunnelName)'")
return ""
Expand Down

0 comments on commit 3eafb59

Please sign in to comment.