diff --git a/src/test/java/com/example/database/UserDatabaseTest.java b/src/test/java/com/example/database/UserDatabaseTest.java new file mode 100644 index 0000000..dda9cff --- /dev/null +++ b/src/test/java/com/example/database/UserDatabaseTest.java @@ -0,0 +1,154 @@ +package com.example.database; + +import org.junit.Test; +import org.junit.Before; +import static org.junit.Assert.*; + +/** + * Unit tests for UserDatabase class + * Note: These tests verify the methods execute without crashing. + * Actual database connections will fail in test environment, which is expected. + */ +public class UserDatabaseTest { + + private UserDatabase userDatabase; + + @Before + public void setUp() { + userDatabase = new UserDatabase(); + } + + @Test + public void testUserDatabaseCreation() { + assertNotNull("UserDatabase should be created", userDatabase); + } + + @Test + public void testAuthenticateUserDoesNotCrash() { + // Should handle database connection failure gracefully + boolean result = userDatabase.authenticateUser("testuser", "testpass"); + assertFalse("Authentication should fail without database", result); + } + + @Test + public void testAuthenticateUserWithEmptyUsername() { + boolean result = userDatabase.authenticateUser("", "password"); + assertFalse("Authentication with empty username should fail", result); + } + + @Test + public void testAuthenticateUserWithEmptyPassword() { + boolean result = userDatabase.authenticateUser("username", ""); + assertFalse("Authentication with empty password should fail", result); + } + + @Test + public void testAuthenticateUserWithSpecialCharacters() { + // Testing with SQL injection attempt + boolean result = userDatabase.authenticateUser("admin' OR '1'='1", "password"); + assertFalse("Authentication should fail without database", result); + } + + @Test + public void testAuthenticateUserWithQuotes() { + boolean result = userDatabase.authenticateUser("test'user", "test'pass"); + assertFalse("Authentication with quotes should fail without database", result); + } + + @Test + public void testUpdateUserProfileDoesNotCrash() { + // Should handle database connection failure gracefully + try { + userDatabase.updateUserProfile("1", "test@example.com", "Test User"); + // If no exception is thrown, test passes + assertTrue("Update method should complete", true); + } catch (Exception e) { + fail("Update method should not throw exception: " + e.getMessage()); + } + } + + @Test + public void testUpdateUserProfileWithEmptyValues() { + try { + userDatabase.updateUserProfile("", "", ""); + assertTrue("Update with empty values should complete", true); + } catch (Exception e) { + fail("Update with empty values should not throw exception"); + } + } + + @Test + public void testUpdateUserProfileWithSpecialCharacters() { + try { + userDatabase.updateUserProfile("1", "test@example.com", "O'Brien"); + assertTrue("Update with special characters should complete", true); + } catch (Exception e) { + fail("Update with special characters should not throw exception"); + } + } + + @Test + public void testUpdateUserProfileWithLongValues() { + String longEmail = "verylongemailaddress" + "@".repeat(10) + "example.com"; + String longName = "A".repeat(100); + try { + userDatabase.updateUserProfile("1", longEmail, longName); + assertTrue("Update with long values should complete", true); + } catch (Exception e) { + fail("Update with long values should not throw exception"); + } + } + + @Test + public void testDeleteUserDoesNotCrash() { + try { + userDatabase.deleteUser("1"); + assertTrue("Delete method should complete", true); + } catch (Exception e) { + fail("Delete method should not throw exception: " + e.getMessage()); + } + } + + @Test + public void testDeleteUserWithEmptyId() { + try { + userDatabase.deleteUser(""); + assertTrue("Delete with empty ID should complete", true); + } catch (Exception e) { + fail("Delete with empty ID should not throw exception"); + } + } + + @Test + public void testDeleteUserWithSpecialCharacters() { + try { + userDatabase.deleteUser("1' OR '1'='1"); + assertTrue("Delete with special characters should complete", true); + } catch (Exception e) { + fail("Delete with special characters should not throw exception"); + } + } + + @Test + public void testDeleteUserWithNonNumericId() { + try { + userDatabase.deleteUser("abc"); + assertTrue("Delete with non-numeric ID should complete", true); + } catch (Exception e) { + fail("Delete with non-numeric ID should not throw exception"); + } + } + + @Test + public void testMultipleOperationsSequence() { + // Test that multiple operations can be called in sequence + try { + userDatabase.authenticateUser("user1", "pass1"); + userDatabase.updateUserProfile("1", "email@test.com", "Name"); + userDatabase.deleteUser("2"); + assertTrue("Multiple operations should complete", true); + } catch (Exception e) { + fail("Multiple operations should not throw exception"); + } + } +} diff --git a/src/test/java/com/example/ldap/LdapAuthTest.java b/src/test/java/com/example/ldap/LdapAuthTest.java new file mode 100644 index 0000000..2624a04 --- /dev/null +++ b/src/test/java/com/example/ldap/LdapAuthTest.java @@ -0,0 +1,142 @@ +package com.example.ldap; + +import org.junit.Test; +import org.junit.Before; +import static org.junit.Assert.*; + +/** + * Unit tests for LdapAuth class + * Note: These tests verify the methods execute without crashing. + * Actual LDAP connections will fail in test environment, which is expected. + */ +public class LdapAuthTest { + + private LdapAuth ldapAuth; + + @Before + public void setUp() { + ldapAuth = new LdapAuth(); + } + + @Test + public void testLdapAuthCreation() { + assertNotNull("LdapAuth should be created", ldapAuth); + } + + @Test + public void testAuthenticateUserDoesNotCrash() { + // Should handle LDAP connection failure gracefully + boolean result = ldapAuth.authenticateUser("testuser", "testpass"); + assertFalse("Authentication should fail without LDAP server", result); + } + + @Test + public void testAuthenticateUserWithEmptyUsername() { + boolean result = ldapAuth.authenticateUser("", "password"); + assertFalse("Authentication with empty username should fail", result); + } + + @Test + public void testAuthenticateUserWithEmptyPassword() { + boolean result = ldapAuth.authenticateUser("username", ""); + assertFalse("Authentication with empty password should fail", result); + } + + @Test + public void testAuthenticateUserWithBothEmpty() { + boolean result = ldapAuth.authenticateUser("", ""); + assertFalse("Authentication with both empty should fail", result); + } + + @Test + public void testAuthenticateUserWithSpecialCharacters() { + // Testing with LDAP injection attempt + boolean result = ldapAuth.authenticateUser("admin*", "password"); + assertFalse("Authentication should fail without LDAP server", result); + } + + @Test + public void testAuthenticateUserWithParentheses() { + boolean result = ldapAuth.authenticateUser("user)(uid=*", "pass"); + assertFalse("Authentication with parentheses should fail without LDAP server", result); + } + + @Test + public void testAuthenticateUserWithAsterisk() { + boolean result = ldapAuth.authenticateUser("*", "*"); + assertFalse("Authentication with wildcards should fail without LDAP server", result); + } + + @Test + public void testAuthenticateUserNormalCredentials() { + boolean result = ldapAuth.authenticateUser("john.doe", "secretpassword"); + assertFalse("Authentication should fail without LDAP server", result); + } + + @Test + public void testGetUserInfoDoesNotCrash() { + try { + String result = ldapAuth.getUserInfo("testuser"); + // Should return null or handle error gracefully + assertNull("getUserInfo should return null without LDAP server", result); + } catch (Exception e) { + fail("getUserInfo should not throw exception: " + e.getMessage()); + } + } + + @Test + public void testGetUserInfoWithEmptyUserId() { + String result = ldapAuth.getUserInfo(""); + assertNull("getUserInfo with empty ID should return null", result); + } + + @Test + public void testGetUserInfoWithSpecialCharacters() { + String result = ldapAuth.getUserInfo("admin*"); + assertNull("getUserInfo with special characters should return null", result); + } + + @Test + public void testGetUserInfoWithWildcard() { + String result = ldapAuth.getUserInfo("*"); + assertNull("getUserInfo with wildcard should return null", result); + } + + @Test + public void testGetUserInfoWithLdapInjection() { + String result = ldapAuth.getUserInfo("admin)(uid=*)"); + assertNull("getUserInfo with injection attempt should return null", result); + } + + @Test + public void testGetUserInfoNormalUserId() { + String result = ldapAuth.getUserInfo("john.doe"); + assertNull("getUserInfo should return null without LDAP server", result); + } + + @Test + public void testMultipleAuthenticationAttempts() { + // Test that multiple operations can be called in sequence + try { + ldapAuth.authenticateUser("user1", "pass1"); + ldapAuth.authenticateUser("user2", "pass2"); + ldapAuth.getUserInfo("user1"); + ldapAuth.getUserInfo("user2"); + assertTrue("Multiple operations should complete", true); + } catch (Exception e) { + fail("Multiple operations should not throw exception"); + } + } + + @Test + public void testAuthenticateUserWithUnicodeCharacters() { + boolean result = ldapAuth.authenticateUser("用户", "密码"); + assertFalse("Authentication with unicode should fail without LDAP server", result); + } + + @Test + public void testGetUserInfoWithUnicodeCharacters() { + String result = ldapAuth.getUserInfo("用户"); + assertNull("getUserInfo with unicode should return null without LDAP server", result); + } +} diff --git a/src/test/java/com/example/security/CryptoUtilsTest.java b/src/test/java/com/example/security/CryptoUtilsTest.java new file mode 100644 index 0000000..05f4c6c --- /dev/null +++ b/src/test/java/com/example/security/CryptoUtilsTest.java @@ -0,0 +1,131 @@ +package com.example.security; + +import org.junit.Test; +import org.junit.Before; +import static org.junit.Assert.*; + +/** + * Unit tests for CryptoUtils class + */ +public class CryptoUtilsTest { + + private CryptoUtils cryptoUtils; + + @Before + public void setUp() { + cryptoUtils = new CryptoUtils(); + } + + @Test + public void testGenerateTokenNotNull() { + String token = cryptoUtils.generateToken(); + assertNotNull("Token should not be null", token); + } + + @Test + public void testGenerateTokenHasCorrectLength() { + String token = cryptoUtils.generateToken(); + assertEquals("Token should have 32 characters", 32, token.length()); + } + + @Test + public void testGenerateTokenContainsOnlyValidCharacters() { + String token = cryptoUtils.generateToken(); + assertTrue("Token should contain only alphanumeric characters", + token.matches("[a-z0-9]+")); + } + + @Test + public void testGenerateTokenGeneratesDifferentTokens() { + String token1 = cryptoUtils.generateToken(); + String token2 = cryptoUtils.generateToken(); + assertNotEquals("Two consecutive tokens should be different", token1, token2); + } + + @Test + public void testGenerateSessionIdNotNull() { + String sessionId = cryptoUtils.generateSessionId(); + assertNotNull("Session ID should not be null", sessionId); + } + + @Test + public void testGenerateSessionIdNotEmpty() { + String sessionId = cryptoUtils.generateSessionId(); + assertTrue("Session ID should not be empty", sessionId.length() > 0); + } + + @Test + public void testGenerateSessionIdIsNumeric() { + String sessionId = cryptoUtils.generateSessionId(); + assertTrue("Session ID should be numeric", sessionId.matches("\\d+")); + } + + @Test + public void testGenerateSessionIdGeneratesDifferentIds() { + String sessionId1 = cryptoUtils.generateSessionId(); + String sessionId2 = cryptoUtils.generateSessionId(); + assertNotEquals("Two consecutive session IDs should be different", + sessionId1, sessionId2); + } + + @Test + public void testHashPasswordNotNull() { + String hash = cryptoUtils.hashPassword("password123"); + assertNotNull("Hash should not be null", hash); + } + + @Test + public void testHashPasswordNotEmpty() { + String hash = cryptoUtils.hashPassword("password123"); + assertTrue("Hash should not be empty", hash.length() > 0); + } + + @Test + public void testHashPasswordConsistency() { + String password = "testPassword"; + String hash1 = cryptoUtils.hashPassword(password); + String hash2 = cryptoUtils.hashPassword(password); + assertEquals("Same password should produce same hash", hash1, hash2); + } + + @Test + public void testHashPasswordDifferentForDifferentPasswords() { + String hash1 = cryptoUtils.hashPassword("password1"); + String hash2 = cryptoUtils.hashPassword("password2"); + assertNotEquals("Different passwords should produce different hashes", + hash1, hash2); + } + + @Test + public void testHashPasswordEmptyString() { + String hash = cryptoUtils.hashPassword(""); + assertNotNull("Hash of empty string should not be null", hash); + assertTrue("Hash of empty string should not be empty", hash.length() > 0); + } + + @Test + public void testHashPasswordSpecialCharacters() { + String hash = cryptoUtils.hashPassword("p@ssw0rd!#$"); + assertNotNull("Hash with special characters should not be null", hash); + assertTrue("Hash with special characters should not be empty", hash.length() > 0); + } + + @Test + public void testGetEncryptionKeyNotNull() { + String key = cryptoUtils.getEncryptionKey(); + assertNotNull("Encryption key should not be null", key); + } + + @Test + public void testGetEncryptionKeyNotEmpty() { + String key = cryptoUtils.getEncryptionKey(); + assertTrue("Encryption key should not be empty", key.length() > 0); + } + + @Test + public void testGetEncryptionKeyConsistent() { + String key1 = cryptoUtils.getEncryptionKey(); + String key2 = cryptoUtils.getEncryptionKey(); + assertEquals("Encryption key should be consistent", key1, key2); + } +} diff --git a/src/test/java/com/example/web/FileControllerTest.java b/src/test/java/com/example/web/FileControllerTest.java new file mode 100644 index 0000000..299b109 --- /dev/null +++ b/src/test/java/com/example/web/FileControllerTest.java @@ -0,0 +1,140 @@ +package com.example.web; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import static org.junit.Assert.*; +import java.io.File; +import java.io.IOException; + +/** + * Unit tests for FileController class + */ +public class FileControllerTest { + + private FileController fileController; + private static final String TEST_DIR = "/tmp/uploads/"; + private static final String TEST_FILE = "testfile.txt"; + private static final String TEST_CONTENT = "This is test content"; + + @Before + public void setUp() { + fileController = new FileController(); + // Create test directory + new File(TEST_DIR).mkdirs(); + } + + @After + public void tearDown() { + // Clean up test files + File testFile = new File(TEST_DIR + TEST_FILE); + if (testFile.exists()) { + testFile.delete(); + } + } + + @Test + public void testFileControllerCreation() { + assertNotNull("FileController should be created", fileController); + } + + @Test + public void testReadFileNonExistent() { + String content = fileController.readFile("nonexistent.txt"); + assertNull("Reading non-existent file should return null", content); + } + + @Test + public void testWriteFileSuccess() throws IOException { + boolean result = fileController.writeFile(TEST_FILE, TEST_CONTENT); + assertTrue("File write should succeed", result); + + File file = new File(TEST_DIR + TEST_FILE); + assertTrue("File should exist after write", file.exists()); + } + + @Test + public void testWriteAndReadFile() throws IOException { + // Write a file + boolean writeResult = fileController.writeFile(TEST_FILE, TEST_CONTENT); + assertTrue("File write should succeed", writeResult); + + // Read the file back + String content = fileController.readFile(TEST_FILE); + assertNotNull("Read content should not be null", content); + assertTrue("Content should contain test data", content.contains(TEST_CONTENT)); + } + + @Test + public void testWriteFileEmptyContent() { + boolean result = fileController.writeFile(TEST_FILE, ""); + assertTrue("Writing empty content should succeed", result); + } + + @Test + public void testWriteFileWithSpecialCharacters() { + String specialContent = "Special chars: !@#$%^&*()"; + boolean result = fileController.writeFile(TEST_FILE, specialContent); + assertTrue("Writing special characters should succeed", result); + + String content = fileController.readFile(TEST_FILE); + assertNotNull("Content should not be null", content); + assertTrue("Content should contain special characters", + content.contains(specialContent)); + } + + @Test + public void testReadFileEmptyFilename() { + String content = fileController.readFile(""); + assertNull("Reading empty filename should return null", content); + } + + @Test + public void testExecuteCommandReturnsResult() { + String result = fileController.executeCommand("echo test"); + assertNotNull("Command execution should return result", result); + } + + @Test + public void testExecuteCommandWithEmptyCommand() { + String result = fileController.executeCommand(""); + // May return empty or error, but should not crash + assertNotNull("Command execution should return something", result); + } + + @Test + public void testExecuteSystemCommandReturnsResult() { + String result = fileController.executeSystemCommand("echo test"); + assertNotNull("System command execution should return result", result); + } + + @Test + public void testExecuteSystemCommandWithEmptyCommand() { + String result = fileController.executeSystemCommand(""); + assertNotNull("System command execution should return something", result); + } + + @Test + public void testWriteMultipleFiles() { + boolean result1 = fileController.writeFile("file1.txt", "Content 1"); + boolean result2 = fileController.writeFile("file2.txt", "Content 2"); + + assertTrue("First file write should succeed", result1); + assertTrue("Second file write should succeed", result2); + + // Clean up + new File(TEST_DIR + "file1.txt").delete(); + new File(TEST_DIR + "file2.txt").delete(); + } + + @Test + public void testWriteFileMultilineContent() { + String multilineContent = "Line 1\nLine 2\nLine 3"; + boolean result = fileController.writeFile(TEST_FILE, multilineContent); + assertTrue("Writing multiline content should succeed", result); + + String content = fileController.readFile(TEST_FILE); + assertNotNull("Content should not be null", content); + assertTrue("Content should contain multiline data", content.contains("Line 1")); + } +}