Search in sources :

Example 16 with SymmetricKey

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());
}
Also used : ArrayList(java.util.ArrayList) SymmetricKey(com.icodici.crypto.SymmetricKey) AsyncEvent(net.sergeych.tools.AsyncEvent) Random(java.util.Random) NodeInfo(com.icodici.universa.node2.NodeInfo) NetConfig(com.icodici.universa.node2.NetConfig) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

SymmetricKey (com.icodici.crypto.SymmetricKey)16 NetConfig (com.icodici.universa.node2.NetConfig)13 NodeInfo (com.icodici.universa.node2.NodeInfo)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)11 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)9 Random (java.util.Random)5 TimeoutException (java.util.concurrent.TimeoutException)5 AsyncEvent (net.sergeych.tools.AsyncEvent)5 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 BasicHttpClientSession (com.icodici.universa.node2.network.BasicHttpClientSession)1 IOException (java.io.IOException)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 OptionSet (joptsimple.OptionSet)1 Binder (net.sergeych.tools.Binder)1