Beispiel #1
0
  @Test
  public void testUserRegistry() throws CryptoException, L2pSecurityException, AgentException {
    UserAgent a = UserAgent.createUserAgent("a");
    UserAgent b = UserAgent.createUserAgent("b");

    a.unlockPrivateKey("a");
    b.unlockPrivateKey("b");

    a.setLoginName("alpha");
    b.setLoginName("beta");

    LocalNode testee = LocalNode.launchNode();
    testee.storeAgent(a);
    testee.storeAgent(b);

    assertEquals(a.getId(), testee.getAgentIdForLogin("alpha"));
    assertEquals(b.getId(), testee.getAgentIdForLogin("beta"));

    try {
      testee.getAgentIdForLogin("bla");
      fail("AgentNotKnownException expected");
    } catch (AgentNotKnownException e) {
      // corrects
    }
  }
Beispiel #2
0
  @Test
  public void testPending()
      throws L2pSecurityException, EncodingFailedException, SerializationException,
          InterruptedException, AgentException {
    adam.unlockPrivateKey("adamspass");
    eve.unlockPrivateKey("evespass");

    LocalNode testee = LocalNode.launchAgent(adam);

    MessageResultListener l =
        new MessageResultListener(4000) {
          @Override
          public void notifySuccess() {
            LocalNodeTest.testVariable = true;
          }
        };

    Message m = new Message(adam, eve, new PingPongContent());
    testee.sendMessage(m, l);

    Thread.sleep(2000);

    assertFalse(testVariable);
    assertFalse(l.isSuccess());
    assertFalse(l.isFinished());

    // launch another node hosting eve
    LocalNode.launchAgent(eve);
    Thread.sleep(LocalNode.getMaxMessageWait() + 3000);

    assertTrue(l.isSuccess());
    assertTrue(l.isFinished());
    assertTrue(testVariable);
  }
Beispiel #3
0
  @Test
  public void testTwoNodes()
      throws L2pSecurityException, EncodingFailedException, SerializationException,
          InterruptedException, AgentException {
    adam.unlockPrivateKey("adamspass");
    eve.unlockPrivateKey("evespass");

    // launch to nodes with one agent each
    LocalNode testee1 = LocalNode.launchAgent(adam);
    LocalNode.launchAgent(eve);

    assertTrue(LocalNode.findAllNodesWithAgent(adam.getId()).length > 0);
    assertTrue(LocalNode.findAllNodesWithAgent(eve.getId()).length > 0);

    MessageResultListener l = new MessageResultListener(10000);
    Message m = new Message(adam, eve, new PingPongContent());

    testee1.sendMessage(m, l);

    l.waitForAllAnswers();

    assertEquals(1, l.getNumberOfExpectedResults());
    assertTrue(l.isFinished());
    assertTrue(l.isSuccess());
  }
Beispiel #4
0
  @Test
  public void test()
      throws AgentAlreadyRegisteredException, EncodingFailedException, L2pSecurityException,
          SerializationException, InterruptedException, AgentException, AgentNotKnownException {
    System.out.println("start: " + new Date());

    LocalNode testee = LocalNode.launchNode();

    try {
      testee.registerReceiver(eve);
      fail("L2pSecurityException expected");
    } catch (L2pSecurityException e) {
    }

    eve.unlockPrivateKey("evespass");
    adam.unlockPrivateKey("adamspass");

    testee.registerReceiver(eve);
    testee.registerReceiver(adam);

    assertFalse(eve.isLocked());
    assertFalse(adam.isLocked());

    System.out.println("check1: " + new Date());

    testVariable = false;
    MessageResultListener listener =
        new MessageResultListener(10000) {
          public void notifySuccess() {
            LocalNodeTest.testVariable = true;
          }
        };

    PingPongContent c = new PingPongContent();
    Message m = new Message(adam, eve, c);

    testee.sendMessage(m, listener);

    listener.waitForAllAnswers();

    assertFalse(listener.isTimedOut());
    assertFalse(listener.hasException());
    assertTrue(listener.isSuccess());
    assertTrue(listener.isFinished());

    Message answer = listener.getResults()[0];
    answer.open(adam, testee);
    assertTrue(c.getTimestamp() < ((PingPongContent) answer.getContent()).getTimestamp());
    assertTrue(testVariable);
  }
  /**
   * Called before the tests start.
   *
   * <p>Sets up the node and initializes connector and users that can be used throughout the tests.
   *
   * @throws Exception
   */
  @BeforeClass
  public static void startServer() throws Exception {

    // start node
    node = LocalNode.newNode();
    testAgent = MockAgentFactory.getAdam();
    testAgent.unlockPrivateKey(testPass); // agent must be unlocked in order to be stored
    node.storeAgent(testAgent);
    node.launch();

    ServiceAgent testService = ServiceAgent.createServiceAgent(testTemplateService, "a pass");
    testService.unlockPrivateKey("a pass");

    node.registerReceiver(testService);

    // start connector
    logStream = new ByteArrayOutputStream();

    connector = new WebConnector(true, HTTP_PORT, false, 1000);
    connector.setLogStream(new PrintStream(logStream));
    connector.start(node);
    Thread.sleep(1000); // wait a second for the connector to become ready
    testAgent = MockAgentFactory.getAdam();

    connector.updateServiceList();
    // avoid timing errors: wait for the repository manager to get all services before continuing
    try {
      System.out.println("waiting..");
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Beispiel #6
0
  @Test
  public void testSimpleInvokation()
      throws L2pSecurityException, CryptoException, AgentAlreadyRegisteredException, AgentException,
          SecurityException, IllegalArgumentException, AgentNotKnownException,
          NoSuchMethodException, IllegalAccessException, InvocationTargetException,
          InterruptedException {
    String serviceClass = "i5.las2peer.api.TestService";
    ServiceAgent testService = ServiceAgent.generateNewAgent(serviceClass, "a passphrase");
    testService.unlockPrivateKey("a passphrase");

    LocalNode testee = LocalNode.launchNode();

    eve.unlockPrivateKey("evespass");
    testee.storeAgent(eve);
    eve.lockPrivateKey();

    testee.storeAgent(testService);
    testee.registerReceiver(testService);

    Serializable result =
        testee.invokeLocally(
            eve.getId(), serviceClass, "inc", new Serializable[] {new Integer(10)});

    assertEquals(12, result);
  }
Beispiel #7
0
  @Test
  public void testTimeout()
      throws EncodingFailedException, L2pSecurityException, SerializationException,
          InterruptedException, AgentException {
    adam.unlockPrivateKey("adamspass");

    LocalNode testee1 = LocalNode.launchAgent(adam);
    MessageResultListener l =
        new MessageResultListener(2000) {
          public void notifyTimeout() {
            LocalNodeTest.testVariable = true;
          }
        };
    Message m = new Message(adam, eve, new PingPongContent(), 1000);

    LocalNode.setPendingTimeOut(1000);

    testee1.sendMessage(m, l);

    Thread.sleep(30000);

    assertFalse(l.isSuccess());
    assertTrue(l.isTimedOut());
    assertEquals(0, l.getResults().length);
    assertTrue(testVariable);
  }
Beispiel #8
0
  @Test
  public void testBroadcast()
      throws EncodingFailedException, L2pSecurityException, SerializationException,
          InterruptedException, AgentException, AgentNotKnownException {
    adam.unlockPrivateKey("adamspass");
    eve.unlockPrivateKey("evespass");

    // launch three nodes with one agent each
    LocalNode testee1 = LocalNode.launchAgent(adam);
    LocalNode hosting1 = LocalNode.launchAgent(eve);
    assertEquals(1, LocalNode.findAllNodesWithAgent(eve.getId()).length);

    LocalNode hosting2 = LocalNode.launchAgent(eve);

    assertTrue(hosting1.hasLocalAgent(eve));
    assertTrue(hosting2.hasLocalAgent(eve));

    assertNotSame(hosting1.getAgent(eve.getId()), hosting2.getAgent(eve.getId()));

    assertEquals(2, LocalNode.findAllNodesWithAgent(eve.getId()).length);

    MessageResultListener l =
        new MessageResultListener(10000) {
          public void notifySuccess() {
            synchronized (this) {
              System.out.println("result retrieved");
              LocalNodeTest.counter++;
            }
          }
        };
    // l.addRecipient();
    assertEquals(1, l.getNumberOfExpectedResults());

    Message m = new Message(adam, eve, new PingPongContent());
    testee1.sendMessage(m, l, Node.SendMode.BROADCAST);
    assertEquals(2, l.getNumberOfExpectedResults());

    l.waitForAllAnswers();

    assertEquals(2, l.getNumberOfResults());
    assertEquals(counter, 2);
    assertTrue(l.isSuccess());
    assertTrue(l.isFinished());
  }
Beispiel #9
0
  @Test
  public void testRegisteringAgents()
      throws L2pSecurityException, MalformedXMLException, IOException,
          AgentAlreadyRegisteredException, AgentNotKnownException, AgentException {
    adam.unlockPrivateKey("adamspass");
    // abel.unlockPrivateKey ( "abelspass");

    LocalNode testee = LocalNode.launchAgent(adam);

    // testee.storeAgent(abel);

    try {
      testee.storeAgent(abel);
      fail("L2sSecurityAxception expected");
    } catch (L2pSecurityException e) {
    }

    try {
      testee.storeAgent(adam);
      fail("AgentAlreadyRegistered exception expected");
    } catch (AgentAlreadyRegisteredException e) {
    }

    abel.unlockPrivateKey("abelspass");
    testee.storeAgent(abel);

    LocalNode testee2 = LocalNode.launchNode();

    UserAgent retrieve = (UserAgent) testee2.getAgent(abel.getId());
    assertTrue(retrieve.isLocked());

    try {
      testee2.updateAgent(retrieve);
      fail("SecurtityException expected");
    } catch (L2pSecurityException e) {
    }

    retrieve.unlockPrivateKey("abelspass");
    testee2.updateAgent(retrieve);
  }
Beispiel #10
0
  @Test
  public void testStartupAgents() throws L2pSecurityException, AgentException {

    LocalNode testee = LocalNode.newNode();
    testee.storeAgent(adam);

    testee.launch();

    try {
      testee.storeAgent(abel);
      fail("L2pSecurityException expected");
    } catch (L2pSecurityException e) {
      // ok
    }

    abel.unlockPrivateKey("abelspass");
    testee.storeAgent(abel);
  }
  @Before
  public void startServer() throws Exception {
    // start Node
    node = LocalNode.newNode();

    adam = MockAgentFactory.getAdam();
    adam.unlockPrivateKey(adamsPass);
    node.storeAgent(adam);

    node.launch();

    ServiceAgent testService = ServiceAgent.createServiceAgent(testServiceClass, "a pass");
    testService.unlockPrivateKey("a pass");

    node.registerReceiver(testService);

    // start connector
    logStream = new ByteArrayOutputStream();
    connector = new HttpConnector();
    connector.setSocketTimeout(10000);
    connector.setLogStream(new PrintStream(logStream));
    connector.start(node);
  }
Beispiel #12
0
  @Test
  public void testUserRegDistribution()
      throws L2pSecurityException, AgentException, CryptoException, ArtifactNotFoundException {
    LocalNode testee1 = LocalNode.launchNode();

    long id = Envelope.getClassEnvelopeId(UserAgentList.class, "mainlist");

    for (int i = 0; i < 11; i++) {
      UserAgent a = UserAgent.createUserAgent("pass" + i);
      a.unlockPrivateKey("pass" + i);
      a.setLoginName("login_" + i);
      testee1.storeAgent(a);
    }

    // check, that the user list is stored
    testee1.fetchArtifact(id);

    LocalNode testee2 = LocalNode.launchNode();

    // check, that the user list is stored
    testee2.fetchArtifact(id);

    testee2.getAgentIdForLogin("login_2");
  }