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

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