summaryrefslogtreecommitdiffstats
blob: d5dc3daf59d5829c1d5538ad62928596fc5982ee (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
/*  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.filetransfer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.util.Arrays;

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

import com.isode.stroke.base.ByteArray;
import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.base.StartStopper;
import com.isode.stroke.eventloop.DummyEventLoop;
import com.isode.stroke.network.DummyConnection;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot1;

/**
 * Tests for {@link SOCKS5BytestreamServerSession}
 */
public class SOCKS5BytestreamServerSessionTest {

    private final DummyEventLoop eventLoop = new DummyEventLoop();
    private final SOCKS5BytestreamRegistry bytestreams = new SOCKS5BytestreamRegistry();
    private final DummyConnection connection = new DummyConnection(eventLoop);
    private final ByteArray receivedData = new ByteArray();
    private int receivedDataChunks = 0;
    private final ByteArrayReadBytestream stream1 = 
            new ByteArrayReadBytestream(new ByteArray("abcdefg"));
    private boolean finished = false;
    private FileTransferError error = null;
    private SignalConnection onDataSentConnection;
    
    
    @Before
    public void setUp() {
        onDataSentConnection = connection.onDataSent.connect(new Slot1<SafeByteArray>() {
            
            @Override
            public void call(SafeByteArray data) {
                handleDataWritten(data);
            }
            
        });
    }
    
    @After
    public void tearDown() {
        onDataSentConnection.disconnect();
    }
    
    @Test
    public void testAuthenticate() {
        SOCKS5BytestreamServerSession testling = createSession();
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        receive(new SafeByteArray(new byte[] {0x05,0x02,0x01,0x02}));
        SafeByteArray expected = new SafeByteArray(new byte[] {0x05,0x00});
        assertEquals(expected,receivedData);
    }
    
    
    @Test
    public void testAuthenticate_Chunked() {
        SOCKS5BytestreamServerSession testling = createSession();
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        receive(new SafeByteArray(new byte[] {0x05,0x02,0x01}));
        assertEquals(0,receivedData.getSize());
        receive(new SafeByteArray(new byte[] {0x02}));
        SafeByteArray expected = new SafeByteArray(new byte[] {0x05,0x00});
        assertEquals(expected,receivedData);
    }
    
    @Test
    public void testRequest() {
        SOCKS5BytestreamServerSession testling = createSession();
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        bytestreams.setHasBytestream("abcdef", true);
        authenticate();
        
        ByteArray hostname = new ByteArray("abcdef");
        SafeByteArray data = new SafeByteArray();
        data.append(new byte[] {0x05,0x01,0x00,0x03});
        data.append((byte)hostname.getSize());
        data.append(hostname);
        data.append(new byte[] {0x00,0x00});
        receive(data);
        // Compare first 13 bytes of received data with what we expect
        ByteArray expectedData = 
                new ByteArray(new byte[] {0x05,0x00,0x00,0x03,0x06,0x61,0x62,
                        0x63,0x64,0x65,0x66,0x00,0x00});
        assertEquals(expectedData,receivedData);
        
    }
    
    @Test
    public void testRequest_UnknownBytestream() {
        SOCKS5BytestreamServerSession testling = createSession();
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        authenticate();
        ByteArray hostname = new ByteArray("abcdef");
        SafeByteArray data = new SafeByteArray();
        data.append(new byte[]{0x05,0x01,0x00,0x03});
        data.append((byte)hostname.getSize());
        data.append(hostname);
        data.append(new byte[] {0x00,0x00});
        receive(data);

        ByteArray expected = 
                new ByteArray(new byte[] {0x05,0x04,0x00,0x03,0x06,0x61,0x62,
                        0x63,0x64,0x65,0x66,0x00,0x00});
        
        assertEquals(expected,receivedData);
    }
    
    @Test
    public void testReceiveData() {
        SOCKS5BytestreamServerSession testling = createSession();
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        bytestreams.setHasBytestream("abcdef", true);
        authenticate();
        request("abcdef");
        eventLoop.processEvents();
        testling.startSending(stream1);
        skipHeader("abcdef");
        eventLoop.processEvents();
        assertEquals(new ByteArray("abcdefg"),receivedData);
        assertEquals(2,receivedDataChunks);
    }
    
    @Test
    public void testReceiveData_Chunked() {
        SOCKS5BytestreamServerSession testling = createSession();
        testling.setChunkSize(3);
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        bytestreams.setHasBytestream("abcdef", true);
        authenticate();
        request("abcdef");
        eventLoop.processEvents();
        testling.startSending(stream1);
        eventLoop.processEvents();
        skipHeader("abcdef");
        assertEquals(new ByteArray("abcdefg"),receivedData);
        assertEquals(4,receivedDataChunks);
    }
    
    @Test
    public void testDataStreamPauseStopsSendingData() {
        SOCKS5BytestreamServerSession testling = createSession();
        testling.setChunkSize(3);
        stream1.setDataComplete(false);
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        bytestreams.setHasBytestream("abcdef", true);
        authenticate();
        request("abcdef");
        eventLoop.processEvents();
        testling.startSending(stream1);
        eventLoop.processEvents();
        skipHeader("abcdef");
        assertEquals(new ByteArray("abcdefg"),receivedData);
        assertEquals(4,receivedDataChunks);
        assertFalse(finished);
        assertNull(error);
    }
    
    @Test
    public void testDataStreamResumeAfterPauseSendsData() {
        SOCKS5BytestreamServerSession testling = createSession();
        testling.setChunkSize(3);
        stream1.setDataComplete(false);
        StartStopper<SOCKS5BytestreamServerSession> stopper = 
                new StartStopper<SOCKS5BytestreamServerSession>(testling);
        bytestreams.setHasBytestream("abcdef", true);
        authenticate();
        request("abcdef");
        eventLoop.processEvents();
        testling.startSending(stream1);
        eventLoop.processEvents();
        skipHeader("abcdef");
        stream1.addData(new ByteArray("xyz"));
        eventLoop.processEvents();
        assertEquals(new ByteArray("abcdefgxyz"),receivedData);
        assertFalse(finished);
        assertNull(error);
    }
    
    private void receive(SafeByteArray data) {
        connection.receive(data);
        eventLoop.processEvents();
    }
    
    private void authenticate() {
        receive(new SafeByteArray(new byte[] {0x05,0x02,0x01,0x02}));
        receivedData.clear();
        receivedDataChunks = 0;
    }
    
    private void request(String hostname) {
        SafeByteArray results = new SafeByteArray();
        results.append(new byte[] {0x05,0x01,0x00,0x03});
        results.append((byte) hostname.length());
        results.append(hostname);
        results.append(new byte[] {0x00,0x00});
        receive(results);
    }
    
    private void skipHeader(String hostname) {
        int headerSize = 7 + hostname.length();
        byte[] currentReceivedData = receivedData.getData();
        byte[] newContents = Arrays.copyOfRange(currentReceivedData, headerSize, currentReceivedData.length);
        receivedData.clear();
        receivedData.append(newContents);
    }
    
    private void handleDataWritten(SafeByteArray data) {
        receivedData.append(data);
        receivedDataChunks++;
    }

    private SOCKS5BytestreamServerSession createSession() {
        SOCKS5BytestreamServerSession session = new SOCKS5BytestreamServerSession(connection, bytestreams);
        session.onFinished.connect(new Slot1<FileTransferError>() {
    
            @Override
            public void call(FileTransferError error) {
                handleFinished(error);
            }
            
        });
        return session;
    }

    private void handleFinished(FileTransferError error) {
        finished = true;
        this.error = error;
    }
    
}