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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.identd;
  18. import com.dmdirc.interfaces.Connection;
  19. import com.dmdirc.interfaces.ConnectionManager;
  20. import com.dmdirc.interfaces.User;
  21. import com.dmdirc.config.provider.AggregateConfigProvider;
  22. import com.dmdirc.parser.irc.IRCClientInfo;
  23. import com.dmdirc.parser.irc.IRCParser;
  24. import com.dmdirc.util.system.SystemInfo;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Optional;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.junit.Assert.assertEquals;
  33. import static org.junit.Assert.assertTrue;
  34. import static org.mockito.Mockito.when;
  35. @RunWith(MockitoJUnitRunner.class)
  36. public class IdentClientTest {
  37. @Mock private AggregateConfigProvider acp;
  38. @Mock private ConnectionManager sm;
  39. @Mock private Connection connection;
  40. @Mock private IRCParser parser;
  41. @Mock private IRCClientInfo client;
  42. @Mock private User user;
  43. @Mock private AggregateConfigProvider config;
  44. @Mock private SystemInfo systemInfo;
  45. protected IdentClient getClient() {
  46. final List<Connection> servers = new ArrayList<>();
  47. servers.add(connection);
  48. when(sm.getConnections()).thenReturn(servers);
  49. when(connection.getParser()).thenReturn(Optional.of(parser));
  50. when(connection.getLocalUser()).thenReturn(Optional.of(user));
  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. when(user.getNickname()).thenReturn("nickname");
  56. when(user.getUsername()).thenReturn(Optional.of("username"));
  57. return new IdentClient(null, null, sm, config, "plugin-Identd", systemInfo);
  58. }
  59. @Test
  60. public void testInvalidIdent() {
  61. final String response = getClient().getIdentResponse("invalid request!", acp);
  62. assertContains("Illegal requests must result in an ERROR response",
  63. response, "ERROR");
  64. }
  65. @Test
  66. public void testQuoting() {
  67. final String response = getClient().getIdentResponse("in\\valid:invalid", acp);
  68. assertStartsWith("Special chars in illegal requests must be quoted",
  69. response, "in\\\\valid\\:invalid");
  70. }
  71. @Test
  72. public void testQuoting2() {
  73. final String response = getClient().getIdentResponse("in\\\\valid\\ inv\\:alid", acp);
  74. assertStartsWith("Escaped characters in illegal requests shouldn't be doubly-escaped",
  75. response, "in\\\\valid\\ inv\\:alid");
  76. }
  77. @Test
  78. public void testNonNumericPort() {
  79. final String response = getClient().getIdentResponse("abc, def", acp);
  80. assertContains("Non-numeric ports must result in an ERROR response",
  81. response, "ERROR");
  82. assertStartsWith("Specified ports must be returned in the response",
  83. response.replaceAll("\\s+", ""), "abc,def:");
  84. }
  85. private void doPortTest(final String ports) {
  86. final String response = getClient().getIdentResponse(ports, acp);
  87. assertContains("Illegal ports must result in an ERROR response",
  88. response, "ERROR");
  89. assertContains("Illegal ports must result in an INVALID-PORT response",
  90. response, "INVALID-PORT");
  91. assertStartsWith("Port numbers must be returned as part of the response",
  92. response.replaceAll("\\s+", ""), ports.replaceAll("\\s+", ""));
  93. }
  94. @Test
  95. public void testOutOfRangePorts() {
  96. doPortTest("0, 50");
  97. doPortTest("65536, 50");
  98. doPortTest("50, 0");
  99. doPortTest("50, 65536");
  100. }
  101. @Test
  102. public void testAlwaysOn() {
  103. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(false);
  104. final String response = getClient().getIdentResponse("50, 50", acp);
  105. assertContains("Unknown port requests must return an ERROR response",
  106. response, "ERROR");
  107. assertContains("Unknown port requests must return a NO-USER response",
  108. response, "NO-USER");
  109. }
  110. @Test
  111. public void testHidden() {
  112. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  113. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(true);
  114. final String response = getClient().getIdentResponse("50, 50", acp);
  115. assertContains("Hidden requests must return an ERROR response",
  116. response, "ERROR");
  117. assertContains("Hidden requests must return a HIDDEN-USER response",
  118. response, "HIDDEN-USER");
  119. }
  120. @Test
  121. public void testSystemNameQuoting() {
  122. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  123. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  124. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  125. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
  126. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
  127. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("");
  128. when(systemInfo.getProperty("user.name")).thenReturn("test");
  129. when(systemInfo.getProperty("os.name")).thenReturn("test");
  130. final String response = getClient().getIdentResponse("50, 50", acp);
  131. assertContains("Special characters must be quoted in system names",
  132. response, "a\\:b\\\\c\\,d");
  133. }
  134. @Test
  135. public void testCustomNameQuoting() {
  136. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  137. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  138. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  139. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
  140. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  141. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
  142. when(systemInfo.getProperty("user.name")).thenReturn("test");
  143. when(systemInfo.getProperty("os.name")).thenReturn("test");
  144. final String response = getClient().getIdentResponse("50, 50", acp);
  145. assertContains("Special characters must be quoted in custom names",
  146. response, "a\\:b\\\\c\\,d");
  147. }
  148. @Test
  149. public void testCustomNames() {
  150. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  151. when(acp.getOptionBool("plugin-Identd", "advanced.isHiddenUser")).thenReturn(false);
  152. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(true);
  153. when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
  154. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  155. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  156. when(systemInfo.getProperty("user.name")).thenReturn("test");
  157. when(systemInfo.getProperty("os.name")).thenReturn("test");
  158. final String response = getClient().getIdentResponse("50, 60", acp);
  159. final String[] bits = response.split(":");
  160. assertTrue("Responses must include port pair",
  161. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  162. assertEquals("Positive response must include USERID",
  163. "USERID", bits[1].trim());
  164. assertEquals("Must use custom system name", "system", bits[2].trim());
  165. assertEquals("Must use custom name", "name", bits[3].trim());
  166. }
  167. @Test
  168. public void testOSWindows() {
  169. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  170. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  171. when(systemInfo.getProperty("user.name")).thenReturn("test");
  172. when(systemInfo.getProperty("os.name")).thenReturn("windows");
  173. final String response = getClient().getIdentResponse("50, 50", acp);
  174. assertEquals("50 , 50 : USERID : WIN32 : test", response);
  175. }
  176. @Test
  177. public void testOSMac() {
  178. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  179. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  180. when(systemInfo.getProperty("user.name")).thenReturn("test");
  181. when(systemInfo.getProperty("os.name")).thenReturn("mac");
  182. final String response = getClient().getIdentResponse("50, 50", acp);
  183. assertEquals("50 , 50 : USERID : MACOS : test", response);
  184. }
  185. @Test
  186. public void testOSLinux() {
  187. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  188. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  189. when(systemInfo.getProperty("user.name")).thenReturn("test");
  190. when(systemInfo.getProperty("os.name")).thenReturn("linux");
  191. final String response = getClient().getIdentResponse("50, 50", acp);
  192. assertEquals("50 , 50 : USERID : UNIX : test", response);
  193. }
  194. @Test
  195. public void testOSBSD() {
  196. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  197. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  198. when(systemInfo.getProperty("user.name")).thenReturn("test");
  199. when(systemInfo.getProperty("os.name")).thenReturn("bsd");
  200. final String response = getClient().getIdentResponse("50, 50", acp);
  201. assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
  202. }
  203. @Test
  204. public void testOSOS2() {
  205. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  206. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  207. when(systemInfo.getProperty("user.name")).thenReturn("test");
  208. when(systemInfo.getProperty("os.name")).thenReturn("os/2");
  209. final String response = getClient().getIdentResponse("50, 50", acp);
  210. assertEquals("50 , 50 : USERID : OS/2 : test", response);
  211. }
  212. @Test
  213. public void testOSUnix() {
  214. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  215. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  216. when(systemInfo.getProperty("user.name")).thenReturn("test");
  217. when(systemInfo.getProperty("os.name")).thenReturn("unix");
  218. final String response = getClient().getIdentResponse("50, 50", acp);
  219. assertEquals("50 , 50 : USERID : UNIX : test", response);
  220. }
  221. @Test
  222. public void testOSIrix() {
  223. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  224. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  225. when(systemInfo.getProperty("user.name")).thenReturn("test");
  226. when(systemInfo.getProperty("os.name")).thenReturn("irix");
  227. final String response = getClient().getIdentResponse("50, 50", acp);
  228. assertEquals("50 , 50 : USERID : IRIX : test", response);
  229. }
  230. @Test
  231. public void testOSUnknown() {
  232. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  233. when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
  234. when(systemInfo.getProperty("user.name")).thenReturn("test");
  235. when(systemInfo.getProperty("os.name")).thenReturn("test");
  236. final String response = getClient().getIdentResponse("50, 50", acp);
  237. assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
  238. }
  239. @Test
  240. public void testNameCustom() {
  241. when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
  242. when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
  243. when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
  244. when(systemInfo.getProperty("user.name")).thenReturn("test");
  245. when(systemInfo.getProperty("os.name")).thenReturn("test");
  246. final String response = getClient().getIdentResponse("50, 50", acp);
  247. assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
  248. }
  249. @Test
  250. public void testNameNickname() {
  251. when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
  252. when(systemInfo.getProperty("user.name")).thenReturn("test");
  253. when(systemInfo.getProperty("os.name")).thenReturn("test");
  254. final String response = getClient().getIdentResponse("60, 50", acp);
  255. assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
  256. }
  257. @Test
  258. public void testNameUsername() {
  259. when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
  260. when(systemInfo.getProperty("user.name")).thenReturn("test");
  261. when(systemInfo.getProperty("os.name")).thenReturn("test");
  262. final String response = getClient().getIdentResponse("60, 50", acp);
  263. assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
  264. }
  265. private static void assertContains(final String msg, final String haystack,
  266. final CharSequence needle) {
  267. assertTrue(msg, haystack.contains(needle));
  268. }
  269. private static void assertStartsWith(final String msg, final String haystack,
  270. final String needle) {
  271. assertTrue(msg, haystack.startsWith(needle));
  272. }
  273. }