use of com.icodici.crypto.SymmetricKey in project universa by UniversaBlockchain.
the class DatagramAdapterTest method createNodeToMany.
@Test
public void createNodeToMany() throws Exception {
int numNodes = 100;
NodeInfo node1 = new NodeInfo(TestKeys.publicKey(0), 1, "test_node_1", "localhost", 16201, 16202, 16301);
List<NodeInfo> nodeInfos = new ArrayList<>();
nodeInfos.add(node1);
NetConfig nc = new NetConfig(nodeInfos);
SymmetricKey symmetricKey1 = new SymmetricKey();
// create implemented class with node1
DatagramAdapter d1 = new UDPAdapter(TestKeys.privateKey(0), symmetricKey1, node1, nc);
List symmetricKeyErrors = new ArrayList();
d1.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
byte[] payload = "test data set 1".getBytes();
int attempts = 5;
int numSends = 5;
ArrayList<byte[]> receviedFor = new ArrayList<>();
AsyncEvent<Void> ae = new AsyncEvent<>();
d1.receive(data -> {
receviedFor.add(data);
if ((receviedFor.size()) == attempts * numSends * numNodes)
ae.fire();
});
List<NodeInfo> nodes = new ArrayList<>();
List<DatagramAdapter> adapters = new ArrayList<>();
for (int i = 0; i < numNodes; i++) {
int keyIndex = new Random().nextInt(2);
NodeInfo n = new NodeInfo(TestKeys.publicKey(keyIndex), 2 + i, "test_node_r_" + i, "localhost", 16203 + i, 16204 + i, 16302 + i);
nc.addNode(n);
nodes.add(n);
SymmetricKey sk = new SymmetricKey();
// create implemented class with node1
DatagramAdapter d = new UDPAdapter(TestKeys.privateKey(keyIndex), sk, n, nc);
adapters.add(d);
d.addErrorsCallback(m -> {
System.err.println(m);
if (m.indexOf("SymmetricKey.AuthenticationFailed") >= 0)
symmetricKeyErrors.add(m);
return m;
});
d.receive(data -> {
receviedFor.add(data);
if ((receviedFor.size()) == attempts * numSends * numNodes)
ae.fire();
});
}
for (int i = 0; i < attempts; i++) {
System.out.println("Send part: " + i);
// send from adapter d1, to d2 as it is connected with node2 credentials:
for (int j = 0; j < numSends; j++) {
for (int k = 0; k < numNodes; k++) {
d1.send(nodes.get(k), payload);
}
}
Thread.sleep(new Random().nextInt(200));
// if(new Random().nextBoolean()) ((UDPAdapter)d1).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d2).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d3).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d4).brakeSessions();
// if(new Random().nextBoolean()) ((UDPAdapter)d5).brakeSessions();
}
try {
ae.await(30000);
} catch (TimeoutException e) {
System.out.println("time is up");
}
assertEquals(0, symmetricKeyErrors.size());
System.out.println("all got: " + (receviedFor.size()));
d1.shutdown();
for (int i = 0; i < numNodes; i++) {
adapters.get(i).shutdown();
}
assertEquals(attempts * numSends * numNodes, receviedFor.size());
}
Aggregations