aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/test/java/org/spongycastle/util/io/test/BufferingOutputStreamTest.java
blob: faaaa53297fba2f0988e4839f276b3f9c68b636e (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
package org.spongycastle.util.io.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;

import org.spongycastle.util.io.BufferingOutputStream;
import org.spongycastle.util.test.SimpleTest;

public class BufferingOutputStreamTest
    extends SimpleTest
{
    public String getName()
    {
        return "BufferingStreamTest";
    }

    public void performTest()
        throws Exception
    {
        SecureRandom random = new SecureRandom();

        for (int i = 1; i != 256; i++)
        {
            byte[] data = new byte[i];

            random.nextBytes(data);

            checkStream(data, 16);
            checkStream(data, 33);
            checkStream(data, 128);
        }
    }

    private void checkStream(byte[] data, int bufsize)
        throws IOException
    {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        BufferingOutputStream bfOut = new BufferingOutputStream(bOut, bufsize);

        for (int i = 0; i != 10; i++)
        {
            bfOut.write(data[0]);
            bfOut.write(data, 1, data.length - 1);
        }

        bfOut.close();

        byte[] output = bOut.toByteArray();

        for (int i = 0; i != 10; i++)
        {
             for (int j = 0; j != data.length; j++)
             {
                 if (output[i * data.length + j] != data[j])
                 {
                     fail("data mismatch!");
                 }
             }
        }
    }

    public static void main(
        String[]    args)
    {
        runTest(new BufferingOutputStreamTest());
    }
}