Skip to content

Commit

Permalink
make method testable and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jul 31, 2023
1 parent 5b58289 commit 2a90717
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public WindowsProtectedKeychainAccess() {
}

private static List<Path> readKeychainPathsFromEnv() {
return Optional.ofNullable(System.getProperty(KEYCHAIN_PATHS_PROPERTY))
.stream().flatMap(rawPaths -> Arrays.stream(rawPaths.split(System.getProperty("path.separator"))))
var keychainPaths = System.getProperty(KEYCHAIN_PATHS_PROPERTY, "");
return parsePaths(keychainPaths, System.getProperty("path.separator"));
}

// visible for testing
static List<Path> parsePaths(String listOfPaths, String pathSeparator) {
return Arrays.stream(listOfPaths.split(pathSeparator))
.filter(Predicate.not(String::isEmpty))
.map(Path::of)
.map(WindowsProtectedKeychainAccess::resolveHomeDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.cryptomator.integrations.keychain.KeychainAccessException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;
Expand Down Expand Up @@ -46,4 +48,35 @@ public void testStoreAndLoad() throws KeychainAccessException {
Assertions.assertNull(keychain.loadPassphrase("nonExistingPassword"));
}

@Nested
public class ParsePaths {
@Test
@DisplayName("String is split with path separator")
public void testParsePaths() {
String paths = "C:\\foo\\bar;bar\\kuz";
var result = WindowsProtectedKeychainAccess.parsePaths(paths, ";");
Assertions.assertEquals(2, result.size());
Assertions.assertTrue(result.contains(Path.of("C:\\foo\\bar")));
Assertions.assertTrue(result.contains(Path.of("bar\\kuz")));
}

@Test
@DisplayName("Empty string returns empty list")
public void testParsePathsEmpty() {
var result = WindowsProtectedKeychainAccess.parsePaths("", ";");
Assertions.assertEquals(0, result.size());
}

@Test
@DisplayName("Strings starting with ~ are resolved to user home")
public void testParsePathsUserHome() {
var userHome = Path.of(System.getProperty("user.home"));
var result = WindowsProtectedKeychainAccess.parsePaths("this\\~\\not;~\\foo\\bar", ";");
Assertions.assertEquals(2, result.size());
Assertions.assertTrue(result.contains(Path.of("this\\~\\not")));
Assertions.assertTrue(result.contains(userHome.resolve("foo\\bar")));
}

}

}

0 comments on commit 2a90717

Please sign in to comment.