From df008335c36e6d29fa0f0d6278777543ad18f166 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Wed, 13 May 2015 08:57:48 +0100 Subject: [PATCH 1/2] added a check for git version (we rely on 2.3 being there for key verification), plus tests --- BuildaUtils/SSHKeyVerification.swift | 28 ++++++++++++++++++++++++++++ BuildaUtilsTests/ScriptTests.swift | 27 +++++++++++++-------------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/BuildaUtils/SSHKeyVerification.swift b/BuildaUtils/SSHKeyVerification.swift index 1c0e326..2f0dfa5 100644 --- a/BuildaUtils/SSHKeyVerification.swift +++ b/BuildaUtils/SSHKeyVerification.swift @@ -10,8 +10,36 @@ import Foundation public class SSHKeyVerification { + public class func getGitVersion() -> Script.ScriptResponse { + + let response = Script.run("/usr/bin/git", arguments: ["--version"]) + return response + } + + public class func verifyGitVersion(response: Script.ScriptResponse) -> (Bool, String) { + + let versionString = response.standardOutput.stripTrailingNewline() + let comps = versionString.componentsSeparatedByString(" ") + + if comps.count >= 3 { + let version = comps[2] + if version >= "2.3.0" { + return (true, "") + } + return (false, "Git version >= 2.3 required, found \(version)") + } + return (false, "Couldn't verify git version") + } + public class func verifyKeys(path: String, repoSSHUrl: String) -> Script.ScriptResponse { + let git = self.getGitVersion() + let (gitVersionSuccess, gitVersionErrorString) = self.verifyGitVersion(git) + if !gitVersionSuccess { + Log.error("Insufficient git version. Found \(git.standardOutput.stripTrailingNewline()), we require at least 2.3") + return (1, "", gitVersionErrorString) + } + //create a temp script, because NSTask is being difficult and doesn't play nice with environment variables, //which we need for forcing SSH keys from a specific path to verify they are valid for your repo. sigh. let uuid = NSUUID().UUIDString diff --git a/BuildaUtilsTests/ScriptTests.swift b/BuildaUtilsTests/ScriptTests.swift index 8202d05..9b2edf7 100644 --- a/BuildaUtilsTests/ScriptTests.swift +++ b/BuildaUtilsTests/ScriptTests.swift @@ -12,26 +12,25 @@ import BuildaUtils class ScriptTests: XCTestCase { - func testGitVersion() { + func testRealGitVersion() { - //here we have to specify the exact path, unfortunately `which git` leads to the prebundled git in xcode - //whereas when you run Buildasaur normally (under your user), `which git` will do what you'd expect, choose - //your active git binary (probably in /usr/bin/git or /usr/local/bin/git - - let response = Script.run("/usr/bin/git", arguments: ["--version"]) + let response = SSHKeyVerification.getGitVersion() XCTAssertEqual(response.terminationStatus, 0) XCTAssertEqual(response.standardError, "") - let versionString = response.standardOutput - XCTAssert(count(versionString) > 0, "Git version must not be an empty string") + let (success, errorString) = SSHKeyVerification.verifyGitVersion(response) + XCTAssertTrue(success) + XCTAssertEqual(errorString, "") + } + + func testFakeWrongGitVersion() { - let comps = versionString.componentsSeparatedByString(" ") - XCTAssertGreaterThanOrEqual(comps.count, 3) + //test we catch lower versions... + let response = (0, "git version 2.1.0 bla bla bla", "") - if comps.count >= 3 { - let version = comps[2] - XCTAssertGreaterThanOrEqual(version, "2.3.0", "Git version must be at least 2.3") - } + let (success, errorString) = SSHKeyVerification.verifyGitVersion(response) + XCTAssertFalse(success) + XCTAssertNotEqual(errorString, "") } func testWhich() { From 529b56d0486b6f01f93e2288ae92b3227e8c72db Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Wed, 13 May 2015 09:00:12 +0100 Subject: [PATCH 2/2] one more test for the right git version --- BuildaUtilsTests/ScriptTests.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/BuildaUtilsTests/ScriptTests.swift b/BuildaUtilsTests/ScriptTests.swift index 9b2edf7..18eded6 100644 --- a/BuildaUtilsTests/ScriptTests.swift +++ b/BuildaUtilsTests/ScriptTests.swift @@ -23,6 +23,16 @@ class ScriptTests: XCTestCase { XCTAssertEqual(errorString, "") } + func testFakeRightGitVersion() { + + //test we pass higher versions... + let response = (0, "git version 2.3.1 bla bla bla", "") + + let (success, errorString) = SSHKeyVerification.verifyGitVersion(response) + XCTAssertTrue(success) + XCTAssertEqual(errorString, "") + } + func testFakeWrongGitVersion() { //test we catch lower versions...