Search in sources :

Example 1 with Jid

use of rocks.xmpp.addr.Jid in project com.juick.xmpp by juick.

the class StreamComponentServer method handshake.

@Override
public void handshake() throws XmlPullParserException, IOException {
    parser.next();
    if (!parser.getName().equals("stream") || !parser.getNamespace(null).equals(NS_COMPONENT_ACCEPT) || !parser.getNamespace("stream").equals(NS_STREAM)) {
        throw new IOException("invalid stream");
    }
    Jid domain = Jid.of(parser.getAttributeValue(null, "to"));
    if (listenersStream.stream().anyMatch(l -> l.filter(null, domain))) {
        send(new XMPPError(XMPPError.Type.cancel, "forbidden").toString());
        throw new IOException("invalid domain");
    }
    from = domain;
    to = domain;
    send(String.format("<stream:stream xmlns:stream='%s' " + "xmlns='%s' from='%s' id='%s'>", NS_STREAM, NS_COMPONENT_ACCEPT, from.asBareJid().toEscapedString(), streamId));
    Handshake handshake = Handshake.parse(parser);
    boolean authenticated = handshake.getValue().equals(DigestUtils.sha1Hex(streamId + secret));
    setLoggedIn(authenticated);
    if (!authenticated) {
        send(new XMPPError(XMPPError.Type.cancel, "not-authorized").toString());
        for (StreamListener streamListener : listenersStream) {
            streamListener.fail(new IOException("stream:stream, failed authentication"));
        }
        return;
    }
    send(new Handshake().toString());
    listenersStream.forEach(StreamListener::ready);
}
Also used : Jid(rocks.xmpp.addr.Jid) IOException(java.io.IOException) XMPPError(com.juick.xmpp.extensions.XMPPError) Handshake(com.juick.xmpp.extensions.Handshake)

Example 2 with Jid

use of rocks.xmpp.addr.Jid in project com.juick.xmpp by juick.

the class ComponentTests method componentTest.

@Test
public void componentTest() throws IOException, InterruptedException, XmlPullParserException {
    ServerSocket serverSocket = mock(ServerSocket.class);
    Socket client = mock(Socket.class);
    Socket server = mock(Socket.class);
    when(serverSocket.accept()).thenReturn(server);
    PipedInputStream serverInputStream = new PipedInputStream();
    PipedOutputStream clientOutputStream = new PipedOutputStream(serverInputStream);
    PipedInputStream clientInputStream = new PipedInputStream();
    PipedOutputStream serverOutputStream = new PipedOutputStream(clientInputStream);
    when(client.getInputStream()).thenReturn(clientInputStream);
    when(client.getOutputStream()).thenReturn(clientOutputStream);
    when(server.getInputStream()).thenReturn(serverInputStream);
    when(server.getOutputStream()).thenReturn(serverOutputStream);
    Jid localhost = Jid.of("localhost");
    StreamListener serverListener = mock(StreamListener.class);
    when(serverListener.filter(null, localhost)).thenReturn(false);
    final StreamComponentServer[] componentServer = new StreamComponentServer[1];
    executorService.submit(() -> {
        try {
            Socket clientSocket = serverSocket.accept();
            componentServer[0] = new StreamComponentServer(clientSocket.getInputStream(), clientSocket.getOutputStream(), "secret");
            componentServer[0].addListener(serverListener);
            componentServer[0].connect();
        } catch (IOException | XmlPullParserException e) {
            e.printStackTrace();
        }
    });
    StreamComponent component = new StreamComponent(localhost, client.getInputStream(), client.getOutputStream(), "secret");
    component.addListener(testListener);
    component.addListener(messageListener);
    executorService.submit(component::connect);
    verify(testListener, timeout(5000).times(1)).ready();
    Message msg = new Message();
    msg.from = Jid.of("vasya@localhost");
    msg.to = Jid.of("masha@localhost");
    msg.body = "test";
    componentServer[0].send(msg);
    verify(messageListener, times(1)).onMessage(messageCaptor.capture());
    Message received = messageCaptor.getValue();
    assertEquals("test", received.body);
    component.send("<yo:people/>");
    verify(testListener, timeout(5000).times(1)).fail(exceptionArgumentCaptor.capture());
    assertEquals("invalid-xml", exceptionArgumentCaptor.getValue().getMessage());
}
Also used : Jid(rocks.xmpp.addr.Jid) ServerSocket(java.net.ServerSocket) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 Jid (rocks.xmpp.addr.Jid)2 Handshake (com.juick.xmpp.extensions.Handshake)1 XMPPError (com.juick.xmpp.extensions.XMPPError)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Test (org.junit.Test)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1