diff options
| author | cyberta <cyberta@riseup.net> | 2026-03-09 23:59:50 +0100 |
|---|---|---|
| committer | cyberta <cyberta@riseup.net> | 2026-03-09 23:59:50 +0100 |
| commit | 733d1ae5bbb5356a228e42a013660743db1c7073 (patch) | |
| tree | 550f2285ef072e7dcc482d648fccc09222a35791 /main/src/test/java/de | |
| parent | b1c21e7e1fbc0d09e3d121b89651482a0bb02efd (diff) | |
| parent | 73ce3c99dde39207990d66c21be8201228cd1f09 (diff) | |
Merge branch 'schwabe_master' into ssh_new_master
Diffstat (limited to 'main/src/test/java/de')
3 files changed, 183 insertions, 0 deletions
diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestLog.kt b/main/src/test/java/de/blinkt/openvpn/core/TestLog.kt new file mode 100644 index 00000000..4cce96ad --- /dev/null +++ b/main/src/test/java/de/blinkt/openvpn/core/TestLog.kt @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2012-2024 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 + +class TestLog { + + @Test + fun testMarschTooLong() + { + /* generate a string that is 16k long */ + var longtsring = ""; + + while (longtsring.length < 16384) + { + longtsring += "very very long string indeed" + } + val li = LogItem(VpnStatus.LogLevel.VERBOSE, longtsring) + + val libytes = li.marschaledBytes; + + Assert.assertTrue(libytes.size > 2000); + Assert.assertTrue(libytes.size < 12000); + + + val liback = LogItem(libytes, libytes.size) + val msgback = liback.getString(null) + Assert.assertTrue(msgback.endsWith("...[too long]")) + } +}
\ No newline at end of file diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java b/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java index e69de29b..a224011e 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java +++ b/main/src/test/java/de/blinkt/openvpn/core/TestLogFileHandler.java @@ -0,0 +1,143 @@ +/* + * 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 org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + +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.assertArrayEquals(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); + } + + @Test + public void testLogInsertByTime() + { + VpnStatus vpnStatus = new VpnStatus(); + /* Add the generic information message */ + VpnStatus.clearLog(); + + long[] testTimes = {1000, 20000, 1500, 500, 6000, 70000, System.currentTimeMillis()+5000}; + for (long time: testTimes) { + LogItem li = new LogItem(VpnStatus.LogLevel.INFO, "unit test", time); + VpnStatus.newLogItemIfUnique(li); + } + + long lastlogTime = 0; + for(LogItem li:VpnStatus.getlogbuffer()) + { + org.junit.Assert.assertTrue(li.getLogtime() >= lastlogTime); + lastlogTime = li.getLogtime(); + } + } + + + 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); + } + } + +} diff --git a/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt b/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt index 75505655..0d3d5c7d 100644 --- a/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt +++ b/main/src/test/java/de/blinkt/openvpn/core/TestRestrictions.kt @@ -4,14 +4,19 @@ */ package de.blinkt.openvpn.core +import android.app.Application import android.content.Context import android.os.Bundle import androidx.test.core.app.ApplicationProvider import de.blinkt.openvpn.VpnProfile import de.blinkt.openvpn.api.AppRestrictions import org.junit.Assert +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Spy import org.robolectric.RobolectricTestRunner interface createTestBundle |
