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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.interfaces.Connection;
  24. import com.dmdirc.interfaces.ConnectionManager;
  25. import com.dmdirc.interfaces.User;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.parser.irc.IRCClientInfo;
  28. import com.dmdirc.parser.irc.IRCParser;
  29. import com.dmdirc.util.SystemInfo;
  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 SystemInfo systemInfo;
  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(null, null, sm, config, "plugin-Identd", systemInfo);
  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. when(systemInfo.getProperty("user.name")).thenReturn("test");
  134. when(systemInfo.getProperty("os.name")).thenReturn("test");
  135. final String response = getClient().getIdentResponse("50, 50", acp);
  136. assertContains("Special characters must be quoted in system names",
  137. response, "a\\:b\\\\c\\,d");
  138. }
  139. @Test
  140. public void testCustomNameQuoting() {
  141. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  142. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  143. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  144. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
  145. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  146. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
  147. when(systemInfo.getProperty("user.name")).thenReturn("test");
  148. when(systemInfo.getProperty("os.name")).thenReturn("test");
  149. final String response = getClient().getIdentResponse("50, 50", acp);
  150. assertContains("Special characters must be quoted in custom names",
  151. response, "a\\:b\\\\c\\,d");
  152. }
  153. @Test
  154. public void testCustomNames() {
  155. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  156. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  157. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  158. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
  159. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  160. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  161. when(systemInfo.getProperty("user.name")).thenReturn("test");
  162. when(systemInfo.getProperty("os.name")).thenReturn("test");
  163. final String response = getClient().getIdentResponse("50, 60", acp);
  164. final String[] bits = response.split(":");
  165. assertTrue("Responses must include port pair",
  166. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  167. assertEquals("Positive response must include USERID",
  168. "USERID", bits[1].trim());
  169. assertEquals("Must use custom system name", "system", bits[2].trim());
  170. assertEquals("Must use custom name", "name", bits[3].trim());
  171. }
  172. @Test
  173. public void testOSWindows() {
  174. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  175. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  176. when(systemInfo.getProperty("user.name")).thenReturn("test");
  177. when(systemInfo.getProperty("os.name")).thenReturn("windows");
  178. final String response = getClient().getIdentResponse("50, 50", acp);
  179. assertEquals("50 , 50 : USERID : WIN32 : test", response);
  180. }
  181. @Test
  182. public void testOSMac() {
  183. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  184. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  185. when(systemInfo.getProperty("user.name")).thenReturn("test");
  186. when(systemInfo.getProperty("os.name")).thenReturn("mac");
  187. final String response = getClient().getIdentResponse("50, 50", acp);
  188. assertEquals("50 , 50 : USERID : MACOS : test", response);
  189. }
  190. @Test
  191. public void testOSLinux() {
  192. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  193. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  194. when(systemInfo.getProperty("user.name")).thenReturn("test");
  195. when(systemInfo.getProperty("os.name")).thenReturn("linux");
  196. final String response = getClient().getIdentResponse("50, 50", acp);
  197. assertEquals("50 , 50 : USERID : UNIX : test", response);
  198. }
  199. @Test
  200. public void testOSBSD() {
  201. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  202. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  203. when(systemInfo.getProperty("user.name")).thenReturn("test");
  204. when(systemInfo.getProperty("os.name")).thenReturn("bsd");
  205. final String response = getClient().getIdentResponse("50, 50", acp);
  206. assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
  207. }
  208. @Test
  209. public void testOSOS2() {
  210. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  211. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  212. when(systemInfo.getProperty("user.name")).thenReturn("test");
  213. when(systemInfo.getProperty("os.name")).thenReturn("os/2");
  214. final String response = getClient().getIdentResponse("50, 50", acp);
  215. assertEquals("50 , 50 : USERID : OS/2 : test", response);
  216. }
  217. @Test
  218. public void testOSUnix() {
  219. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  220. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  221. when(systemInfo.getProperty("user.name")).thenReturn("test");
  222. when(systemInfo.getProperty("os.name")).thenReturn("unix");
  223. final String response = getClient().getIdentResponse("50, 50", acp);
  224. assertEquals("50 , 50 : USERID : UNIX : test", response);
  225. }
  226. @Test
  227. public void testOSIrix() {
  228. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  229. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  230. when(systemInfo.getProperty("user.name")).thenReturn("test");
  231. when(systemInfo.getProperty("os.name")).thenReturn("irix");
  232. final String response = getClient().getIdentResponse("50, 50", acp);
  233. assertEquals("50 , 50 : USERID : IRIX : test", response);
  234. }
  235. @Test
  236. public void testOSUnknown() {
  237. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  238. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  239. when(systemInfo.getProperty("user.name")).thenReturn("test");
  240. when(systemInfo.getProperty("os.name")).thenReturn("test");
  241. final String response = getClient().getIdentResponse("50, 50", acp);
  242. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  243. }
  244. @Test
  245. public void testNameCustom() {
  246. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  247. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  248. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  249. when(systemInfo.getProperty("user.name")).thenReturn("test");
  250. when(systemInfo.getProperty("os.name")).thenReturn("test");
  251. final String response = getClient().getIdentResponse("50, 50", acp);
  252. assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
  253. }
  254. @Test
  255. public void testNameNickname() {
  256. when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
  257. when(systemInfo.getProperty("user.name")).thenReturn("test");
  258. when(systemInfo.getProperty("os.name")).thenReturn("test");
  259. final String response = getClient().getIdentResponse("60, 50", acp);
  260. assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
  261. }
  262. @Test
  263. public void testNameUsername() {
  264. when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
  265. when(systemInfo.getProperty("user.name")).thenReturn("test");
  266. when(systemInfo.getProperty("os.name")).thenReturn("test");
  267. final String response = getClient().getIdentResponse("60, 50", acp);
  268. assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
  269. }
  270. private static void assertContains(final String msg, final String haystack,
  271. final CharSequence needle) {
  272. assertTrue(msg, haystack.contains(needle));
  273. }
  274. private static void assertStartsWith(final String msg, final String haystack,
  275. final String needle) {
  276. assertTrue(msg, haystack.startsWith(needle));
  277. }
  278. }