Package org.hive2hive.core.network

Examples of org.hive2hive.core.network.NetworkManager


  @Test
  public void testStepSuccess() throws InterruptedException, InvalidKeySpecException, DataLengthException,
      IllegalStateException, InvalidCipherTextException, ClassNotFoundException, IOException,
      NoPeerConnectionException, GetFailedException {
    NetworkManager putter = network.get(0); // where the process runs

    // create the needed objects
    UserCredentials credentials = NetworkTestUtil.generateRandomCredentials();

    UserProfile testProfile = new UserProfile(credentials.getUserId());

    // add them already to the DHT
    SecretKey encryptionKeys = PasswordUtil.generateAESKeyFromPassword(credentials.getPassword(), credentials.getPin(),
        H2HConstants.KEYLENGTH_USER_PROFILE);
    EncryptedNetworkContent encrypted = H2HEncryptionUtil.encryptAES(testProfile, encryptionKeys);
    FuturePut putGlobal = putter.getDataManager().putUnblocked(
        new Parameters().setLocationKey(credentials.getProfileLocationKey())
            .setContentKey(H2HConstants.USER_PROFILE).setData(encrypted));
    putGlobal.awaitUninterruptibly();

    UserProfile profile = UseCaseTestUtil.getUserProfile(putter, credentials);
View Full Code Here


   */
  @Test
  public void baseMessageProcessStepTestWithARequestMessage() throws ClassNotFoundException, IOException,
      NoPeerConnectionException {
    // select two random nodes
    final NetworkManager nodeA = network.get(random.nextInt(network.size() / 2));
    final NetworkManager nodeB = network.get(random.nextInt(network.size() / 2) + network.size() / 2);
    // generate a random content key
    final String contentKey = NetworkTestUtil.randomString();
    final Parameters parametersA = new Parameters().setLocationKey(nodeA.getNodeId()).setContentKey(contentKey);
    final Parameters parametersB = new Parameters().setLocationKey(nodeB.getNodeId()).setContentKey(contentKey);
   
    // check if selected locations are empty
    FutureGet futureGet = nodeA.getDataManager().getUnblocked(parametersB);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());
    futureGet = nodeB.getDataManager().getUnblocked(parametersA);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());

    // create a message with target node B
    final TestMessageWithReply message = new TestMessageWithReply(nodeB.getNodeId(), contentKey);

    // initialize the process and the one and only step to test
    BaseMessageProcessStep step = new BaseMessageProcessStep(nodeA.getMessageManager()) {

      @Override
View Full Code Here

  }

  @Test
  public void testValidCredentials() throws ClassNotFoundException, IOException, NoSessionException,
      NoPeerConnectionException {
    NetworkManager client = network.get(new Random().nextInt(networkSize));

    // login with valid credentials
    UseCaseTestUtil.login(userCredentials, client, root);

    Assert.assertNotNull(client.getSession());
    Assert.assertEquals(userCredentials.getUserId(), client.getUserId());

    // verify the locations map
    FutureGet futureGet = client.getDataManager().getUnblocked(
        new Parameters().setLocationKey(userCredentials.getUserId()).setContentKey(H2HConstants.USER_LOCATIONS));
    futureGet.awaitUninterruptibly();

    Locations locations = (Locations) futureGet.getData().object();
    Assert.assertEquals(1, locations.getPeerAddresses().size());
View Full Code Here

    loginAndWaitToFail(wrongCredentials);
  }

  public H2HSession loginAndWaitToFail(UserCredentials wrongCredentials) throws InvalidProcessStateException,
      NoSessionException, NoPeerConnectionException {
    NetworkManager client = network.get(new Random().nextInt(networkSize));
    SessionParameters sessionParameters = new SessionParameters();
    sessionParameters.setProfileManager(new UserProfileManager(client.getDataManager(), wrongCredentials));

    IProcessComponent loginProcess = ProcessFactory.instance().createLoginProcess(wrongCredentials, sessionParameters,
        client);
    TestProcessComponentListener listener = new TestProcessComponentListener();
    loginProcess.attachListener(listener);
    loginProcess.start();

    UseCaseTestUtil.waitTillFailed(listener, 20);

    return client.getSession();
  }
View Full Code Here

   */
  @Test
  public void baseDirectMessageProcessStepTestOnSuccess() throws ClassNotFoundException, IOException,
      NoPeerConnectionException {
    // select two random nodes
    NetworkManager nodeA = network.get(random.nextInt(network.size() / 2));
    final NetworkManager nodeB = network.get(random.nextInt(network.size() / 2) + network.size() / 2);
    // generate random data and content key
    String data = NetworkTestUtil.randomString();
    String contentKey = NetworkTestUtil.randomString();
    Parameters parameters = new Parameters().setLocationKey(nodeB.getNodeId()).setContentKey(contentKey);

    // check if selected location is empty
    FutureGet futureGet = nodeA.getDataManager().getUnblocked(parameters);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());

    // create a message with target node B
    final TestDirectMessage message = new TestDirectMessage(nodeB.getNodeId(), nodeB.getConnection()
        .getPeer().getPeerAddress(), contentKey, new H2HTestData(data), false);

    // initialize the process and the one and only step to test
    BaseDirectMessageProcessStep step = new BaseDirectMessageProcessStep(nodeA.getMessageManager()) {
      @Override
      public void handleResponseMessage(ResponseMessage responseMessage) {
        Assert.fail("Should be not used.");
      }

      @Override
      protected void doExecute() throws InvalidProcessStateException {
        try {
          sendDirect(message, getPublicKey(nodeB));
        } catch (SendFailedException e) {
          Assert.fail();
        }
      }
    };
    UseCaseTestUtil.executeProcess(step);

    // wait till message gets handled
    H2HWaiter w = new H2HWaiter(10);
    do {
      w.tickASecond();
      futureGet = nodeB.getDataManager().getUnblocked(parameters);
      futureGet.awaitUninterruptibly();
    } while (futureGet.getData() == null);

    // verify that data arrived
    String result = ((H2HTestData) futureGet.getData().object()).getTestString();
View Full Code Here

   */
  @Test
  public void baseDirectMessageProcessStepTestOnFailure() throws NoPeerConnectionException,
      InvalidProcessStateException {
    // select two random nodes
    NetworkManager nodeA = network.get(random.nextInt(network.size() / 2));
    final NetworkManager nodeB = network.remove(random.nextInt(network.size() / 2) + network.size() / 2);
    // generate random data and content key
    String data = NetworkTestUtil.randomString();
    String contentKey = NetworkTestUtil.randomString();
    Parameters parameters = new Parameters().setLocationKey(nodeB.getNodeId()).setContentKey(contentKey);

    // check if selected location is empty
    FutureGet futureGet = nodeA.getDataManager().getUnblocked(parameters);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());

    // assign a denying message handler at target node
    nodeB.getConnection().getPeer().setObjectDataReply(new DenyingMessageReplyHandler());

    // create a message with target node B
    final TestDirectMessage message = new TestDirectMessage(nodeB.getNodeId(), nodeB.getConnection()
        .getPeer().getPeerAddress(), contentKey, new H2HTestData(data), false);

    // initialize the process and the one and only step to test
    BaseDirectMessageProcessStep step = new BaseDirectMessageProcessStep(nodeA.getMessageManager()) {
      @Override
      public void handleResponseMessage(ResponseMessage responseMessage) {
        Assert.fail("Should be not used.");
      }

      @Override
      protected void doExecute() throws InvalidProcessStateException, ProcessExecutionException {
        try {
          sendDirect(message, getPublicKey(nodeB));
          Assert.fail();
        } catch (SendFailedException e) {
          throw new ProcessExecutionException("Expected behavior.", e);
        }
      }
    };
    TestProcessComponentListener listener = new TestProcessComponentListener();
    step.attachListener(listener);
    step.start();
    // wait for the process to finish
    UseCaseTestUtil.waitTillFailed(listener, 10);

    // check if selected location is still empty
    futureGet = nodeB.getDataManager().getUnblocked(parameters);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());
  }
View Full Code Here

   */
  @Test
  public void baseDirectMessageProcessStepTestWithARequestMessage() throws ClassNotFoundException,
      IOException, NoPeerConnectionException {
    // select two random nodes
    final NetworkManager nodeA = network.get(random.nextInt(network.size() / 2));
    final NetworkManager nodeB = network.get(random.nextInt(network.size() / 2) + network.size() / 2);
    // generate a random content key
    final String contentKey = NetworkTestUtil.randomString();
    final Parameters parametersA = new Parameters().setLocationKey(nodeA.getNodeId()).setContentKey(
        contentKey);
    final Parameters parametersB = new Parameters().setLocationKey(nodeB.getNodeId()).setContentKey(
        contentKey);

    // check if selected locations are empty
    FutureGet futureGet = nodeB.getDataManager().getUnblocked(parametersA);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());
    futureGet = nodeA.getDataManager().getUnblocked(parametersB);
    futureGet.awaitUninterruptibly();
    assertNull(futureGet.getData());

    // create a message with target node B
    final TestDirectMessageWithReply message = new TestDirectMessageWithReply(nodeB.getConnection()
        .getPeer().getPeerAddress(), contentKey);

    // initialize the process and the one and only step to test
    BaseDirectMessageProcessStep step = new BaseDirectMessageProcessStep(nodeA.getMessageManager()) {
      @Override
      public void handleResponseMessage(ResponseMessage responseMessage) {
        // locally store on requesting node received data
        String receivedSecret = (String) responseMessage.getContent();
        try {
          nodeA.getDataManager().putUnblocked(parametersA.setData(new H2HTestData(receivedSecret)))
              .awaitUninterruptibly();
        } catch (NoPeerConnectionException e) {
          Assert.fail();
        }
      }

      @Override
      protected void doExecute() throws InvalidProcessStateException {
        try {
          sendDirect(message, getPublicKey(nodeB));
        } catch (SendFailedException e) {
          Assert.fail();
        }
      }
    };
    UseCaseTestUtil.executeProcess(step);

    // wait till response message gets handled
    H2HWaiter waiter = new H2HWaiter(10);
    do {
      waiter.tickASecond();
      futureGet = nodeA.getDataManager().getUnblocked(parametersA);
      futureGet.awaitUninterruptibly();
    } while (futureGet.getData() == null);

    // load and verify if same secret was shared
    String receivedSecret = ((H2HTestData) futureGet.getData().object()).getTestString();
    futureGet = nodeB.getDataManager().getUnblocked(parametersB);
    futureGet.awaitUninterruptibly();
    String originalSecret = ((H2HTestData) futureGet.getData().object()).getTestString();
    assertEquals(originalSecret, receivedSecret);
  }
View Full Code Here

  @Before
  public void setupFiles() throws IOException, IllegalFileLocation, NoSessionException,
      NoPeerConnectionException {
    network = NetworkTestUtil.createNetwork(networkSize);
    NetworkManager uploader = network.get(0);

    // create two root directories
    root0 = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
    root1 = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());

    // first, register and login a new user
    userCredentials = NetworkTestUtil.generateRandomCredentials();
    UseCaseTestUtil.registerAndLogin(userCredentials, uploader, root0);

    // create default tree that can be used later. Upload it to the DHT
    // - file 1
    // - file 2
    // - folder 1
    // - - file 3
    File file1 = new File(root0, "file 1");
    FileUtils.writeStringToFile(file1, NetworkTestUtil.randomString());
    UseCaseTestUtil.uploadNewFile(uploader, file1);

    File file2 = new File(root0, "file 2");
    FileUtils.writeStringToFile(file2, NetworkTestUtil.randomString());
    UseCaseTestUtil.uploadNewFile(uploader, file2);

    File folder1 = new File(root0, "folder 1");
    folder1.mkdir();
    UseCaseTestUtil.uploadNewFile(uploader, folder1);

    File file3 = new File(folder1, "file 3");
    FileUtils.writeStringToFile(file3, NetworkTestUtil.randomString());
    UseCaseTestUtil.uploadNewFile(uploader, file3);

    // copy the content to the other client such that they are in sync
    FileUtils.copyDirectory(root0, root1);

    // write both versions to disc
    FileUtil.writePersistentMetaData(uploader.getSession().getRoot(), null, null);
    FileUtil.writePersistentMetaData(root1.toPath(), null, null);
  }
View Full Code Here

    // delete file 1
    File file1 = new File(root1, "file 1");
    file1.delete();

    /** do some modifications on the remote **/
    NetworkManager remoteClient = network.get(0);

    // add a file 4 within folder 1
    File file4 = new File(new File(root0, "folder 1"), "file 4");
    FileUtils.write(file4, NetworkTestUtil.randomString());
    UseCaseTestUtil.uploadNewFile(remoteClient, file4);
View Full Code Here

    File folder = new File(root1, "folder 1");
    File file3 = new File(folder, "file 3");
    FileUtils.write(file3, NetworkTestUtil.randomString());

    /** do some modifications on the remote **/
    NetworkManager remoteClient = network.get(0);

    // modify file 2
    File file2 = new File(root0, "file 2");
    String file2Content = NetworkTestUtil.randomString();
    FileUtils.write(file2, file2Content);
View Full Code Here

TOP

Related Classes of org.hive2hive.core.network.NetworkManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.