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

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