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