Skip to content

Commit b6848c0

Browse files
committed
Mock git configuration in git-related tests
We do not want to pick up any real config of the user running the tests! Such configs may contain settings that break our tests. Also mock the home directory for JGit, and hide some environment variables that could potentially affect JGit adversely if set.
1 parent d839c95 commit b6848c0

3 files changed

Lines changed: 125 additions & 10 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sshd.git;
20+
21+
import java.nio.file.Path;
22+
23+
import org.apache.sshd.util.test.BaseTestSupport;
24+
import org.eclipse.jgit.lib.Config;
25+
import org.eclipse.jgit.lib.Constants;
26+
import org.eclipse.jgit.storage.file.FileBasedConfig;
27+
import org.eclipse.jgit.util.FS;
28+
import org.eclipse.jgit.util.SystemReader;
29+
30+
public class GitTestSupport extends BaseTestSupport {
31+
32+
protected GitTestSupport() {
33+
super();
34+
}
35+
36+
protected SystemReader mockGitConfig(Path directory) throws Exception {
37+
SystemReader defaultSystemReader = SystemReader.getInstance();
38+
SystemReader.setInstance(new MockSystemReader(defaultSystemReader, directory));
39+
return defaultSystemReader;
40+
}
41+
42+
private static class MockSystemReader extends SystemReader {
43+
44+
private static final String[] HIDDEN_VARIABLES = { //
45+
Constants.GIT_DIR_KEY, //
46+
Constants.GIT_WORK_TREE_KEY, //
47+
Constants.GIT_OBJECT_DIRECTORY_KEY, //
48+
Constants.GIT_INDEX_FILE_KEY, //
49+
Constants.GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY };
50+
51+
private final SystemReader delegate;
52+
private final Path tempDir;
53+
54+
MockSystemReader(SystemReader delegate, Path directory) {
55+
this.delegate = delegate;
56+
this.tempDir = directory;
57+
}
58+
59+
@Override
60+
public String getHostname() {
61+
return "localhost";
62+
}
63+
64+
@Override
65+
public String getenv(String variable) {
66+
String result = delegate.getenv(variable);
67+
if (result != null) {
68+
// Hide some environment variables that if set might confuse JGit.
69+
boolean isWin = isWindows();
70+
for (String gitvar : HIDDEN_VARIABLES) {
71+
if (isWin && gitvar.equalsIgnoreCase(variable) || !isWin && gitvar.equals(variable)) {
72+
return null;
73+
}
74+
}
75+
}
76+
return result;
77+
}
78+
79+
@Override
80+
public String getProperty(String key) {
81+
if (Constants.OS_USER_DIR.equals(key)) {
82+
// Return a fake "home" directory
83+
return tempDir.toString();
84+
}
85+
return delegate.getProperty(key);
86+
}
87+
88+
@Override
89+
public FileBasedConfig openUserConfig(Config parent, FS fs) {
90+
return new FileBasedConfig(parent, tempDir.resolve(".userGitConfig").toFile(), fs);
91+
}
92+
93+
@Override
94+
public FileBasedConfig openSystemConfig(Config parent, FS fs) {
95+
return new FileBasedConfig(parent, tempDir.resolve(".systemGitConfig").toFile(), fs);
96+
}
97+
98+
@Override
99+
public FileBasedConfig openJGitConfig(Config parent, FS fs) {
100+
return new FileBasedConfig(parent, tempDir.resolve(".jGitConfig").toFile(), fs);
101+
}
102+
103+
@Override
104+
public long getCurrentTime() {
105+
return delegate.getCurrentTime();
106+
}
107+
108+
@Override
109+
public int getTimezone(long when) {
110+
return delegate.getTimezone(when);
111+
}
112+
}
113+
}

sshd-git/src/test/java/org/apache/sshd/git/pack/GitPackCommandTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import org.apache.sshd.common.util.OsUtils;
3030
import org.apache.sshd.git.GitLocationResolver;
3131
import org.apache.sshd.git.GitModuleProperties;
32+
import org.apache.sshd.git.GitTestSupport;
3233
import org.apache.sshd.git.transport.GitSshdSessionFactory;
3334
import org.apache.sshd.server.SshServer;
3435
import org.apache.sshd.server.auth.password.AcceptAllPasswordAuthenticator;
3536
import org.apache.sshd.server.session.ServerSession;
3637
import org.apache.sshd.sftp.server.SftpSubsystemFactory;
37-
import org.apache.sshd.util.test.BaseTestSupport;
3838
import org.apache.sshd.util.test.CommonTestSupportUtils;
3939
import org.apache.sshd.util.test.JSchLogger;
4040
import org.eclipse.jgit.api.Git;
@@ -45,19 +45,17 @@
4545
import org.eclipse.jgit.transport.SshSessionFactory;
4646
import org.eclipse.jgit.transport.UploadPack;
4747
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
48+
import org.eclipse.jgit.util.SystemReader;
4849
import org.junit.jupiter.api.Assumptions;
4950
import org.junit.jupiter.api.BeforeAll;
5051
import org.junit.jupiter.api.MethodOrderer.MethodName;
5152
import org.junit.jupiter.api.Test;
5253
import org.junit.jupiter.api.TestMethodOrder;
5354

54-
import static org.junit.jupiter.api.Assertions.assertFalse;
55-
import static org.junit.jupiter.api.Assertions.assertTrue;
56-
5755
/**
5856
*/
5957
@TestMethodOrder(MethodName.class)
60-
public class GitPackCommandTest extends BaseTestSupport {
58+
public class GitPackCommandTest extends GitTestSupport {
6159
public GitPackCommandTest() {
6260
super();
6361
}
@@ -79,6 +77,7 @@ void gitPack() throws Exception {
7977
Assumptions.assumeFalse(OsUtils.isWin32(), "On windows this activates TortoisePlink");
8078

8179
Path gitRootDir = getTempTargetRelativeFile(getClass().getSimpleName());
80+
SystemReader defaultSystemReader = mockGitConfig(gitRootDir);
8281
try (SshServer sshd = setupTestServer()) {
8382
GitPackTestConfig packConfig = new GitPackTestConfig();
8483
Path serverRootDir = gitRootDir.resolve("server");
@@ -140,6 +139,8 @@ void gitPack() throws Exception {
140139
} finally {
141140
sshd.stop();
142141
}
142+
} finally {
143+
SystemReader.setInstance(defaultSystemReader);
143144
}
144145
}
145146

sshd-git/src/test/java/org/apache/sshd/git/pgm/GitPgmCommandTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,28 @@
3131
import org.apache.sshd.client.session.ClientSession;
3232
import org.apache.sshd.common.util.net.SshdSocketAddress;
3333
import org.apache.sshd.git.GitLocationResolver;
34+
import org.apache.sshd.git.GitTestSupport;
3435
import org.apache.sshd.server.SshServer;
3536
import org.apache.sshd.sftp.server.SftpSubsystemFactory;
36-
import org.apache.sshd.util.test.BaseTestSupport;
3737
import org.apache.sshd.util.test.CommonTestSupportUtils;
3838
import org.eclipse.jgit.api.Git;
39+
import org.eclipse.jgit.util.SystemReader;
3940
import org.junit.jupiter.api.MethodOrderer.MethodName;
4041
import org.junit.jupiter.api.Test;
4142
import org.junit.jupiter.api.TestMethodOrder;
4243

43-
import static org.junit.jupiter.api.Assertions.assertEquals;
44-
import static org.junit.jupiter.api.Assertions.assertTrue;
45-
4644
/**
4745
*/
4846
@TestMethodOrder(MethodName.class)
49-
public class GitPgmCommandTest extends BaseTestSupport {
47+
public class GitPgmCommandTest extends GitTestSupport {
5048
public GitPgmCommandTest() {
5149
super();
5250
}
5351

5452
@Test
5553
void gitPgm() throws Exception {
5654
Path serverDir = getTempTargetRelativeFile(getClass().getSimpleName());
55+
SystemReader defaultSystemReader = mockGitConfig(serverDir.getParent());
5756
try (SshServer sshd = setupTestServer()) {
5857
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
5958
sshd.setCommandFactory(new GitPgmCommandFactory(GitLocationResolver.constantPath(serverDir)));
@@ -87,6 +86,8 @@ void gitPgm() throws Exception {
8786
} finally {
8887
sshd.stop();
8988
}
89+
} finally {
90+
SystemReader.setInstance(defaultSystemReader);
9091
}
9192
}
9293

0 commit comments

Comments
 (0)