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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.harness.TestConfigManagerMap;
  24. import com.dmdirc.config.IdentityManager;
  25. import org.junit.BeforeClass;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. import static org.mockito.Mockito.*;
  29. public class IdentClientTest {
  30. @BeforeClass
  31. public static void setUpClass() throws Exception {
  32. IdentityManager.load();
  33. }
  34. protected IdentClient getClient() {
  35. IdentdPlugin plugin = mock(IdentdPlugin.class);
  36. when(plugin.getDomain()).thenReturn("plugin-Identd");
  37. return new IdentClient(null, null, plugin);
  38. }
  39. @Test
  40. public void testInvalidIdent() {
  41. final String response = getClient().getIdentResponse("invalid request!",
  42. IdentityManager.getGlobalConfig());
  43. assertContains("Illegal requests must result in an ERROR response",
  44. response, "ERROR");
  45. }
  46. @Test
  47. public void testQuoting() {
  48. final String response = getClient().getIdentResponse("in\\valid:invalid",
  49. IdentityManager.getGlobalConfig());
  50. assertStartsWith("Special chars in illegal requests must be quoted",
  51. response, "in\\\\valid\\:invalid");
  52. }
  53. @Test
  54. public void testQuoting2() {
  55. final String response = getClient().getIdentResponse("in\\\\valid\\ inv\\:alid",
  56. IdentityManager.getGlobalConfig());
  57. assertStartsWith("Escaped characters in illegal requests shouldn't be doubly-escaped",
  58. response, "in\\\\valid\\ inv\\:alid");
  59. }
  60. @Test
  61. public void testNonNumericPort() {
  62. final String response = getClient().getIdentResponse("abc, def",
  63. IdentityManager.getGlobalConfig());
  64. assertContains("Non-numeric ports must result in an ERROR response",
  65. response, "ERROR");
  66. assertStartsWith("Specified ports must be returned in the response",
  67. response.replaceAll("\\s+", ""), "abc,def:");
  68. }
  69. private void doPortTest(final String ports) {
  70. final String response = getClient().getIdentResponse(ports,
  71. IdentityManager.getGlobalConfig());
  72. assertContains("Illegal ports must result in an ERROR response",
  73. response, "ERROR");
  74. assertContains("Illegal ports must result in an INVALID-PORT response",
  75. response, "INVALID-PORT");
  76. assertStartsWith("Port numbers must be returned as part of the response",
  77. response.replaceAll("\\s+", ""), ports.replaceAll("\\s+", ""));
  78. }
  79. @Test
  80. public void testOutOfRangePorts() {
  81. doPortTest("0, 50");
  82. doPortTest("65536, 50");
  83. doPortTest("50, 0");
  84. doPortTest("50, 65536");
  85. }
  86. @Test
  87. public void testAlwaysOn() {
  88. final TestConfigManagerMap tcm = new TestConfigManagerMap();
  89. tcm.settings.put("plugin-Identd.advanced.alwaysOn", "false");
  90. final String response = getClient().getIdentResponse("50, 50", tcm);
  91. assertContains("Unknown port requests must return an ERROR response",
  92. response, "ERROR");
  93. assertContains("Unknown port requests must return a NO-USER response",
  94. response, "NO-USER");
  95. }
  96. @Test
  97. public void testHidden() {
  98. final TestConfigManagerMap tcm = new TestConfigManagerMap();
  99. tcm.settings.put("plugin-Identd.advanced.alwaysOn", "true");
  100. tcm.settings.put("plugin-Identd.advanced.isHiddenUser", "true");
  101. final String response = getClient().getIdentResponse("50, 50", tcm);
  102. assertContains("Hidden requests must return an ERROR response",
  103. response, "ERROR");
  104. assertContains("Hidden requests must return a HIDDEN-USER response",
  105. response, "HIDDEN-USER");
  106. }
  107. @Test
  108. public void testSystemNameQuoting() {
  109. final TestConfigManagerMap tcm = new TestConfigManagerMap();
  110. tcm.settings.put("plugin-Identd.advanced.alwaysOn", "true");
  111. tcm.settings.put("plugin-Identd.advanced.isHiddenUser", "false");
  112. tcm.settings.put("plugin-Identd.advanced.useCustomSystem", "true");
  113. tcm.settings.put("plugin-Identd.advanced.customSystem", "a:b\\c,d");
  114. tcm.settings.put("plugin-Identd.general.useCustomName", "false");
  115. tcm.settings.put("plugin-Identd.general.customName", "");
  116. final String response = getClient().getIdentResponse("50, 50", tcm);
  117. assertContains("Special characters must be quoted in system names",
  118. response, "a\\:b\\\\c\\,d");
  119. }
  120. @Test
  121. public void testCustomNameQuoting() {
  122. final TestConfigManagerMap tcm = new TestConfigManagerMap();
  123. tcm.settings.put("plugin-Identd.advanced.alwaysOn", "true");
  124. tcm.settings.put("plugin-Identd.advanced.isHiddenUser", "false");
  125. tcm.settings.put("plugin-Identd.advanced.useCustomSystem", "false");
  126. tcm.settings.put("plugin-Identd.advanced.customSystem", "");
  127. tcm.settings.put("plugin-Identd.general.useCustomName", "true");
  128. tcm.settings.put("plugin-Identd.general.customName", "a:b\\c,d");
  129. final String response = getClient().getIdentResponse("50, 50", tcm);
  130. assertContains("Special characters must be quoted in custom names",
  131. response, "a\\:b\\\\c\\,d");
  132. }
  133. @Test
  134. public void testCustomNames() {
  135. final TestConfigManagerMap tcm = new TestConfigManagerMap();
  136. tcm.settings.put("plugin-Identd.advanced.alwaysOn", "true");
  137. tcm.settings.put("plugin-Identd.advanced.isHiddenUser", "false");
  138. tcm.settings.put("plugin-Identd.advanced.useCustomSystem", "true");
  139. tcm.settings.put("plugin-Identd.advanced.customSystem", "system");
  140. tcm.settings.put("plugin-Identd.general.useCustomName", "true");
  141. tcm.settings.put("plugin-Identd.general.customName", "name");
  142. final String response = getClient().getIdentResponse("50, 60", tcm);
  143. final String[] bits = response.split(":");
  144. assertTrue("Responses must include port pair",
  145. bits[0].matches("\\s*50\\s*,\\s*60\\s*"));
  146. assertEquals("Positive response must include USERID",
  147. "USERID", bits[1].trim());
  148. assertEquals("Must use custom system name", "system", bits[2].trim());
  149. assertEquals("Must use custom name", "name", bits[3].trim());
  150. }
  151. private static void assertContains(final String msg, final String haystack,
  152. final String needle) {
  153. assertTrue(msg, haystack.indexOf(needle) > -1);
  154. }
  155. private static void assertStartsWith(final String msg, final String haystack,
  156. final String needle) {
  157. assertTrue(msg, haystack.startsWith(needle));
  158. }
  159. }