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.

Sip003PluginTest.cs 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System.Threading;
  4. using System.Collections.Generic;
  5. using Shadowsocks.Model;
  6. using Shadowsocks.Controller.Service;
  7. using System.Diagnostics;
  8. using System.Net;
  9. namespace Shadowsocks.Test
  10. {
  11. [TestClass]
  12. public class Sip003PluginTest
  13. {
  14. string fake_plugin = "ftp";
  15. [TestMethod]
  16. public void TestSip003Plugin_NoPlugin()
  17. {
  18. var NoPlugin = Sip003Plugin.CreateIfConfigured(
  19. new Server
  20. {
  21. server = "192.168.100.1",
  22. server_port = 8888,
  23. password = "test",
  24. method = "bf-cfb"
  25. },
  26. false);
  27. RunPluginSupportTest(
  28. NoPlugin,
  29. "",
  30. "",
  31. "",
  32. "192.168.100.1",
  33. 8888);
  34. }
  35. [TestMethod]
  36. public void TestSip003Plugin_Plugin()
  37. {
  38. var Plugin = Sip003Plugin.CreateIfConfigured(
  39. new Server
  40. {
  41. server = "192.168.100.1",
  42. server_port = 8888,
  43. password = "test",
  44. method = "bf-cfb",
  45. plugin = fake_plugin
  46. },
  47. false);
  48. RunPluginSupportTest(
  49. Plugin,
  50. fake_plugin,
  51. "",
  52. "",
  53. "192.168.100.1",
  54. 8888);
  55. }
  56. [TestMethod]
  57. public void TestSip003Plugin_PluginWithOpts()
  58. {
  59. var PluginWithOpts = Sip003Plugin.CreateIfConfigured(
  60. new Server
  61. {
  62. server = "192.168.100.1",
  63. server_port = 8888,
  64. password = "test",
  65. method = "bf-cfb",
  66. plugin = fake_plugin,
  67. plugin_opts = "_option"
  68. },
  69. false);
  70. RunPluginSupportTest(
  71. PluginWithOpts,
  72. fake_plugin,
  73. "_option",
  74. "",
  75. "192.168.100.1",
  76. 8888);
  77. }
  78. [TestMethod]
  79. public void TestSip003Plugin_PluginWithArgs()
  80. {
  81. var PluginWithArgs = Sip003Plugin.CreateIfConfigured(
  82. new Server
  83. {
  84. server = "192.168.100.1",
  85. server_port = 8888,
  86. password = "test",
  87. method = "bf-cfb",
  88. plugin = fake_plugin,
  89. plugin_args = "_test"
  90. },
  91. false);
  92. RunPluginSupportTest(
  93. PluginWithArgs,
  94. fake_plugin,
  95. "",
  96. "_test",
  97. "192.168.100.1",
  98. 8888);
  99. }
  100. [TestMethod]
  101. public void TestSip003Plugin_PluginWithOptsAndArgs()
  102. {
  103. var PluginWithOptsAndArgs = Sip003Plugin.CreateIfConfigured(
  104. new Server
  105. {
  106. server = "192.168.100.1",
  107. server_port = 8888,
  108. password = "test",
  109. method = "bf-cfb",
  110. plugin = fake_plugin,
  111. plugin_opts = "_option",
  112. plugin_args = "_test"
  113. },
  114. false);
  115. RunPluginSupportTest(
  116. PluginWithOptsAndArgs,
  117. fake_plugin,
  118. "_option",
  119. "_test",
  120. "192.168.100.1",
  121. 8888);
  122. }
  123. [TestMethod]
  124. public void TestSip003Plugin_PluginWithArgsReplaced()
  125. {
  126. var PluginWithArgsReplaced = Sip003Plugin.CreateIfConfigured(
  127. new Server
  128. {
  129. server = "192.168.100.1",
  130. server_port = 8888,
  131. password = "test",
  132. method = "bf-cfb",
  133. plugin = fake_plugin,
  134. plugin_args = "_test,%SS_REMOTE_HOST%"
  135. },
  136. false);
  137. RunPluginSupportTest(
  138. PluginWithArgsReplaced,
  139. fake_plugin,
  140. "",
  141. "_test,192.168.100.1",
  142. "192.168.100.1",
  143. 8888);
  144. }
  145. [TestMethod]
  146. public void TestSip003Plugin_PluginWithOptsAndArgsReplaced()
  147. {
  148. var PluginWithOptsAndArgsReplaced = Sip003Plugin.CreateIfConfigured(
  149. new Server
  150. {
  151. server = "192.168.100.1",
  152. server_port = 8888,
  153. password = "test",
  154. method = "bf-cfb",
  155. plugin = fake_plugin,
  156. plugin_opts = "_option",
  157. plugin_args = "_test,%SS_REMOTE_HOST%,%SS_PLUGIN_OPTIONS%"
  158. },
  159. false);
  160. RunPluginSupportTest(
  161. PluginWithOptsAndArgsReplaced,
  162. fake_plugin,
  163. "_option",
  164. "_test,192.168.100.1,_option",
  165. "192.168.100.1",
  166. 8888);
  167. }
  168. private static void RunPluginSupportTest(Sip003Plugin plugin, string pluginName, string pluginOpts, string pluginArgs, string serverAddress, int serverPort)
  169. {
  170. if (string.IsNullOrWhiteSpace(pluginName)) return;
  171. plugin.StartIfNeeded();
  172. Process[] processes = Process.GetProcessesByName(pluginName);
  173. Assert.AreEqual(processes.Length, 1);
  174. Process p = processes[0];
  175. var penv = ProcessEnvironment.ReadEnvironmentVariables(p);
  176. var pcmd = ProcessEnvironment.GetCommandLine(p).Trim();
  177. pcmd = pcmd.IndexOf(' ') >= 0 ? pcmd.Substring(pcmd.IndexOf(' ') + 1) : "";
  178. Assert.AreEqual(penv["SS_REMOTE_HOST"], serverAddress);
  179. Assert.AreEqual(penv["SS_REMOTE_PORT"], serverPort.ToString());
  180. Assert.AreEqual(penv["SS_LOCAL_HOST"], IPAddress.Loopback.ToString());
  181. int _ignored;
  182. Assert.IsTrue(int.TryParse(penv["SS_LOCAL_PORT"], out _ignored));
  183. Assert.AreEqual(penv["SS_PLUGIN_OPTIONS"], pluginOpts);
  184. Assert.AreEqual(pcmd, pluginArgs);
  185. plugin.Dispose();
  186. for (int i = 0; i < 50; i++)
  187. {
  188. if (Process.GetProcessesByName(pluginName).Length == 0) return;
  189. Thread.Sleep(50);
  190. }
  191. }
  192. }
  193. }