From 9dafb4fb79465e1a880ad4cf307aeaea0ef63f08 Mon Sep 17 00:00:00 2001 From: Rainux Luo Date: Sat, 7 Jan 2017 09:26:32 +0800 Subject: [PATCH] Add tests for ServerProfile --- ShadowsocksX-NG.xcodeproj/project.pbxproj | 4 ++ ShadowsocksX-NG/AppDelegate.swift | 2 +- ShadowsocksX-NG/ServerProfile.swift | 2 +- ShadowsocksX-NG/ServerProfileManager.swift | 2 +- ShadowsocksX-NGTests/ServerProfileTests.swift | 57 +++++++++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 ShadowsocksX-NGTests/ServerProfileTests.swift diff --git a/ShadowsocksX-NG.xcodeproj/project.pbxproj b/ShadowsocksX-NG.xcodeproj/project.pbxproj index 74c6442..792c650 100755 --- a/ShadowsocksX-NG.xcodeproj/project.pbxproj +++ b/ShadowsocksX-NG.xcodeproj/project.pbxproj @@ -60,6 +60,7 @@ 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 */; }; + D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E3630A1E2072980027449B /* ServerProfileTests.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 */ @@ -178,6 +179,7 @@ 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 = ""; }; + D8E3630A1E2072980027449B /* ServerProfileTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServerProfileTests.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 */ @@ -311,6 +313,7 @@ 9B0BFFF71D0460A70040E62B /* ShadowsocksX-NGTests */ = { isa = PBXGroup; children = ( + D8E3630A1E2072980027449B /* ServerProfileTests.swift */, D829DB3E1E20262300FB5E1E /* UtilsTests.swift */, 9B0BFFF81D0460A70040E62B /* ShadowsocksX_NGTests.swift */, 9B0BFFFA1D0460A70040E62B /* Info.plist */, @@ -670,6 +673,7 @@ files = ( 9B0BFFF91D0460A70040E62B /* ShadowsocksX_NGTests.swift in Sources */, D829DB3F1E20262300FB5E1E /* UtilsTests.swift in Sources */, + D8E3630B1E2072980027449B /* ServerProfileTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ShadowsocksX-NG/AppDelegate.swift b/ShadowsocksX-NG/AppDelegate.swift index 6b5545a..0e98f02 100755 --- a/ShadowsocksX-NG/AppDelegate.swift +++ b/ShadowsocksX-NG/AppDelegate.swift @@ -123,7 +123,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele for url in urls { let profielDict = ParseSSURL(url) if let profielDict = profielDict { - let profile = ServerProfile.fromDictionary(profielDict as [String : AnyObject]) + let profile = ServerProfile.fromDictionary(profielDict) mgr.profiles.append(profile) isChanged = true diff --git a/ShadowsocksX-NG/ServerProfile.swift b/ShadowsocksX-NG/ServerProfile.swift index c63085d..853d980 100644 --- a/ShadowsocksX-NG/ServerProfile.swift +++ b/ShadowsocksX-NG/ServerProfile.swift @@ -28,7 +28,7 @@ class ServerProfile: NSObject { self.uuid = uuid } - static func fromDictionary(_ data:[String:AnyObject]) -> ServerProfile { + static func fromDictionary(_ data:[String: Any?]) -> ServerProfile { let cp = { (profile: ServerProfile) in profile.serverHost = data["ServerHost"] as! String diff --git a/ShadowsocksX-NG/ServerProfileManager.swift b/ShadowsocksX-NG/ServerProfileManager.swift index 61b62c7..c3ee9ac 100644 --- a/ShadowsocksX-NG/ServerProfileManager.swift +++ b/ShadowsocksX-NG/ServerProfileManager.swift @@ -21,7 +21,7 @@ class ServerProfileManager: NSObject { let defaults = UserDefaults.standard if let _profiles = defaults.array(forKey: "ServerProfiles") { for _profile in _profiles { - let profile = ServerProfile.fromDictionary(_profile as! [String : AnyObject]) + let profile = ServerProfile.fromDictionary(_profile as! [String: Any]) profiles.append(profile) } } diff --git a/ShadowsocksX-NGTests/ServerProfileTests.swift b/ShadowsocksX-NGTests/ServerProfileTests.swift new file mode 100644 index 0000000..22bedd6 --- /dev/null +++ b/ShadowsocksX-NGTests/ServerProfileTests.swift @@ -0,0 +1,57 @@ +// +// ServerProfileTests.swift +// ShadowsocksX-NG +// +// Created by Rainux Luo on 07/01/2017. +// Copyright © 2017 qiuyuzhou. All rights reserved. +// + +import XCTest +@testable import ShadowsocksX_NG + +class ServerProfileTests: XCTestCase { + + // "ss://aes-256-cfb:password@example.com:8388" + let profileUrl = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA") + + // "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true" + let profileFullUrl = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU") + + var profile: ServerProfile! + + override func setUp() { + super.setUp() + + profile = ServerProfile.fromDictionary(["ServerHost": "example.com", + "ServerPort": 8388, + "Method": "aes-256-cfb", + "Password": "password", + "Remark": "Prism", + "OTA": true]) + XCTAssertNotNil(profile) + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testServerProfileURL() { + let parsed = ParseSSURL(profile.URL()) + + XCTAssertNotNil(parsed) + + XCTAssertEqual(parsed?["ServerHost"] as? String, profile.serverHost) + XCTAssertEqual(parsed?["ServerPort"] as? UInt16, profile.serverPort) + XCTAssertEqual(parsed?["Method"] as? String, profile.method) + XCTAssertEqual(parsed?["Password"] as? String, profile.password) + } + + func testPerformanceExample() { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +}