Added tests for all repositories, fixed some bugs and made database configurable

This commit is contained in:
2015-06-05 13:43:56 +02:00
parent 274c16b748
commit f76864eebd
25 changed files with 860 additions and 184 deletions

View File

@ -97,7 +97,6 @@ public class BitmessageContext {
Plaintext msg = new Plaintext.Builder()
.from(from)
.to(to)
.encoding(Encoding.SIMPLE)
.message(subject, message)
.build();
if (to.getPubkey() == null) {

View File

@ -340,6 +340,7 @@ public class Plaintext implements Streamable {
public Builder message(String subject, String message) {
try {
this.encoding = Encoding.SIMPLE.getCode();
this.message = ("Subject:" + subject + '\n' + "Body:" + message).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
package ch.dissem.bitmessage.exception;
/**
* Indicates an illegal Bitmessage address

View File

@ -1,3 +1,19 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exception;
public class DecryptionFailedException extends Exception {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.exception;
import ch.dissem.bitmessage.utils.Strings;

View File

@ -17,23 +17,35 @@
package ch.dissem.bitmessage.utils;
/**
* Created by chris on 13.04.15.
* Intended to count the bytes read or written during (de-)serialization.
*/
public class AccessCounter {
private int count;
/**
* Increases the counter by one, if not null.
*/
public static void inc(AccessCounter counter) {
if (counter != null) counter.inc();
}
public static void inc(AccessCounter counter, int count) {
if (counter != null) counter.inc(count);
/**
* Increases the counter by length, if not null.
*/
public static void inc(AccessCounter counter, int length) {
if (counter != null) counter.inc(length);
}
/**
* Increases the counter by one.
*/
private void inc() {
count++;
}
/**
* Increases the counter by length.
*/
private void inc(int length) {
count += length;
}

View File

@ -17,12 +17,16 @@
package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.exception.AddressFormatException;
import java.io.UnsupportedEncodingException;
import static java.util.Arrays.copyOfRange;
/**
* Base58 encoder and decoder
* Base58 encoder and decoder.
*
* @author Christian Basler: I removed some dependencies to the BitcoinJ code so it can be used here more easily.
*/
public class Base58 {
private static char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();

View File

@ -24,6 +24,9 @@ import java.util.Arrays;
/**
* A helper class for working with byte arrays interpreted as unsigned big endian integers.
* This is one part due to the fact that Java doesn't support unsigned numbers, and another
* part so we don't have to convert between byte arrays and numbers in time critical
* situations.
*/
public class Bytes {
public static void inc(byte[] nonce) {
@ -33,6 +36,9 @@ public class Bytes {
}
}
/**
* Increases nonce by value, which is used as an unsigned byte value.
*/
public static void inc(byte[] nonce, byte value) {
int i = nonce.length - 1;
nonce[i] += value;
@ -67,7 +73,7 @@ public class Bytes {
}
/**
* Returns true if a < b, where the first [size] bytes are checked.
* Returns true if a < b, where the first [size] bytes are used as the numbers to check.
*/
public static boolean lt(byte[] a, byte[] b, int size) {
for (int i = 0; i < size; i++) {
@ -84,6 +90,9 @@ public class Bytes {
return a < b;
}
/**
* Returns a new byte array of length, left-padded with '0'.
*/
public static byte[] expand(byte[] source, int size) {
byte[] result = new byte[size];
System.arraycopy(source, 0, result, size - source.length, source.length);
@ -99,19 +108,10 @@ public class Bytes {
return result;
}
public static byte[] subArray(byte[] source, int offset, int length) {
byte[] result = new byte[length];
System.arraycopy(source, offset, result, 0, length);
return result;
}
public static byte[] concatenate(byte first, byte[] bytes) {
byte[] result = new byte[bytes.length + 1];
result[0] = first;
System.arraycopy(bytes, 0, result, 1, bytes.length);
return result;
}
/**
* Returns the byte array that hex represents. This is meant for test use and should be rewritten if used in
* production code.
*/
public static byte[] fromHex(String hex) {
if (hex.length() % 2 != 0) throw new IllegalArgumentException("expected even number of characters");
byte[] result = new byte[hex.length() / 2];
@ -135,6 +135,9 @@ public class Bytes {
throw new IllegalArgumentException("'" + c + "' is not a valid hex value");
}
/**
* Returns the number of leading '0' of a byte array.
*/
public static int numberOfLeadingZeros(byte[] bytes) {
int i;
for (i = 0; i < bytes.length; i++) {
@ -143,10 +146,16 @@ public class Bytes {
return i;
}
/**
* Returns a copy of bytes with leading zeroes stripped.
*/
public static byte[] stripLeadingZeros(byte[] bytes) {
return Arrays.copyOfRange(bytes, numberOfLeadingZeros(bytes), bytes.length);
}
/**
* Returns the byte array of the serialized data.
*/
public static byte[] from(Streamable data) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();

View File

@ -23,42 +23,6 @@ import ch.dissem.bitmessage.entity.payload.ObjectType;
* TODO: Probably this should be split in a GUI related and an SQL related utility class.
*/
public class Strings {
public static StringBuilder join(byte[]... objects) {
StringBuilder streamList = new StringBuilder();
for (int i = 0; i < objects.length; i++) {
if (i > 0) streamList.append(", ");
streamList.append(hex(objects[i]));
}
return streamList;
}
public static StringBuilder join(long... objects) {
StringBuilder streamList = new StringBuilder();
for (int i = 0; i < objects.length; i++) {
if (i > 0) streamList.append(", ");
streamList.append(objects[i]);
}
return streamList;
}
public static StringBuilder join(ObjectType... types) {
StringBuilder streamList = new StringBuilder();
for (int i = 0; i < types.length; i++) {
if (i > 0) streamList.append(", ");
streamList.append(types[i].getNumber());
}
return streamList;
}
public static StringBuilder join(Enum... types) {
StringBuilder streamList = new StringBuilder();
for (int i = 0; i < types.length; i++) {
if (i > 0) streamList.append(", ");
streamList.append('\'').append(types[i].name()).append('\'');
}
return streamList;
}
public static StringBuilder join(Object... objects) {
StringBuilder streamList = new StringBuilder();
for (int i = 0; i < objects.length; i++) {

View File

@ -17,9 +17,12 @@
package ch.dissem.bitmessage.utils;
/**
* Created by chris on 18.04.15.
* A simple utility class that simplifies using the second based time used in Bitmessage.
*/
public class UnixTime {
/**
* Length of a day in seconds, intended for use with {@link #now(long)}.
*/
public static final long DAY = 60 * 60 * 24;
/**
@ -29,6 +32,9 @@ public class UnixTime {
return System.currentTimeMillis() / 1000;
}
/**
* Same as {@link #now()} + shiftSeconds, but might be more readable.
*/
public static long now(long shiftSeconds) {
return (System.currentTimeMillis() / 1000) + shiftSeconds;
}

View File

@ -81,7 +81,6 @@ public class SerializationTest {
Plaintext p1 = new Plaintext.Builder()
.from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.to(TestUtils.loadContact())
.encoding(Plaintext.Encoding.SIMPLE)
.message("Subject", "Message")
.ack("ack".getBytes())
.signature(new byte[0])

View File

@ -21,12 +21,6 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringsTest {
@Test
public void ensureJoinWorksWithLongArray() {
long[] test = {1L, 2L};
assertEquals("1, 2", Strings.join(test).toString());
}
@Test
public void testHexString() {
assertEquals("48656c6c6f21", Strings.hex("Hello!".getBytes()).toString());