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

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