1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
161 assertEquals("test5", netrc.getEntry("ignore.host.example").login);
162 assertEquals("3333", new String(
163 netrc.getEntry("ignore.host.example").password));
164
165
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
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
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 }