選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IdentClientTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.identd;
  23. import com.dmdirc.Server;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.parser.irc.IRCClientInfo;
  27. import com.dmdirc.parser.irc.IRCParser;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.Mock;
  33. import org.mockito.runners.MockitoJUnitRunner;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertTrue;
  36. import static org.mockito.Mockito.when;
  37. @RunWith(MockitoJUnitRunner.class)
  38. public class IdentClientTest {
  39. @Mock private AggregateConfigProvider acp;
  40. @Mock private ServerManager sm;
  41. @Mock private Server server;
  42. @Mock private IRCParser parser;
  43. @Mock private IRCClientInfo client;
  44. @Mock private AggregateConfigProvider config;
  45. protected IdentClient getClient() {
  46. final List<Server> servers = new ArrayList<>();
  47. servers.add(server);
  48. when(sm.getServers()).thenReturn(servers);
  49. when(server.getParser()).thenReturn(parser);
  50. when(parser.getLocalPort()).thenReturn(60);
  51. when(parser.getLocalClient()).thenReturn(client);
  52. when(client.getNickname()).thenReturn("nickname");
  53. when(client.getUsername()).thenReturn("username");
  54. return new IdentClient(null, null, sm, config, "plugin-Identd");
  55. }
  56. @Test
  57. public void testInvalidIdent() {
  58. final String response = getClient().getIdentResponse("invalid request!", acp);
  59. assertContains("Illegal requests must result in an ERROR response",
  60. response, "ERROR");
  61. }
  62. @Test
  63. public void testQuoting() {
  64. final String response = getClient().getIdentResponse("in\\valid:invalid", acp);
  65. assertStartsWith("Special chars in illegal requests must be quoted",
  66. response, "in\\\\valid\\:invalid");
  67. }
  68. @Test
  69. public void testQuoting2() {
  70. final String response = getClient().getIdentResponse("in\\\\valid\\ inv\\:alid", acp);
  71. assertStartsWith("Escaped characters in illegal requests shouldn't be doubly-escaped",
  72. response, "in\\\\valid\\ inv\\:alid");
  73. }
  74. @Test
  75. public void testNonNumericPort() {
  76. final String response = getClient().getIdentResponse("abc, def", acp);
  77. assertContains("Non-numeric ports must result in an ERROR response",
  78. response, "ERROR");
  79. assertStartsWith("Specified ports must be returned in the response",
  80. response.replaceAll("\\s+", ""), "abc,def:");
  81. }
  82. private void doPortTest(final String ports) {
  83. final String response = getClient().getIdentResponse(ports, acp);
  84. assertContains("Illegal ports must result in an ERROR response",
  85. response, "ERROR");
  86. assertContains("Illegal ports must result in an INVALID-PORT response",
  87. response, "INVALID-PORT");
  88. assertStartsWith("Port numbers must be returned as part of the response",
  89. response.replaceAll("\\s+", ""), ports.replaceAll("\\s+", ""));
  90. }
  91. @Test
  92. public void testOutOfRangePorts() {
  93. doPortTest("0, 50");
  94. doPortTest("65536, 50");
  95. doPortTest("50, 0");
  96. doPortTest("50, 65536");
  97. }
  98. @Test
  99. public void testAlwaysOn() {
  100. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(false);
  101. final String response = getClient().getIdentResponse("50, 50", acp);
  102. assertContains("Unknown port requests must return an ERROR response",
  103. response, "ERROR");
  104. assertContains("Unknown port requests must return a NO-USER response",
  105. response, "NO-USER");
  106. }
  107. @Test
  108. public void testHidden() {
  109. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  110. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(true);
  111. final String response = getClient().getIdentResponse("50, 50", acp);
  112. assertContains("Hidden requests must return an ERROR response",
  113. response, "ERROR");
  114. assertContains("Hidden requests must return a HIDDEN-USER response",
  115. response, "HIDDEN-USER");
  116. }
  117. @Test
  118. public void testSystemNameQuoting() {
  119. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  120. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  121. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  122. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
  123. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
  124. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("");
  125. final String response = getClient().getIdentResponse("50, 50", acp);
  126. assertContains("Special characters must be quoted in system names",
  127. response, "a\\:b\\\\c\\,d");
  128. }
  129. @Test
  130. public void testCustomNameQuoting() {
  131. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  132. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  133. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  134. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
  135. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  136. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
  137. final String response = getClient().getIdentResponse("50, 50", acp);
  138. assertContains("Special characters must be quoted in custom names",
  139. response, "a\\:b\\\\c\\,d");
  140. }
  141. @Test
  142. public void testCustomNames() {
  143. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  144. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  145. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  146. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
  147. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  148. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  149. final String response = getClient().getIdentResponse("50, 60", acp);
  150. final String[] bits = response.split(":");
  151. assertTrue("Responses must include port pair",
  152. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  153. assertEquals("Positive response must include USERID",
  154. "USERID", bits[1].trim());
  155. assertEquals("Must use custom system name", "system", bits[2].trim());
  156. assertEquals("Must use custom name", "name", bits[3].trim());
  157. }
  158. @Test
  159. public void testOSWindows() {
  160. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  161. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  162. System.setProperty("user.name", "test");
  163. System.setProperty("os.name", "windows");
  164. final String response = getClient().getIdentResponse("50, 50", acp);
  165. assertEquals("50 , 50 : USERID : WIN32 : test", response);
  166. }
  167. @Test
  168. public void testOSMac() {
  169. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  170. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  171. System.setProperty("user.name", "test");
  172. System.setProperty("os.name", "mac");
  173. final String response = getClient().getIdentResponse("50, 50", acp);
  174. assertEquals("50 , 50 : USERID : MACOS : test", response);
  175. }
  176. @Test
  177. public void testOSLinux() {
  178. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  179. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  180. System.setProperty("user.name", "test");
  181. System.setProperty("os.name", "linux");
  182. final String response = getClient().getIdentResponse("50, 50", acp);
  183. assertEquals("50 , 50 : USERID : UNIX : test", response);
  184. }
  185. @Test
  186. public void testOSBSD() {
  187. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  188. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  189. System.setProperty("user.name", "test");
  190. System.setProperty("os.name", "bsd");
  191. final String response = getClient().getIdentResponse("50, 50", acp);
  192. assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
  193. }
  194. @Test
  195. public void testOSOS2() {
  196. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  197. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  198. System.setProperty("user.name", "test");
  199. System.setProperty("os.name", "os/2");
  200. final String response = getClient().getIdentResponse("50, 50", acp);
  201. assertEquals("50 , 50 : USERID : OS/2 : test", response);
  202. }
  203. @Test
  204. public void testOSUnix() {
  205. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  206. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  207. System.setProperty("user.name", "test");
  208. System.setProperty("os.name", "unix");
  209. final String response = getClient().getIdentResponse("50, 50", acp);
  210. assertEquals("50 , 50 : USERID : UNIX : test", response);
  211. }
  212. @Test
  213. public void testOSIrix() {
  214. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  215. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  216. System.setProperty("user.name", "test");
  217. System.setProperty("os.name", "irix");
  218. final String response = getClient().getIdentResponse("50, 50", acp);
  219. assertEquals("50 , 50 : USERID : IRIX : test", response);
  220. }
  221. @Test
  222. public void testOSUnknown() {
  223. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  224. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  225. System.setProperty("user.name", "test");
  226. System.setProperty("os.name", "test");
  227. final String response = getClient().getIdentResponse("50, 50", acp);
  228. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  229. }
  230. @Test
  231. public void testNameSystem() {
  232. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  233. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  234. System.setProperty("user.name", "test");
  235. System.setProperty("os.name", "test");
  236. final String response = getClient().getIdentResponse("50, 50", acp);
  237. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  238. }
  239. @Test
  240. public void testNameCustom() {
  241. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  242. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  243. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  244. System.setProperty("user.name", "test");
  245. System.setProperty("os.name", "test");
  246. final String response = getClient().getIdentResponse("50, 50", acp);
  247. assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
  248. }
  249. @Test
  250. public void testNameNickname() {
  251. when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
  252. System.setProperty("user.name", "test");
  253. System.setProperty("os.name", "test");
  254. final String response = getClient().getIdentResponse("60, 50", acp);
  255. assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
  256. }
  257. @Test
  258. public void testNameUsername() {
  259. when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
  260. System.setProperty("user.name", "test");
  261. System.setProperty("os.name", "test");
  262. final String response = getClient().getIdentResponse("60, 50", acp);
  263. assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
  264. }
  265. private static void assertContains(final String msg, final String haystack,
  266. final String needle) {
  267. assertTrue(msg, haystack.indexOf(needle) > -1);
  268. }
  269. private static void assertStartsWith(final String msg, final String haystack,
  270. final String needle) {
  271. assertTrue(msg, haystack.startsWith(needle));
  272. }
  273. }