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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2006-2013 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.config.ConfigManager;
  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 static org.junit.Assert.*;
  32. import static org.mockito.Mockito.*;
  33. public class IdentClientTest {
  34. protected IdentClient getClient() {
  35. IdentdPlugin plugin = mock(IdentdPlugin.class);
  36. ServerManager sm = mock(ServerManager.class);
  37. Server server = mock(Server.class);
  38. IRCParser parser = mock(IRCParser.class);
  39. IRCClientInfo client = mock(IRCClientInfo.class);
  40. final List<Server> servers = new ArrayList<Server>();
  41. servers.add(server);
  42. when(plugin.getDomain()).thenReturn("plugin-Identd");
  43. when(sm.getServers()).thenReturn(servers);
  44. when(server.getParser()).thenReturn(parser);
  45. when(parser.getLocalPort()).thenReturn(60);
  46. when(parser.getLocalClient()).thenReturn(client);
  47. when(client.getNickname()).thenReturn("nickname");
  48. when(client.getUsername()).thenReturn("username");
  49. return new IdentClient(null, null, plugin, sm);
  50. }
  51. @Test
  52. public void testInvalidIdent() {
  53. final ConfigManager cm = mock(ConfigManager.class);
  54. final String response = getClient().getIdentResponse("invalid request!", cm);
  55. assertContains("Illegal requests must result in an ERROR response",
  56. response, "ERROR");
  57. }
  58. @Test
  59. public void testQuoting() {
  60. final ConfigManager cm = mock(ConfigManager.class);
  61. final String response = getClient().getIdentResponse("in\\valid:invalid", cm);
  62. assertStartsWith("Special chars in illegal requests must be quoted",
  63. response, "in\\\\valid\\:invalid");
  64. }
  65. @Test
  66. public void testQuoting2() {
  67. final ConfigManager cm = mock(ConfigManager.class);
  68. final String response = getClient().getIdentResponse("in\\\\valid\\ inv\\:alid", cm);
  69. assertStartsWith("Escaped characters in illegal requests shouldn't be doubly-escaped",
  70. response, "in\\\\valid\\ inv\\:alid");
  71. }
  72. @Test
  73. public void testNonNumericPort() {
  74. final ConfigManager cm = mock(ConfigManager.class);
  75. final String response = getClient().getIdentResponse("abc, def", cm);
  76. assertContains("Non-numeric ports must result in an ERROR response",
  77. response, "ERROR");
  78. assertStartsWith("Specified ports must be returned in the response",
  79. response.replaceAll("\\s+", ""), "abc,def:");
  80. }
  81. private void doPortTest(final String ports) {
  82. final ConfigManager cm = mock(ConfigManager.class);
  83. final String response = getClient().getIdentResponse(ports, cm);
  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. final ConfigManager cm = mock(ConfigManager.class);
  101. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(false);
  102. final String response = getClient().getIdentResponse("50, 50", cm);
  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. final ConfigManager cm = mock(ConfigManager.class);
  111. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  112. when(cm.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(true);
  113. final String response = getClient().getIdentResponse("50, 50", cm);
  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. final ConfigManager cm = mock(ConfigManager.class);
  122. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  123. when(cm.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  124. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  125. when(cm.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
  126. when(cm.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
  127. when(cm.getOption("plugin-Identd", "general.customName")).thenReturn("");
  128. final String response = getClient().getIdentResponse("50, 50", cm);
  129. assertContains("Special characters must be quoted in system names",
  130. response, "a\\:b\\\\c\\,d");
  131. }
  132. @Test
  133. public void testCustomNameQuoting() {
  134. final ConfigManager cm = mock(ConfigManager.class);
  135. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  136. when(cm.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  137. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  138. when(cm.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
  139. when(cm.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  140. when(cm.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
  141. final String response = getClient().getIdentResponse("50, 50", cm);
  142. assertContains("Special characters must be quoted in custom names",
  143. response, "a\\:b\\\\c\\,d");
  144. }
  145. @Test
  146. public void testCustomNames() {
  147. final ConfigManager cm = mock(ConfigManager.class);
  148. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  149. when(cm.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  150. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  151. when(cm.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
  152. when(cm.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  153. when(cm.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  154. final String response = getClient().getIdentResponse("50, 60", cm);
  155. final String[] bits = response.split(":");
  156. assertTrue("Responses must include port pair",
  157. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  158. assertEquals("Positive response must include USERID",
  159. "USERID", bits[1].trim());
  160. assertEquals("Must use custom system name", "system", bits[2].trim());
  161. assertEquals("Must use custom name", "name", bits[3].trim());
  162. }
  163. @Test
  164. public void testOSWindows() {
  165. final ConfigManager cm = mock(ConfigManager.class);
  166. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  167. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  168. System.setProperty("user.name", "test");
  169. System.setProperty("os.name", "windows");
  170. final String response = getClient().getIdentResponse("50, 50", cm);
  171. assertEquals("50 , 50 : USERID : WIN32 : test", response);
  172. }
  173. @Test
  174. public void testOSMac() {
  175. final ConfigManager cm = mock(ConfigManager.class);
  176. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  177. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  178. System.setProperty("user.name", "test");
  179. System.setProperty("os.name", "mac");
  180. final String response = getClient().getIdentResponse("50, 50", cm);
  181. assertEquals("50 , 50 : USERID : MACOS : test", response);
  182. }
  183. @Test
  184. public void testOSLinux() {
  185. final ConfigManager cm = mock(ConfigManager.class);
  186. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  187. when(cm.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", cm);
  191. assertEquals("50 , 50 : USERID : UNIX : test", response);
  192. }
  193. @Test
  194. public void testOSBSD() {
  195. final ConfigManager cm = mock(ConfigManager.class);
  196. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  197. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  198. System.setProperty("user.name", "test");
  199. System.setProperty("os.name", "bsd");
  200. final String response = getClient().getIdentResponse("50, 50", cm);
  201. assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
  202. }
  203. @Test
  204. public void testOSOS2() {
  205. final ConfigManager cm = mock(ConfigManager.class);
  206. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  207. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  208. System.setProperty("user.name", "test");
  209. System.setProperty("os.name", "os/2");
  210. final String response = getClient().getIdentResponse("50, 50", cm);
  211. assertEquals("50 , 50 : USERID : OS/2 : test", response);
  212. }
  213. @Test
  214. public void testOSUnix() {
  215. final ConfigManager cm = mock(ConfigManager.class);
  216. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  217. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  218. System.setProperty("user.name", "test");
  219. System.setProperty("os.name", "unix");
  220. final String response = getClient().getIdentResponse("50, 50", cm);
  221. assertEquals("50 , 50 : USERID : UNIX : test", response);
  222. }
  223. @Test
  224. public void testOSIrix() {
  225. final ConfigManager cm = mock(ConfigManager.class);
  226. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  227. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  228. System.setProperty("user.name", "test");
  229. System.setProperty("os.name", "irix");
  230. final String response = getClient().getIdentResponse("50, 50", cm);
  231. assertEquals("50 , 50 : USERID : IRIX : test", response);
  232. }
  233. @Test
  234. public void testOSUnknown() {
  235. final ConfigManager cm = mock(ConfigManager.class);
  236. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  237. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  238. System.setProperty("user.name", "test");
  239. System.setProperty("os.name", "test");
  240. final String response = getClient().getIdentResponse("50, 50", cm);
  241. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  242. }
  243. @Test
  244. public void testNameSystem() {
  245. final ConfigManager cm = mock(ConfigManager.class);
  246. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  247. when(cm.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  248. System.setProperty("user.name", "test");
  249. System.setProperty("os.name", "test");
  250. final String response = getClient().getIdentResponse("50, 50", cm);
  251. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  252. }
  253. @Test
  254. public void testNameCustom() {
  255. final ConfigManager cm = mock(ConfigManager.class);
  256. when(cm.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  257. when(cm.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  258. when(cm.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  259. System.setProperty("user.name", "test");
  260. System.setProperty("os.name", "test");
  261. final String response = getClient().getIdentResponse("50, 50", cm);
  262. assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
  263. }
  264. @Test
  265. public void testNameNickname() {
  266. final ConfigManager cm = mock(ConfigManager.class);
  267. when(cm.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
  268. System.setProperty("user.name", "test");
  269. System.setProperty("os.name", "test");
  270. final String response = getClient().getIdentResponse("60, 50", cm);
  271. assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
  272. }
  273. @Test
  274. public void testNameUsername() {
  275. final ConfigManager cm = mock(ConfigManager.class);
  276. when(cm.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
  277. System.setProperty("user.name", "test");
  278. System.setProperty("os.name", "test");
  279. final String response = getClient().getIdentResponse("60, 50", cm);
  280. assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
  281. }
  282. private static void assertContains(final String msg, final String haystack,
  283. final String needle) {
  284. assertTrue(msg, haystack.indexOf(needle) > -1);
  285. }
  286. private static void assertStartsWith(final String msg, final String haystack,
  287. final String needle) {
  288. assertTrue(msg, haystack.startsWith(needle));
  289. }
  290. }