Javadocs and unit tests, God help me.

This commit is contained in:
akwizgran
2011-06-27 13:01:31 +01:00
parent d193f23e4c
commit 5281113f24
34 changed files with 169 additions and 54 deletions

View File

@@ -1,6 +1,14 @@
package net.sf.briar.api.crypto;
/**
* Encapsulates a password. Implementations may keep the password encrypted in
* memory to reduce the chances of writing it to the swapfile in plaintext.
*/
public interface Password {
/**
* Returns the password as a character array, which should be filled with
* zeroes as soon as it has been used.
*/
char[] getPassword();
}

View File

@@ -7,11 +7,16 @@ import net.sf.briar.api.protocol.Bundle;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
/**
* Encapsulates the database implementation and exposes high-level operations
* to other components.
*/
public interface DatabaseComponent {
static final long MEGABYTES = 1024L * 1024L;
static final long GIGABYTES = 1024L * MEGABYTES;
// FIXME: Some of these should be configurable
static final long MAX_DB_SIZE = 2L * GIGABYTES;
static final long MIN_FREE_SPACE = 300L * MEGABYTES;
static final long CRITICAL_FREE_SPACE = 100L * MEGABYTES;
@@ -21,23 +26,42 @@ public interface DatabaseComponent {
static final int CLEANER_SLEEP_MS = 1000; // 1 sec
static final int RETRANSMIT_THRESHOLD = 3;
/** Waits for any open transactions to finish and closes the database. */
void close() throws DbException;
/** Adds a locally generated message to the database. */
void addLocallyGeneratedMessage(Message m) throws DbException;
/** Adds a new neighbour to the database. */
void addNeighbour(NeighbourId n) throws DbException;
/** Generates a bundle of messages for the given neighbour. */
void generateBundle(NeighbourId n, Bundle b) throws DbException;
/**
* Returns the rating assigned to the given author, which may be
* Rating.UNRATED if no rating has been assigned.
*/
Rating getRating(AuthorId a) throws DbException;
/** Returns the set of groups to which we subscribe. */
Set<GroupId> getSubscriptions() throws DbException;
/**
* Processes a bundle of messages received from the given neighbour. Some
* or all of the messages in the bundle may be stored.
*/
void receiveBundle(NeighbourId n, Bundle b) throws DbException;
/** Stores a rating for the given author. */
void setRating(AuthorId a, Rating r) throws DbException;
/** Subscribes to the given group. */
void subscribe(GroupId g) throws DbException;
/**
* Unsubscribes from the given group. Any messages belonging to the group
* will be deleted from the database.
*/
void unsubscribe(GroupId g) throws DbException;
}

View File

@@ -8,6 +8,10 @@ import java.lang.annotation.Target;
import com.google.inject.BindingAnnotation;
/**
* Annotation for injecting the password from which the database encryption
* key is derived.
*/
@BindingAnnotation
@Target({ PARAMETER })
@Retention(RUNTIME)

View File

@@ -1,5 +1,6 @@
package net.sf.briar.api.db;
/** Uniquely identifies a neighbour. */
public class NeighbourId {
private final int id;

View File

@@ -1,5 +1,6 @@
package net.sf.briar.api.db;
/** The ratings that may be applied to an author in peer moderation. */
public enum Rating {
BAD, UNRATED, GOOD
}

View File

@@ -1,5 +1,17 @@
package net.sf.briar.api.db;
/** The status of a message with respect to a neighbour. */
public enum Status {
NEW, SENT, SEEN
/**
* The message has not been sent to, received from, or acked by the
* neighbour.
*/
NEW,
/**
* The message has been sent to, but not received from or acked by, the
* neighbour.
*/
SENT,
/** The message has been received from or acked by the neighbour. */
SEEN
}

View File

@@ -6,13 +6,15 @@ import java.util.Locale;
public interface FontManager {
/** Initializes the FontManager for the given locale. */
void initialize(Locale locale) throws IOException;
String[] getBundledFontFilenames();
/** Returns the appropriate font for the given language. */
Font getFontForLanguage(String language);
/** Returns the current user interface font. */
Font getUiFont();
/** Sets the user interface font appropriately for the given language. */
void setUiFontForLanguage(String language);
}

View File

@@ -8,26 +8,43 @@ import java.util.Locale;
public interface I18n {
/** Returns the named string, translated for the current i18n locale. */
String tr(String name);
/** Returns the i18n locale. This may not match the system locale. */
Locale getLocale();
/** Sets the i18n locale. */
void setLocale(Locale locale);
/** Loads the i18n locale from Briar/Data/locale.cfg. */
void loadLocale() throws IOException;
/** Saves the i18n locale to Briar/Data/locale.cfg. */
void saveLocale() throws IOException;
/** Saves the i18n locale to the given file. */
void saveLocale(File dir) throws IOException;
/** Returns the ComponentOrientation of the current i18n locale. */
ComponentOrientation getComponentOrientation();
/** Registers a listener for changes to the i18n locale. */
void addListener(Listener l);
/** Unregisters a listener for changes to the i18n locale. */
void removeListener(Listener l);
/**
* Implemented by classes that wish to be informed of changes to the i18n
* locale.
*/
public interface Listener {
/**
* Called whenever the i18n locale changes.
* @param uiFont The user interface font for the new locale.
*/
void localeChanged(Font uiFont);
}
}

View File

@@ -1,5 +1,6 @@
package net.sf.briar.api.i18n;
/** A named translatable string. */
public class Stri18ng {
private static final String HTML_OPEN_LEFT = "<html><body align='left'>";
@@ -15,16 +16,23 @@ public class Stri18ng {
this.i18n = i18n;
}
/** Returns the string translated for the current i18n locale. */
public String tr() {
return i18n.tr(name);
}
/** Returns the string, translated for the current i18n locale, as HTML. */
public String html() {
if(i18n.getComponentOrientation().isLeftToRight())
return HTML_OPEN_LEFT + i18n.tr(name) + HTML_CLOSE;
else return HTML_OPEN_RIGHT + i18n.tr(name) + HTML_CLOSE;
}
/**
* Returns the string, translated for the current locale, as HTML.
* @param paras Additional (pre-translated) paragraphs that should be
* appended to the HTML.
*/
public String html(String... paras) {
StringBuilder s = new StringBuilder();
if(i18n.getComponentOrientation().isLeftToRight())

View File

@@ -3,8 +3,10 @@ package net.sf.briar.api.invitation;
import java.io.File;
import java.util.List;
/** A progress callback for creating an invitation. */
public interface InvitationCallback {
/** Returns true if the process has been cancelled by the user. */
boolean isCancelled();
void copyingFile(File f);

View File

@@ -2,6 +2,7 @@ package net.sf.briar.api.invitation;
import java.io.File;
/** Provides the parameters for creating an invitation. */
public interface InvitationParameters {
boolean shouldCreateExe();
@@ -12,5 +13,5 @@ public interface InvitationParameters {
File getChosenLocation();
String[] getBundledFontFilenames();
File getSetupDat();
}

View File

@@ -2,10 +2,13 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Uniquely identifies a pseudonymous author. */
public class AuthorId {
public static final int LENGTH = 32;
// FIXME: Replace this with an isSelf() method that compares an AuthorId
// to any and all local AuthorIds.
public static final AuthorId SELF = new AuthorId(new byte[] {
0, 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

View File

@@ -1,13 +1,24 @@
package net.sf.briar.api.protocol;
/** A batch of messages up to CAPACITY bytes in total size. */
public interface Batch {
public static final long CAPACITY = 1024L * 1024L;
/** Prepares the batch for transmission and generates its identifier. */
public void seal();
/**
* Returns the batch's unique identifier. Cannot be called before seal().
*/
BatchId getId();
/** Returns the size of the batch in bytes. */
long getSize();
/** Returns the messages contained in the batch. */
Iterable<Message> getMessages();
/** Adds a message to the batch. Cannot be called after seal(). */
void addMessage(Message m);
}

View File

@@ -2,6 +2,7 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Uniquely identifies a batch of messages. */
public class BatchId {
public static final int LENGTH = 32;

View File

@@ -1,16 +1,39 @@
package net.sf.briar.api.protocol;
/** A bundle of acknowledgements, subscriptions, and batches of messages. */
public interface Bundle {
/** Prepares the bundle for transmission and generates its identifier. */
public void seal();
/**
* Returns the bundle's unique identifier. Cannot be called before seal().
*/
BundleId getId();
/** Returns the bundle's capacity in bytes. */
long getCapacity();
/** Returns the bundle's size in bytes. */
long getSize();
/** Returns the acknowledgements contained in the bundle. */
Iterable<BatchId> getAcks();
/** Adds an acknowledgement to the bundle. Cannot be called after seal(). */
void addAck(BatchId b);
/** Returns the subscriptions contained in the bundle. */
Iterable<GroupId> getSubscriptions();
/** Adds a subscription to the bundle. Cannot be called after seal(). */
void addSubscription(GroupId g);
/** Returns the batches of messages contained in the bundle. */
Iterable<Batch> getBatches();
/**
* Adds a batch of messages to the bundle. Cannot be called after seal().
*/
void addBatch(Batch b);
}

View File

@@ -2,6 +2,7 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Uniquely identifies a group to which a user may subscribe. */
public class GroupId {
public static final int LENGTH = 32;

View File

@@ -1,13 +1,28 @@
package net.sf.briar.api.protocol;
public interface Message {
/** Returns the message's unique identifier. */
MessageId getId();
/**
* Returns the message's parent, or MessageId.NONE if this is the first
* message in a thread.
*/
MessageId getParent();
/** Returns the group to which the message belongs. */
GroupId getGroup();
/** Returns the message's author. */
AuthorId getAuthor();
/** Returns the timestamp created by the message's author. */
long getTimestamp();
/** Returns the size of the message in bytes. */
int getSize();
/** Returns the message in wire format. */
byte[] getBody();
}

View File

@@ -2,8 +2,10 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Uniquely identifies a message. */
public class MessageId {
/** Used to indicate that the first message in a thread has no parent. */
public static final MessageId NONE = new MessageId(new byte[] {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

View File

@@ -2,8 +2,10 @@ package net.sf.briar.api.setup;
import java.io.File;
/** A progress callback for the installation process. */
public interface SetupCallback {
/** Returns true if the process has been cancelled by the user. */
boolean isCancelled();
void extractingFile(File f);

View File

@@ -2,11 +2,10 @@ package net.sf.briar.api.setup;
import java.io.File;
/** Provides the parameters for the installation process. */
public interface SetupParameters {
File getChosenLocation();
String[] getBundledFontFilenames();
long getExeHeaderSize();
}

View File

@@ -58,13 +58,6 @@ public class FontManagerImpl implements FontManager {
return Font.getFont(attr);
}
public String[] getBundledFontFilenames() {
String[] names = new String[BUNDLED_FONTS.length];
for(int i = 0; i < BUNDLED_FONTS.length; i++)
names[i] = BUNDLED_FONTS[i].filename;
return names;
}
public Font getFontForLanguage(String language) {
assert defaultFont != null;
Font font = fonts.get(language);

View File

@@ -94,8 +94,7 @@ class InvitationWorker implements Runnable {
}
private void copyInstaller(File dest) throws IOException {
File root = FileUtils.getBriarDirectory();
File src = new File(root, "Data/setup.dat");
File src = parameters.getSetupDat();
if(!src.exists() || !src.isFile())
throw new IOException("File not found: " + src.getPath());
callback.copyingFile(dest);

View File

@@ -27,8 +27,7 @@ public class SetupMain {
new AlreadyInstalledPanel(wizard, i18n);
new InstructionsPanel(wizard, i18n);
LocationPanel locationPanel = new LocationPanel(wizard, i18n);
SetupParameters parameters =
new SetupParametersImpl(locationPanel, fontManager);
SetupParameters parameters = new SetupParametersImpl(locationPanel);
new SetupWorkerPanel(wizard, workerFactory, parameters, i18n);
fontManager.initialize(Locale.getDefault());

View File

@@ -4,9 +4,13 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicInteger;
public class TestUtils {
private static final AtomicInteger nextTestDir =
new AtomicInteger((int) (Math.random() * 1000 * 1000));
public static void delete(File f) throws IOException {
if(f.isDirectory()) for(File child : f.listFiles()) delete(child);
f.delete();
@@ -19,4 +23,10 @@ public class TestUtils {
out.flush();
out.close();
}
public static File getTestDirectory() {
int name = nextTestDir.getAndIncrement();
File testDir = new File("test.tmp/" + name);
return testDir;
}
}

View File

@@ -22,7 +22,7 @@ public class SetupWorkerTest extends TestCase {
private static final int HEADER_SIZE = 1234;
private final File testDir = new File("test.tmp");
private final File testDir = TestUtils.getTestDirectory();
private final File jar = new File(testDir, "test.jar");
@Before

View File

@@ -18,7 +18,7 @@ import org.junit.Test;
public class FileUtilsTest extends TestCase {
private final File testDir = new File("test.tmp");
private final File testDir = TestUtils.getTestDirectory();
@Before
public void setUp() {

View File

@@ -24,7 +24,7 @@ import org.junit.Test;
public class ZipUtilsTest extends TestCase {
private final File testDir = new File("test.tmp");
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");

View File

@@ -2,10 +2,8 @@ package net.sf.briar.ui.invitation;
import java.io.File;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.invitation.InvitationParameters;
import com.google.inject.Inject;
import net.sf.briar.util.FileUtils;
class InvitationParametersImpl implements InvitationParameters {
@@ -13,17 +11,14 @@ class InvitationParametersImpl implements InvitationParameters {
private final OperatingSystemPanel osPanel;
private final PasswordPanel passwordPanel;
private final LocationPanel locationPanel;
private final FontManager fontManager;
@Inject
InvitationParametersImpl(ExistingUserPanel existingUserPanel,
OperatingSystemPanel osPanel, PasswordPanel passwordPanel,
LocationPanel locationPanel, FontManager fontManager) {
LocationPanel locationPanel) {
this.existingUserPanel = existingUserPanel;
this.osPanel = osPanel;
this.passwordPanel = passwordPanel;
this.locationPanel = locationPanel;
this.fontManager = fontManager;
}
public boolean shouldCreateExe() {
@@ -44,7 +39,7 @@ class InvitationParametersImpl implements InvitationParameters {
return locationPanel.getChosenDirectory();
}
public String[] getBundledFontFilenames() {
return fontManager.getBundledFontFilenames();
public File getSetupDat() {
return FileUtils.getBriarDirectory();
}
}

View File

@@ -5,13 +5,10 @@ import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.DirectoryChooserPanel;
import net.sf.briar.ui.wizard.Wizard;
import com.google.inject.Inject;
class LocationPanel extends DirectoryChooserPanel {
private static final long serialVersionUID = 3788640725729516888L;
@Inject
LocationPanel(Wizard wizard, I18n i18n) {
super(wizard, "Location", "Password", "InvitationWorker",
new Stri18ng("INVITATION_LOCATION_TITLE", i18n),

View File

@@ -15,8 +15,6 @@ import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.Wizard;
import net.sf.briar.ui.wizard.WizardPanel;
import com.google.inject.Inject;
class OperatingSystemPanel extends WizardPanel {
private static final long serialVersionUID = -8370132633634629466L;
@@ -26,7 +24,6 @@ class OperatingSystemPanel extends WizardPanel {
private final JRadioButton windowsButton, macButton, linuxButton;
private final JRadioButton unknownButton;
@Inject
OperatingSystemPanel(Wizard wizard, I18n i18n) {
super(wizard, "OperatingSystem");
question = new Stri18ng("INVITATION_OPERATING_SYSTEM", i18n);

View File

@@ -17,8 +17,6 @@ import net.sf.briar.api.i18n.Stri18ng;
import net.sf.briar.ui.wizard.Wizard;
import net.sf.briar.ui.wizard.WizardPanel;
import com.google.inject.Inject;
public class PasswordPanel extends WizardPanel {
private static final long serialVersionUID = -1012132977732308293L;
@@ -28,7 +26,6 @@ public class PasswordPanel extends WizardPanel {
private final JLabel introLabel, enterPasswordLabel, confirmPasswordLabel;
private final JPasswordField password1, password2;
@Inject
PasswordPanel(Wizard wizard, ExistingUserPanel existingUserPanel,
I18n i18n) {
super(wizard, "Password");

View File

@@ -1,6 +1,5 @@
package net.sf.briar.ui.invitation;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.i18n.I18n;
import net.sf.briar.api.invitation.InvitationParameters;
import net.sf.briar.api.invitation.InvitationWorkerFactory;
@@ -15,7 +14,7 @@ public class UiInvitationModule extends AbstractModule {
protected void configure() {}
@Provides @Singleton
InvitationWizard getInvitationWizard(I18n i18n, FontManager fontManager,
InvitationWizard getInvitationWizard(I18n i18n,
InvitationWorkerFactory workerFactory) {
InvitationWizard wizard = new InvitationWizard(i18n);
new IntroPanel(wizard, i18n);
@@ -25,7 +24,7 @@ public class UiInvitationModule extends AbstractModule {
new PasswordPanel(wizard, userPanel, i18n);
LocationPanel locationPanel = new LocationPanel(wizard, i18n);
InvitationParameters parameters = new InvitationParametersImpl(
userPanel, osPanel, passwordPanel, locationPanel, fontManager);
userPanel, osPanel, passwordPanel, locationPanel);
new InvitationWorkerPanel(wizard, workerFactory, parameters, i18n);
return wizard;
}

View File

@@ -2,7 +2,6 @@ package net.sf.briar.ui.setup;
import java.io.File;
import net.sf.briar.api.i18n.FontManager;
import net.sf.briar.api.setup.SetupParameters;
class SetupParametersImpl implements SetupParameters {
@@ -10,21 +9,15 @@ class SetupParametersImpl implements SetupParameters {
private static final int EXE_HEADER_SIZE = 62976;
private final LocationPanel locationPanel;
private final FontManager fontManager;
SetupParametersImpl(LocationPanel locationPanel, FontManager fontManager) {
SetupParametersImpl(LocationPanel locationPanel) {
this.locationPanel = locationPanel;
this.fontManager = fontManager;
}
public File getChosenLocation() {
return locationPanel.getChosenDirectory();
}
public String[] getBundledFontFilenames() {
return fontManager.getBundledFontFilenames();
}
public long getExeHeaderSize() {
return EXE_HEADER_SIZE;
}

View File

@@ -22,8 +22,7 @@ public class UiSetupModule extends AbstractModule {
new AlreadyInstalledPanel(wizard, i18n);
new InstructionsPanel(wizard, i18n);
LocationPanel locationPanel = new LocationPanel(wizard, i18n);
SetupParameters parameters =
new SetupParametersImpl(locationPanel, fontManager);
SetupParameters parameters = new SetupParametersImpl(locationPanel);
new SetupWorkerPanel(wizard, workerFactory, parameters, i18n);
return wizard;
}