Skip to content
Snippets Groups Projects
GroupInvitationValidatorTest.java 15.6 KiB
Newer Older
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
package org.briarproject.privategroup.invitation;

import org.briarproject.ValidatorTestCase;
import org.briarproject.api.FormatException;
import org.briarproject.api.UniqueId;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.jmock.Expectations;
import org.junit.Test;

import java.security.GeneralSecurityException;

import static org.briarproject.TestUtils.getRandomBytes;
import static org.briarproject.TestUtils.getRandomId;
import static org.briarproject.TestUtils.getRandomString;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_SIGNATURE_LENGTH;
import static org.briarproject.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.privategroup.PrivateGroupConstants.MAX_GROUP_INVITATION_MSG_LENGTH;
import static org.briarproject.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH;
import static org.briarproject.api.privategroup.invitation.GroupInvitationFactory.SIGNING_LABEL_INVITE;
import static org.briarproject.privategroup.invitation.MessageType.ABORT;
import static org.briarproject.privategroup.invitation.MessageType.INVITE;
import static org.briarproject.privategroup.invitation.MessageType.JOIN;
import static org.briarproject.privategroup.invitation.MessageType.LEAVE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class GroupInvitationValidatorTest extends ValidatorTestCase {

	private final PrivateGroupFactory privateGroupFactory =
			context.mock(PrivateGroupFactory.class);
	private final MessageEncoder messageEncoder =
			context.mock(MessageEncoder.class);

	private final String groupName = "Group Name";
	private final String creatorName = "Creator Name";
	private final byte[] creatorKey = getRandomBytes(MAX_PUBLIC_KEY_LENGTH);
	private final Author creator =
			new Author(new AuthorId(getRandomId()), creatorName, creatorKey);
	private final byte[] salt = getRandomBytes(GROUP_SALT_LENGTH);
	private final PrivateGroup privateGroup =
			new PrivateGroup(group, groupName, creator, salt);
	private final String inviteText = "Invitation Text";
	private final byte[] signature = getRandomBytes(MAX_SIGNATURE_LENGTH);
	private final BdfDictionary meta =
			BdfDictionary.of(new BdfEntry("meta", "data"));
	private final MessageId previousMessageId = new MessageId(getRandomId());

	private GroupInvitationValidator validator =
			new GroupInvitationValidator(clientHelper, metadataEncoder,
					clock, authorFactory, privateGroupFactory, messageEncoder);

	@Test(expected = FormatException.class)
	public void testRejectsTooShortInviteMessage() throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsTooLongInviteMessage() throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText, signature, "");
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongGroupName()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(),
				getRandomString(MAX_GROUP_NAME_LENGTH + 1), creatorName,
				creatorKey, salt, inviteText, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithEmptyGroupName()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), "", creatorName,
				creatorKey, salt, inviteText, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongCreatorName()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName,
				getRandomString(MAX_AUTHOR_NAME_LENGTH + 1), creatorKey, salt,
				inviteText, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithEmptyCreatorName()
			throws Exception {
		BdfList list =
				BdfList.of(INVITE.getValue(), groupName, "", creatorKey, salt,
						inviteText, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongCreatorKey()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				getRandomBytes(MAX_PUBLIC_KEY_LENGTH + 1), salt, inviteText,
				signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithEmptyCreatorKey()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				new byte[0], salt, inviteText, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongGroupSalt()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, getRandomBytes(GROUP_SALT_LENGTH + 1), inviteText,
				signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooShortGroupSalt()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, getRandomBytes(GROUP_SALT_LENGTH - 1), inviteText,
				signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongMessage()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt,
				getRandomString(MAX_GROUP_INVITATION_MSG_LENGTH + 1),
				signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithTooLongSignature()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText,
				getRandomBytes(MAX_SIGNATURE_LENGTH + 1));
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithEmptySignature()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText, new byte[0]);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithNullSignature()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText, null);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithNonRawSignature()
			throws Exception {
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText, "non raw signature");
		validator.validateMessage(message, group, list);
	}

	@Test
	public void testAcceptsInviteMessageWithNullMessage()
			throws Exception {
		expectInviteMessage(false);
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, null, signature);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsInviteMessageWithInvalidSignature()
			throws Exception {
		expectInviteMessage(true);
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, null, signature);
		validator.validateMessage(message, group, list);
	}

	@Test
	public void testAcceptsProperInviteMessage()
			throws Exception {
		expectInviteMessage(false);
		BdfList list = BdfList.of(INVITE.getValue(), groupName, creatorName,
				creatorKey, salt, inviteText, signature);
		BdfMessageContext messageContext =
				validator.validateMessage(message, group, list);
		assertTrue(messageContext.getDependencies().isEmpty());
		assertEquals(meta ,messageContext.getDictionary());
	}

	private void expectInviteMessage(final boolean exception) throws Exception {
		final BdfList toSign =
				BdfList.of(message.getTimestamp(), message.getGroupId(),
						privateGroup.getId());
		context.checking(new Expectations() {{
			oneOf(authorFactory).createAuthor(creatorName, creatorKey);
			will(returnValue(creator));
			oneOf(privateGroupFactory)
					.createPrivateGroup(groupName, creator, salt);
			will(returnValue(privateGroup));
			oneOf(clientHelper).verifySignature(SIGNING_LABEL_INVITE, signature,
					creatorKey, toSign);
			if (exception) will(throwException(new GeneralSecurityException()));
			else {
				oneOf(messageEncoder)
						.encodeMetadata(INVITE, message.getGroupId(),
								message.getTimestamp(), false, false, false,
								false);
				will(returnValue(meta));
			}
		}});
	}

	// JOIN Message

	@Test(expected = FormatException.class)
	public void testRejectsTooShortJoinMessage() throws Exception {
		BdfList list = BdfList.of(JOIN.getValue(), privateGroup.getId());
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsTooLongJoinMessage() throws Exception {
		BdfList list = BdfList.of(JOIN.getValue(), privateGroup.getId(),
				previousMessageId, "");
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsJoinMessageWithTooLongGroupId() throws Exception {
		BdfList list =
				BdfList.of(JOIN.getValue(), getRandomBytes(GroupId.LENGTH + 1),
						previousMessageId);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsJoinMessageWithTooShortGroupId() throws Exception {
		BdfList list =
				BdfList.of(JOIN.getValue(), getRandomBytes(GroupId.LENGTH - 1),
						previousMessageId);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsJoinMessageWithTooLongPreviousMessageId()
			throws Exception {
		BdfList list = BdfList.of(JOIN.getValue(), privateGroup.getId(),
				getRandomBytes(UniqueId.LENGTH + 1));
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsJoinMessageWithTooShortPreviousMessageId()
			throws Exception {
		BdfList list = BdfList.of(JOIN.getValue(), privateGroup.getId(),
				getRandomBytes(UniqueId.LENGTH - 1));
		validator.validateMessage(message, group, list);
	}

	@Test
	public void testAcceptsProperJoinMessage()
			throws Exception {
		context.checking(new Expectations() {{
			oneOf(messageEncoder)
					.encodeMetadata(JOIN, message.getGroupId(),
							message.getTimestamp(), false, false, false,
							false);
			will(returnValue(meta));
		}});
		BdfList list = BdfList.of(JOIN.getValue(), privateGroup.getId(),
				previousMessageId);
		BdfMessageContext messageContext =
				validator.validateMessage(message, group, list);
		assertEquals(1, messageContext.getDependencies().size());
		assertEquals(previousMessageId,
				messageContext.getDependencies().iterator().next());
		assertEquals(meta ,messageContext.getDictionary());
	}

	// LEAVE message

	@Test(expected = FormatException.class)
	public void testRejectsTooShortLeaveMessage() throws Exception {
		BdfList list = BdfList.of(LEAVE.getValue(), privateGroup.getId());
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsTooLongLeaveMessage() throws Exception {
		BdfList list = BdfList.of(LEAVE.getValue(), privateGroup.getId(),
				previousMessageId, "");
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsLeaveMessageWithTooLongGroupId() throws Exception {
		BdfList list =
				BdfList.of(LEAVE.getValue(), getRandomBytes(GroupId.LENGTH + 1),
						previousMessageId);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsLeaveMessageWithTooShortGroupId() throws Exception {
		BdfList list =
				BdfList.of(LEAVE.getValue(), getRandomBytes(GroupId.LENGTH - 1),
						previousMessageId);
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsLeaveMessageWithTooLongPreviousMessageId()
			throws Exception {
		BdfList list = BdfList.of(LEAVE.getValue(), privateGroup.getId(),
				getRandomBytes(UniqueId.LENGTH + 1));
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsLeaveMessageWithTooShortPreviousMessageId()
			throws Exception {
		BdfList list = BdfList.of(LEAVE.getValue(), privateGroup.getId(),
				getRandomBytes(UniqueId.LENGTH - 1));
		validator.validateMessage(message, group, list);
	}

	@Test
	public void testAcceptsProperLeaveMessage()
			throws Exception {
		context.checking(new Expectations() {{
			oneOf(messageEncoder).encodeMetadata(LEAVE, message.getGroupId(),
					message.getTimestamp(), false, false, false, false);
			will(returnValue(meta));
		}});
		BdfList list = BdfList.of(LEAVE.getValue(), privateGroup.getId(),
				previousMessageId);
		BdfMessageContext messageContext =
				validator.validateMessage(message, group, list);
		assertEquals(1, messageContext.getDependencies().size());
		assertEquals(previousMessageId,
				messageContext.getDependencies().iterator().next());
		assertEquals(meta ,messageContext.getDictionary());
	}

	// ABORT message

	@Test(expected = FormatException.class)
	public void testRejectsTooShortAbortMessage() throws Exception {
		BdfList list = BdfList.of(ABORT.getValue());
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsTooLongAbortMessage() throws Exception {
		BdfList list = BdfList.of(ABORT.getValue(), privateGroup.getId(), "");
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsAbortMessageWithTooLongGroupId() throws Exception {
		BdfList list = BdfList.of(ABORT.getValue(),
				getRandomBytes(GroupId.LENGTH + 1));
		validator.validateMessage(message, group, list);
	}

	@Test(expected = FormatException.class)
	public void testRejectsAbortMessageWithTooShortGroupId() throws Exception {
		BdfList list = BdfList.of(ABORT.getValue(),
				getRandomBytes(GroupId.LENGTH - 1));
		validator.validateMessage(message, group, list);
	}

	@Test
	public void testAcceptsProperAbortMessage()
			throws Exception {
		context.checking(new Expectations() {{
			oneOf(messageEncoder).encodeMetadata(ABORT, message.getGroupId(),
					message.getTimestamp(), false, false, false, false);
			will(returnValue(meta));
		}});
		BdfList list = BdfList.of(ABORT.getValue(), privateGroup.getId());
		BdfMessageContext messageContext =
				validator.validateMessage(message, group, list);
		assertEquals(0, messageContext.getDependencies().size());
		assertEquals(meta ,messageContext.getDictionary());
	}

	@Test(expected = FormatException.class)
	public void testRejectsMessageWithUnknownType() throws Exception {
		BdfList list = BdfList.of(ABORT.getValue() + 1);
		validator.validateMessage(message, group, list);
	}

}