diff options
Diffstat (limited to 'app/src/test')
3 files changed, 197 insertions, 135 deletions
diff --git a/app/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java b/app/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java new file mode 100644 index 00000000..560d4fc8 --- /dev/null +++ b/app/src/test/java/de/blinkt/openvpn/core/TestConfigParser.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2012-2016 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 org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.StringReader; + +import de.blinkt.openvpn.VpnProfile; + +/** + * Created by arne on 03.10.16. + */ + +public class TestConfigParser { + + String miniconfig = "client\nremote test.blinkt.de\n"; + + @Test + public void testHttpProxyPass() throws IOException, ConfigParser.ConfigParseError { + String httpproxypass = "<http-proxy-user-pass>\n" + + "foo\n" + + "bar\n" + + "</http-proxy-user-pass>\n"; + + ConfigParser cp = new ConfigParser(); + cp.parseConfig(new StringReader(miniconfig + httpproxypass)); + VpnProfile p = cp.convertProfile(); + Assert.assertTrue(p.mCustomConfigOptions.contains(httpproxypass)); + + } +} diff --git a/app/src/test/java/de/blinkt/openvpn/core/TestIpParser.java b/app/src/test/java/de/blinkt/openvpn/core/TestIpParser.java new file mode 100644 index 00000000..37f9fdcd --- /dev/null +++ b/app/src/test/java/de/blinkt/openvpn/core/TestIpParser.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2012-2016 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 junit.framework.Assert; + +import org.junit.Test; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * Created by arne on 23.07.16. + */ + +public class TestIpParser { + + @Test + public void parseIPv6Zeros() throws UnknownHostException { + + testAddress("2020:0:1234::", 45, "2020:0:1234::/45"); + testAddress("::", 0, "::/0"); + testAddress("2a02:2e0:3fe:1001:302::", 128, "2a02:2e0:3fe:1001:302::/128"); + testAddress("2a02:2e0:3fe:1001:302::70", 128, "2a02:2e0:3fe:1001:302:0:0:70/128"); + } + + void testAddress(String input, int mask, String output) throws UnknownHostException { + Inet6Address ip = (Inet6Address) InetAddress.getByName(input); + + NetworkSpace.ipAddress netIp = new NetworkSpace.ipAddress(ip, mask, true); + + Assert.assertEquals(output, netIp.toString()); + } +} diff --git a/app/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java b/app/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java index 476d8151..b32e5efa 100644 --- a/app/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java +++ b/app/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java @@ -1,137 +1,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.Ignore; -//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; - -/** - * NOTE [@aguestuser|04.11.17]: - * - * As the tests below: - * - * (1) are testing the logic of library code (the openvpn class `LogFileHandler`) - * -- thus making them low-value tests - * (2) currently diverge from the signatures of the class under test (overriding non-existent methods, etc.) - * -- thus failing to compile and breaking the build - * - * I am taking the liberty of commenting them out, and proposing we delete this file altogether - * upon feedback from the team. - * - **/ - - -//@Ignore -//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); -// } -// } -// -// -//}
\ No newline at end of file +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); + } + } + + +}
\ No newline at end of file |