From 97801c4ee6527124f3d0d67293427cd8e47dc05b Mon Sep 17 00:00:00 2001 From: Rainux Luo Date: Sat, 7 Jan 2017 04:08:28 +0800 Subject: [PATCH] Add tests for ParseSSURL() --- ShadowsocksX-NG.xcodeproj/project.pbxproj | 6 ++ ShadowsocksX-NGTests/UtilsTests.swift | 109 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 ShadowsocksX-NGTests/UtilsTests.swift diff --git a/ShadowsocksX-NG.xcodeproj/project.pbxproj b/ShadowsocksX-NG.xcodeproj/project.pbxproj index 2a78063..74c6442 100755 --- a/ShadowsocksX-NG.xcodeproj/project.pbxproj +++ b/ShadowsocksX-NG.xcodeproj/project.pbxproj @@ -59,6 +59,7 @@ C6E28E951DA79705004F8330 /* HTTPPreferencesWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C6E28E971DA79705004F8330 /* HTTPPreferencesWindowController.xib */; }; C8E42A6C1D4F270A0074C7EA /* UserRulesController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */; }; C8E42A6E1D4F2CAF0074C7EA /* UserRulesController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8E42A701D4F2CAF0074C7EA /* UserRulesController.xib */; }; + D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D829DB3E1E20262300FB5E1E /* UtilsTests.swift */; }; E0E57CCA7EB34B90F9D340F2 /* Pods_ShadowsocksX_NGTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 388120F062D7EB7DD0D8DDCA /* Pods_ShadowsocksX_NGTests.framework */; }; F0809FF1595BE2966343D3C7 /* libPods-proxy_conf_helper.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E7783AEDB4A3BDDC9FF16AC /* libPods-proxy_conf_helper.a */; }; /* End PBXBuildFile section */ @@ -176,6 +177,7 @@ C8E42A6A1D4F270A0074C7EA /* UserRulesController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserRulesController.swift; sourceTree = ""; }; C8E42A6F1D4F2CAF0074C7EA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserRulesController.xib; sourceTree = ""; }; C8E42A721D4F2CB10074C7EA /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/UserRulesController.strings"; sourceTree = ""; }; + D829DB3E1E20262300FB5E1E /* UtilsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = ""; }; E9E9FB3855DA55D0710EE7BD /* Pods-ShadowsocksX-NG.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.release.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.release.xcconfig"; sourceTree = ""; }; FE3237E9FB24D9B924A0E630 /* Pods-ShadowsocksX-NG.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ShadowsocksX-NG.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ShadowsocksX-NG/Pods-ShadowsocksX-NG.debug.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -309,6 +311,7 @@ 9B0BFFF71D0460A70040E62B /* ShadowsocksX-NGTests */ = { isa = PBXGroup; children = ( + D829DB3E1E20262300FB5E1E /* UtilsTests.swift */, 9B0BFFF81D0460A70040E62B /* ShadowsocksX_NGTests.swift */, 9B0BFFFA1D0460A70040E62B /* Info.plist */, ); @@ -666,6 +669,7 @@ buildActionMask = 2147483647; files = ( 9B0BFFF91D0460A70040E62B /* ShadowsocksX_NGTests.swift in Sources */, + D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -905,6 +909,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.qiuyuzhou.ShadowsocksX-NGTests"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "ShadowsocksX-NG/ShadowsocksX-NG-Bridging-Header.h"; SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ShadowsocksX-NG.app/Contents/MacOS/ShadowsocksX-NG"; }; @@ -920,6 +925,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.qiuyuzhou.ShadowsocksX-NGTests"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "ShadowsocksX-NG/ShadowsocksX-NG-Bridging-Header.h"; SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ShadowsocksX-NG.app/Contents/MacOS/ShadowsocksX-NG"; }; diff --git a/ShadowsocksX-NGTests/UtilsTests.swift b/ShadowsocksX-NGTests/UtilsTests.swift new file mode 100644 index 0000000..765cd57 --- /dev/null +++ b/ShadowsocksX-NGTests/UtilsTests.swift @@ -0,0 +1,109 @@ +// +// UtilsTests.swift +// ShadowsocksX-NG +// +// Created by Rainux Luo on 07/01/2017. +// Copyright © 2017 qiuyuzhou. All rights reserved. +// + +import XCTest + +class UtilsTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testParseSSURLwithPlainURL() { + let url = URL(string: "ss://aes-256-cfb:password@example.com:8388") + + let profile = ParseSSURL(url) + + XCTAssertNotNil(profile) + + XCTAssertEqual(profile?["ServerHost"] as? String, "example.com") + XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388) + XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb") + XCTAssertEqual(profile?["Password"] as? String, "password") + } + + func testParseSSURLwithPlainURLandQuery() { + let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true") + + let profile = ParseSSURL(url) + + XCTAssertNotNil(profile) + + XCTAssertEqual(profile?["ServerHost"] as? String, "example.com") + XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388) + XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb") + XCTAssertEqual(profile?["Password"] as? String, "password") + } + + func testParseSSURLwithBase64EncodedURL() { + // "ss://aes-256-cfb:password@example.com:8388" + let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA") + + let profile = ParseSSURL(url) + + XCTAssertNotNil(profile) + + XCTAssertEqual(profile?["ServerHost"] as? String, "example.com") + XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388) + XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb") + XCTAssertEqual(profile?["Password"] as? String, "password") + } + + func testParseSSURLwithBase64EncodedURLandQuery() { + // "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true" + let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU") + + let profile = ParseSSURL(url) + + XCTAssertNotNil(profile) + + XCTAssertEqual(profile?["ServerHost"] as? String, "example.com") + XCTAssertEqual(profile?["ServerPort"] as? UInt16, 8388) + XCTAssertEqual(profile?["Method"] as? String, "aes-256-cfb") + XCTAssertEqual(profile?["Password"] as? String, "password") + } + + func testParseSSURLwithEmptyURL() { + let url = URL(string: "ss://") + + let profile = ParseSSURL(url) + + XCTAssertNil(profile) + } + + func testParseSSURLwithInvalidURL() { + let url = URL(string: "ss://invalid url") + + let profile = ParseSSURL(url) + + XCTAssertNil(profile) + } + + func testParseSSURLwithBase64EncodedInvalidURL() { + // "ss://invalid url" + let url = URL(string: "ss://aW52YWxpZCB1cmw") + + let profile = ParseSSURL(url) + + XCTAssertNil(profile) + } + + func testPerformanceExample() { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +}