You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlTests.cs 7.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Shadowsocks.Models;
  2. using System;
  3. using Xunit;
  4. namespace Shadowsocks.Tests
  5. {
  6. public class UrlTests
  7. {
  8. [Theory]
  9. [InlineData("chacha20-ietf-poly1305", "kf!V!TFzgeNd93GE", "Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTprZiFWIVRGemdlTmQ5M0dF")]
  10. [InlineData("aes-256-gcm", "ymghiR#75TNqpa", "YWVzLTI1Ni1nY206eW1naGlSIzc1VE5xcGE")]
  11. [InlineData("aes-128-gcm", "tK*sk!9N8@86:UVm", "YWVzLTEyOC1nY206dEsqc2shOU44QDg2OlVWbQ")]
  12. public void Utilities_Base64Url_Encode(string method, string password, string expectedUserinfoBase64url)
  13. {
  14. var userinfoBase64url = Utilities.Base64Url.Encode($"{method}:{password}");
  15. Assert.Equal(expectedUserinfoBase64url, userinfoBase64url);
  16. }
  17. [Theory]
  18. [InlineData("Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTo2JW04RDlhTUI1YkElYTQl", "chacha20-ietf-poly1305:6%m8D9aMB5bA%a4%")]
  19. [InlineData("YWVzLTI1Ni1nY206YnBOZ2sqSjNrYUFZeXhIRQ", "aes-256-gcm:bpNgk*J3kaAYyxHE")]
  20. [InlineData("YWVzLTEyOC1nY206dkFBbiY4a1I6JGlBRTQ", "aes-128-gcm:vAAn&8kR:$iAE4")]
  21. public void Utilities_Base64Url_Decode(string userinfoBase64url, string expectedUserinfo)
  22. {
  23. var userinfo = Utilities.Base64Url.DecodeToString(userinfoBase64url);
  24. Assert.Equal(expectedUserinfo, userinfo);
  25. }
  26. [Theory]
  27. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "", null, null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/")] // domain name
  28. [InlineData("aes-256-gcm", "wLhN2STZ", "1.1.1.1", 853, "", null, null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@1.1.1.1:853/")] // IPv4
  29. [InlineData("aes-256-gcm", "wLhN2STZ", "2001:db8:85a3::8a2e:370:7334", 8388, "", null, null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@[2001:db8:85a3::8a2e:370:7334]:8388/")] // IPv6
  30. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "GitHub", null, null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/#GitHub")] // fragment
  31. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "👩‍💻", null, null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/#%F0%9F%91%A9%E2%80%8D%F0%9F%92%BB")] // fragment
  32. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "", "v2ray-plugin", null, "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin")] // plugin
  33. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "", null, "server;tls;host=github.com", "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/")] // pluginOpts
  34. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "", "v2ray-plugin", "server;tls;host=github.com", "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin%3Bserver%3Btls%3Bhost%3Dgithub.com")] // plugin + pluginOpts
  35. [InlineData("aes-256-gcm", "wLhN2STZ", "github.com", 443, "GitHub", "v2ray-plugin", "server;tls;host=github.com", "ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin%3Bserver%3Btls%3Bhost%3Dgithub.com#GitHub")] // fragment + plugin + pluginOpts
  36. public void Server_ToUrl(string method, string password, string host, int port, string fragment, string? plugin, string? pluginOpts, string expectedSSUri)
  37. {
  38. var server = new Server()
  39. {
  40. Password = password,
  41. Method = method,
  42. Host = host,
  43. Port = port,
  44. Name = fragment,
  45. Plugin = plugin,
  46. PluginOpts = pluginOpts,
  47. };
  48. var ssUriString = server.ToUrl().AbsoluteUri;
  49. Assert.Equal(expectedSSUri, ssUriString);
  50. }
  51. [Theory]
  52. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "", null, null)] // domain name
  53. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@1.1.1.1:853/", true, "aes-256-gcm", "wLhN2STZ", "1.1.1.1", 853, "", null, null)] // IPv4
  54. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@[2001:db8:85a3::8a2e:370:7334]:8388/", true, "aes-256-gcm", "wLhN2STZ", "2001:db8:85a3::8a2e:370:7334", 8388, "", null, null)] // IPv6
  55. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/#GitHub", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "GitHub", null, null)] // fragment
  56. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/#%F0%9F%91%A9%E2%80%8D%F0%9F%92%BB", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "👩‍💻", null, null)] // fragment
  57. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "", "v2ray-plugin", null)] // plugin
  58. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin%3Bserver%3Btls%3Bhost%3Dgithub.com", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "", "v2ray-plugin", "server;tls;host=github.com")] // plugin + pluginOpts
  59. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFo@github.com:443/?plugin=v2ray-plugin%3Bserver%3Btls%3Bhost%3Dgithub.com#GitHub", true, "aes-256-gcm", "wLhN2STZ", "github.com", 443, "GitHub", "v2ray-plugin", "server;tls;host=github.com")] // fragment + plugin + pluginOpts
  60. [InlineData("ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTo2JW04RDlhTUI1YkElYTQl@github.com:443/", true, "chacha20-ietf-poly1305", "6%m8D9aMB5bA%a4%", "github.com", 443, "", null, null)] // userinfo parsing
  61. [InlineData("ss://YWVzLTI1Ni1nY206YnBOZ2sqSjNrYUFZeXhIRQ@github.com:443/", true, "aes-256-gcm", "bpNgk*J3kaAYyxHE", "github.com", 443, "", null, null)] // userinfo parsing
  62. [InlineData("ss://YWVzLTEyOC1nY206dkFBbiY4a1I6JGlBRTQ@github.com:443/", true, "aes-128-gcm", "vAAn&8kR:$iAE4", "github.com", 443, "", null, null)] // userinfo parsing
  63. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFpAZ2l0aHViLmNvbTo0NDM", false, "", "", "", 0, "", null, null)] // unsupported legacy URL
  64. [InlineData("ss://YWVzLTI1Ni1nY206d0xoTjJTVFpAZ2l0aHViLmNvbTo0NDM#some-legacy-url", false, "", "", "", 0, "", null, null)] // unsupported legacy URL with fragment
  65. [InlineData("https://github.com/", false, "", "", "", 0, "", null, null)] // non-Shadowsocks URL
  66. public void Server_TryParse(string ssUrl, bool expectedResult, string expectedMethod, string expectedPassword, string expectedHost, int expectedPort, string expectedFragment, string? expectedPlugin, string? expectedPluginOpts)
  67. {
  68. var result = Server.TryParse(ssUrl, out var server);
  69. Assert.Equal(expectedResult, result);
  70. if (result)
  71. {
  72. Assert.Equal(expectedPassword, server.Password);
  73. Assert.Equal(expectedMethod, server.Method);
  74. Assert.Equal(expectedHost, server.Host);
  75. Assert.Equal(expectedPort, server.Port);
  76. Assert.Equal(expectedFragment, server.Name);
  77. Assert.Equal(expectedPlugin, server.Plugin);
  78. Assert.Equal(expectedPluginOpts, server.PluginOpts);
  79. }
  80. }
  81. }
  82. }