summaryrefslogtreecommitdiff
path: root/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java
blob: b32e5efa2235a3e239ccf5530e58d2557eb0b3ae (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
/*
 * Copyright (c) 2012-2017 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.core;

import android.annotation.SuppressLint;

import junit.framework.Assert;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class TestLogFileHandler {

    byte[] testUnescaped = new byte[]{0x00, 0x55, -27, 0x00, 0x56, 0x10, -128, 0x55, 0x54};
    byte[] expectedEscaped = new byte[]{0x55, 0x00, 0x00, 0x00, 0x09, 0x00, 0x56, 0x00, -27, 0x00, 0x56, 0x01, 0x10, -128, 0x56, 0x00, 0x54};
    private TestingLogFileHandler lfh;


    @Before
    public void setup() {
        lfh = new TestingLogFileHandler();
    }

    @Test
    public void testWriteByteArray() throws IOException {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        lfh.setLogFile(byteArrayOutputStream);

        lfh.writeEscapedBytes(testUnescaped);

        byte[] result = byteArrayOutputStream.toByteArray();
        Assert.assertTrue(Arrays.equals(expectedEscaped, result));
    }

    @Test
    public void readByteArray() throws IOException {

        ByteArrayInputStream in = new ByteArrayInputStream(expectedEscaped);

        lfh.readCacheContents(in);

        Assert.assertTrue(Arrays.equals(testUnescaped, lfh.mRestoredByteArray));

    }

    @Test
    public void testMarschal() throws UnsupportedEncodingException {
        LogItem li = new LogItem(VpnStatus.LogLevel.DEBUG, 72, "foobar");
        LogItem li2 = marschalAndBack(li);
        testEquals(li, li2);
        Assert.assertEquals(li, li2);
    }

    @Test
    public void testMarschalArgs() throws UnsupportedEncodingException {
        LogItem li = new LogItem(VpnStatus.LogLevel.DEBUG, 72, 772, "sinnloser Text", 7723, 723.2f, 7.2);
        LogItem li2 = marschalAndBack(li);
        testEquals(li, li2);
        Assert.assertEquals(li, li2);
    }

    @Test
    public void testMarschalString() throws UnsupportedEncodingException {
        LogItem li = new LogItem(VpnStatus.LogLevel.DEBUG, "Nutzlose Nachricht");
        LogItem li2 = marschalAndBack(li);
        testEquals(li, li2);
        Assert.assertEquals(li, li2);
    }


    private void testEquals(LogItem li, LogItem li2) {
        Assert.assertEquals(li.getLogLevel(), li2.getLogLevel());
        Assert.assertEquals(li.getLogtime(), li2.getLogtime());
        Assert.assertEquals(li.getVerbosityLevel(), li2.getVerbosityLevel());
        Assert.assertEquals(li.toString(), li2.toString());

    }

    private LogItem marschalAndBack(LogItem li) throws UnsupportedEncodingException {
        byte[] bytes = li.getMarschaledBytes();

        return new LogItem(bytes, bytes.length);
    }


    @SuppressLint("HandlerLeak")
    static class TestingLogFileHandler extends LogFileHandler {

        public byte[] mRestoredByteArray;

        public TestingLogFileHandler() {
            super(null);
        }

        public void setLogFile(OutputStream out) {
            mLogFile = out;
        }

        @Override
        public void readCacheContents(InputStream in) throws IOException {
            super.readCacheContents(in);
        }

        @Override
        protected void restoreLogItem(byte[] buf, int len) {
            mRestoredByteArray = Arrays.copyOf(buf, len);
        }
    }


}