ServerProfileTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // ServerProfileTests.swift
  3. // ShadowsocksX-NG
  4. //
  5. // Created by Rainux Luo on 07/01/2017.
  6. // Copyright © 2017 qiuyuzhou. All rights reserved.
  7. //
  8. import XCTest
  9. @testable import ShadowsocksX_NG
  10. class ServerProfileTests: XCTestCase {
  11. var profile: ServerProfile!
  12. override func setUp() {
  13. super.setUp()
  14. profile = ServerProfile.fromDictionary(["ServerHost": "example.com",
  15. "ServerPort": 8388,
  16. "Method": "aes-256-cfb",
  17. "Password": "password",
  18. "Remark": "Protoss Prism",
  19. "OTA": true])
  20. XCTAssertNotNil(profile)
  21. }
  22. override func tearDown() {
  23. // Put teardown code here. This method is called after the invocation of each test method in the class.
  24. super.tearDown()
  25. }
  26. func testInitWithSelfGeneratedURL() {
  27. let newProfile = ServerProfile.init(url: profile.URL()!)
  28. XCTAssertEqual(newProfile?.serverHost, profile.serverHost)
  29. XCTAssertEqual(newProfile?.serverPort, profile.serverPort)
  30. XCTAssertEqual(newProfile?.method, profile.method)
  31. XCTAssertEqual(newProfile?.password, profile.password)
  32. XCTAssertEqual(newProfile?.remark, profile.remark)
  33. }
  34. func testInitWithBase64EncodedURL() {
  35. // "ss://aes-256-cfb:password@example.com:8388"
  36. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")!
  37. let profile = ServerProfile(url: url)
  38. XCTAssertNotNil(profile)
  39. XCTAssertEqual(profile?.serverHost, "example.com")
  40. XCTAssertEqual(profile?.serverPort, 8388)
  41. XCTAssertEqual(profile?.method, "aes-256-cfb")
  42. XCTAssertEqual(profile?.password, "password")
  43. XCTAssertEqual(profile?.remark, "")
  44. }
  45. func testInitWithBase64EncodedURLandQuery() {
  46. // "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
  47. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")!
  48. let profile = ServerProfile(url: url)
  49. XCTAssertNotNil(profile)
  50. XCTAssertEqual(profile?.serverHost, "example.com")
  51. XCTAssertEqual(profile?.serverPort, 8388)
  52. XCTAssertEqual(profile?.method, "aes-256-cfb")
  53. XCTAssertEqual(profile?.password, "password")
  54. XCTAssertEqual(profile?.remark, "Prism")
  55. }
  56. func testInitWithLegacyBase64EncodedURLWithTag() {
  57. let url = URL(string: "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4Cg#example-server")!
  58. let profile = ServerProfile(url: url)
  59. XCTAssertNotNil(profile)
  60. XCTAssertEqual(profile?.remark, "example-server")
  61. }
  62. func testInitWithLegacyBase64EncodedURLWithSymboInPassword() {
  63. // Note that the legacy URI doesn't follow RFC3986. It means the password here
  64. // should be plain text, not percent-encoded.
  65. // Ref: https://shadowsocks.org/en/config/quick-guide.html
  66. // `ss://bf-cfb:test/!@#:@192.168.100.1:8888`
  67. let url = URL(string: "ss://YmYtY2ZiOnRlc3QvIUAjOkAxOTIuMTY4LjEwMC4xOjg4ODg#example")!
  68. let profile = ServerProfile(url: url)
  69. XCTAssertNotNil(profile)
  70. XCTAssertEqual(profile?.password, "test/!@#:")
  71. }
  72. func testInitWithLegacyURLWithEscapedChineseRemark() {
  73. let url = URL(string: "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4#%e4%bd%a0%e5%a5%bd")!
  74. let profile = ServerProfile(url: url)
  75. XCTAssertNotNil(profile)
  76. XCTAssertEqual(profile?.remark, "你好")
  77. }
  78. func testInitWithEmptyURL() {
  79. let url = URL(string: "ss://")!
  80. let profile = ServerProfile(url: url)
  81. XCTAssertNil(profile)
  82. }
  83. func testInitWithBase64EncodedInvalidURL() {
  84. // "ss://invalid url"
  85. let url = URL(string: "ss://aW52YWxpZCB1cmw")!
  86. let profile = ServerProfile(url: url)
  87. XCTAssertNil(profile)
  88. }
  89. func testInitWithSIP002URL() {
  90. // "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true"
  91. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmQ=@example.com:8388/?Remark=Prism&OTA=true")!
  92. let profile = ServerProfile(url: url)
  93. XCTAssertNotNil(profile)
  94. XCTAssertEqual(profile?.serverHost, "example.com")
  95. XCTAssertEqual(profile?.serverPort, 8388)
  96. XCTAssertEqual(profile?.method, "aes-256-cfb")
  97. XCTAssertEqual(profile?.password, "password")
  98. XCTAssertEqual(profile?.remark, "Prism")
  99. }
  100. func testInitWithSIP002URLProfileName() {
  101. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmQ=@example.com:8388/#Name")!
  102. let profile = ServerProfile(url: url)
  103. XCTAssertNotNil(profile)
  104. XCTAssertEqual(profile?.remark, "Name")
  105. }
  106. func testInitWithSIP002URLProfileNameOverride() {
  107. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmQ=@example.com:8388/?Remark=Name#Overriden")!
  108. let profile = ServerProfile(url: url)
  109. XCTAssertNotNil(profile)
  110. XCTAssertEqual(profile?.remark, "Overriden")
  111. }
  112. func testInitWithSIP002URLProfileWithSIP003PluginNoPluginOpts() {
  113. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmQ=@134.209.56.100:8088/?plugin=v2ray-plugin;#moon-v2ray")!
  114. let profile = ServerProfile(url: url)
  115. XCTAssertNotNil(profile)
  116. XCTAssertEqual(profile?.plugin, "v2ray-plugin")
  117. }
  118. func testInitWithSIP002URLProfileWithSIP003Plugin() {
  119. let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmQ=@134.209.56.100:8088/?plugin=v2ray-plugin;tls#moon-v2ray")!
  120. let profile = ServerProfile(url: url)
  121. XCTAssertNotNil(profile)
  122. XCTAssertEqual(profile?.plugin, "v2ray-plugin")
  123. }
  124. func testPerformanceExample() {
  125. // This is an example of a performance test case.
  126. self.measure {
  127. // Put the code you want to measure the time of here.
  128. }
  129. }
  130. }