1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.http.test;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.fail;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import org.eclipse.jgit.errors.RepositoryNotFoundException;
23 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
24 import org.eclipse.jgit.lib.Repository;
25 import org.eclipse.jgit.transport.resolver.FileResolver;
26 import org.eclipse.jgit.transport.resolver.RepositoryResolver;
27 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
28 import org.eclipse.jgit.util.FileUtils;
29 import org.junit.Test;
30
31 public class FileResolverTest extends LocalDiskRepositoryTestCase {
32 @Test
33 public void testUnreasonableNames() throws ServiceNotEnabledException {
34 assertUnreasonable("");
35 assertUnreasonable("a\\b");
36 assertUnreasonable("../b");
37 assertUnreasonable("a/../b");
38 assertUnreasonable("a/./b");
39 assertUnreasonable("a//b");
40
41 if (new File("/foo").isAbsolute())
42 assertUnreasonable("/foo");
43
44 if (new File("//server/share").isAbsolute())
45 assertUnreasonable("//server/share");
46
47 if (new File("C:/windows").isAbsolute())
48 assertUnreasonable("C:/windows");
49 }
50
51 private static void assertUnreasonable(String name)
52 throws ServiceNotEnabledException {
53 FileResolver<RepositoryResolver> r = new FileResolver<>(
54 new File("."), false);
55 try {
56 r.open(null, name);
57 fail("Opened unreasonable name \"" + name + "\"");
58 } catch (RepositoryNotFoundException e) {
59 assertEquals("repository not found: " + name, e.getMessage());
60 assertNull("has no cause", e.getCause());
61 }
62 }
63
64 @Test
65 public void testExportOk() throws IOException {
66 final Repository a = createBareRepository();
67 final String name = a.getDirectory().getName();
68 final File base = a.getDirectory().getParentFile();
69 final File export = new File(a.getDirectory(), "git-daemon-export-ok");
70 FileResolver<RepositoryResolver> resolver;
71
72 assertFalse("no git-daemon-export-ok", export.exists());
73 resolver = new FileResolver<>(base, false
74
75
76 );
77 try {
78 resolver.open(null, name);
79 fail("opened non-exported repository");
80 } catch (ServiceNotEnabledException e) {
81 assertEquals("Service not enabled", e.getMessage());
82 }
83
84 resolver = new FileResolver<>(base, true
85
86
87 );
88 try {
89 resolver.open(null, name).close();
90 } catch (ServiceNotEnabledException e) {
91 fail("did not honor export-all flag");
92 }
93
94 FileUtils.createNewFile(export);
95 resolver = new FileResolver<>(base, false
96
97
98 );
99 try {
100 resolver.open(null, name).close();
101 } catch (ServiceNotEnabledException e) {
102 fail("did not honor git-daemon-export-ok");
103 }
104 }
105
106 @Test
107 public void testNotAGitRepository() throws IOException,
108 ServiceNotEnabledException {
109 final Repository a = createBareRepository();
110 final String name = a.getDirectory().getName() + "-not-a-git";
111 final File base = a.getDirectory().getParentFile();
112 FileResolver<RepositoryResolver> resolver = new FileResolver<>(
113 base, false);
114
115 try {
116 resolver.open(null, name);
117 fail("opened non-git repository");
118 } catch (RepositoryNotFoundException e) {
119 assertEquals("repository not found: " + name, e.getMessage());
120
121 Throwable why = e.getCause();
122 assertNotNull("has cause", why);
123 assertEquals("repository not found: "
124 + new File(base, name).getPath(), why.getMessage());
125 }
126 }
127 }