summaryrefslogtreecommitdiffstats
blob: 0fa4020412c58fb51650038fc49f3360ad98edfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *
 *  Acquisition and use of this software and related materials for any
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.network;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.After;
import org.junit.Test;

import com.isode.stroke.base.ByteArray;
import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.base.URL;
import com.isode.stroke.eventloop.DummyEventLoop;
import com.isode.stroke.eventloop.Event.Callback;
import com.isode.stroke.eventloop.EventLoop;
import com.isode.stroke.network.BOSHConnection.Pair;
import com.isode.stroke.parser.PlatformXMLParserFactory;
import com.isode.stroke.signals.Slot1;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.tls.TLSContextFactory;
import com.isode.stroke.tls.TLSOptions;

/**
 * Test for {@link BoshConnection}
 */
public class BoshConnectionTest {

    private final DummyEventLoop eventLoop = new DummyEventLoop();
    private final MockConnectionFactory connectionFactory = new MockConnectionFactory(eventLoop);
    private boolean connectFinished = false;
    private boolean connectFinishedWithError = false;
    private boolean disconnected = false;
    private boolean disconnectedError = false;
    private final ByteArray dataRead = new ByteArray();
    private final PlatformXMLParserFactory parserFactory = new PlatformXMLParserFactory();
    private final StaticDomainNameResolver resolver = new StaticDomainNameResolver(eventLoop);
    private final TimerFactory timerFactory = new DummyTimerFactory();
    private final TLSContextFactory tlsContextFactory = null;
    private String sid;
    
    @After
    public void tearDown() {
        eventLoop.processEvents();
    }
    
    @Test
    public void testHeader() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.startStream("wonderland.lit",1);
        String initial = "<body wait='60' "
                            +"inactivity='30' "
                            +"polling='5' "
                            +"requests='2' "
                            +"hold='1' "
                            +"maxpause='120' "
                            +"sid='MyShinySID' "
                            +"ver='1.6' "
                            +"from='wonderland.lit' "
                            +"xmlns='http://jabber.org/protocol/httpbind'/>";
        readResponse(initial, connectionFactory.connections.get(0));
        assertEquals("MyShinySID",sid);
        assertTrue(testling.isReadyToSend());
    }
    
    @Test
    public void testReadiness_ok() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.setSID("blahhhh");
        assertTrue(testling.isReadyToSend());
    }
    
    @Test
    public void testReadiness_pending() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.setSID("mySID");
        assertTrue(testling.isReadyToSend());
        testling.write(new SafeByteArray("<mypayload/>"));
        assertFalse(testling.isReadyToSend());
        readResponse("<body><blah/></body>", connectionFactory.connections.get(0));
        assertTrue(testling.isReadyToSend());
    }
    
    @Test
    public void testReadiness_disconnect() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.setSID("mySID");
        assertTrue(testling.isReadyToSend());
        connectionFactory.connections.get(0).onDisconnected.emit(null);
        assertFalse(testling.isReadyToSend());
    }
    
    @Test
    public void testReadiness_noSID() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        assertFalse(testling.isReadyToSend());
    }
    
    @Test
    public void testWrite_Receive() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.setSID("mySID");
        testling.write(new SafeByteArray("<mypayload/>"));
        readResponse("<body><blah/></body>", connectionFactory.connections.get(0));
        assertEquals("<blah/>",dataRead.toString());
    }
    
    @Test
    public void testWrite_ReceiveTwice() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        testling.setSID("mySID");
        testling.write(new SafeByteArray("<mypayload/>"));
        readResponse("<body><blah/></body>", connectionFactory.connections.get(0));
        assertEquals("<blah/>",dataRead.toString());
        dataRead.clear();
        testling.write(new SafeByteArray("<mypayload2/>"));
        readResponse("<body><bleh/></body>", connectionFactory.connections.get(0));
        assertEquals("<bleh/>",dataRead.toString());
    }
    
    @Test
    public void testRead_Fragment() {
        BOSHConnection testling = createTestling();
        testling.connect();
        eventLoop.processEvents();
        assertEquals(1, connectionFactory.connections.size());
        MockConnection connection = connectionFactory.connections.get(0);
        SafeByteArray data1 = new SafeByteArray(
            "HTTP/1.1 200 OK\r\n"+
            "Content-Type: text/xml; charset=utf-8\r\n"+
            "Access-Control-Allow-Origin: *\r\n"+
            "Access-Control-Allow-Headers: Content-Type\r\n"+
            "Content-Length: 64\r\n");
        SafeByteArray data2 = new SafeByteArray(
            "\r\n<body xmlns='http://jabber.org/protocol/httpbind'>"+
            "<bl");
        SafeByteArray data3 = new SafeByteArray(
            "ah/>"+
            "</body>");
        connection.onDataRead.emit(data1);
        connection.onDataRead.emit(data2);
        assertTrue(dataRead.isEmpty());
        connection.onDataRead.emit(data3);
        assertEquals("<blah/>",dataRead.toString());
    }
    
    @Test
    public void testHTTPRequest() {
        String data = "<blah/>";
        String sid = "wigglebloom";
        String fullBody = "<body xmlns='http://jabber.org/protocol/httpbind' sid='" + sid + "' rid='20'>" + data + "</body>";
        Pair<SafeByteArray, Integer> http = 
                BOSHConnection.createHTTPRequest(new SafeByteArray(data), false, false, 
                        20, sid, new URL());
        assertEquals(fullBody.length(),http.second.intValue());
    }
    
    @Test
    public void testHTTPRequest_Empty() {
        String data = "";
        String sid = "wigglebloomsickle";
        String fullBody = "<body rid='42' sid='" + sid + "' xmlns='http://jabber.org/protocol/httpbind'>" + data + "</body>";
        Pair<SafeByteArray, Integer> http = 
                BOSHConnection.createHTTPRequest(new SafeByteArray(data), false, false, 
                        42, sid, new URL());
        assertEquals(fullBody.length(),http.second.intValue());
        String response = http.first.toString();
        int bodyPosition = response.indexOf("\r\n\r\n");
        assertFalse("bodyPosition is equal to -1",-1 == bodyPosition);
        assertEquals(fullBody,response.substring(bodyPosition+4));
    }

    private BOSHConnection createTestling() {
        resolver.addAddress("wonderland.lit", new HostAddress("127.0.0.1"));
        Connector connector = Connector.create("wonderland.lit", 5280, null, resolver, connectionFactory, timerFactory);
        BOSHConnection connection = BOSHConnection.create(new URL(), connector, parserFactory, 
                tlsContextFactory, new TLSOptions());
        connection.onConnectionFinished.connect(new Slot1<Boolean>() {
            
            @Override
            public void call(Boolean hadError) {
                handleConnectFinished(hadError.booleanValue());
            }
            
        });
        connection.onDisconnected.connect(new Slot1<Boolean>() {

            @Override
            public void call(Boolean hadError) {
                handleDisconnected(hadError.booleanValue());
            }
            
        });
        connection.onXMPPDataRead.connect(new Slot1<SafeByteArray>() {

            @Override
            public void call(SafeByteArray p1) {
                handleDataRead(p1);
            }
            
        });
        connection.onSessionStarted.connect(new Slot2<String, Integer>() {
            
            @Override
            public void call(String sid, Integer requests) {
                handleSID(sid);
            }
            
        });
        connection.setRID(42);
        return connection;
    }

    private void handleConnectFinished(boolean hadError) {
        connectFinished = true;
        connectFinishedWithError = hadError;
    }

    private void handleDisconnected(boolean hadError) {
        disconnected = true;
        disconnectedError = hadError;
    }

    private void handleDataRead(SafeByteArray data) {
        dataRead.append(data);
    }

    private void handleSID(String s) {
        sid = s;
    }
    
    private void readResponse(String response,MockConnection connection) {
        SafeByteArray data1 = new SafeByteArray(
                "HTTP/1.1 200 OK\r\n"+
              "Content-Type: text/xml; charset=utf-8\r\n"+
              "Access-Control-Allow-Origin: *\r\n"+
              "Access-Control-Allow-Headers: Content-Type\r\n"+
              "Content-Length: "
                );
        connection.onDataRead.emit(data1);
        SafeByteArray data2 = new SafeByteArray(Integer.toString(response.length()));
        connection.onDataRead.emit(data2);
        SafeByteArray data3 = new SafeByteArray("\r\n\r\n");
        connection.onDataRead.emit(data3);
        SafeByteArray data4 = new SafeByteArray(response);
        connection.onDataRead.emit(data4);
    }
    
    private static class MockConnection extends Connection {
        
        public MockConnection(Collection<HostAddressPort> failingPorts,
                EventLoop eventLoop) {
            this.failingPorts = new ArrayList<HostAddressPort>(failingPorts);
            this.eventLoop = eventLoop;
        }
        
        @Override
        public void listen() {
            fail();
        }
        
        @Override
        public void connect(HostAddressPort address) {
            hostAddressPort = address;
            final boolean fail = failingPorts.contains(address);
            eventLoop.postEvent(new Callback() {
                
                @Override
                public void run() {
                    onConnectFinished.emit(fail);
                }
                
            });
        }
        
        @Override
        public void disconnect() {
            disconnected = true;
            onDisconnected.emit(null);
        }
        
        @Override
        public void write(SafeByteArray data) {
            dataWritten.append(data);
        }

        /* (non-Javadoc)
         * @see com.isode.stroke.network.Connection#getLocalAddress()
         */
        @Override
        public HostAddressPort getLocalAddress() {
            return new HostAddressPort();
        }
        
        public HostAddressPort getRemoteAddress() {
            return new HostAddressPort();
        }
        
        private final EventLoop eventLoop;
        private HostAddressPort hostAddressPort;
        private final List<HostAddressPort> failingPorts;
        private final ByteArray dataWritten = new ByteArray();
        private boolean disconnected;

    }
    
    private static class MockConnectionFactory implements ConnectionFactory {
        
        public MockConnectionFactory(EventLoop eventLoop) {
            this.eventLoop = eventLoop;
        }
        
        @Override
        public Connection createConnection() {
            MockConnection connection = new MockConnection(failingPorts, eventLoop);
            connections.add(connection);
            return connection;
        }
        
        private final EventLoop eventLoop;
        private List<MockConnection> connections = new ArrayList<MockConnection>();
        private List<HostAddressPort> failingPorts = new ArrayList<HostAddressPort>();
    }
 
}