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.

StoredUserConfigTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.gitblit;
  2. import org.eclipse.jgit.lib.StoredConfig;
  3. import org.eclipse.jgit.storage.file.FileBasedConfig;
  4. import org.eclipse.jgit.util.FS;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.io.File;
  9. import static org.junit.Assert.*;
  10. public class StoredUserConfigTest
  11. {
  12. private static File file;
  13. @Before
  14. public void setup()
  15. {
  16. file = new File("./suc-test.conf");
  17. file.delete();
  18. }
  19. @After
  20. public void teardown()
  21. {
  22. file.delete();
  23. }
  24. @Test
  25. public void testSection() throws Exception
  26. {
  27. StoredUserConfig config = new StoredUserConfig(file);
  28. config.setString("USER", "norman", "key", "value");
  29. config.setString("USER", "admin", "displayName", "marusha");
  30. config.setString("USER", null, "role", "none");
  31. config.setString("TEAM", "admin", "role", "admin");
  32. config.setString("TEAM", "ci", "email", "ci@example.com");
  33. config.setString("TEAM", null, "displayName", "noone");
  34. config.save();
  35. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  36. cfg.load();
  37. assertEquals("value", cfg.getString("USER", "norman", "key"));
  38. assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
  39. assertEquals("none", cfg.getString("USER", null, "role"));
  40. assertEquals("admin", cfg.getString("TEAM", "admin", "role"));
  41. assertEquals("ci@example.com", cfg.getString("TEAM", "ci", "email"));
  42. assertEquals("noone", cfg.getString("TEAM", null, "displayName"));
  43. }
  44. @Test
  45. public void testStringFields() throws Exception
  46. {
  47. StoredUserConfig config = new StoredUserConfig(file);
  48. config.setString("USER", "admin", "password", "secret");
  49. config.setString("USER", "admin", "displayName", "marusha");
  50. config.setString("USER", "admin", "email", "name@example.com");
  51. config.setString("USER", "other", "password", "password");
  52. config.setString("USER", "other", "displayName", "mama");
  53. config.setString("USER", "other", "email", "other@example.com");
  54. config.setString("USER", "other", "repository", "RW+:repo1");
  55. config.setString("USER", "other", "repository", "RW+:repo2");
  56. config.setString("USER", null, "displayName", "default");
  57. config.save();
  58. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  59. cfg.load();
  60. assertEquals("secret", cfg.getString("USER", "admin", "password"));
  61. assertEquals("marusha", cfg.getString("USER", "admin", "displayName"));
  62. assertEquals("name@example.com", cfg.getString("USER", "admin", "email"));
  63. assertEquals("password", cfg.getString("USER", "other", "password"));
  64. assertEquals("mama", cfg.getString("USER", "other", "displayName"));
  65. assertEquals("other@example.com", cfg.getString("USER", "other", "email"));
  66. String[] stringList = cfg.getStringList("USER", "other", "repository");
  67. assertNotNull(stringList);
  68. assertEquals(2, stringList.length);
  69. int i = 0;
  70. for (String s : stringList) {
  71. if (s.equalsIgnoreCase("RW+:repo1")) i += 1;
  72. else if (s.equalsIgnoreCase("RW+:repo2")) i += 2;
  73. }
  74. assertEquals("Not all repository strings found", 3, i);
  75. assertEquals("default", cfg.getString("USER", null, "displayName"));
  76. }
  77. @Test
  78. public void testBooleanFields() throws Exception
  79. {
  80. StoredUserConfig config = new StoredUserConfig(file);
  81. config.setBoolean("USER", "admin", "emailMeOnMyTicketChanges", true);
  82. config.setBoolean("USER", "master", "emailMeOnMyTicketChanges", false);
  83. config.setBoolean("TEAM", "admin", "excludeFromFederation", true);
  84. config.setBoolean("USER", null, "excludeFromFederation", false);
  85. config.save();
  86. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  87. cfg.load();
  88. assertTrue(cfg.getBoolean("USER", "admin", "emailMeOnMyTicketChanges", false));
  89. assertFalse(cfg.getBoolean("USER", "master", "emailMeOnMyTicketChanges", true));
  90. assertTrue(cfg.getBoolean("TEAM", "admin", "excludeFromFederation", false));
  91. assertFalse(cfg.getBoolean("USER", null, "excludeFromFederation", true));
  92. }
  93. @Test
  94. public void testHashEscape() throws Exception
  95. {
  96. StoredUserConfig config = new StoredUserConfig(file);
  97. config.setString("USER", "admin", "role", "#admin");
  98. config.setString("USER", "other", "role", "#none");
  99. config.setString("USER", "other", "displayName", "big#");
  100. config.setString("USER", "other", "email", "user#name@home.de");
  101. config.save();
  102. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  103. cfg.load();
  104. assertEquals("#admin", cfg.getString("USER", "admin", "role"));
  105. assertEquals("#none", cfg.getString("USER", "other", "role"));
  106. assertEquals("big#", cfg.getString("USER", "other", "displayName"));
  107. assertEquals("user#name@home.de", cfg.getString("USER", "other", "email"));
  108. }
  109. @Test
  110. public void testCtrlEscape() throws Exception
  111. {
  112. StoredUserConfig config = new StoredUserConfig(file);
  113. config.setString("USER", "name", "password", "bing\bbong");
  114. config.setString("USER", "name", "role", "domain\\admin");
  115. config.setString("USER", "name", "displayName", "horny\n\telephant");
  116. config.setString("USER", "name", "org", "\tbig\tblue");
  117. config.setString("USER", "name", "unit", "the end\n");
  118. config.setString("USER", null, "unit", "the\ndefault");
  119. config.save();
  120. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  121. cfg.load();
  122. assertEquals("bing\bbong", cfg.getString("USER", "name", "password"));
  123. assertEquals("domain\\admin", cfg.getString("USER", "name", "role"));
  124. assertEquals("horny\n\telephant", cfg.getString("USER", "name", "displayName"));
  125. assertEquals("\tbig\tblue", cfg.getString("USER", "name", "org"));
  126. assertEquals("the end\n", cfg.getString("USER", "name", "unit"));
  127. assertEquals("the\ndefault", cfg.getString("USER", null, "unit"));
  128. }
  129. @Test
  130. public void testQuoteEscape() throws Exception
  131. {
  132. StoredUserConfig config = new StoredUserConfig(file);
  133. config.setString("USER", "dude", "password", "going\"places");
  134. config.setString("USER", "dude", "role", "\"dude\"");
  135. config.setString("USER", "dude", "displayName", "John \"The Dude\" Lebowski");
  136. config.setString("USER", "dude", "repo", "\"front matter");
  137. config.setString("USER", "dude", "peepo", "leadout\"");
  138. config.save();
  139. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  140. cfg.load();
  141. assertEquals("going\"places", cfg.getString("USER", "dude", "password"));
  142. assertEquals("\"dude\"", cfg.getString("USER", "dude", "role"));
  143. assertEquals("John \"The Dude\" Lebowski", cfg.getString("USER", "dude", "displayName"));
  144. assertEquals("\"front matter", cfg.getString("USER", "dude", "repo"));
  145. assertEquals("leadout\"", cfg.getString("USER", "dude", "peepo"));
  146. }
  147. @Test
  148. public void testUTF8() throws Exception
  149. {
  150. StoredUserConfig config = new StoredUserConfig(file);
  151. config.setString("USER", "ming", "password", "一\t二\n三");
  152. config.setString("USER", "ming", "displayName", "白老鼠");
  153. config.setString("USER", "ming", "peepo", "Mickey \"白老鼠\" Whitfield");
  154. config.save();
  155. StoredConfig cfg = new FileBasedConfig(file, FS.detect());
  156. cfg.load();
  157. assertEquals("一\t二\n三", cfg.getString("USER", "ming", "password"));
  158. assertEquals("白老鼠", cfg.getString("USER", "ming", "displayName"));
  159. assertEquals("Mickey \"白老鼠\" Whitfield", cfg.getString("USER", "ming", "peepo"));
  160. }
  161. }