View Javadoc
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  
43  package org.eclipse.jgit.transport;
44  
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  
49  import java.io.File;
50  import java.io.FileOutputStream;
51  import java.io.IOException;
52  import java.io.OutputStreamWriter;
53  
54  import org.eclipse.jgit.junit.RepositoryTestCase;
55  import org.eclipse.jgit.util.FileUtils;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class NetRCTest extends RepositoryTestCase {
60  	private File home;
61  
62  	private NetRC netrc;
63  
64  	private File configFile;
65  
66  	@Override
67  	@Before
68  	public void setUp() throws Exception {
69  		super.setUp();
70  
71  		home = new File(trash, "home");
72  		FileUtils.mkdir(home);
73  
74  		configFile = new File(home, ".netrc");
75  	}
76  
77  	private void config(String data) throws IOException {
78  		try (OutputStreamWriter fw = new OutputStreamWriter(
79  				new FileOutputStream(configFile), UTF_8)) {
80  			fw.write(data);
81  		}
82  	}
83  
84  	@Test
85  	public void testNetRCFile() throws Exception {
86  		config("machine my.host login test password 2222"
87  				+ "\n"
88  
89  				+ "#machine ignore.host.example login ignoreuser password 5555"
90  				+ "\n"
91  
92  				+ "machine twolinehost.example #login kuznetsov.alexey password 5555"
93  				+ "\n"
94  
95  				+ "login twologin password 6666"
96  				+ "\n"
97  
98  				+ "machine afterlinehost.example login test1 password 8888"
99  				+ "\n"
100 
101 				+ "machine macdef.example login test7 password 7777 macdef mac1"
102 				+ "\n"
103 
104 				+ "init +apc 1" + "\n"
105 
106 				+ "init2 +apc 2" + "\n");
107 
108 		netrc = new NetRC(configFile);
109 
110 		assertNull(netrc.getEntry("ignore.host.example"));
111 
112 		assertNull(netrc.getEntry("cignore.host.example"));
113 
114 		assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
115 		assertEquals("6666", new String(
116 				netrc.getEntry("twolinehost.example").password));
117 
118 		assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
119 		assertEquals("8888", new String(
120 				netrc.getEntry("afterlinehost.example").password));
121 
122 		// macdef test
123 		assertEquals("test7", netrc.getEntry("macdef.example").login);
124 		assertEquals("7777", new String(
125 				netrc.getEntry("macdef.example").password));
126 		assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
127 				netrc.getEntry("macdef.example").macbody);
128 	}
129 
130 	@Test
131 	public void testNetRCDefault() throws Exception {
132 		config("machine my.host login test password 2222"
133 				+ "\n"
134 
135 				+ "#machine ignore.host.example login ignoreuser password 5555"
136 				+ "\n"
137 
138 				+ "machine twolinehost.example #login kuznetsov.alexey password 5555"
139 				+ "\n"
140 
141 				+ "login twologin password 6666"
142 				+ "\n"
143 
144 				+ "machine afterlinehost.example login test1 password 8888"
145 				+ "\n"
146 
147 				+ "machine macdef.example login test7 password 7777 macdef mac1"
148 				+ "\n"
149 
150 				+ "init +apc 1" + "\n"
151 
152 				+ "init2 +apc 2" + "\n"
153 
154 				+ "\n"
155 
156 				+ "default login test5 password 3333" + "\n"
157 
158 		);
159 
160 		netrc = new NetRC(configFile);
161 
162 		// default test
163 		assertEquals("test5", netrc.getEntry("ignore.host.example").login);
164 		assertEquals("3333", new String(
165 				netrc.getEntry("ignore.host.example").password));
166 
167 		// default test
168 		assertEquals("test5", new String(
169 				netrc.getEntry("ignore.host.example").login));
170 		assertEquals("3333", new String(
171 				netrc.getEntry("ignore.host.example").password));
172 
173 		// default test
174 		assertEquals("test5", netrc.getEntry("cignore.host.example").login);
175 		assertEquals("3333", new String(
176 				netrc.getEntry("cignore.host.example").password));
177 
178 		assertEquals("twologin", netrc.getEntry("twolinehost.example").login);
179 		assertEquals("6666", new String(
180 				netrc.getEntry("twolinehost.example").password));
181 
182 		assertEquals("test1", netrc.getEntry("afterlinehost.example").login);
183 		assertEquals("8888", new String(
184 				netrc.getEntry("afterlinehost.example").password));
185 
186 		// macdef test
187 		assertEquals("test7", netrc.getEntry("macdef.example").login);
188 		assertEquals("7777", new String(
189 				netrc.getEntry("macdef.example").password));
190 		assertEquals("init +apc 1" + "\n" + "init2 +apc 2" + "\n",
191 				netrc.getEntry("macdef.example").macbody);
192 	}
193 }