Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, timestamp + 1);
db.addGroupMessage(txn, message);
// Set the sendability to > 0 and the status to NEW
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
// The message is not sendable because it's too old
assertNull(db.getMessageIfSendable(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetMessageIfSendableReturnsMessage() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L);
db.addGroupMessage(txn, message);
// Set the sendability to > 0 and the status to NEW
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
// The message is sendable so it should be returned
byte[] b = db.getMessageIfSendable(txn, contactId, messageId);
assertArrayEquals(raw, b);
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleRequiresMessageInDatabase()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L);
// The message is not in the database
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleRequiresLocalSubscription()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact with a subscription
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, contactId, group, 0L);
// There's no local subscription for the group
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleRequiresContactSubscription()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
// There's no contact subscription for the group
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleRequiresVisibility()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
db.addSubscription(txn, contactId, group, 0L);
db.setStatus(txn, contactId, messageId, Status.NEW);
// The subscription is not visible
assertFalse(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleReturnsTrueIfAlreadySeen()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L);
db.addGroupMessage(txn, message);
// The message has already been seen by the contact
db.setStatus(txn, contactId, messageId, Status.SEEN);
assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetStatusSeenIfVisibleReturnsTrueIfNew()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact, subscribe to a group and store a message
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
db.addVisibility(txn, contactId, groupId);
db.addSubscription(txn, contactId, group, 0L);
db.addGroupMessage(txn, message);
// The message has not been seen by the contact
db.setStatus(txn, contactId, messageId, Status.NEW);
assertTrue(db.setStatusSeenIfVisible(txn, contactId, messageId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testVisibility() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
// The group should not be visible to the contact
assertEquals(Collections.emptyList(), db.getVisibility(txn, groupId));
// Make the group visible to the contact
db.addVisibility(txn, contactId, groupId);
assertEquals(Collections.singletonList(contactId),
db.getVisibility(txn, groupId));
// Make the group invisible again
db.removeVisibility(txn, contactId, groupId);
assertEquals(Collections.emptyList(), db.getVisibility(txn, groupId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetGroupMessageParentWithNoParent() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group
db.addSubscription(txn, group);
// A message with no parent should return null
MessageId childId = new MessageId(TestUtils.getRandomId());
Message child = new TestMessage(childId, null, groupId, null, subject,
timestamp, raw);
db.addGroupMessage(txn, child);
assertTrue(db.containsMessage(txn, childId));
assertNull(db.getGroupMessageParent(txn, childId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetGroupMessageParentWithAbsentParent() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group
db.addSubscription(txn, group);
// A message with an absent parent should return null
MessageId childId = new MessageId(TestUtils.getRandomId());
MessageId parentId = new MessageId(TestUtils.getRandomId());
Message child = new TestMessage(childId, parentId, groupId, null,
subject, timestamp, raw);
db.addGroupMessage(txn, child);
assertTrue(db.containsMessage(txn, childId));
assertFalse(db.containsMessage(txn, parentId));
assertNull(db.getGroupMessageParent(txn, childId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetGroupMessageParentWithParentInAnotherGroup()
throws Exception {
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Group name", null);
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to two groups
db.addSubscription(txn, group);
db.addSubscription(txn, group1);
// A message with a parent in another group should return null
MessageId childId = new MessageId(TestUtils.getRandomId());
MessageId parentId = new MessageId(TestUtils.getRandomId());
Message child = new TestMessage(childId, parentId, groupId, null,
subject, timestamp, raw);
Message parent = new TestMessage(parentId, null, groupId1, null,
subject, timestamp, raw);
db.addGroupMessage(txn, child);
db.addGroupMessage(txn, parent);
assertTrue(db.containsMessage(txn, childId));
assertTrue(db.containsMessage(txn, parentId));
assertNull(db.getGroupMessageParent(txn, childId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetGroupMessageParentWithPrivateParent() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
// A message with a private parent should return null
MessageId childId = new MessageId(TestUtils.getRandomId());
Message child = new TestMessage(childId, messageId1, groupId,
null, subject, timestamp, raw);
db.addGroupMessage(txn, child);
db.addPrivateMessage(txn, privateMessage, contactId);
assertTrue(db.containsMessage(txn, childId));
assertTrue(db.containsMessage(txn, messageId1));
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
assertNull(db.getGroupMessageParent(txn, childId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetGroupMessageParentWithParentInSameGroup()
throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group
db.addSubscription(txn, group);
// A message with a parent in the same group should return the parent
MessageId childId = new MessageId(TestUtils.getRandomId());
MessageId parentId = new MessageId(TestUtils.getRandomId());
Message child = new TestMessage(childId, parentId, groupId, null,
subject, timestamp, raw);
Message parent = new TestMessage(parentId, null, groupId, null,
subject, timestamp, raw);
db.addGroupMessage(txn, child);
db.addGroupMessage(txn, parent);
assertTrue(db.containsMessage(txn, childId));
assertTrue(db.containsMessage(txn, parentId));
assertEquals(parentId, db.getGroupMessageParent(txn, childId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetMessageBody() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact and subscribe to a group
assertEquals(contactId, db.addContact(txn));
db.addSubscription(txn, group);
// Store a couple of messages
int bodyLength = raw.length - 20;
Message message1 = new TestMessage(messageId, null, groupId, null,
subject, timestamp, raw, 5, bodyLength);
Message privateMessage1 = new TestMessage(messageId1, null, null,
null, subject, timestamp, raw, 10, bodyLength);
db.addGroupMessage(txn, message1);
db.addPrivateMessage(txn, privateMessage1, contactId);
// Calculate the expected message bodies
byte[] expectedBody = new byte[bodyLength];
System.arraycopy(raw, 5, expectedBody, 0, bodyLength);
assertFalse(Arrays.equals(expectedBody, new byte[bodyLength]));
byte[] expectedBody1 = new byte[bodyLength];
System.arraycopy(raw, 10, expectedBody1, 0, bodyLength);
System.arraycopy(raw, 10, expectedBody1, 0, bodyLength);
// Retrieve the raw messages
assertArrayEquals(raw, db.getMessage(txn, messageId));
assertArrayEquals(raw, db.getMessage(txn, messageId1));
// Retrieve the message bodies
byte[] body = db.getMessageBody(txn, messageId);
assertArrayEquals(expectedBody, body);
byte[] body1 = db.getMessageBody(txn, messageId1);
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
assertArrayEquals(expectedBody1, body1);
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetMessageHeaders() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group
db.addSubscription(txn, group);
// Store a couple of messages
db.addGroupMessage(txn, message);
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
MessageId parentId = new MessageId(TestUtils.getRandomId());
long timestamp1 = System.currentTimeMillis();
Message message1 = new TestMessage(messageId1, parentId, groupId,
authorId, subject, timestamp1, raw);
db.addGroupMessage(txn, message1);
// Mark one of the messages read
assertFalse(db.setRead(txn, messageId, true));
// Retrieve the message headers
Collection<MessageHeader> headers = db.getMessageHeaders(txn, groupId);
Iterator<MessageHeader> it = headers.iterator();
boolean messageFound = false, message1Found = false;
// First header (order is undefined)
assertTrue(it.hasNext());
MessageHeader header = it.next();
if(messageId.equals(header.getId())) {
assertHeadersMatch(message, header);
assertTrue(header.getRead());
assertFalse(header.getStarred());
messageFound = true;
} else if(messageId1.equals(header.getId())) {
assertHeadersMatch(message1, header);
assertFalse(header.getRead());
assertFalse(header.getStarred());
message1Found = true;
} else {
fail();
}
// Second header
assertTrue(it.hasNext());
header = it.next();
if(messageId.equals(header.getId())) {
assertHeadersMatch(message, header);
assertTrue(header.getRead());
assertFalse(header.getStarred());
messageFound = true;
} else if(messageId1.equals(header.getId())) {
assertHeadersMatch(message1, header);
assertFalse(header.getRead());
assertFalse(header.getStarred());
message1Found = true;
} else {
fail();
}
// No more headers
assertFalse(it.hasNext());
assertTrue(messageFound);
assertTrue(message1Found);
db.commitTransaction(txn);
db.close();
}
private void assertHeadersMatch(Message m, MessageHeader h) {
assertEquals(m.getId(), h.getId());
if(m.getParent() == null) assertNull(h.getParent());
else assertEquals(m.getParent(), h.getParent());
if(m.getGroup() == null) assertNull(h.getGroup());
else assertEquals(m.getGroup(), h.getGroup());
if(m.getAuthor() == null) assertNull(h.getAuthor());
else assertEquals(m.getAuthor(), h.getAuthor());
assertEquals(m.getSubject(), h.getSubject());
assertEquals(m.getTimestamp(), h.getTimestamp());
}
@Test
public void testReadFlag() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group and store a message
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
// The message should be unread by default
assertFalse(db.getReadFlag(txn, messageId));
// Marking the message read should return the old value
assertFalse(db.setRead(txn, messageId, true));
assertTrue(db.setRead(txn, messageId, true));
// The message should be read
assertTrue(db.getReadFlag(txn, messageId));
// Marking the message unread should return the old value
assertTrue(db.setRead(txn, messageId, false));
assertFalse(db.setRead(txn, messageId, false));
// Unsubscribe from the group
db.removeSubscription(txn, groupId);
db.commitTransaction(txn);
db.close();
}
@Test
public void testStarredFlag() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a group and store a message
db.addSubscription(txn, group);
db.addGroupMessage(txn, message);
// The message should be unstarred by default
assertFalse(db.getStarredFlag(txn, messageId));
// Starring the message should return the old value
assertFalse(db.setStarred(txn, messageId, true));
assertTrue(db.setStarred(txn, messageId, true));
// The message should be starred
assertTrue(db.getStarredFlag(txn, messageId));
// Unstarring the message should return the old value
assertTrue(db.setStarred(txn, messageId, false));
assertFalse(db.setStarred(txn, messageId, false));
// Unsubscribe from the group
db.removeSubscription(txn, groupId);
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetUnreadMessageCounts() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to a couple of groups
db.addSubscription(txn, group);
GroupId groupId1 = new GroupId(TestUtils.getRandomId());
Group group1 = new Group(groupId1, "Another group", null);
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
db.addSubscription(txn, group1);
// Store two messages in the first group
db.addGroupMessage(txn, message);
MessageId messageId1 = new MessageId(TestUtils.getRandomId());
Message message1 = new TestMessage(messageId1, null, groupId,
authorId, subject, timestamp, raw);
db.addGroupMessage(txn, message1);
// Store one message in the second group
MessageId messageId2 = new MessageId(TestUtils.getRandomId());
Message message2 = new TestMessage(messageId2, null, groupId1,
authorId, subject, timestamp, raw);
db.addGroupMessage(txn, message2);
// Mark one of the messages in the first group read
assertFalse(db.setRead(txn, messageId, true));
// There should be one unread message in each group
Map<GroupId, Integer> counts = db.getUnreadMessageCounts(txn);
assertEquals(2, counts.size());
Integer count = counts.get(groupId);
assertNotNull(count);
assertEquals(1, count.intValue());
count = counts.get(groupId1);
assertNotNull(count);
assertEquals(1, count.intValue());
// Mark the read message unread (it will now be false rather than null)
assertTrue(db.setRead(txn, messageId, false));
// Mark the message in the second group read
assertFalse(db.setRead(txn, messageId2, true));
// There should be two unread messages in the first group, none in
// the second group
counts = db.getUnreadMessageCounts(txn);
assertEquals(1, counts.size());
count = counts.get(groupId);
assertNotNull(count);
assertEquals(2, count.intValue());
db.commitTransaction(txn);
db.close();
}
@Test
public void testMultipleSubscriptionsAndUnsubscriptions() throws Exception {
// Create some groups
List<Group> groups = new ArrayList<Group>();
for(int i = 0; i < 100; i++) {
GroupId id = new GroupId(TestUtils.getRandomId());
groups.add(new Group(id, "Group name", null));
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
}
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Subscribe to the groups and add a contact
for(Group g : groups) db.addSubscription(txn, g);
assertEquals(contactId, db.addContact(txn));
// Make the groups visible to the contact
Collections.shuffle(groups);
for(Group g : groups) db.addVisibility(txn, contactId, g.getId());
// Make some of the groups invisible to the contact and remove them all
Collections.shuffle(groups);
for(Group g : groups) {
if(Math.random() < 0.5)
db.removeVisibility(txn, contactId, g.getId());
db.removeSubscription(txn, g.getId());
}
db.commitTransaction(txn);
db.close();
}
@Test
public void testTemporarySecrets() throws Exception {
// Create a contact transport and three consecutive temporary secrets
long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false;
long outgoing1 = 456L, centre1 = 567L;
long outgoing2 = 678L, centre2 = 789L;
long outgoing3 = 890L, centre3 = 901L;
Endpoint ct = new Endpoint(contactId, transportId,
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
epoch, clockDiff, latency, alice);
Random random = new Random();
byte[] secret1 = new byte[32], bitmap1 = new byte[4];
random.nextBytes(secret1);
random.nextBytes(bitmap1);
TemporarySecret s1 = new TemporarySecret(contactId, transportId, epoch,
clockDiff, latency, alice, 0L, secret1, outgoing1, centre1,
bitmap1);
byte[] secret2 = new byte[32], bitmap2 = new byte[4];
random.nextBytes(secret2);
random.nextBytes(bitmap2);
TemporarySecret s2 = new TemporarySecret(contactId, transportId, epoch,
clockDiff, latency, alice, 1L, secret2, outgoing2, centre2,
bitmap2);
byte[] secret3 = new byte[32], bitmap3 = new byte[4];
random.nextBytes(secret3);
random.nextBytes(bitmap3);
TemporarySecret s3 = new TemporarySecret(contactId, transportId, epoch,
clockDiff, latency, alice, 2L, secret3, outgoing3, centre3,
bitmap3);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Initially there should be no secrets in the database
assertEquals(Collections.emptyList(), db.getSecrets(txn));
// Add the contact transport and the first two secrets
assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct);
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
db.addSecrets(txn, Arrays.asList(s1, s2));
// Retrieve the first two secrets
Collection<TemporarySecret> secrets = db.getSecrets(txn);
assertEquals(2, secrets.size());
boolean foundFirst = false, foundSecond = false;
for(TemporarySecret s : secrets) {
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(epoch, s.getEpoch());
assertEquals(clockDiff, s.getClockDifference());
assertEquals(latency, s.getLatency());
assertEquals(alice, s.getAlice());
if(s.getPeriod() == 0L) {
assertArrayEquals(secret1, s.getSecret());
assertEquals(outgoing1, s.getOutgoingConnectionCounter());
assertEquals(centre1, s.getWindowCentre());
assertArrayEquals(bitmap1, s.getWindowBitmap());
foundFirst = true;
} else if(s.getPeriod() == 1L) {
assertArrayEquals(secret2, s.getSecret());
assertEquals(outgoing2, s.getOutgoingConnectionCounter());
assertEquals(centre2, s.getWindowCentre());
assertArrayEquals(bitmap2, s.getWindowBitmap());
foundSecond = true;
} else {
fail();
}
}
assertTrue(foundFirst);
assertTrue(foundSecond);
// Adding the third secret (period 2) should delete the first (period 0)
db.addSecrets(txn, Arrays.asList(s3));
secrets = db.getSecrets(txn);
assertEquals(2, secrets.size());
foundSecond = false;
boolean foundThird = false;
for(TemporarySecret s : secrets) {
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(epoch, s.getEpoch());
assertEquals(clockDiff, s.getClockDifference());
assertEquals(latency, s.getLatency());
assertEquals(alice, s.getAlice());
if(s.getPeriod() == 1L) {
assertArrayEquals(secret2, s.getSecret());
assertEquals(outgoing2, s.getOutgoingConnectionCounter());
assertEquals(centre2, s.getWindowCentre());
assertArrayEquals(bitmap2, s.getWindowBitmap());
foundSecond = true;
} else if(s.getPeriod() == 2L) {
assertArrayEquals(secret3, s.getSecret());
assertEquals(outgoing3, s.getOutgoingConnectionCounter());
assertEquals(centre3, s.getWindowCentre());
assertArrayEquals(bitmap3, s.getWindowBitmap());
foundThird = true;
} else {
fail();
}
}
assertTrue(foundSecond);
assertTrue(foundThird);
// Removing the contact should remove the secrets
db.removeContact(txn, contactId);
assertEquals(Collections.emptyList(), db.getSecrets(txn));
db.commitTransaction(txn);
db.close();
}
@Test
public void testIncrementConnectionCounter() throws Exception {
// Create a contact transport and a temporary secret
long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false;
long period = 456L, outgoing = 567L, centre = 678L;
Endpoint ct = new Endpoint(contactId, transportId,
epoch, clockDiff, latency, alice);
Random random = new Random();
byte[] secret = new byte[32], bitmap = new byte[4];
random.nextBytes(secret);
TemporarySecret s = new TemporarySecret(contactId, transportId, epoch,
clockDiff, latency, alice, period, secret, outgoing, centre,
bitmap);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add the contact transport and the temporary secret
assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct);
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
db.addSecrets(txn, Arrays.asList(s));
// Retrieve the secret
Collection<TemporarySecret> secrets = db.getSecrets(txn);
assertEquals(1, secrets.size());
s = secrets.iterator().next();
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(period, s.getPeriod());
assertArrayEquals(secret, s.getSecret());
assertEquals(outgoing, s.getOutgoingConnectionCounter());
assertEquals(centre, s.getWindowCentre());
assertArrayEquals(bitmap, s.getWindowBitmap());
// Increment the connection counter twice and retrieve the secret again
assertEquals(outgoing, db.incrementConnectionCounter(txn,
s.getContactId(), s.getTransportId(), s.getPeriod()));
assertEquals(outgoing + 1L, db.incrementConnectionCounter(txn,
s.getContactId(), s.getTransportId(), s.getPeriod()));
secrets = db.getSecrets(txn);
assertEquals(1, secrets.size());
s = secrets.iterator().next();
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(period, s.getPeriod());
assertArrayEquals(secret, s.getSecret());
assertEquals(outgoing + 2L, s.getOutgoingConnectionCounter());
assertEquals(centre, s.getWindowCentre());
assertArrayEquals(bitmap, s.getWindowBitmap());
db.commitTransaction(txn);
db.close();
}
@Test
public void testSetConnectionWindow() throws Exception {
// Create a contact transport and a temporary secret
long epoch = 123L, clockDiff = 234L, latency = 345L;
boolean alice = false;
long period = 456L, outgoing = 567L, centre = 678L;
Endpoint ct = new Endpoint(contactId, transportId,
epoch, clockDiff, latency, alice);
Random random = new Random();
byte[] secret = new byte[32], bitmap = new byte[4];
random.nextBytes(secret);
TemporarySecret s = new TemporarySecret(contactId, transportId, epoch,
clockDiff, latency, alice, period, secret, outgoing, centre,
bitmap);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add the contact transport and the temporary secret
assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct);
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
db.addSecrets(txn, Arrays.asList(s));
// Retrieve the secret
Collection<TemporarySecret> secrets = db.getSecrets(txn);
assertEquals(1, secrets.size());
s = secrets.iterator().next();
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(period, s.getPeriod());
assertArrayEquals(secret, s.getSecret());
assertEquals(outgoing, s.getOutgoingConnectionCounter());
assertEquals(centre, s.getWindowCentre());
assertArrayEquals(bitmap, s.getWindowBitmap());
// Update the connection window and retrieve the secret again
random.nextBytes(bitmap);
db.setConnectionWindow(txn, contactId, transportId, period, centre,
bitmap);
secrets = db.getSecrets(txn);
assertEquals(1, secrets.size());
s = secrets.iterator().next();
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(period, s.getPeriod());
assertArrayEquals(secret, s.getSecret());
assertEquals(outgoing, s.getOutgoingConnectionCounter());
assertEquals(centre, s.getWindowCentre());
assertArrayEquals(bitmap, s.getWindowBitmap());
// Updating a nonexistent window should not throw an exception
db.setConnectionWindow(txn, contactId, transportId, period + 1L, 1L,
bitmap);
// The nonexistent window should not have been created
secrets = db.getSecrets(txn);
assertEquals(1, secrets.size());
s = secrets.iterator().next();
assertEquals(contactId, s.getContactId());
assertEquals(transportId, s.getTransportId());
assertEquals(period, s.getPeriod());
assertArrayEquals(secret, s.getSecret());
assertEquals(outgoing, s.getOutgoingConnectionCounter());
assertEquals(centre, s.getWindowCentre());
assertArrayEquals(bitmap, s.getWindowBitmap());
db.commitTransaction(txn);
db.close();
}
@Test
public void testContactTransports() throws Exception {
// Create some contact transports
long epoch1 = 123L, clockDiff1 = 234L, latency1 = 345L;
long epoch2 = 456L, clockDiff2 = 567L, latency2 = 678L;
boolean alice1 = true, alice2 = false;
TransportId transportId1 = new TransportId(TestUtils.getRandomId());
TransportId transportId2 = new TransportId(TestUtils.getRandomId());
Endpoint ct1 = new Endpoint(contactId, transportId1,
epoch1, clockDiff1, latency1, alice1);
Endpoint ct2 = new Endpoint(contactId, transportId2,
epoch2, clockDiff2, latency2, alice2);
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Initially there should be no contact transports in the database
assertEquals(Collections.emptyList(), db.getEndpoints(txn));
// Add a contact and the contact transports
assertEquals(contactId, db.addContact(txn));
db.addEndpoint(txn, ct1);
db.addEndpoint(txn, ct2);
// Retrieve the contact transports
Collection<Endpoint> cts = db.getEndpoints(txn);
assertEquals(2, cts.size());
boolean foundFirst = false, foundSecond = false;
for(Endpoint ct : cts) {
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
assertEquals(contactId, ct.getContactId());
if(ct.getTransportId().equals(transportId1)) {
assertEquals(epoch1, ct.getEpoch());
assertEquals(clockDiff1, ct.getClockDifference());
assertEquals(latency1, ct.getLatency());
assertEquals(alice1, ct.getAlice());
foundFirst = true;
} else if(ct.getTransportId().equals(transportId2)) {
assertEquals(epoch2, ct.getEpoch());
assertEquals(clockDiff2, ct.getClockDifference());
assertEquals(latency2, ct.getLatency());
assertEquals(alice2, ct.getAlice());
foundSecond = true;
} else {
fail();
}
}
assertTrue(foundFirst);
assertTrue(foundSecond);
// Removing the contact should remove the contact transports
db.removeContact(txn, contactId);
assertEquals(Collections.emptyList(), db.getEndpoints(txn));
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
db.commitTransaction(txn);
db.close();
}
@Test
public void testExceptionHandling() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
try {
// Ask for a nonexistent message - an exception should be thrown
db.getMessage(txn, messageId);
fail();
} catch(DbException expected) {
// It should be possible to abort the transaction without error
db.abortTransaction(txn);
}
// It should be possible to close the database cleanly
db.close();
}
private Database<Connection> open(boolean resume) throws Exception {
Database<Connection> db = new H2Database(new SystemClock(),
new TestDatabaseConfig(testDir, MAX_SIZE));