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 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
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
163 assertEquals("test5", netrc.getEntry("ignore.host.example").login);
164 assertEquals("3333", new String(
165 netrc.getEntry("ignore.host.example").password));
166
167
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
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
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 }