mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Changed the root package from net.sf.briar to org.briarproject.
This commit is contained in:
66
briar-tests/src/org/briarproject/util/ByteUtilsTest.java
Normal file
66
briar-tests/src/org/briarproject/util/ByteUtilsTest.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package org.briarproject.util;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ByteUtilsTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testReadUint16() {
|
||||
byte[] b = StringUtils.fromHexString("000000");
|
||||
assertEquals(0, ByteUtils.readUint16(b, 1));
|
||||
b = StringUtils.fromHexString("000001");
|
||||
assertEquals(1, ByteUtils.readUint16(b, 1));
|
||||
b = StringUtils.fromHexString("00FFFF");
|
||||
assertEquals(65535, ByteUtils.readUint16(b, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUint32() {
|
||||
byte[] b = StringUtils.fromHexString("0000000000");
|
||||
assertEquals(0, ByteUtils.readUint32(b, 1));
|
||||
b = StringUtils.fromHexString("0000000001");
|
||||
assertEquals(1, ByteUtils.readUint32(b, 1));
|
||||
b = StringUtils.fromHexString("00FFFFFFFF");
|
||||
assertEquals(4294967295L, ByteUtils.readUint32(b, 1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWriteUint16() {
|
||||
byte[] b = new byte[3];
|
||||
ByteUtils.writeUint16(0, b, 1);
|
||||
assertEquals("000000", StringUtils.toHexString(b));
|
||||
ByteUtils.writeUint16(1, b, 1);
|
||||
assertEquals("000001", StringUtils.toHexString(b));
|
||||
ByteUtils.writeUint16(65535, b, 1);
|
||||
assertEquals("00FFFF", StringUtils.toHexString(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteUint32() {
|
||||
byte[] b = new byte[5];
|
||||
ByteUtils.writeUint32(0, b, 1);
|
||||
assertEquals("0000000000", StringUtils.toHexString(b));
|
||||
ByteUtils.writeUint32(1, b, 1);
|
||||
assertEquals("0000000001", StringUtils.toHexString(b));
|
||||
ByteUtils.writeUint32(4294967295L, b, 1);
|
||||
assertEquals("00FFFFFFFF", StringUtils.toHexString(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUint() {
|
||||
byte[] b = new byte[1];
|
||||
b[0] = (byte) 128;
|
||||
for(int i = 0; i < 8; i++) {
|
||||
assertEquals(1 << i, ByteUtils.readUint(b, i + 1));
|
||||
}
|
||||
b = new byte[2];
|
||||
for(int i = 0; i < 65535; i++) {
|
||||
ByteUtils.writeUint16(i, b, 0);
|
||||
assertEquals(i, ByteUtils.readUint(b, 16));
|
||||
assertEquals(i >> 1, ByteUtils.readUint(b, 15));
|
||||
}
|
||||
}
|
||||
}
|
||||
44
briar-tests/src/org/briarproject/util/StringUtilsTest.java
Normal file
44
briar-tests/src/org/briarproject/util/StringUtilsTest.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package org.briarproject.util;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import org.briarproject.BriarTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringUtilsTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testHead() {
|
||||
String head = StringUtils.head("123456789", 5);
|
||||
assertEquals("12345...", head);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTail() {
|
||||
String tail = StringUtils.tail("987654321", 5);
|
||||
assertEquals("...54321", tail);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToHexString() {
|
||||
byte[] b = new byte[] {1, 2, 3, 127, -128};
|
||||
String s = StringUtils.toHexString(b);
|
||||
assertEquals("0102037F80", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromHexString() {
|
||||
try {
|
||||
StringUtils.fromHexString("12345");
|
||||
fail();
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
try {
|
||||
StringUtils.fromHexString("ABCDEFGH");
|
||||
fail();
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
byte[] b = StringUtils.fromHexString("0102037F80");
|
||||
assertArrayEquals(new byte[] {1, 2, 3, 127, -128}, b);
|
||||
b = StringUtils.fromHexString("0a0b0c0d0e0f");
|
||||
assertArrayEquals(new byte[] {10, 11, 12, 13, 14, 15}, b);
|
||||
}
|
||||
}
|
||||
202
briar-tests/src/org/briarproject/util/ZipUtilsTest.java
Normal file
202
briar-tests/src/org/briarproject/util/ZipUtilsTest.java
Normal file
@@ -0,0 +1,202 @@
|
||||
package org.briarproject.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.util.ZipUtils.Callback;
|
||||
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ZipUtilsTest extends BriarTestCase {
|
||||
|
||||
private final File testDir = TestUtils.getTestDirectory();
|
||||
|
||||
private final File f1 = new File(testDir, "abc/def/1");
|
||||
private final File f2 = new File(testDir, "abc/def/2");
|
||||
private final File f3 = new File(testDir, "abc/3");
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
testDir.mkdirs();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToZip() throws IOException {
|
||||
File src = new File(testDir, "src");
|
||||
File dest = new File(testDir, "dest");
|
||||
TestUtils.createFile(src, "foo bar baz");
|
||||
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
|
||||
|
||||
ZipUtils.copyToZip("abc/def", src, zip);
|
||||
zip.flush();
|
||||
zip.close();
|
||||
|
||||
Map<String, String> expected = Collections.singletonMap("abc/def",
|
||||
"foo bar baz");
|
||||
checkZipEntries(dest, expected);
|
||||
}
|
||||
|
||||
private void checkZipEntries(File f, Map<String, String> expected)
|
||||
throws IOException {
|
||||
Map<String, String> found = new HashMap<String, String>();
|
||||
assertTrue(f.exists());
|
||||
assertTrue(f.isFile());
|
||||
ZipInputStream unzip = new ZipInputStream(new FileInputStream(f));
|
||||
ZipEntry entry;
|
||||
while((entry = unzip.getNextEntry()) != null) {
|
||||
String name = entry.getName();
|
||||
Scanner s = new Scanner(unzip);
|
||||
assertTrue(s.hasNextLine());
|
||||
String contents = s.nextLine();
|
||||
assertFalse(s.hasNextLine());
|
||||
unzip.closeEntry();
|
||||
found.put(name, contents);
|
||||
}
|
||||
unzip.close();
|
||||
assertEquals(expected.size(), found.size());
|
||||
for(String name : expected.keySet()) {
|
||||
String contents = found.get(name);
|
||||
assertNotNull(contents);
|
||||
assertEquals(expected.get(name), contents);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToZipRecursively() throws IOException {
|
||||
Mockery context = new Mockery();
|
||||
final Callback callback = context.mock(Callback.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(callback).processingFile(f1);
|
||||
oneOf(callback).processingFile(f2);
|
||||
oneOf(callback).processingFile(f3);
|
||||
}});
|
||||
|
||||
copyRecursively(callback);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyToZipRecursivelyNoCallback() throws IOException {
|
||||
copyRecursively(null);
|
||||
}
|
||||
|
||||
private void copyRecursively(Callback callback) throws IOException {
|
||||
TestUtils.createFile(f1, "one one one");
|
||||
TestUtils.createFile(f2, "two two two");
|
||||
TestUtils.createFile(f3, "three three three");
|
||||
File src = new File(testDir, "abc");
|
||||
File dest = new File(testDir, "dest");
|
||||
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
|
||||
|
||||
ZipUtils.copyToZipRecursively("ghi", src, zip, callback);
|
||||
zip.flush();
|
||||
zip.close();
|
||||
|
||||
Map<String, String> expected = new HashMap<String, String>();
|
||||
expected.put("ghi/def/1", "one one one");
|
||||
expected.put("ghi/def/2", "two two two");
|
||||
expected.put("ghi/3", "three three three");
|
||||
checkZipEntries(dest, expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnzipStream() throws IOException {
|
||||
Mockery context = new Mockery();
|
||||
final Callback callback = context.mock(Callback.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(callback).processingFile(f1);
|
||||
oneOf(callback).processingFile(f2);
|
||||
oneOf(callback).processingFile(f3);
|
||||
}});
|
||||
|
||||
unzipStream(null, callback);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
|
||||
assertTrue(f1.exists());
|
||||
assertTrue(f1.isFile());
|
||||
assertEquals("one one one".length(), f1.length());
|
||||
assertTrue(f2.exists());
|
||||
assertTrue(f2.isFile());
|
||||
assertEquals("two two two".length(), f2.length());
|
||||
assertTrue(f3.exists());
|
||||
assertTrue(f3.isFile());
|
||||
assertEquals("three three three".length(), f3.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnzipStreamWithRegex() throws IOException {
|
||||
Mockery context = new Mockery();
|
||||
final Callback callback = context.mock(Callback.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(callback).processingFile(f1);
|
||||
oneOf(callback).processingFile(f2);
|
||||
}});
|
||||
|
||||
unzipStream("^abc/def/.*", callback);
|
||||
|
||||
context.assertIsSatisfied();
|
||||
|
||||
assertTrue(f1.exists());
|
||||
assertTrue(f1.isFile());
|
||||
assertEquals("one one one".length(), f1.length());
|
||||
assertTrue(f2.exists());
|
||||
assertTrue(f2.isFile());
|
||||
assertEquals("two two two".length(), f2.length());
|
||||
assertFalse(f3.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnzipStreamNoCallback() throws IOException {
|
||||
unzipStream(null, null);
|
||||
|
||||
assertTrue(f1.exists());
|
||||
assertTrue(f1.isFile());
|
||||
assertEquals("one one one".length(), f1.length());
|
||||
assertTrue(f2.exists());
|
||||
assertTrue(f2.isFile());
|
||||
assertEquals("two two two".length(), f2.length());
|
||||
assertTrue(f3.exists());
|
||||
assertTrue(f3.isFile());
|
||||
assertEquals("three three three".length(), f3.length());
|
||||
}
|
||||
|
||||
private void unzipStream(String regex, Callback callback)
|
||||
throws IOException {
|
||||
TestUtils.createFile(f1, "one one one");
|
||||
TestUtils.createFile(f2, "two two two");
|
||||
TestUtils.createFile(f3, "three three three");
|
||||
File src = new File(testDir, "abc");
|
||||
File dest = new File(testDir, "dest");
|
||||
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(dest));
|
||||
ZipUtils.copyToZipRecursively(src.getName(), src, zip, null);
|
||||
zip.flush();
|
||||
zip.close();
|
||||
TestUtils.delete(src);
|
||||
|
||||
InputStream in = new FileInputStream(dest);
|
||||
ZipUtils.unzipStream(in, testDir, regex, callback);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TestUtils.deleteTestDirectory(testDir);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user