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

IdentClientTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.DMDircMBassador;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.parser.irc.IRCClientInfo;
  28. import com.dmdirc.parser.irc.IRCParser;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static org.junit.Assert.assertEquals;
  36. import static org.junit.Assert.assertTrue;
  37. import static org.mockito.Mockito.when;
  38. @RunWith(MockitoJUnitRunner.class)
  39. public class IdentClientTest {
  40. @Mock private AggregateConfigProvider acp;
  41. @Mock private ServerManager sm;
  42. @Mock private Server server;
  43. @Mock private IRCParser parser;
  44. @Mock private IRCClientInfo client;
  45. @Mock private AggregateConfigProvider config;
  46. @Mock private DMDircMBassador eventBus;
  47. protected IdentClient getClient() {
  48. final List<Server> servers = new ArrayList<>();
  49. servers.add(server);
  50. when(sm.getServers()).thenReturn(servers);
  51. when(server.getParser()).thenReturn(parser);
  52. when(parser.getLocalPort()).thenReturn(60);
  53. when(parser.getLocalClient()).thenReturn(client);
  54. when(client.getNickname()).thenReturn("nickname");
  55. when(client.getUsername()).thenReturn("username");
  56. return new IdentClient(eventBus, null, null, sm, config, "plugin-Identd");
  57. }
  58. @Test
  59. public void testInvalidIdent() {
  60. final String response = getClient().getIdentResponse("invalid request!", acp);
  61. assertContains("Illegal requests must result in an ERROR response",
  62. response, "ERROR");
  63. }
  64. @Test
  65. public void testQuoting() {
  66. final String response = getClient().getIdentResponse("in\\valid:invalid", acp);
  67. assertStartsWith("Special chars in illegal requests must be quoted",
  68. response, "in\\\\valid\\:invalid");
  69. }
  70. @Test
  71. public void testQuoting2() {
  72. final String response = getClient().getIdentResponse("in\\\\valid\\ inv\\:alid", acp);
  73. assertStartsWith("Escaped characters in illegal requests shouldn't be doubly-escaped",
  74. response, "in\\\\valid\\ inv\\:alid");
  75. }
  76. @Test
  77. public void testNonNumericPort() {
  78. final String response = getClient().getIdentResponse("abc, def", acp);
  79. assertContains("Non-numeric ports must result in an ERROR response",
  80. response, "ERROR");
  81. assertStartsWith("Specified ports must be returned in the response",
  82. response.replaceAll("\\s+", ""), "abc,def:");
  83. }
  84. private void doPortTest(final String ports) {
  85. final String response = getClient().getIdentResponse(ports, acp);
  86. assertContains("Illegal ports must result in an ERROR response",
  87. response, "ERROR");
  88. assertContains("Illegal ports must result in an INVALID-PORT response",
  89. response, "INVALID-PORT");
  90. assertStartsWith("Port numbers must be returned as part of the response",
  91. response.replaceAll("\\s+", ""), ports.replaceAll("\\s+", ""));
  92. }
  93. @Test
  94. public void testOutOfRangePorts() {
  95. doPortTest("0, 50");
  96. doPortTest("65536, 50");
  97. doPortTest("50, 0");
  98. doPortTest("50, 65536");
  99. }
  100. @Test
  101. public void testAlwaysOn() {
  102. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(false);
  103. final String response = getClient().getIdentResponse("50, 50", acp);
  104. assertContains("Unknown port requests must return an ERROR response",
  105. response, "ERROR");
  106. assertContains("Unknown port requests must return a NO-USER response",
  107. response, "NO-USER");
  108. }
  109. @Test
  110. public void testHidden() {
  111. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  112. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(true);
  113. final String response = getClient().getIdentResponse("50, 50", acp);
  114. assertContains("Hidden requests must return an ERROR response",
  115. response, "ERROR");
  116. assertContains("Hidden requests must return a HIDDEN-USER response",
  117. response, "HIDDEN-USER");
  118. }
  119. @Test
  120. public void testSystemNameQuoting() {
  121. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  122. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  123. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  124. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
  125. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
  126. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("");
  127. final String response = getClient().getIdentResponse("50, 50", acp);
  128. assertContains("Special characters must be quoted in system names",
  129. response, "a\\:b\\\\c\\,d");
  130. }
  131. @Test
  132. public void testCustomNameQuoting() {
  133. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  134. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  135. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  136. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
  137. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  138. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
  139. final String response = getClient().getIdentResponse("50, 50", acp);
  140. assertContains("Special characters must be quoted in custom names",
  141. response, "a\\:b\\\\c\\,d");
  142. }
  143. @Test
  144. public void testCustomNames() {
  145. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  146. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  147. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  148. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
  149. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  150. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  151. final String response = getClient().getIdentResponse("50, 60", acp);
  152. final String[] bits = response.split(":");
  153. assertTrue("Responses must include port pair",
  154. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  155. assertEquals("Positive response must include USERID",
  156. "USERID", bits[1].trim());
  157. assertEquals("Must use custom system name", "system", bits[2].trim());
  158. assertEquals("Must use custom name", "name", bits[3].trim());
  159. }
  160. @Test
  161. public void testOSWindows() {
  162. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  163. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  164. System.setProperty("user.name", "test");
  165. System.setProperty("os.name", "windows");
  166. final String response = getClient().getIdentResponse("50, 50", acp);
  167. assertEquals("50 , 50 : USERID : WIN32 : test", response);
  168. }
  169. @Test
  170. public void testOSMac() {
  171. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  172. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  173. System.setProperty("user.name", "test");
  174. System.setProperty("os.name", "mac");
  175. final String response = getClient().getIdentResponse("50, 50", acp);
  176. assertEquals("50 , 50 : USERID : MACOS : test", response);
  177. }
  178. @Test
  179. public void testOSLinux() {
  180. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  181. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  182. System.setProperty("user.name", "test");
  183. System.setProperty("os.name", "linux");
  184. final String response = getClient().getIdentResponse("50, 50", acp);
  185. assertEquals("50 , 50 : USERID : UNIX : test", response);
  186. }
  187. @Test
  188. public void testOSBSD() {
  189. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  190. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  191. System.setProperty("user.name", "test");
  192. System.setProperty("os.name", "bsd");
  193. final String response = getClient().getIdentResponse("50, 50", acp);
  194. assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
  195. }
  196. @Test
  197. public void testOSOS2() {
  198. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  199. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  200. System.setProperty("user.name", "test");
  201. System.setProperty("os.name", "os/2");
  202. final String response = getClient().getIdentResponse("50, 50", acp);
  203. assertEquals("50 , 50 : USERID : OS/2 : test", response);
  204. }
  205. @Test
  206. public void testOSUnix() {
  207. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  208. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  209. System.setProperty("user.name", "test");
  210. System.setProperty("os.name", "unix");
  211. final String response = getClient().getIdentResponse("50, 50", acp);
  212. assertEquals("50 , 50 : USERID : UNIX : test", response);
  213. }
  214. @Test
  215. public void testOSIrix() {
  216. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  217. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  218. System.setProperty("user.name", "test");
  219. System.setProperty("os.name", "irix");
  220. final String response = getClient().getIdentResponse("50, 50", acp);
  221. assertEquals("50 , 50 : USERID : IRIX : test", response);
  222. }
  223. @Test
  224. public void testOSUnknown() {
  225. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  226. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  227. System.setProperty("user.name", "test");
  228. System.setProperty("os.name", "test");
  229. final String response = getClient().getIdentResponse("50, 50", acp);
  230. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  231. }
  232. @Test
  233. public void testNameSystem() {
  234. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  235. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  236. System.setProperty("user.name", "test");
  237. System.setProperty("os.name", "test");
  238. final String response = getClient().getIdentResponse("50, 50", acp);
  239. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  240. }
  241. @Test
  242. public void testNameCustom() {
  243. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  244. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  245. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  246. System.setProperty("user.name", "test");
  247. System.setProperty("os.name", "test");
  248. final String response = getClient().getIdentResponse("50, 50", acp);
  249. assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
  250. }
  251. @Test
  252. public void testNameNickname() {
  253. when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
  254. System.setProperty("user.name", "test");
  255. System.setProperty("os.name", "test");
  256. final String response = getClient().getIdentResponse("60, 50", acp);
  257. assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
  258. }
  259. @Test
  260. public void testNameUsername() {
  261. when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
  262. System.setProperty("user.name", "test");
  263. System.setProperty("os.name", "test");
  264. final String response = getClient().getIdentResponse("60, 50", acp);
  265. assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
  266. }
  267. private static void assertContains(final String msg, final String haystack,
  268. final String needle) {
  269. assertTrue(msg, haystack.indexOf(needle) > -1);
  270. }
  271. private static void assertStartsWith(final String msg, final String haystack,
  272. final String needle) {
  273. assertTrue(msg, haystack.startsWith(needle));
  274. }
  275. }