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

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