/** * Generate key pair. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ private KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { LOG.debug("Generating key pair (private at {}; public at {})", privateKeyLocation, publicKeyLocation); return KeyUtil.generateKeyPair(privateKeyLocation, publicKeyLocation); }
/** * Generate key pair. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ private KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { LOG.debug("Generating Key pair"); KeyPair kp = KeyUtil.generateKeyPair(privateKeyLocation, publicKeyLocation); LOG.debug("Private key location: {}", privateKeyLocation); LOG.debug("Public key location: {}", publicKeyLocation); return kp; }
public DefaultOperationTcpChannelTest() throws Exception { clientKeys = KeyUtil.generateKeyPair(); }
/** * Generate key pair and saves it to specified files. * * @param privateKeyLocation the private key location * @param publicKeyLocation the public key location * @return the key pair */ public static KeyPair generateKeyPair(String privateKeyLocation, String publicKeyLocation) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyLocation, publicKeyLocation); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
/** * Generate key pair and saves it to specified streams. * * @param privateKeyOutput the private key output stream * @param publicKeyOutput the public key output stream * @return the key pair */ public static KeyPair generateKeyPair( OutputStream privateKeyOutput, OutputStream publicKeyOutput) { try { KeyPair clientKeyPair = generateKeyPair(); saveKeyPair(clientKeyPair, privateKeyOutput, publicKeyOutput); return clientKeyPair; } catch (Exception ex) { LOG.error("Error generating client key pair", ex); } return null; }
private Map<TransportProtocolId, List<TransportConnectionInfo>> getDefaultBootstrapServers() throws NoSuchAlgorithmException { Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = new HashMap<>(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo( ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); bootststrapServers.put(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, Collections.singletonList(server)); return bootststrapServers; }
protected void initStorageMock(PersistentStorage storage) throws NoSuchAlgorithmException, IOException { KeyPair kp = KeyUtil.generateKeyPair(); Mockito.when(storage.exists(KaaClientProperties.CLIENT_PUBLIC_KEY_NAME_DEFAULT)).thenReturn(true); Mockito.when(storage.exists(KaaClientProperties.CLIENT_PRIVATE_KEY_NAME_DEFAULT)).thenReturn(true); Mockito.when(storage.openForRead(KaaClientProperties.CLIENT_PUBLIC_KEY_NAME_DEFAULT)).thenReturn( new ByteArrayInputStream(kp.getPublic().getEncoded())); Mockito.when(storage.openForRead(KaaClientProperties.CLIENT_PRIVATE_KEY_NAME_DEFAULT)).thenReturn( new ByteArrayInputStream(kp.getPrivate().getEncoded())); Mockito.when(storage.openForWrite(Mockito.anyString())).thenReturn(Mockito.mock(OutputStream.class)); }
@Test public void testConnectivity() throws NoSuchAlgorithmException { KaaClientState clientState = Mockito.mock(KaaClientState.class); Mockito.when(clientState.getPrivateKey()).thenReturn(clientKeys.getPrivate()); Mockito.when(clientState.getPublicKey()).thenReturn(clientKeys.getPublic()); FailoverManager failoverManager = Mockito.mock(FailoverManager.class); DefaultOperationTcpChannel channel = new DefaultOperationTcpChannel(clientState, failoverManager, null); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo(ServerType.OPERATIONS, TransportProtocolIdConstants.TCP_TRANSPORT_ID, "www.test.fake", 999, KeyUtil.generateKeyPair().getPublic()); ConnectivityChecker checker = Mockito.mock(ConnectivityChecker.class); Mockito.when(checker.checkConnectivity()).thenReturn(false); channel.setConnectivityChecker(checker); }
@Test public void testSingleBootstrapServerFailed() throws NoSuchAlgorithmException, InvalidKeySpecException { Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = new HashMap<>(); bootststrapServers.put(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, Arrays.asList( IpTransportInfoTest.createTestServerInfo( ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()))); BootstrapManager bootstrapManager = Mockito.mock(BootstrapManager.class); KaaDataChannel channel = Mockito.mock(KaaDataChannel.class); Mockito.when(channel.getSupportedTransportTypes()).thenReturn(SUPPORTED_TYPES); Mockito.when(channel.getTransportProtocolId()).thenReturn(TransportProtocolIdConstants.HTTP_TRANSPORT_ID); Mockito.when(channel.getServerType()).thenReturn(ServerType.BOOTSTRAP); Mockito.when(channel.getId()).thenReturn("mock_channel"); KaaChannelManager channelManager = new DefaultChannelManager(bootstrapManager, bootststrapServers, CONTEXT, null); FailoverManager failoverManager = Mockito.spy(new DefaultFailoverManager(channelManager, CONTEXT)); channelManager.setFailoverManager(failoverManager); channelManager.addChannel(channel); Mockito.verify(failoverManager, Mockito.times(1)).onServerChanged(Mockito.any(TransportConnectionInfo.class)); channelManager.onServerFailed(bootststrapServers.get(TransportProtocolIdConstants.HTTP_TRANSPORT_ID).get(0), FailoverStatus.CURRENT_BOOTSTRAP_SERVER_NA); }
"localhost", 9889, KeyUtil.generateKeyPair().getPublic()); channel.setServer(server);
@Test public void testOperationServerFailed() throws NoSuchAlgorithmException, InvalidKeySpecException { Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = getDefaultBootstrapServers(); BootstrapManager bootstrapManager = Mockito.mock(BootstrapManager.class); KaaDataChannel channel = Mockito.mock(KaaDataChannel.class); Mockito.when(channel.getSupportedTransportTypes()).thenReturn(SUPPORTED_TYPES); Mockito.when(channel.getTransportProtocolId()).thenReturn(TransportProtocolIdConstants.HTTP_TRANSPORT_ID); Mockito.when(channel.getId()).thenReturn("mock_channel"); KaaInternalChannelManager channelManager = new DefaultChannelManager(bootstrapManager, bootststrapServers, null, null); channelManager.addChannel(channel); TransportConnectionInfo opServer = IpTransportInfoTest.createTestServerInfo( ServerType.OPERATIONS, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9999, KeyUtil.generateKeyPair().getPublic()); channelManager.onTransportConnectionInfoUpdated(opServer); channelManager.onServerFailed(opServer, FailoverStatus.NO_CONNECTIVITY); Mockito.verify(bootstrapManager, Mockito.times(1)) .useNextOperationsServer(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, FailoverStatus.NO_CONNECTIVITY); }
@Test public void testShutdown() throws Exception { KaaChannelManager manager = Mockito.mock(KaaChannelManager.class); AbstractHttpClient httpClient = Mockito.mock(AbstractHttpClient.class); FailoverManager failoverManager = Mockito.mock(FailoverManager.class); Mockito.when(httpClient.executeHttpRequest(Mockito.anyString(), Mockito.any(LinkedHashMap.class), Mockito.anyBoolean())).thenThrow( new Exception()); AbstractKaaClient client = Mockito.mock(AbstractKaaClient.class); Mockito.when( client.createHttpClient(Mockito.anyString(), Mockito.any(PrivateKey.class), Mockito.any(PublicKey.class), Mockito.any(PublicKey.class))).thenReturn(httpClient); Mockito.when(client.getChannelManager()).thenReturn(manager); KaaClientState state = Mockito.mock(KaaClientState.class); KaaDataMultiplexer multiplexer = Mockito.mock(KaaDataMultiplexer.class); KaaDataDemultiplexer demultiplexer = Mockito.mock(KaaDataDemultiplexer.class); DefaultBootstrapChannelFake channel = new DefaultBootstrapChannelFake(client, state, failoverManager, 0); channel.setDemultiplexer(demultiplexer); channel.setMultiplexer(multiplexer); channel.shutdown(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo(ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); channel.setServer(server); channel.sync(TransportType.BOOTSTRAP); channel.syncAll(); channel.verify(); }
@Test public void testShutdown() throws Exception { KaaChannelManager manager = Mockito.mock(KaaChannelManager.class); AbstractHttpClient httpClient = Mockito.mock(AbstractHttpClient.class); FailoverManager failoverManager = Mockito.mock(FailoverManager.class); Mockito.when(httpClient.executeHttpRequest(Mockito.anyString(), Mockito.any(LinkedHashMap.class), Mockito.anyBoolean())).thenThrow( new Exception()); AbstractKaaClient client = Mockito.mock(AbstractKaaClient.class); Mockito.when( client.createHttpClient(Mockito.anyString(), Mockito.any(PrivateKey.class), Mockito.any(PublicKey.class), Mockito.any(PublicKey.class))).thenReturn(httpClient); Mockito.when(client.getChannelManager()).thenReturn(manager); KaaClientState state = Mockito.mock(KaaClientState.class); KaaDataMultiplexer multiplexer = Mockito.mock(KaaDataMultiplexer.class); KaaDataDemultiplexer demultiplexer = Mockito.mock(KaaDataDemultiplexer.class); DefaultOperationHttpChannelFake channel = new DefaultOperationHttpChannelFake(client, state, failoverManager, 0); channel.syncAll(); channel.setDemultiplexer(demultiplexer); channel.setMultiplexer(multiplexer); channel.shutdown(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo(ServerType.OPERATIONS, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); channel.setServer(server); channel.sync(TransportType.EVENT); channel.syncAll(); channel.verify(); }
@Test public void testShutdown() throws Exception { KaaChannelManager manager = Mockito.mock(KaaChannelManager.class); AbstractHttpClient httpClient = Mockito.mock(AbstractHttpClient.class); FailoverManager failoverManager = Mockito.mock(FailoverManager.class); Mockito.when( httpClient.executeHttpRequest(Mockito.anyString(), Mockito.any(LinkedHashMap.class), Mockito.anyBoolean())).thenThrow(new Exception()); AbstractKaaClient client = Mockito.mock(AbstractKaaClient.class); Mockito.when( client.createHttpClient(Mockito.anyString(), Mockito.any(PrivateKey.class), Mockito.any(PublicKey.class), Mockito.any(PublicKey.class))).thenReturn(httpClient); Mockito.when(client.getChannelManager()).thenReturn(manager); KaaClientState state = Mockito.mock(KaaClientState.class); KaaDataMultiplexer multiplexer = Mockito.mock(KaaDataMultiplexer.class); KaaDataDemultiplexer demultiplexer = Mockito.mock(KaaDataDemultiplexer.class); DefaultOperationsChannelFake channel = new DefaultOperationsChannelFake(client, state, failoverManager, 0); channel.syncAll(); channel.setDemultiplexer(demultiplexer); channel.setMultiplexer(multiplexer); channel.shutdown(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo(ServerType.OPERATIONS, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); channel.setServer(server); channel.sync(TransportType.EVENT); channel.syncAll(); channel.verify(); }
@Test public void testChannelSync() throws Exception { KaaChannelManager manager = Mockito.mock(KaaChannelManager.class); AbstractHttpClient httpClient = Mockito.mock(AbstractHttpClient.class); FailoverManager failoverManager = Mockito.mock(FailoverManager.class); Mockito.when(httpClient.executeHttpRequest(Mockito.anyString(), Mockito.any(LinkedHashMap.class), Mockito.anyBoolean())) .thenReturn(new byte[]{5, 5, 5}); AbstractKaaClient client = Mockito.mock(AbstractKaaClient.class); Mockito.when( client.createHttpClient(Mockito.anyString(), Mockito.any(PrivateKey.class), Mockito.any(PublicKey.class), Mockito.any(PublicKey.class))).thenReturn(httpClient); Mockito.when(client.getChannelManager()).thenReturn(manager); KaaClientState state = Mockito.mock(KaaClientState.class); KaaDataMultiplexer multiplexer = Mockito.mock(KaaDataMultiplexer.class); KaaDataDemultiplexer demultiplexer = Mockito.mock(KaaDataDemultiplexer.class); DefaultBootstrapChannelMock channel = new DefaultBootstrapChannelMock(client, state, failoverManager, 2); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo(ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); channel.setServer(server); channel.sync(TransportType.BOOTSTRAP); channel.setDemultiplexer(demultiplexer); channel.setDemultiplexer(null); channel.sync(TransportType.BOOTSTRAP); channel.setMultiplexer(multiplexer); channel.setMultiplexer(null); channel.sync(TransportType.CONFIGURATION); channel.sync(TransportType.BOOTSTRAP); channel.verify(); }
Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = new HashMap<>(); TransportConnectionInfo server = IpTransportInfoTest.createTestServerInfo( ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()); bootststrapServers.put(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, Collections.singletonList(server));
@Test public void testDefaultStrategyKeys() throws IOException, InvalidKeyException, NoSuchAlgorithmException { KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties()); PersistentStorage storage = new FilePersistentStorage(); String clientPrivateKeyFileLocation = getProperties().getPrivateKeyFileFullName(); String clientPublicKeyFileLocation = getProperties().getPublicKeyFileFullName(); OutputStream privateKeyOutput = storage.openForWrite(clientPrivateKeyFileLocation); OutputStream publicKeyOutput = storage.openForWrite(clientPublicKeyFileLocation); KeyPair keyPair = KeyUtil.generateKeyPair(privateKeyOutput, publicKeyOutput); assertArrayEquals(keyPair.getPrivate().getEncoded(), state.getPrivateKey().getEncoded()); assertArrayEquals(keyPair.getPublic().getEncoded(), state.getPublicKey().getEncoded()); //clean new File(WORK_DIR + KEY_PUBLIC).delete(); new File(WORK_DIR + KEY_PRIVATE).delete(); }
Map<TransportProtocolId, List<TransportConnectionInfo>> bootststrapServers = new HashMap<>(); bootststrapServers.put(TransportProtocolIdConstants.HTTP_TRANSPORT_ID, Collections.singletonList(IpTransportInfoTest.createTestServerInfo( ServerType.BOOTSTRAP, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9889, KeyUtil.generateKeyPair().getPublic()))); BootstrapManager bootstrapManager = Mockito.mock(BootstrapManager.class); ServerType.OPERATIONS, TransportProtocolIdConstants.HTTP_TRANSPORT_ID, "localhost", 9999, KeyUtil.generateKeyPair().getPublic()); channelManager.onTransportConnectionInfoUpdated(server); Mockito.verify(failoverManager, Mockito.times(1)).onServerChanged(Mockito.any(TransportConnectionInfo.class));
@Test(expected = KaaRuntimeException.class) public void testDefaultStrategyRecreateKeys() throws IOException, InvalidKeyException, NoSuchAlgorithmException { KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties()); PersistentStorage storage = new FilePersistentStorage(); String clientPrivateKeyFileLocation = getProperties().getPrivateKeyFileFullName(); String clientPublicKeyFileLocation = getProperties().getPublicKeyFileFullName(); OutputStream privateKeyOutput = storage.openForWrite(clientPrivateKeyFileLocation); OutputStream publicKeyOutput = storage.openForWrite(clientPublicKeyFileLocation); KeyPair keyPair = KeyUtil.generateKeyPair(privateKeyOutput, publicKeyOutput); assertArrayEquals(keyPair.getPrivate().getEncoded(), state.getPrivateKey().getEncoded()); assertArrayEquals(keyPair.getPublic().getEncoded(), state.getPublicKey().getEncoded()); File pub = new File(WORK_DIR + KEY_PUBLIC); File priv = new File(WORK_DIR + KEY_PRIVATE); //clean Files.delete(Paths.get(WORK_DIR + KEY_PUBLIC)); new File(WORK_DIR + KEY_PRIVATE).delete(); state.clean(); state.getPublicKey(); state.getPrivateKey(); }
@Test public void testInit() throws NoSuchAlgorithmException { IpTransportInfo info = new IpTransportInfo(createTestServerInfo(ServerType.OPERATIONS, TransportProtocolIdConstants.TCP_TRANSPORT_ID, "localhost", 80, KeyUtil.generateKeyPair().getPublic())); Assert.assertEquals(ServerType.OPERATIONS, info.getServerType()); Assert.assertEquals(TransportProtocolIdConstants.TCP_TRANSPORT_ID, info.getTransportId()); Assert.assertEquals("localhost", info.getHost()); Assert.assertEquals(80, info.getPort()); }