private static MockConfigPart createConfigPart() { return new MockConfigPart() // By using the RSH port, it means that the connection will be kept open // (NTS connection). By keeping the connection open until explicitly // disconnected, this will indirectly be testing that the // SimpleConnectionManager closes the connection. .withServerName("1234") .withUsername("u") .withNoPassword() .withClientname("client1"); }
@Test void isSameServer_empty() { MockConfigPart part1 = new MockConfigPart() .withUsername("u1") .withServerName("n1:1"); ServerConfig sc = ServerConfig.createFrom(part1); assertTrue(sc.isSameServerConnection(part1)); // Change it in the client ways, and it's still the same server. part1.withClientHostname("h1").withClientname("cc") .withDefaultCharset("a1").withIgnoreFileName("i") .withReloadValue(false); assertTrue(sc.isSameServerConnection(part1)); // Change the user MockConfigPart part2 = part1.copy() .withUsername("u2"); assertFalse(sc.isSameServerConnection(part2)); // change the server name MockConfigPart part3 = part1.copy() .withServerName("n2:1"); assertFalse(sc.isSameServerConnection(part3)); // change the password MockConfigPart part4 = part1.copy() .withPassword("a"); assertFalse(sc.isSameServerConnection(part4)); part4.withNoPassword().withRequiresUserEnteredPassword(true); assertFalse(sc.isSameServerConnection(part4)); }
@Override public boolean hasError() { for (ConfigProblem problem : getConfigProblems()) { if (problem.isError()) { return true; } } return false; }
public MockConfigPart copy() { MockConfigPart ret = new MockConfigPart() .withSourceName(sourceName) .withP4ServerName(serverName) .withClientname(clientname) .withUsername(username) .withAuthTicketFile(authTicketFile) .withTrustTicketFile(trustTicketFile) .withServerFingerprint(serverFingerprint) .withClientHostname(clientHostname) .withIgnoreFileName(ignoreFileName) .withDefaultCharset(defaultCharset) .withLoginSso(loginSso) .withReloadValue(reloadValue) .withRequiresUserEnteredPassword(requiresUserEnteredPassword); if (hasPasswordSet) { ret.withPassword(password); } else { ret.withNoPassword(); } return ret; }
@Test void getServerIdForDataPart_full() { MockConfigPart data = new MockConfigPart() .withServerName("1234") .withUsername("luser") .withPassword("my password") .withAuthTicketFile(new File("auth-ticket-file")) .withTrustTicketFile(new File("trust-ticket-file")) .withServerFingerprint("1234asdf1234asdf"); assertEquals("localhost:1234" + SEP + "luser" + SEP + "auth-ticket-file" + SEP + "trust-ticket-file" + SEP + "1234asdf1234asdf", ServerConfig.getServerIdForDataPart(data)); }
Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") .withClientname("clientname") .withConfigProblems(createWarning()) .withPassword("password") .withServerFingerprint("fingerprint") .withAuthTicketFile(authFile) .withTrustTicketFile(trustFile) .withClientHostname("hostname") .withDefaultCharset("charset") .withIgnoreFileName("ignore") .withLoginSso("sso") .withRequiresUserEnteredPassword(true) .withSourceName("s") ), "<unset>", "<req>", "<nr>"
@Test void createFrom_invalid_noClientname() { MockConfigPart data = new MockConfigPart() .withUsername("u") .withServerName("blah"); assertThrows(IllegalArgumentException.class, () -> ClientConfig.createFrom( ServerConfig.createFrom(data), data )); }
@Test void isValid() { assertFalse(ServerConfig.isValidServerConfig(null)); MockConfigPart part = new MockConfigPart(); part.withConfigProblems(createError()); assertFalse(ServerConfig.isValidServerConfig(part)); part.withConfigProblems(); assertFalse(ServerConfig.isValidServerConfig(part)); P4ServerName mockName = mock(P4ServerName.class); when(mockName.getFullPort()).thenReturn(""); part.withP4ServerName(mockName); assertFalse(ServerConfig.isValidServerConfig(part)); part.withConfigProblems(createWarning()) .withServerName("my-server"); assertFalse(ServerConfig.isValidServerConfig(part)); part.withUsername(""); assertFalse(ServerConfig.isValidServerConfig(part)); part.withUsername("abc"); assertTrue(ServerConfig.isValidServerConfig(part)); }
@Test void toProperties_ServerConfig_emptyPassword() { Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") .withPassword("") ), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4USER", "username"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>"), // The user supplied an empty password. This means that a password is stored. new Pair<>("P4PASSWD", "<stored>") )); }
@Test void createFrom_error() { assertThrows(IllegalArgumentException.class, () -> ServerConfig.createFrom(new MockConfigPart())); }
@Test void getters_2() { MockConfigPart part = new MockConfigPart(); ConfigProblem cp = new ConfigProblem(part, "Too bad", false); assertFalse(cp.isError()); assertEquals("Too bad", cp.getMessage()); assertEquals(part, cp.getSource()); assertEquals( "problem(" + part + ": Too bad)", cp.toString()); part.withUsername("u"); assertEquals(part, cp.getSource()); assertEquals( "problem(" + part + ": Too bad)", cp.toString()); }
@Test void getServerIdForDataPart_empty() { MockConfigPart data = new MockConfigPart() .withNoPassword(); assertEquals("null" + SEP + "null" + SEP + "null" + SEP + "null" + SEP + "null", ServerConfig.getServerIdForDataPart(data)); }
@Test void getServerIdForDataPart_nullPassword() { MockConfigPart data = new MockConfigPart() .withPassword(null); assertEquals("null" + SEP + "null" + SEP + "null" + SEP + "null" + SEP + "null", ServerConfig.getServerIdForDataPart(data)); }
File trustFile = new File("trust.txt"); Map<String, String> props = ConfigPropertiesUtil.toProperties( new MockConfigPart() .withUsername("username") .withServerName("servername") .withClientname("clientname") .withConfigProblems(createError()) .withPassword("password") .withServerFingerprint("fingerprint") .withAuthTicketFile(authFile) .withTrustTicketFile(trustFile) .withClientHostname("hostname") .withDefaultCharset("charset") .withIgnoreFileName("ignore") .withLoginSso("sso") .withRequiresUserEnteredPassword(true) .withSourceName("s"), "<unset>", "<ep>", "<set>" );
@Test void toProperties_ServerConfig_empty() { Map<String, String> props = ConfigPropertiesUtil.toProperties( ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") ), "<unset>", "<stored>", "<ns>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", portName("servername")), new Pair<>("P4USER", "username"), new Pair<>("P4PASSWD", "<ns>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@Test void compareTo_err2() { MockConfigPart part = new MockConfigPart(); ConfigProblem cpF1 = new ConfigProblem(part, "Abc", false); ConfigProblem cpF2 = new ConfigProblem(part, "Def", false); ConfigProblem cpT1 = new ConfigProblem(part, "Abc", true); ConfigProblem cpT2 = new ConfigProblem(part, "Def", true); assertThat(cpF1.compareTo(cpF2), lessThan(0)); assertThat(cpF2.compareTo(cpF1), greaterThan(0)); assertThat(cpT1.compareTo(cpT2), lessThan(0)); assertThat(cpT2.compareTo(cpT1), greaterThan(0)); } }
@SuppressWarnings("unchecked") @Test void toProperties_configPartEmptyPassword() { Map<String, String> props = ConfigPropertiesUtil.toProperties( new MockConfigPart() .withPassword(""), "<unset>", "<ep>", "<set>" ); assertThat(props, mapContainsAll( new Pair<>("P4PORT", "<unset>"), new Pair<>("P4TRUST", "<unset>"), new Pair<>("P4USER", "<unset>"), new Pair<>("P4TICKETS", "<unset>"), new Pair<>("P4FINGERPRINT", "<unset>"), new Pair<>("P4PASSWD", "<ep>"), new Pair<>("P4CHARSET", "<unset>"), new Pair<>("P4IGNORE", "<unset>"), new Pair<>("P4CLIENT", "<unset>"), new Pair<>("P4HOST", "<unset>"), new Pair<>("P4LOGINSSO", "<unset>") )); }
@Test void createFrom_invalid_invalidServerConfigSettings() { MockConfigPart data = new MockConfigPart() .withUsername("u") .withServerName("blah") .withClientname("c"); ServerConfig sc = ServerConfig.createFrom(data); data.withUsername(null).withServerName(null); // Should not throw an error - it only needs client stuff. ClientConfig.createFrom(sc, data); }
File authFile = new File("auth.txt"); File trustFile = new File("trust.txt"); MockConfigPart part = new MockConfigPart() .withUsername("username") .withPassword("pass") .withServerName("servername") .withClientname("client") .withTrustTicketFile(trustFile) .withAuthTicketFile(authFile) .withServerFingerprint("abcd") .withDefaultCharset("char") .withIgnoreFileName("ignore-these") .withClientHostname("c-host") .withLoginSso("log-sso.cmd -t my_auth"); Map<String, String> props = ConfigPropertiesUtil.toProperties( ClientConfig.createFrom(ServerConfig.createFrom(part), part),
@Test void createFrom_empty() { ServerConfig sc = ServerConfig.createFrom( new MockConfigPart() .withUsername("username") .withServerName("servername") ); assertEquals(P4ServerName.forPort("servername"), sc.getServerName()); assertEquals("username", sc.getUsername()); assertNull(sc.getServerFingerprint()); assertNull(sc.getAuthTicket()); assertNull(sc.getTrustTicket()); assertNull(sc.getLoginSso()); assertThat(sc.getConfigVersion(), greaterThan(0)); assertFalse(sc.hasServerFingerprint()); assertFalse(sc.hasAuthTicket()); assertFalse(sc.hasTrustTicket()); assertFalse(sc.hasLoginSso()); assertFalse(sc.usesStoredPassword()); assertEquals("localhost:servername" + SEP + "username" + SEP + "null" + SEP + "null" + SEP + "null", sc.getServerId()); }