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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertNull;
  45. import java.io.File;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.io.OutputStreamWriter;
  49. import org.eclipse.jgit.junit.RepositoryTestCase;
  50. import org.eclipse.jgit.util.FileUtils;
  51. import org.junit.Before;
  52. import org.junit.Test;
  53. public class NetRCTest extends RepositoryTestCase {
  54. private File home;
  55. private NetRC netrc;
  56. private File configFile;
  57. @Before
  58. public void setUp() throws Exception {
  59. super.setUp();
  60. home = new File(trash, "home");
  61. FileUtils.mkdir(home);
  62. configFile = new File(home, ".netrc");
  63. }
  64. private void config(final String data) throws IOException {
  65. final OutputStreamWriter fw = new OutputStreamWriter(
  66. new FileOutputStream(configFile), "UTF-8");
  67. fw.write(data);
  68. fw.close();
  69. }
  70. @Test
  71. public void testNetRCFile() throws Exception {
  72. config("machine my.host login test password 2222"
  73. + "\n"
  74. + "#machine ignore.host.example login ignoreuser password 5555"
  75. + "\n"
  76. + "machine twolinehost.example #login kuznetsov.alexey password 5555"
  77. + "\n"
  78. + "login twologin password 6666"
  79. + "\n"
  80. + "machine afterlinehost.example login test1 password 8888"
  81. + "\n"
  82. + "machine macdef.example login test7 password 7777 macdef mac1"
  83. + "\n"
  84. + "init +apc 1" + "\n"
  85. + "init2 +apc 2" + "\n");
  86. netrc = new NetRC(configFile);
  87. assertNull(netrc.getEntry("ignore.host.example"));
  88. assertNull(netrc.getEntry("cignore.host.example"));
  89. assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
  90. assertEquals("6666", new String(
  91. netrc.getEntry("twolinehost.example").password));
  92. assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
  93. assertEquals("8888", new String(
  94. netrc.getEntry("afterlinehost.example").password));
  95. // macdef test
  96. assertEquals("test7", netrc.getEntry("macdef.example").login);
  97. assertEquals("7777", new String(
  98. netrc.getEntry("macdef.example").password));
  99. assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
  100. netrc.getEntry("macdef.example").macbody);
  101. }
  102. @Test
  103. public void testNetRCDefault() throws Exception {
  104. config("machine my.host login test password 2222"
  105. + "\n"
  106. + "#machine ignore.host.example login ignoreuser password 5555"
  107. + "\n"
  108. + "machine twolinehost.example #login kuznetsov.alexey password 5555"
  109. + "\n"
  110. + "login twologin password 6666"
  111. + "\n"
  112. + "machine afterlinehost.example login test1 password 8888"
  113. + "\n"
  114. + "machine macdef.example login test7 password 7777 macdef mac1"
  115. + "\n"
  116. + "init +apc 1" + "\n"
  117. + "init2 +apc 2" + "\n"
  118. + "\n"
  119. + "default login test5 password 3333" + "\n"
  120. );
  121. netrc = new NetRC(configFile);
  122. // default test
  123. assertEquals("test5", netrc.getEntry("ignore.host.example").login);
  124. assertEquals("3333", new String(
  125. netrc.getEntry("ignore.host.example").password));
  126. // default test
  127. assertEquals("test5", new String(
  128. netrc.getEntry("ignore.host.example").login));
  129. assertEquals("3333", new String(
  130. netrc.getEntry("ignore.host.example").password));
  131. // default test
  132. assertEquals("test5", netrc.getEntry("cignore.host.example").login);
  133. assertEquals("3333", new String(
  134. netrc.getEntry("cignore.host.example").password));
  135. assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
  136. assertEquals("6666", new String(
  137. netrc.getEntry("twolinehost.example").password));
  138. assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
  139. assertEquals("8888", new String(
  140. netrc.getEntry("afterlinehost.example").password));
  141. // macdef test
  142. assertEquals("test7", netrc.getEntry("macdef.example").login);
  143. assertEquals("7777", new String(
  144. netrc.getEntry("macdef.example").password));
  145. assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
  146. netrc.getEntry("macdef.example").macbody);
  147. }
  148. }