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

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