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.

NetRCTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2014, Alexey Kuznetsov <axet@me.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.transport;
  43. import static java.nio.charset.StandardCharsets.UTF_8;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import java.io.File;
  47. import java.io.FileOutputStream;
  48. import java.io.IOException;
  49. import java.io.OutputStreamWriter;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.util.FileUtils;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. public class NetRCTest extends RepositoryTestCase {
  55. private File home;
  56. private NetRC netrc;
  57. private File configFile;
  58. @Override
  59. @Before
  60. public void setUp() throws Exception {
  61. super.setUp();
  62. home = new File(trash, "home");
  63. FileUtils.mkdir(home);
  64. configFile = new File(home, ".netrc");
  65. }
  66. private void config(String data) throws IOException {
  67. try (OutputStreamWriter fw = new OutputStreamWriter(
  68. new FileOutputStream(configFile), UTF_8)) {
  69. fw.write(data);
  70. }
  71. }
  72. @Test
  73. public void testNetRCFile() throws Exception {
  74. config("machine my.host login test password 2222"
  75. + "\n"
  76. + "#machine ignore.host.example login ignoreuser password 5555"
  77. + "\n"
  78. + "machine twolinehost.example #login kuznetsov.alexey password 5555"
  79. + "\n"
  80. + "login twologin password 6666"
  81. + "\n"
  82. + "machine afterlinehost.example login test1 password 8888"
  83. + "\n"
  84. + "machine macdef.example login test7 password 7777 macdef mac1"
  85. + "\n"
  86. + "init +apc 1" + "\n"
  87. + "init2 +apc 2" + "\n");
  88. netrc = new NetRC(configFile);
  89. assertNull(netrc.getEntry("ignore.host.example"));
  90. assertNull(netrc.getEntry("cignore.host.example"));
  91. assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
  92. assertEquals("6666", new String(
  93. netrc.getEntry("twolinehost.example").password));
  94. assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
  95. assertEquals("8888", new String(
  96. netrc.getEntry("afterlinehost.example").password));
  97. // macdef test
  98. assertEquals("test7", netrc.getEntry("macdef.example").login);
  99. assertEquals("7777", new String(
  100. netrc.getEntry("macdef.example").password));
  101. assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
  102. netrc.getEntry("macdef.example").macbody);
  103. }
  104. @Test
  105. public void testNetRCDefault() throws Exception {
  106. config("machine my.host login test password 2222"
  107. + "\n"
  108. + "#machine ignore.host.example login ignoreuser password 5555"
  109. + "\n"
  110. + "machine twolinehost.example #login kuznetsov.alexey password 5555"
  111. + "\n"
  112. + "login twologin password 6666"
  113. + "\n"
  114. + "machine afterlinehost.example login test1 password 8888"
  115. + "\n"
  116. + "machine macdef.example login test7 password 7777 macdef mac1"
  117. + "\n"
  118. + "init +apc 1" + "\n"
  119. + "init2 +apc 2" + "\n"
  120. + "\n"
  121. + "default login test5 password 3333" + "\n"
  122. );
  123. netrc = new NetRC(configFile);
  124. // default test
  125. assertEquals("test5", netrc.getEntry("ignore.host.example").login);
  126. assertEquals("3333", new String(
  127. netrc.getEntry("ignore.host.example").password));
  128. // default test
  129. assertEquals("test5", new String(
  130. netrc.getEntry("ignore.host.example").login));
  131. assertEquals("3333", new String(
  132. netrc.getEntry("ignore.host.example").password));
  133. // default test
  134. assertEquals("test5", netrc.getEntry("cignore.host.example").login);
  135. assertEquals("3333", new String(
  136. netrc.getEntry("cignore.host.example").password));
  137. assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
  138. assertEquals("6666", new String(
  139. netrc.getEntry("twolinehost.example").password));
  140. assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
  141. assertEquals("8888", new String(
  142. netrc.getEntry("afterlinehost.example").password));
  143. // macdef test
  144. assertEquals("test7", netrc.getEntry("macdef.example").login);
  145. assertEquals("7777", new String(
  146. netrc.getEntry("macdef.example").password));
  147. assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
  148. netrc.getEntry("macdef.example").macbody);
  149. }
  150. }