mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
private group support for TestDataCreator
This commit is contained in:
@@ -19,9 +19,12 @@ public interface TestDataCreator {
|
|||||||
* @param numBlogPosts Number of blog posts to create.
|
* @param numBlogPosts Number of blog posts to create.
|
||||||
* @param numForums Number of forums to create.
|
* @param numForums Number of forums to create.
|
||||||
* @param numForumPosts Number of forum posts to create per forum.
|
* @param numForumPosts Number of forum posts to create per forum.
|
||||||
|
* @param numPrivateGroups Number of private groups to create.
|
||||||
|
* @param numPrivateGroupMessages Number of messages to create per private group.
|
||||||
*/
|
*/
|
||||||
void createTestData(int numContacts, int numPrivateMsgs, int avatarPercent,
|
void createTestData(int numContacts, int numPrivateMsgs, int avatarPercent,
|
||||||
int numBlogPosts, int numForums, int numForumPosts);
|
int numBlogPosts, int numForums, int numForumPosts,
|
||||||
|
int numPrivateGroups, int numPrivateGroupMessages);
|
||||||
|
|
||||||
@IoExecutor
|
@IoExecutor
|
||||||
Contact addContact(String name, boolean alias, boolean avatar)
|
Contact addContact(String name, boolean alias, boolean avatar)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.Contact;
|
|||||||
import org.briarproject.bramble.api.contact.ContactId;
|
import org.briarproject.bramble.api.contact.ContactId;
|
||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
|
import org.briarproject.bramble.api.crypto.PrivateKey;
|
||||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
@@ -38,6 +39,12 @@ import org.briarproject.briar.api.forum.ForumPost;
|
|||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||||
|
import org.briarproject.briar.api.privategroup.GroupMessage;
|
||||||
|
import org.briarproject.briar.api.privategroup.GroupMessageFactory;
|
||||||
|
import org.briarproject.briar.api.privategroup.PrivateGroup;
|
||||||
|
import org.briarproject.briar.api.privategroup.PrivateGroupFactory;
|
||||||
|
import org.briarproject.briar.api.privategroup.PrivateGroupManager;
|
||||||
|
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationFactory;
|
||||||
import org.briarproject.briar.api.test.TestAvatarCreator;
|
import org.briarproject.briar.api.test.TestAvatarCreator;
|
||||||
import org.briarproject.briar.api.test.TestDataCreator;
|
import org.briarproject.briar.api.test.TestDataCreator;
|
||||||
import org.briarproject.nullsafety.NotNullByDefault;
|
import org.briarproject.nullsafety.NotNullByDefault;
|
||||||
@@ -89,6 +96,10 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
private final MessagingManager messagingManager;
|
private final MessagingManager messagingManager;
|
||||||
private final BlogManager blogManager;
|
private final BlogManager blogManager;
|
||||||
private final ForumManager forumManager;
|
private final ForumManager forumManager;
|
||||||
|
private final PrivateGroupManager privateGroupManager;
|
||||||
|
private final PrivateGroupFactory privateGroupFactory;
|
||||||
|
private final GroupMessageFactory groupMessageFactory;
|
||||||
|
private final GroupInvitationFactory groupInvitationFactory;
|
||||||
private final TestAvatarCreator testAvatarCreator;
|
private final TestAvatarCreator testAvatarCreator;
|
||||||
private final AvatarMessageEncoder avatarMessageEncoder;
|
private final AvatarMessageEncoder avatarMessageEncoder;
|
||||||
|
|
||||||
@@ -111,6 +122,10 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
TransportPropertyManager transportPropertyManager,
|
TransportPropertyManager transportPropertyManager,
|
||||||
MessagingManager messagingManager, BlogManager blogManager,
|
MessagingManager messagingManager, BlogManager blogManager,
|
||||||
ForumManager forumManager,
|
ForumManager forumManager,
|
||||||
|
PrivateGroupManager privateGroupManager,
|
||||||
|
PrivateGroupFactory privateGroupFactory,
|
||||||
|
GroupMessageFactory groupMessageFactory,
|
||||||
|
GroupInvitationFactory groupInvitationFactory,
|
||||||
TestAvatarCreator testAvatarCreator,
|
TestAvatarCreator testAvatarCreator,
|
||||||
AvatarMessageEncoder avatarMessageEncoder,
|
AvatarMessageEncoder avatarMessageEncoder,
|
||||||
FeatureFlags featureFlags,
|
FeatureFlags featureFlags,
|
||||||
@@ -128,6 +143,10 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
this.messagingManager = messagingManager;
|
this.messagingManager = messagingManager;
|
||||||
this.blogManager = blogManager;
|
this.blogManager = blogManager;
|
||||||
this.forumManager = forumManager;
|
this.forumManager = forumManager;
|
||||||
|
this.privateGroupManager = privateGroupManager;
|
||||||
|
this.privateGroupFactory = privateGroupFactory;
|
||||||
|
this.groupMessageFactory = groupMessageFactory;
|
||||||
|
this.groupInvitationFactory = groupInvitationFactory;
|
||||||
this.testAvatarCreator = testAvatarCreator;
|
this.testAvatarCreator = testAvatarCreator;
|
||||||
this.avatarMessageEncoder = avatarMessageEncoder;
|
this.avatarMessageEncoder = avatarMessageEncoder;
|
||||||
this.featureFlags = featureFlags;
|
this.featureFlags = featureFlags;
|
||||||
@@ -137,14 +156,16 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
@Override
|
@Override
|
||||||
public void createTestData(int numContacts, int numPrivateMsgs,
|
public void createTestData(int numContacts, int numPrivateMsgs,
|
||||||
int avatarPercent, int numBlogPosts, int numForums,
|
int avatarPercent, int numBlogPosts, int numForums,
|
||||||
int numForumPosts) {
|
int numForumPosts, int numPrivateGroups,
|
||||||
|
int numPrivateGroupMessages) {
|
||||||
if (numContacts == 0) throw new IllegalArgumentException();
|
if (numContacts == 0) throw new IllegalArgumentException();
|
||||||
if (avatarPercent < 0 || avatarPercent > 100)
|
if (avatarPercent < 0 || avatarPercent > 100)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
ioExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
createTestDataOnIoExecutor(numContacts, numPrivateMsgs,
|
createTestDataOnIoExecutor(numContacts, numPrivateMsgs,
|
||||||
avatarPercent, numBlogPosts, numForums, numForumPosts);
|
avatarPercent, numBlogPosts, numForums, numForumPosts,
|
||||||
|
numPrivateGroups, numPrivateGroupMessages);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
@@ -154,7 +175,8 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
@IoExecutor
|
@IoExecutor
|
||||||
private void createTestDataOnIoExecutor(int numContacts, int numPrivateMsgs,
|
private void createTestDataOnIoExecutor(int numContacts, int numPrivateMsgs,
|
||||||
int avatarPercent, int numBlogPosts, int numForums,
|
int avatarPercent, int numBlogPosts, int numForums,
|
||||||
int numForumPosts) throws DbException {
|
int numForumPosts, int numPrivateGroups,
|
||||||
|
int numPrivateGroupMessages) throws DbException {
|
||||||
List<Contact> contacts = createContacts(numContacts, avatarPercent);
|
List<Contact> contacts = createContacts(numContacts, avatarPercent);
|
||||||
createPrivateMessages(contacts, numPrivateMsgs);
|
createPrivateMessages(contacts, numPrivateMsgs);
|
||||||
createBlogPosts(contacts, numBlogPosts);
|
createBlogPosts(contacts, numBlogPosts);
|
||||||
@@ -162,6 +184,12 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
for (Forum forum : forums) {
|
for (Forum forum : forums) {
|
||||||
createRandomForumPosts(forum, contacts, numForumPosts);
|
createRandomForumPosts(forum, contacts, numForumPosts);
|
||||||
}
|
}
|
||||||
|
List<PrivateGroup> groups =
|
||||||
|
createPrivateGroups(contacts, numPrivateGroups);
|
||||||
|
for (PrivateGroup group : groups) {
|
||||||
|
createRandomPrivateGroupMessages(group, contacts,
|
||||||
|
numPrivateGroupMessages);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Contact> createContacts(int numContacts, int avatarPercent)
|
private List<Contact> createContacts(int numContacts, int avatarPercent)
|
||||||
@@ -442,6 +470,10 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
|
|
||||||
private void createRandomForumPosts(Forum forum, List<Contact> contacts,
|
private void createRandomForumPosts(Forum forum, List<Contact> contacts,
|
||||||
int numForumPosts) throws DbException {
|
int numForumPosts) throws DbException {
|
||||||
|
if (contacts.isEmpty() && LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("No forum posts created due to missing contacts.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
List<ForumPost> posts = new ArrayList<>();
|
List<ForumPost> posts = new ArrayList<>();
|
||||||
for (int i = 0; i < numForumPosts; i++) {
|
for (int i = 0; i < numForumPosts; i++) {
|
||||||
Contact contact = contacts.get(random.nextInt(contacts.size()));
|
Contact contact = contacts.get(random.nextInt(contacts.size()));
|
||||||
@@ -464,6 +496,91 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<PrivateGroup> createPrivateGroups(List<Contact> contacts,
|
||||||
|
int numPrivateGroups) throws DbException {
|
||||||
|
if (!featureFlags.shouldEnablePrivateGroupsInCore()) return emptyList();
|
||||||
|
List<PrivateGroup> groups = new ArrayList<>(numPrivateGroups);
|
||||||
|
for (int i = 0; i < numPrivateGroups; i++) {
|
||||||
|
// create private group
|
||||||
|
String name = GROUP_NAMES[random.nextInt(GROUP_NAMES.length)];
|
||||||
|
LocalAuthor creator = identityManager.getLocalAuthor();
|
||||||
|
PrivateGroup group =
|
||||||
|
privateGroupFactory.createPrivateGroup(name, creator);
|
||||||
|
GroupMessage joinMsg = groupMessageFactory.createJoinMessage(
|
||||||
|
group.getId(),
|
||||||
|
clock.currentTimeMillis() - (long) (100 - i) * 60 * 1000,
|
||||||
|
creator
|
||||||
|
);
|
||||||
|
privateGroupManager.addPrivateGroup(group, joinMsg, true);
|
||||||
|
groups.add(group);
|
||||||
|
}
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Created " + numPrivateGroups + " private groups.");
|
||||||
|
}
|
||||||
|
return groups;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createRandomPrivateGroupMessages(PrivateGroup group,
|
||||||
|
List<Contact> contacts, int amount) throws DbException {
|
||||||
|
if (contacts.isEmpty() && LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("No private group messages created " +
|
||||||
|
"due to missing contacts.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<GroupMessage> messages = new ArrayList<>();
|
||||||
|
PrivateKey creatorPrivateKey =
|
||||||
|
identityManager.getLocalAuthor().getPrivateKey();
|
||||||
|
int numMembers = random.nextInt(contacts.size());
|
||||||
|
if (numMembers == 0) numMembers++;
|
||||||
|
Map<Contact, MessageId> membersLastMessage = new HashMap<>();
|
||||||
|
List<Contact> members = new ArrayList<>(numMembers);
|
||||||
|
for (int i = 0; i < numMembers; i++) {
|
||||||
|
Contact contact = contacts.get(i);
|
||||||
|
members.add(contact);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < amount; i++) {
|
||||||
|
Contact contact = members.get(random.nextInt(numMembers));
|
||||||
|
LocalAuthor author = localAuthors.get(contact);
|
||||||
|
long timestamp =
|
||||||
|
clock.currentTimeMillis() -
|
||||||
|
(long) (amount - i) * 60 * 1000;
|
||||||
|
|
||||||
|
GroupMessage msg;
|
||||||
|
if (!membersLastMessage.containsKey(contact)) {
|
||||||
|
// join message as first message of member
|
||||||
|
shareGroup(contact.getId(), group.getId());
|
||||||
|
long inviteTimestamp = timestamp - 1;
|
||||||
|
byte[] creatorSignature =
|
||||||
|
groupInvitationFactory.signInvitation(contact,
|
||||||
|
group.getId(), inviteTimestamp,
|
||||||
|
creatorPrivateKey);
|
||||||
|
msg = groupMessageFactory.createJoinMessage(group.getId(),
|
||||||
|
timestamp, author, inviteTimestamp,
|
||||||
|
creatorSignature);
|
||||||
|
} else {
|
||||||
|
// random text after first message
|
||||||
|
String text = getRandomText();
|
||||||
|
MessageId parent = null;
|
||||||
|
if (random.nextBoolean() && messages.size() > 0) {
|
||||||
|
GroupMessage parentMessage =
|
||||||
|
messages.get(random.nextInt(messages.size()));
|
||||||
|
parent = parentMessage.getMessage().getId();
|
||||||
|
}
|
||||||
|
MessageId lastMsg = membersLastMessage.get(contact);
|
||||||
|
msg = groupMessageFactory.createGroupMessage(
|
||||||
|
group.getId(), timestamp, parent, author, text,
|
||||||
|
lastMsg);
|
||||||
|
messages.add(msg);
|
||||||
|
}
|
||||||
|
membersLastMessage.put(contact, msg.getMessage().getId());
|
||||||
|
db.transaction(false, txn ->
|
||||||
|
db.receiveMessage(txn, contact.getId(), msg.getMessage()));
|
||||||
|
}
|
||||||
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info("Created " + amount + " private group messages.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String getRandomText() {
|
private String getRandomText() {
|
||||||
int minLength = 3 + random.nextInt(500);
|
int minLength = 3 + random.nextInt(500);
|
||||||
int maxWordLength = 15;
|
int maxWordLength = 15;
|
||||||
|
|||||||
Reference in New Issue
Block a user