Add an option to debug builds to create fake test data

This commit is contained in:
Torsten Grote
2017-09-21 17:16:42 -03:00
parent 237759aac0
commit 1bf0fdfa81
47 changed files with 676 additions and 126 deletions

View File

@@ -26,4 +26,5 @@ public interface BriarCoreEagerSingletons {
void inject(PrivateGroupModule.EagerSingletons init);
void inject(SharingModule.EagerSingletons init);
}

View File

@@ -10,6 +10,7 @@ import org.briarproject.briar.messaging.MessagingModule;
import org.briarproject.briar.privategroup.PrivateGroupModule;
import org.briarproject.briar.privategroup.invitation.GroupInvitationModule;
import org.briarproject.briar.sharing.SharingModule;
import org.briarproject.briar.test.TestModule;
import dagger.Module;
@@ -23,7 +24,8 @@ import dagger.Module;
IntroductionModule.class,
MessagingModule.class,
PrivateGroupModule.class,
SharingModule.class
SharingModule.class,
TestModule.class
})
public class BriarCoreModule {

View File

@@ -0,0 +1,68 @@
package org.briarproject.briar.test;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
public interface TestData {
String AUTHOR_NAMES[] = {
"Thales",
"Pythagoras",
"Plato",
"Aristotle",
"Euclid",
"Archimedes",
"Hipparchus",
"Ptolemy",
"Sun Tzu",
"Ibrahim ibn Sinan",
"Muhammad Al-Karaji",
"Yang Hui",
"René Descartes",
"Pierre de Fermat",
"Blaise Pascal",
"Jacob Bernoulli",
"Christian Goldbach",
"Leonhard Euler",
"Joseph Louis Lagrange",
"Pierre-Simon Laplace",
"Joseph Fourier",
"Carl Friedrich Gauss",
"Charles Babbage",
"George Boole",
"John Venn",
"Gottlob Frege",
"Henri Poincaré",
"David Hilbert",
"Bertrand Russell",
"John von Neumann",
"Kurt Gödel",
"Alan Turing",
"Benoît Mandelbrot",
"John Nash",
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomString(MAX_AUTHOR_NAME_LENGTH),
};
String GROUP_NAMES[] = {
"Private Messengers",
"The Darknet",
"Bletchley Park",
"Acropolis",
"General Discussion",
"The Undiscovered Country",
"The Place to Be",
"Forum Romanum",
getRandomString(MAX_FORUM_NAME_LENGTH),
getRandomString(MAX_FORUM_NAME_LENGTH),
getRandomString(MAX_FORUM_NAME_LENGTH),
getRandomString(MAX_FORUM_NAME_LENGTH),
};
}

View File

@@ -0,0 +1,409 @@
package org.briarproject.briar.test;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.KeyPair;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.AuthorFactory;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.plugin.BluetoothConstants;
import org.briarproject.bramble.api.plugin.LanTcpConstants;
import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.properties.TransportPropertyManager;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.briar.api.blog.Blog;
import org.briarproject.briar.api.blog.BlogManager;
import org.briarproject.briar.api.blog.BlogPost;
import org.briarproject.briar.api.blog.BlogPostFactory;
import org.briarproject.briar.api.client.MessageTracker;
import org.briarproject.briar.api.forum.Forum;
import org.briarproject.briar.api.forum.ForumManager;
import org.briarproject.briar.api.forum.ForumPost;
import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessage;
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
import org.briarproject.briar.api.test.TestDataCreator;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.test.TestData.AUTHOR_NAMES;
import static org.briarproject.briar.test.TestData.GROUP_NAMES;
public class TestDataCreatorImpl implements TestDataCreator {
private final static int NUM_CONTACTS = 20;
private final static int NUM_PRIVATE_MSGS = 15;
private final static int NUM_BLOG_POSTS = 30;
private final static int NUM_FORUMS = 3;
private final static int NUM_FORUM_POSTS = 30;
private final Logger LOG =
Logger.getLogger(TestDataCreatorImpl.class.getName());
private final AuthorFactory authorFactory;
private final Clock clock;
private final PrivateMessageFactory privateMessageFactory;
private final ClientHelper clientHelper;
private final MessageTracker messageTracker;
private final BlogPostFactory blogPostFactory;
private final CryptoComponent cryptoComponent;
private final DatabaseComponent db;
private final IdentityManager identityManager;
private final ContactManager contactManager;
private final TransportPropertyManager transportPropertyManager;
private final MessagingManager messagingManager;
private final BlogManager blogManager;
private final ForumManager forumManager;
@IoExecutor
private final Executor ioExecutor;
private final Random random = new Random();
private final Map<Contact, LocalAuthor> localAuthors =
new HashMap<Contact, LocalAuthor>();
@Inject
TestDataCreatorImpl(AuthorFactory authorFactory, Clock clock,
PrivateMessageFactory privateMessageFactory,
ClientHelper clientHelper, MessageTracker messageTracker,
BlogPostFactory blogPostFactory, CryptoComponent cryptoComponent,
DatabaseComponent db, IdentityManager identityManager,
ContactManager contactManager,
TransportPropertyManager transportPropertyManager,
MessagingManager messagingManager, BlogManager blogManager,
ForumManager forumManager, @IoExecutor Executor ioExecutor) {
this.authorFactory = authorFactory;
this.clock = clock;
this.privateMessageFactory = privateMessageFactory;
this.clientHelper = clientHelper;
this.messageTracker = messageTracker;
this.blogPostFactory = blogPostFactory;
this.cryptoComponent = cryptoComponent;
this.db = db;
this.identityManager = identityManager;
this.contactManager = contactManager;
this.transportPropertyManager = transportPropertyManager;
this.messagingManager = messagingManager;
this.blogManager = blogManager;
this.forumManager = forumManager;
this.ioExecutor = ioExecutor;
}
public void createTestData() {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
try {
createTestDataOnDbExecutor();
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) {
LOG.log(WARNING, "Creating test data failed", e);
}
}
}
});
}
@IoExecutor
private void createTestDataOnDbExecutor() throws DbException {
List<Contact> contacts = createContacts();
createPrivateMessages(contacts);
createBlogPosts(contacts);
List<Forum> forums = createForums(contacts);
for (Forum forum : forums) {
createRandomForumPosts(forum, contacts);
}
}
private List<Contact> createContacts() throws DbException {
List<Contact> contacts = new ArrayList<Contact>(NUM_CONTACTS);
LocalAuthor localAuthor = identityManager.getLocalAuthor();
for (int i = 0; i < NUM_CONTACTS; i++) {
Contact contact = addRandomContact(localAuthor);
contacts.add(contact);
}
return contacts;
}
private Contact addRandomContact(LocalAuthor localAuthor)
throws DbException {
// prepare to add contact
LocalAuthor author = getRandomAuthor();
SecretKey secretKey = getSecretKey();
long timestamp = clock.currentTimeMillis();
boolean verified = random.nextBoolean();
// prepare transport properties
Map<TransportId, TransportProperties> props =
getRandomTransportProperties();
Contact contact;
Transaction txn = db.startTransaction(false);
try {
ContactId contactId = contactManager
.addContact(txn, author, localAuthor.getId(), secretKey,
timestamp, true, verified, true);
transportPropertyManager.addRemoteProperties(txn, contactId, props);
contact = db.getContact(txn, contactId);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
if (LOG.isLoggable(INFO)) {
LOG.info("Added contact " + author.getName());
LOG.info("with transport properties: " + props.toString());
}
localAuthors.put(contact, author);
return contact;
}
private LocalAuthor getRandomAuthor() {
int i = random.nextInt(AUTHOR_NAMES.length);
String authorName = AUTHOR_NAMES[i];
KeyPair keyPair = cryptoComponent.generateSignatureKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
return authorFactory
.createLocalAuthor(authorName, publicKey, privateKey);
}
private SecretKey getSecretKey() {
byte[] b = new byte[SecretKey.LENGTH];
random.nextBytes(b);
return new SecretKey(b);
}
private Map<TransportId, TransportProperties> getRandomTransportProperties() {
Map<TransportId, TransportProperties> props =
new HashMap<TransportId, TransportProperties>();
// Bluetooth
TransportProperties bt = new TransportProperties();
String btAddress = getRandomBluetoothAddress();
bt.put(BluetoothConstants.PROP_ADDRESS, btAddress);
props.put(BluetoothConstants.ID, bt);
// LAN
TransportProperties lan = new TransportProperties();
String lanAddress = getRandomLanAddress();
lan.put(LanTcpConstants.PROP_IP_PORTS, lanAddress);
props.put(LanTcpConstants.ID, lan);
// Tor
TransportProperties tor = new TransportProperties();
String torAddress = getRandomTorAddress();
tor.put(TorConstants.PROP_ONION, torAddress);
props.put(TorConstants.ID, tor);
return props;
}
private String getRandomBluetoothAddress() {
byte[] mac = new byte[6];
random.nextBytes(mac);
StringBuilder sb = new StringBuilder(18);
for (byte b : mac) {
if (sb.length() > 0) sb.append(":");
sb.append(String.format("%02x", b));
}
return sb.toString();
}
private String getRandomLanAddress() {
StringBuilder sb = new StringBuilder();
// address
for (int i = 0; i < 4; i++) {
if (sb.length() > 0) sb.append(".");
sb.append(random.nextInt(256));
}
// port
sb.append(":");
sb.append(1024 + random.nextInt(50000));
return sb.toString();
}
private String getRandomTorAddress() {
StringBuilder sb = new StringBuilder();
// address
for (int i = 0; i < 16; i++) {
if (random.nextBoolean())
sb.append(2 + random.nextInt(5));
else
sb.append((char) (random.nextInt(26) + 'a'));
}
return sb.toString();
}
private void createPrivateMessages(List<Contact> contacts)
throws DbException {
for (Contact contact : contacts) {
Group group = messagingManager.getContactGroup(contact);
for (int i = 0; i < NUM_PRIVATE_MSGS; i++) {
try {
createPrivateMessage(group.getId(), i);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
}
if (LOG.isLoggable(INFO)) {
LOG.info("Created " + NUM_PRIVATE_MSGS +
" private messages per contact.");
}
}
private void createPrivateMessage(GroupId groupId, int num)
throws DbException, FormatException {
long timestamp = clock.currentTimeMillis() - num * 60 * 1000;
String body = getRandomText();
PrivateMessage m = privateMessageFactory
.createPrivateMessage(groupId, timestamp, body);
boolean local = random.nextBoolean();
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", timestamp);
meta.put("local", local);
meta.put("read", local); // all local messages are read
Transaction txn = db.startTransaction(false);
try {
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
if (local) messageTracker.trackOutgoingMessage(txn, m.getMessage());
else messageTracker.trackIncomingMessage(txn, m.getMessage());
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
}
private void createBlogPosts(List<Contact> contacts)
throws DbException {
for (int i = 0; i < NUM_BLOG_POSTS; i++) {
Contact contact = contacts.get(random.nextInt(contacts.size()));
LocalAuthor author = localAuthors.get(contact);
addBlogPost(author, i);
}
if (LOG.isLoggable(INFO)) {
LOG.info("Created " + NUM_BLOG_POSTS + " blog posts.");
}
}
private void addBlogPost(LocalAuthor author, int num) throws DbException {
Blog blog = blogManager.getPersonalBlog(author);
long timestamp = clock.currentTimeMillis() - num * 60 * 1000;
String body = getRandomText();
try {
BlogPost blogPost = blogPostFactory
.createBlogPost(blog.getId(), timestamp, null, author,
body);
blogManager.addLocalPost(blogPost);
} catch (FormatException e) {
throw new RuntimeException(e);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
private List<Forum> createForums(List<Contact> contacts)
throws DbException {
List<Forum> forums = new ArrayList<Forum>(NUM_FORUMS);
for (int i = 0; i < NUM_FORUMS; i++) {
// create forum
String name = GROUP_NAMES[random.nextInt(GROUP_NAMES.length)];
Forum forum = forumManager.addForum(name);
// share with all contacts
Transaction txn = db.startTransaction(false);
try {
for (Contact c : contacts) {
db.setGroupVisibility(txn, c.getId(), forum.getId(),
SHARED);
}
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
}
forums.add(forum);
}
if (LOG.isLoggable(INFO)) {
LOG.info("Created " + NUM_FORUMS + " forums with " +
NUM_FORUM_POSTS + " posts each.");
}
return forums;
}
private void createRandomForumPosts(Forum forum, List<Contact> contacts)
throws DbException {
List<ForumPost> posts = new ArrayList<ForumPost>();
for (int i = 0; i < NUM_FORUM_POSTS; i++) {
Contact contact = contacts.get(random.nextInt(contacts.size()));
LocalAuthor author = localAuthors.get(contact);
long timestamp = clock.currentTimeMillis() - i * 60 * 1000;
String body = getRandomText();
MessageId parent = null;
if (random.nextBoolean() && posts.size() > 0) {
ForumPost parentPost =
posts.get(random.nextInt(posts.size()));
parent = parentPost.getMessage().getId();
}
ForumPost post = forumManager
.createLocalPost(forum.getId(), body, timestamp, parent,
author);
posts.add(post);
forumManager.addLocalPost(post);
if (random.nextBoolean()) {
forumManager
.setReadFlag(forum.getId(), post.getMessage().getId(),
false);
}
}
}
private String getRandomText() {
int minLength = 3 + random.nextInt(500);
int maxWordLength = 15;
StringBuilder sb = new StringBuilder();
while (sb.length() < minLength) {
if (sb.length() > 0) sb.append(' ');
sb.append(getRandomString(random.nextInt(maxWordLength) + 1));
}
if (random.nextBoolean()) {
sb.append(" \uD83D\uDC96 \uD83E\uDD84 \uD83C\uDF08");
}
return sb.toString();
}
}

View File

@@ -0,0 +1,19 @@
package org.briarproject.briar.test;
import org.briarproject.briar.api.test.TestDataCreator;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class TestModule {
@Provides
@Singleton
TestDataCreator getTestDataCreator(TestDataCreatorImpl testDataCreator) {
return testDataCreator;
}
}

View File

@@ -39,7 +39,7 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_K
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR;
import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR_ID;
import static org.briarproject.briar.api.blog.BlogConstants.KEY_AUTHOR_NAME;

View File

@@ -25,7 +25,7 @@ import static junit.framework.Assert.assertNotNull;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.blog.MessageType.COMMENT;
import static org.briarproject.briar.api.blog.MessageType.POST;
import static org.briarproject.briar.api.blog.MessageType.WRAPPED_COMMENT;

View File

@@ -18,6 +18,7 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.system.SystemClock;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.blog.Blog;
import org.briarproject.briar.api.blog.BlogFactory;
import org.briarproject.briar.test.BriarTestCase;
@@ -64,7 +65,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
private final BlogFactory blogFactory = context.mock(BlogFactory.class);
private final ClientHelper clientHelper = context.mock(ClientHelper.class);
private final Author author;
private final String body = TestUtils.getRandomString(42);
private final String body = StringUtils.getRandomString(42);
public BlogPostValidatorTest() {
GroupId groupId = new GroupId(TestUtils.getRandomId());

View File

@@ -19,6 +19,7 @@ import org.briarproject.bramble.api.sync.ValidationManager.MessageValidator;
import org.briarproject.bramble.test.CaptureArgumentAction;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.ByteUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.client.MessageQueueManager.IncomingQueueMessageHook;
import org.briarproject.briar.api.client.MessageQueueManager.QueueMessageValidator;
import org.briarproject.briar.api.client.QueueMessage;
@@ -45,7 +46,7 @@ public class MessageQueueManagerImplTest extends BriarTestCase {
private final GroupId groupId = new GroupId(TestUtils.getRandomId());
private final ClientId clientId =
new ClientId(TestUtils.getRandomString(5));
new ClientId(StringUtils.getRandomString(5));
private final byte[] descriptor = new byte[0];
private final Group group = new Group(groupId, clientId, descriptor);
private final long timestamp = System.currentTimeMillis();

View File

@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.sync.InvalidMessageException;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.test.ValidatorTestCase;
import org.briarproject.bramble.util.StringUtils;
import org.jmock.Expectations;
import org.junit.Test;
@@ -30,12 +31,12 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
private final MessageId parentId = new MessageId(TestUtils.getRandomId());
private final String authorName =
TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
StringUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
private final byte[] authorPublicKey =
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
private final BdfList authorList = BdfList.of(authorName, authorPublicKey);
private final String content =
TestUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH);
StringUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH);
private final byte[] signature =
TestUtils.getRandomBytes(MAX_SIGNATURE_LENGTH);
private final AuthorId authorId = new AuthorId(TestUtils.getRandomId());
@@ -165,7 +166,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
@Test
public void testAcceptsMinLengthAuthorName() throws Exception {
final String shortAuthorName = TestUtils.getRandomString(1);
final String shortAuthorName = StringUtils.getRandomString(1);
BdfList shortNameAuthorList =
BdfList.of(shortAuthorName, authorPublicKey);
final Author shortNameAuthor =
@@ -190,7 +191,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
@Test(expected = FormatException.class)
public void testRejectsTooLongAuthorName() throws Exception {
String invalidAuthorName =
TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH + 1);
StringUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH + 1);
BdfList invalidAuthorList =
BdfList.of(invalidAuthorName, authorPublicKey);
ForumPostValidator v = new ForumPostValidator(authorFactory,
@@ -278,7 +279,7 @@ public class ForumPostValidatorTest extends ValidatorTestCase {
@Test(expected = FormatException.class)
public void testRejectsTooLongContent() throws Exception {
String invalidContent =
TestUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH + 1);
StringUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH + 1);
context.checking(new Expectations() {{
oneOf(authorFactory).createAuthor(authorName, authorPublicKey);

View File

@@ -27,6 +27,7 @@ import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.test.TestDatabaseModule;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.api.introduction.IntroductionManager;
import org.briarproject.briar.api.introduction.IntroductionMessage;
@@ -479,7 +480,7 @@ public class IntroductionIntegrationTest
new BdfEntry(TYPE, TYPE_REQUEST),
new BdfEntry(SESSION_ID, sessionId),
new BdfEntry(GROUP_ID, group.getId()),
new BdfEntry(NAME, TestUtils.getRandomString(42)),
new BdfEntry(NAME, StringUtils.getRandomString(42)),
new BdfEntry(PUBLIC_KEY,
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH))
);

View File

@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.client.MessageTracker;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.test.BriarTestCase;
@@ -81,7 +82,7 @@ public class IntroductionManagerImplTest extends BriarTestCase {
introducee2 =
new Contact(contactId2, author2, localAuthorId2, true, true);
ClientId clientId = new ClientId(TestUtils.getRandomString(5));
ClientId clientId = new ClientId(StringUtils.getRandomString(5));
introductionGroup1 = new Group(new GroupId(TestUtils.getRandomId()),
clientId, new byte[0]);
introductionGroup2 = new Group(new GroupId(TestUtils.getRandomId()),

View File

@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.system.SystemClock;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.test.BriarTestCase;
import org.jmock.Mockery;
@@ -59,7 +60,7 @@ public class IntroductionValidatorTest extends BriarTestCase {
public IntroductionValidatorTest() {
GroupId groupId = new GroupId(TestUtils.getRandomId());
ClientId clientId = new ClientId(TestUtils.getRandomString(5));
ClientId clientId = new ClientId(StringUtils.getRandomString(5));
byte[] descriptor = TestUtils.getRandomBytes(12);
group = new Group(groupId, clientId, descriptor);
@@ -83,11 +84,11 @@ public class IntroductionValidatorTest extends BriarTestCase {
@Test
public void testValidateProperIntroductionRequest() throws IOException {
final byte[] sessionId = TestUtils.getRandomId();
final String name = TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
final String name = StringUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
final byte[] publicKey =
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
final String text =
TestUtils.getRandomString(MAX_INTRODUCTION_MESSAGE_LENGTH);
StringUtils.getRandomString(MAX_INTRODUCTION_MESSAGE_LENGTH);
BdfList body = BdfList.of(TYPE_REQUEST, sessionId,
name, publicKey, text);
@@ -144,9 +145,9 @@ public class IntroductionValidatorTest extends BriarTestCase {
private BdfDictionary getValidIntroductionRequest() throws FormatException {
byte[] sessionId = TestUtils.getRandomId();
String name = TestUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
String name = StringUtils.getRandomString(MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
String text = TestUtils.getRandomString(MAX_MESSAGE_BODY_LENGTH);
String text = StringUtils.getRandomString(MAX_MESSAGE_BODY_LENGTH);
BdfDictionary msg = new BdfDictionary();
msg.put(TYPE, TYPE_REQUEST);
@@ -168,11 +169,11 @@ public class IntroductionValidatorTest extends BriarTestCase {
byte[] sessionId = TestUtils.getRandomId();
long time = clock.currentTimeMillis();
byte[] publicKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
String transportId = TestUtils
String transportId = StringUtils
.getRandomString(TransportId.MAX_TRANSPORT_ID_LENGTH);
BdfDictionary tProps = BdfDictionary.of(
new BdfEntry(TestUtils.getRandomString(MAX_PROPERTY_LENGTH),
TestUtils.getRandomString(MAX_PROPERTY_LENGTH))
new BdfEntry(StringUtils.getRandomString(MAX_PROPERTY_LENGTH),
StringUtils.getRandomString(MAX_PROPERTY_LENGTH))
);
BdfDictionary tp = BdfDictionary.of(
new BdfEntry(transportId, tProps)
@@ -230,7 +231,7 @@ public class IntroductionValidatorTest extends BriarTestCase {
throws IOException {
BdfDictionary msg = getValidIntroductionResponse(true);
BdfDictionary tp = msg.getDictionary(TRANSPORT);
tp.put(TestUtils
tp.put(StringUtils
.getRandomString(TransportId.MAX_TRANSPORT_ID_LENGTH), "X");
msg.put(TRANSPORT, tp);
@@ -260,11 +261,11 @@ public class IntroductionValidatorTest extends BriarTestCase {
byte[] sessionId = TestUtils.getRandomId();
long time = clock.currentTimeMillis();
byte[] publicKey = TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
String transportId = TestUtils
String transportId = StringUtils
.getRandomString(TransportId.MAX_TRANSPORT_ID_LENGTH);
BdfDictionary tProps = BdfDictionary.of(
new BdfEntry(TestUtils.getRandomString(MAX_PROPERTY_LENGTH),
TestUtils.getRandomString(MAX_PROPERTY_LENGTH))
new BdfEntry(StringUtils.getRandomString(MAX_PROPERTY_LENGTH),
StringUtils.getRandomString(MAX_PROPERTY_LENGTH))
);
BdfDictionary tp = BdfDictionary.of(
new BdfEntry(transportId, tProps)
@@ -310,7 +311,7 @@ public class IntroductionValidatorTest extends BriarTestCase {
BdfDictionary msg = BdfDictionary.of(
new BdfEntry(TYPE, TYPE_ACK),
new BdfEntry(SESSION_ID, TestUtils.getRandomId()),
new BdfEntry("garbage", TestUtils.getRandomString(255))
new BdfEntry("garbage", StringUtils.getRandomString(255))
);
BdfList body = BdfList.of(msg.getLong(TYPE), msg.getRaw(SESSION_ID),
msg.getString("garbage"));
@@ -357,7 +358,7 @@ public class IntroductionValidatorTest extends BriarTestCase {
BdfDictionary msg = BdfDictionary.of(
new BdfEntry(TYPE, TYPE_ABORT),
new BdfEntry(SESSION_ID, TestUtils.getRandomId()),
new BdfEntry("garbage", TestUtils.getRandomString(255))
new BdfEntry("garbage", StringUtils.getRandomString(255))
);
BdfList body = BdfList.of(msg.getLong(TYPE), msg.getRaw(SESSION_ID),
msg.getString("garbage"));

View File

@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.client.MessageQueueManager;
import org.briarproject.briar.api.client.SessionId;
import org.briarproject.briar.test.BriarTestCase;
@@ -60,7 +61,7 @@ public class MessageSenderTest extends BriarTestCase {
final Transaction txn = new Transaction(null, false);
final Group privateGroup =
new Group(new GroupId(TestUtils.getRandomId()),
new ClientId(TestUtils.getRandomString(5)),
new ClientId(StringUtils.getRandomString(5)),
new byte[0]);
final SessionId sessionId = new SessionId(TestUtils.getRandomId());
byte[] mac = TestUtils.getRandomBytes(42);

View File

@@ -65,7 +65,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
@Test
public void testForumPostFitsIntoPacket() throws Exception {
// Create a maximum-length author
String authorName = TestUtils.getRandomString(
String authorName = StringUtils.getRandomString(
MAX_AUTHOR_NAME_LENGTH);
byte[] authorPublic = new byte[MAX_PUBLIC_KEY_LENGTH];
PrivateKey privateKey = crypto.generateSignatureKeyPair().getPrivate();
@@ -76,7 +76,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
GroupId groupId = new GroupId(getRandomId());
long timestamp = Long.MAX_VALUE;
MessageId parent = new MessageId(getRandomId());
String body = TestUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH);
String body = StringUtils.getRandomString(MAX_FORUM_POST_BODY_LENGTH);
ForumPost post = forumPostFactory.createPost(groupId,
timestamp, parent, author, body);
// Check the size of the serialised message

View File

@@ -4,8 +4,8 @@ import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.BdfMessageContext;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.test.ValidatorTestCase;
import org.briarproject.bramble.util.StringUtils;
import org.junit.Test;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
@@ -48,7 +48,7 @@ public class PrivateMessageValidatorTest extends ValidatorTestCase {
PrivateMessageValidator v = new PrivateMessageValidator(clientHelper,
metadataEncoder, clock);
String invalidContent =
TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH + 1);
StringUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH + 1);
v.validateMessage(message, group, BdfList.of(invalidContent));
}
@@ -57,7 +57,7 @@ public class PrivateMessageValidatorTest extends ValidatorTestCase {
PrivateMessageValidator v = new PrivateMessageValidator(clientHelper,
metadataEncoder, clock);
String content =
TestUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH);
StringUtils.getRandomString(MAX_PRIVATE_MESSAGE_BODY_LENGTH);
BdfMessageContext messageContext =
v.validateMessage(message, group, BdfList.of(content));
assertExpectedContext(messageContext);

View File

@@ -26,7 +26,7 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_K
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.privategroup.GroupMessageFactory.SIGNING_LABEL_JOIN;
import static org.briarproject.briar.api.privategroup.GroupMessageFactory.SIGNING_LABEL_POST;
import static org.briarproject.briar.api.privategroup.MessageType.JOIN;

View File

@@ -48,7 +48,7 @@ import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID;

View File

@@ -23,7 +23,7 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_K
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_INVITATION_MSG_LENGTH;
import static org.briarproject.briar.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH;

View File

@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.blog.Blog;
import org.jmock.Expectations;
import org.junit.Test;
@@ -19,14 +20,14 @@ import static org.briarproject.briar.sharing.MessageType.INVITE;
public class BlogSharingValidatorTest extends SharingValidatorTest {
private final AuthorId authorId = new AuthorId(getRandomId());
private final String authorName = TestUtils.getRandomString(42);
private final String authorName = StringUtils.getRandomString(42);
private final byte[] publicKey =
TestUtils.getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
private final Author author = new Author(authorId, authorName, publicKey);
private final Blog blog = new Blog(group, author, false);
private final BdfList descriptor = BdfList.of(authorName, publicKey, false);
private final String content =
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
StringUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
@Override
SharingValidator getValidator() {
@@ -88,7 +89,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
@Test
public void testAcceptsMinLengthBlogName() throws Exception {
String shortBlogName = TestUtils.getRandomString(1);
String shortBlogName = StringUtils.getRandomString(1);
BdfList validDescriptor = BdfList.of(shortBlogName, publicKey, false);
expectCreateBlog(shortBlogName, publicKey);
expectEncodeMetadata(INVITE);
@@ -101,7 +102,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
@Test(expected = FormatException.class)
public void testRejectsTooLongBlogName() throws Exception {
String invalidBlogName =
TestUtils.getRandomString(MAX_BLOG_NAME_LENGTH + 1);
StringUtils.getRandomString(MAX_BLOG_NAME_LENGTH + 1);
BdfList invalidDescriptor =
BdfList.of(invalidBlogName, publicKey, false);
v.validateMessage(message, group,
@@ -167,7 +168,7 @@ public class BlogSharingValidatorTest extends SharingValidatorTest {
@Test(expected = FormatException.class)
public void testRejectsTooLongContent() throws Exception {
String invalidContent =
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1);
StringUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1);
expectCreateBlog(authorName, publicKey);
v.validateMessage(message, group,
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,

View File

@@ -37,7 +37,7 @@ import java.util.Collection;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static org.briarproject.bramble.test.TestUtils.getRandomString;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

View File

@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.BdfMessageContext;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.test.TestUtils;
import org.briarproject.bramble.util.StringUtils;
import org.briarproject.briar.api.forum.Forum;
import org.jmock.Expectations;
import org.junit.Test;
@@ -16,12 +17,12 @@ import static org.briarproject.briar.sharing.MessageType.INVITE;
public class ForumSharingValidatorTest extends SharingValidatorTest {
private final String forumName =
TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH);
StringUtils.getRandomString(MAX_FORUM_NAME_LENGTH);
private final byte[] salt = TestUtils.getRandomBytes(FORUM_SALT_LENGTH);
private final Forum forum = new Forum(group, forumName, salt);
private final BdfList descriptor = BdfList.of(forumName, salt);
private final String content =
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
StringUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH);
@Override
SharingValidator getValidator() {
@@ -83,7 +84,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
@Test
public void testAcceptsMinLengthForumName() throws Exception {
String shortForumName = TestUtils.getRandomString(1);
String shortForumName = StringUtils.getRandomString(1);
BdfList validDescriptor = BdfList.of(shortForumName, salt);
expectCreateForum(shortForumName);
expectEncodeMetadata(INVITE);
@@ -96,7 +97,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
@Test(expected = FormatException.class)
public void testRejectsTooLongForumName() throws Exception {
String invalidForumName =
TestUtils.getRandomString(MAX_FORUM_NAME_LENGTH + 1);
StringUtils.getRandomString(MAX_FORUM_NAME_LENGTH + 1);
BdfList invalidDescriptor = BdfList.of(invalidForumName, salt);
v.validateMessage(message, group,
BdfList.of(INVITE.getValue(), previousMsgId, invalidDescriptor,
@@ -157,7 +158,7 @@ public class ForumSharingValidatorTest extends SharingValidatorTest {
@Test(expected = FormatException.class)
public void testRejectsTooLongContent() throws Exception {
String invalidContent =
TestUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1);
StringUtils.getRandomString(MAX_INVITATION_MESSAGE_LENGTH + 1);
expectCreateForum(forumName);
v.validateMessage(message, group,
BdfList.of(INVITE.getValue(), previousMsgId, descriptor,