summaryrefslogtreecommitdiff
path: root/main/client/test/src/ClientTests.java
blob: f3fcf154f2cbf1d09479cdd231127bffa3801f1f (plain) (blame)
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
package mill.main.client;

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

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.*;

public class ClientTests {
    @Test
    public void readWriteInt() throws Exception{
        int[] examples = {
                0, 1, 126, 127, 128, 254, 255, 256, 1024, 99999, 1234567,
                Integer.MAX_VALUE, Integer.MAX_VALUE / 2, Integer.MIN_VALUE
        };
        for(int example0: examples){
            for(int example: new int[]{-example0, example0}){
                ByteArrayOutputStream o = new ByteArrayOutputStream();
                Util.writeInt(o, example);
                ByteArrayInputStream i = new ByteArrayInputStream(o.toByteArray());
                int s = Util.readInt(i);
                assertEquals(example, s);
                assertEquals(i.available(), 0);
            }
        }
    }
    @Test
    public void readWriteString() throws Exception{
        String[] examples = {
                "",
                "hello",
                "i am cow",
                "i am cow\nhear me moo\ni weight twice as much as you",
                "我是一个叉烧包",
        };
        for(String example: examples){
            checkStringRoundTrip(example);
        }
    }

    @Test
    public void readWriteBigString() throws Exception{
        int[] lengths = {0, 1, 126, 127, 128, 254, 255, 256, 1024, 99999, 1234567};
        for(int i = 0; i < lengths.length; i++){
            final char[] bigChars = new char[lengths[i]];
            java.util.Arrays.fill(bigChars, 'X');
            checkStringRoundTrip(new String(bigChars));
        }
    }

    public void checkStringRoundTrip(String example) throws Exception{
        ByteArrayOutputStream o = new ByteArrayOutputStream();
        Util.writeString(o, example);
        ByteArrayInputStream i = new ByteArrayInputStream(o.toByteArray());
        String s = Util.readString(i);
        assertEquals(example, s);
        assertEquals(i.available(), 0);
    }

    public byte[] readSamples(String ...samples) throws Exception{
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for(String sample: samples) {
            byte[] bytes = java.nio.file.Files.readAllBytes(
                java.nio.file.Paths.get(getClass().getResource(sample).getFile())
            );
            out.write(bytes);
        }
        return out.toByteArray();
    }
    @Test
    public void tinyProxyInputOutputStream() throws Exception{
        proxyInputOutputStreams(
                Arrays.copyOf(readSamples("/bandung.jpg"), 30),
                readSamples(),
                10
        );
    }
    @Test
    public void leftProxyInputOutputStream() throws Exception{
        proxyInputOutputStreams(
                readSamples("/bandung.jpg", "/akanon.mid", "/gettysburg.txt", "/pip.tar.gz"),
                readSamples(),
                2950
        );
    }
    @Test
    public void rightProxyInputOutputStream() throws Exception{
        proxyInputOutputStreams(
                readSamples(),
                readSamples("/bandung.jpg", "/akanon.mid", "/gettysburg.txt", "/pip.tar.gz"),
                3000
        );
    }
    @Test
    public void mixedProxyInputOutputStream() throws Exception{
        proxyInputOutputStreams(
                readSamples("/bandung.jpg", "/gettysburg.txt"),
                readSamples("/akanon.mid", "/pip.tar.gz"),
                3050
        );
    }

    /**
     * Make sure that when we shove data through both ProxyOutputStreams in
     * variously sized chunks, we get the exact same bytes back out from the
     * ProxyStreamPumper.
     */
    public void proxyInputOutputStreams(byte[] samples1,
                                        byte[] samples2,
                                        int chunkMax) throws Exception{

        ByteArrayOutputStream pipe = new ByteArrayOutputStream();
        OutputStream src1 = new ProxyOutputStream(pipe, 1);
        OutputStream src2 = new ProxyOutputStream(pipe, -1);

        Random random = new Random(31337);

        int i1 = 0;
        int i2 = 0;
        while(i1 < samples1.length || i2 < samples2.length){
            int chunk = random.nextInt(chunkMax);
            if (random.nextBoolean() && i1 < samples1.length){
                src1.write(samples1, i1, Math.min(samples1.length-i1, chunk));
                src1.flush();
                i1 += chunk;
            }else if (i2 < samples2.length){
                src2.write(samples2, i2, Math.min(samples2.length-i2, chunk));
                src2.flush();
                i2 += chunk;
            }
        }

        byte[] bytes = pipe.toByteArray();


        ByteArrayOutputStream dest1 = new ByteArrayOutputStream();
        ByteArrayOutputStream dest2 = new ByteArrayOutputStream();
        ProxyStreamPumper pumper = new ProxyStreamPumper(
                new ByteArrayInputStream(bytes),
                dest1, dest2
        );
        pumper.run();
        assertTrue(Arrays.equals(samples1, dest1.toByteArray()));
        assertTrue(Arrays.equals(samples2, dest2.toByteArray()));
    }

}