diff options
Diffstat (limited to 'src/test/java/com')
7 files changed, 487 insertions, 4 deletions
diff --git a/src/test/java/com/gitblit/instance/GitblitInstanceIdTest.java b/src/test/java/com/gitblit/instance/GitblitInstanceIdTest.java new file mode 100644 index 00000000..e1c03757 --- /dev/null +++ b/src/test/java/com/gitblit/instance/GitblitInstanceIdTest.java @@ -0,0 +1,158 @@ +package com.gitblit.instance; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.nio.file.Files; +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + + +public class GitblitInstanceIdTest +{ + @Rule + public TemporaryFolder baseFolder = new TemporaryFolder(); + + /** + * Tests that the version nibble is set to 0x8. + */ + @Test + public void testUuidVersion() throws Exception + { + GitblitInstanceId id = new GitblitInstanceId(); + UUID uuid = id.getId(); + assertNotNull(uuid); + long upper = uuid.getMostSignificantBits(); + assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L)); + } + + /** + * Tests that the variant nibble is set to 0x8. + */ + @Test + public void testUuidVariant() throws Exception + { + GitblitInstanceId id = new GitblitInstanceId(); + UUID uuid = id.getId(); + assertNotNull(uuid); + long lower = uuid.getLeastSignificantBits(); + assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L)); + } + + /** + * Test that the first four bytes hold a timestamp in a newly generated id. + */ + @Test + public void testDatePart() throws Exception + { + GitblitInstanceId id = new GitblitInstanceId(); + UUID uuid = id.getId(); + assertNotNull(uuid); + long upper = uuid.getMostSignificantBits(); + long ts = System.currentTimeMillis(); + assertEquals("Date part of UUID does not equal current date/time.", ts >> 2*8, upper >> 4*8); + } + + + + /** + * Test that a new id is generated and stored in a file, when none existed. + */ + @Test + public void testStoreId() throws Exception + { + GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot()); + UUID uuid = id.getId(); + assertNotNull(uuid); + long lower = uuid.getLeastSignificantBits(); + assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L)); + long upper = uuid.getMostSignificantBits(); + assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L)); + + File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE); + assertTrue("Id file was not created", idFile.exists()); + + String string = Files.readAllLines(idFile.toPath()).get(0); + try { + UUID uuidFromFile = UUID.fromString(string); + assertEquals("Returned id and id read from file are not equal.", uuid, uuidFromFile); + } catch (IllegalArgumentException e) { + fail("UUID read from file is not valid: " + string); + } + } + + + /** + * Test that a new id is generated and stored in a file, when none existed. + */ + @Test + public void testStoreIdNonexistingFolder() throws Exception + { + File baseSubFolder = new File(baseFolder.getRoot(), "nonexisting"); + + GitblitInstanceId id = new GitblitInstanceId(baseSubFolder); + UUID uuid = id.getId(); + assertNotNull(uuid); + long lower = uuid.getLeastSignificantBits(); + assertEquals(0x8000000000000000L, (lower & 0xF000000000000000L)); + long upper = uuid.getMostSignificantBits(); + assertEquals(0x0000000000008000L, (upper & 0x000000000000F000L)); + + File idFile = new File(baseSubFolder, GitblitInstanceId.STORAGE_FILE); + assertTrue("Id file was not created", idFile.exists()); + + String string = Files.readAllLines(idFile.toPath()).get(0); + try { + UUID uuidFromFile = UUID.fromString(string); + assertEquals("Returned id and id read from file are not equal.", uuid, uuidFromFile); + } catch (IllegalArgumentException e) { + fail("UUID read from file is not valid: " + string); + } + } + + + /** + * Test that an existing id is read from an existing id file. + */ + @Test + public void testReadId() throws Exception + { + File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE); + String uuidString = "0196e409-c664-82f3-88f1-e963d16c7b8a"; + Files.write(idFile.toPath(), uuidString.getBytes()); + + GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot()); + UUID uuid = id.getId(); + assertNotNull(uuid); + + UUID refUuid = UUID.fromString(uuidString); + assertEquals("Returned id is not equal to reference id", refUuid, uuid); + } + + + /** + * Test reading id from a file with whitespace + */ + @Test + public void testReadIdWhitespace() throws Exception + { + File idFile = new File(baseFolder.getRoot(), GitblitInstanceId.STORAGE_FILE); + String uuidString = "0196e409-c664-82f3-88f1-e963d16c7b8a"; + String fileString = "\n " + uuidString + " \n \n"; + Files.write(idFile.toPath(), fileString.getBytes()); + + GitblitInstanceId id = new GitblitInstanceId(baseFolder.getRoot()); + UUID uuid = id.getId(); + assertNotNull(uuid); + + UUID refUuid = UUID.fromString(uuidString); + assertEquals("Returned id is not equal to reference id", refUuid, uuid); + } + +}
\ No newline at end of file diff --git a/src/test/java/com/gitblit/instance/GitblitInstanceStatTest.java b/src/test/java/com/gitblit/instance/GitblitInstanceStatTest.java new file mode 100644 index 00000000..0b883a33 --- /dev/null +++ b/src/test/java/com/gitblit/instance/GitblitInstanceStatTest.java @@ -0,0 +1,184 @@ +package com.gitblit.instance; + +import com.gitblit.Constants; +import com.gitblit.models.ServerStatus; +import org.junit.Before; +import org.junit.Test; + +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class GitblitInstanceStatTest +{ + + protected GitblitInstanceStat instanceStat; + protected ServerStatus serverStatus; + + @Before + public void setUp() throws Exception + { + instanceStat = new GitblitInstanceStat(); + serverStatus = new ServerStatus(); + instanceStat.init(serverStatus); + } + + + @Test + public void testGetVersion() + { + String version = instanceStat.version; + assertNotNull(version); + assertFalse(version.isEmpty()); + assertEquals(Constants.getVersion(), version); + } + + @Test + public void testGetStartTs() + { + Date date = instanceStat.startTs; + assertNotNull(date); + assertEquals(serverStatus.bootDate, date); + } + + @Test + public void testGetType() + { + String type = instanceStat.instanceType.name(); + assertNotNull(type); + assertFalse(type.isEmpty()); + assertEquals("WAR", type); + } + + @Test + public void testGetOS() + { + String os = instanceStat.os; + + String oslc = System.getProperty("os.name").toLowerCase(); + + if (oslc.contains("windows")) { + assertEquals("Windows", os); + } + else if (oslc.contains("linux")) { + assertEquals("Linux", os); + } + else if (oslc.contains("mac")) { + assertEquals("macOS", os); + } + } + + @Test + public void testGetOSName() + { + String name = instanceStat.osName; + assertNotNull(name); + assertFalse(name.isEmpty()); + assertEquals(System.getProperty("os.name"), name); + } + + @Test + public void testGetOSVersion() + { + String version = instanceStat.osVersion; + assertNotNull(version); + assertFalse(version.isEmpty()); + assertEquals(System.getProperty("os.version"), version); + } + + @Test + public void testGetOSArch() + { + String arch = instanceStat.osArch; + assertNotNull(arch); + assertFalse(arch.isEmpty()); + assertEquals(System.getProperty("os.arch"), arch); + } + + @Test + public void testGetJavaVersion() + { + String version = instanceStat.javaVersion; + assertNotNull(version); + assertFalse(version.isEmpty()); + assertEquals(System.getProperty("java.version"), version); + } + + @Test + public void testGetJavaVendor() + { + String vendor = instanceStat.javaVendor; + assertNotNull(vendor); + assertFalse(vendor.isEmpty()); + assertEquals(System.getProperty("java.vendor"), vendor); + } + + @Test + public void testGetJavaRuntimeVersion() + { + String rt = instanceStat.javaRuntimeVersion; + assertNotNull(rt); + assertFalse(rt.isEmpty()); + assertEquals(System.getProperty("java.runtime.version"), rt); + } + + @Test + public void testGetJavaRuntimeName() + { + String rt = instanceStat.javaRuntimeName; + assertNotNull(rt); + assertFalse(rt.isEmpty()); + assertEquals(System.getProperty("java.runtime.name"), rt); + } + + @Test + public void testGetJavaVmVersion() + { + String vm = instanceStat.javaVmVersion; + assertNotNull(vm); + assertFalse(vm.isEmpty()); + assertEquals(System.getProperty("java.vm.version"), vm); + } + + @Test + public void testGetJavaVmName() + { + String vm = instanceStat.javaVmName; + assertNotNull(vm); + assertFalse(vm.isEmpty()); + assertEquals(System.getProperty("java.vm.name"), vm); + } + + @Test + public void testGetMaxMem() + { + long maxMem = instanceStat.maxMem; + assertTrue(maxMem > 0); + assertEquals(Runtime.getRuntime().maxMemory(), maxMem); + } + + @Test + public void testToString() + { + String str = instanceStat.toString(); + assertNotNull(str); + assertFalse(str.isEmpty()); + assertTrue(str.contains("GitblitInstanceStat")); + assertTrue(str.contains("version")); + assertTrue(str.contains("instanceType")); + assertTrue(str.contains("os")); + assertTrue(str.contains("osName")); + assertTrue(str.contains("osVersion")); + assertTrue(str.contains("osArch")); + assertTrue(str.contains("javaVersion")); + assertTrue(str.contains("javaVendor")); + assertTrue(str.contains("javaRuntimeVersion")); + assertTrue(str.contains("javaRuntimeName")); + assertTrue(str.contains("javaVmVersion")); + assertTrue(str.contains("javaVmName")); + + } +} diff --git a/src/test/java/com/gitblit/instance/GitblitInstanceTest.java b/src/test/java/com/gitblit/instance/GitblitInstanceTest.java new file mode 100644 index 00000000..8ebc5d2a --- /dev/null +++ b/src/test/java/com/gitblit/instance/GitblitInstanceTest.java @@ -0,0 +1,64 @@ +package com.gitblit.instance; + +import com.gitblit.manager.IRuntimeManager; + +import com.gitblit.models.ServerStatus; +import com.gitblit.tests.mock.MockRuntimeManager; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + + +public class GitblitInstanceTest +{ + @Test + public void testShouldNotReportUnintialized() + { + GitblitInstance instance = new GitblitInstance(); + assertFalse(instance.shouldRunReports()); + } + + @Test + public void testShouldNotReportInTests() + { + GitblitInstance instance = new GitblitInstance(); + instance.init(new MockRuntimeManager()); + assertFalse(instance.shouldRunReports()); + } + + @Test + public void testShouldNotReportInSnapshotVersion() + { + GitblitInstance instance = new GitblitInstance(); + IRuntimeManager runtimeManager = new MockRuntimeManager(); + runtimeManager.getSettings().overrideSetting("gitblit.testRun", "false"); + instance.init(runtimeManager); + assertFalse(instance.shouldRunReports()); + } + + @Test + public void testShouldReportIfForced() + { + GitblitInstance instance = new GitblitInstance(); + IRuntimeManager runtimeManager = new MockRuntimeManager(); + runtimeManager.getSettings().overrideSetting("gitblit.testRunReporting", "true"); + instance.init(runtimeManager); + assertTrue(instance.shouldRunReports()); + } + + @Test + public void testShouldReportInReleaseVersion() + { + ServerStatus serverStatus = new ServerStatus("1.10.123"); + MockRuntimeManager runtimeManager = new MockRuntimeManager(); + runtimeManager.setStatus(serverStatus); + runtimeManager.getSettings().overrideSetting("gitblit.testRun", "false"); + + GitblitInstance instance = new GitblitInstance(); + instance.init(runtimeManager); + assertTrue(instance.shouldRunReports()); + } + +} diff --git a/src/test/java/com/gitblit/tests/GitBlitSuite.java b/src/test/java/com/gitblit/tests/GitBlitSuite.java index fbae039c..e0328112 100644 --- a/src/test/java/com/gitblit/tests/GitBlitSuite.java +++ b/src/test/java/com/gitblit/tests/GitBlitSuite.java @@ -26,6 +26,9 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
+import com.gitblit.instance.GitblitInstanceIdTest;
+import com.gitblit.instance.GitblitInstanceStatTest;
+import com.gitblit.instance.GitblitInstanceTest;
import com.gitblit.utils.TimeUtilsTest;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
@@ -74,7 +77,8 @@ import com.gitblit.utils.JGitUtils; ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,
SshKeysDispatcherTest.class, UITicketTest.class, PathUtilsTest.class, SshKerberosAuthenticationTest.class,
- GravatarTest.class, FilestoreManagerTest.class, FilestoreServletTest.class, TicketReferenceTest.class })
+ GravatarTest.class, FilestoreManagerTest.class, FilestoreServletTest.class, TicketReferenceTest.class,
+ GitblitInstanceIdTest.class, GitblitInstanceStatTest.class, GitblitInstanceTest.class })
public class GitBlitSuite {
public static final File BASEFOLDER = new File("data");
@@ -216,7 +220,7 @@ public class GitBlitSuite { }
cloneOrFetch("helloworld.git", HELLOWORLD_REPO_SOURCE.getAbsolutePath());
cloneOrFetch("ticgit.git", TICGIT_REPO_SOURCE.getAbsolutePath());
- cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
+ cloneOrFetch("test/jgit.git", "https://github.com/eclipse-jgit/jgit.git");
cloneOrFetch("test/helloworld.git", HELLOWORLD_REPO_SOURCE.getAbsolutePath());
cloneOrFetch("test/ambition.git", AMBITION_REPO_SOURCE.getAbsolutePath());
cloneOrFetch("test/gitective.git", GITECTIVE_REPO_SOURCE.getAbsolutePath());
diff --git a/src/test/java/com/gitblit/tests/GitblitUnitTest.java b/src/test/java/com/gitblit/tests/GitblitUnitTest.java index 58bc60e4..2d915612 100644 --- a/src/test/java/com/gitblit/tests/GitblitUnitTest.java +++ b/src/test/java/com/gitblit/tests/GitblitUnitTest.java @@ -31,7 +31,10 @@ import com.gitblit.servlet.GitblitContext; public class GitblitUnitTest extends org.junit.Assert { public static IStoredSettings settings() { - return runtime().getSettings(); + IStoredSettings settings = runtime().getSettings(); + // Insert marker that this is running as a test + settings.overrideSetting("gitblit.testRun", "true"); + return settings; } public static IRuntimeManager runtime() { diff --git a/src/test/java/com/gitblit/tests/SshDaemonTest.java b/src/test/java/com/gitblit/tests/SshDaemonTest.java index c7d06198..e88dc9bb 100644 --- a/src/test/java/com/gitblit/tests/SshDaemonTest.java +++ b/src/test/java/com/gitblit/tests/SshDaemonTest.java @@ -16,10 +16,12 @@ package com.gitblit.tests; import java.io.File; +import java.security.KeyPair; import java.text.MessageFormat; import java.util.List; import org.apache.sshd.client.SshClient; +import org.apache.sshd.client.future.AuthFuture; import org.apache.sshd.client.session.ClientSession; import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.Git; @@ -42,11 +44,72 @@ public class SshDaemonTest extends SshUnitTest { String url = GitBlitSuite.sshDaemonUrl; @Test + public void testPasswordAuthentication() throws Exception { + SshClient client = getClient(); + ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).verify().getSession(); + + session.addPasswordIdentity(password); + AuthFuture authFuture = session.auth(); + assertTrue(authFuture.await()); + assertTrue(authFuture.isSuccess()); + } + + @Test public void testPublicKeyAuthentication() throws Exception { SshClient client = getClient(); ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).verify().getSession(); + session.addPublicKeyIdentity(rwKeyPair); - assertTrue(session.auth().await()); + AuthFuture authFuture = session.auth(); + assertTrue(authFuture.await()); + assertTrue(authFuture.isSuccess()); + } + + @Test + public void testWrongPublicKeyAuthentication() throws Exception { + SshClient client = getClient(); + ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).verify().getSession(); + KeyPair attackKeyPair = generator.generateKeyPair(); + + session.addPublicKeyIdentity(attackKeyPair); + AuthFuture authFuture = session.auth(); + assertTrue(authFuture.await()); + assertFalse(authFuture.isSuccess()); + } + + @Test + public void testWrongPublicKeyThenPasswordAuthentication() throws Exception { + SshClient client = getClient(); + ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).verify().getSession(); + KeyPair otherKeyPair = generator.generateKeyPair(); + + session.addPublicKeyIdentity(otherKeyPair); + AuthFuture authFuture = session.auth(); + assertTrue(authFuture.await()); + assertFalse(authFuture.isSuccess()); + + session.addPasswordIdentity(password); + authFuture = session.auth(); + assertTrue(authFuture.await()); + assertTrue(authFuture.isSuccess()); + } + + @Test + public void testWrongPublicKeyThenWrongPasswordAuthentication() throws Exception { + SshClient client = getClient(); + ClientSession session = client.connect(username, "localhost", GitBlitSuite.sshPort).verify().getSession(); + KeyPair otherKeyPair = generator.generateKeyPair(); + KeyPair attackKeyPair = new KeyPair(rwKeyPair.getPublic(), otherKeyPair.getPrivate()); + + session.addPublicKeyIdentity(attackKeyPair); + AuthFuture authFuture = session.auth(); + assertTrue(authFuture.await()); + assertFalse(authFuture.isSuccess()); + + session.addPasswordIdentity("nothing"); + authFuture = session.auth(); + assertTrue(authFuture.await()); + assertFalse(authFuture.isSuccess()); } @Test diff --git a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java index 8897ef7e..1553e2a5 100644 --- a/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java +++ b/src/test/java/com/gitblit/tests/mock/MockRuntimeManager.java @@ -52,6 +52,8 @@ public class MockRuntimeManager implements IRuntimeManager { public MockRuntimeManager(IStoredSettings settings) { this.settings = settings; + // Insert marker that this is running as a test + settings.overrideSetting("gitblit.testRun", "true"); this.serverStatus = new ServerStatus(); this.serverStatus.servletContainer = "MockServer"; @@ -94,6 +96,11 @@ public class MockRuntimeManager implements IRuntimeManager { return serverStatus.bootDate; } + public void setStatus(ServerStatus status) + { + this.serverStatus = status; + } + @Override public ServerStatus getStatus() { // update heap memory status |