session-ios/SignalServiceKit/tests/Util/TSMessageStorageTests.m

176 lines
8.0 KiB
Mathematica
Raw Normal View History

2015-12-07 03:31:43 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
2015-12-07 03:31:43 +01:00
//
#import "OWSPrimaryStorage.h"
2018-09-11 00:43:17 +02:00
#import "SSKBaseTest.h"
2015-12-07 03:31:43 +01:00
#import "TSContactThread.h"
#import "TSGroupThread.h"
#import "TSIncomingMessage.h"
#import "TSMessage.h"
#import "TSOutgoingMessage.h"
#import "TSThread.h"
2018-09-20 18:53:03 +02:00
#import "YapDatabaseConnection+OWS.h"
2018-09-25 19:09:55 +02:00
#import <SignalCoreKit/Cryptography.h>
2015-12-07 03:31:43 +01:00
2018-09-11 00:43:17 +02:00
@interface TSMessageStorageTests : SSKBaseTest
2015-12-07 03:31:43 +01:00
@property TSContactThread *thread;
@end
@implementation TSMessageStorageTests
2018-09-20 18:53:03 +02:00
#ifdef BROKEN_TESTS
- (void)setUp
{
2015-12-07 03:31:43 +01:00
[super setUp];
2017-07-06 16:36:49 +02:00
[self readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
self.thread = [TSContactThread getOrCreateThreadWithContactId:@"aStupidId" transaction:transaction];
2017-07-06 16:36:49 +02:00
[self.thread saveWithTransaction:transaction];
}];
2015-12-07 03:31:43 +01:00
}
- (void)tearDown
{
2015-12-07 03:31:43 +01:00
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testStoreIncomingMessage
{
2015-12-07 03:31:43 +01:00
__block NSString *messageId;
uint64_t timestamp = 666;
2018-09-07 23:01:27 +02:00
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
2018-09-20 18:53:03 +02:00
TSIncomingMessage *newMessage =
[[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:timestamp
inThread:self.thread
authorId:[self.thread contactIdentifier]
sourceDeviceId:1
messageBody:body
attachmentIds:@[]
expiresInSeconds:0
quotedMessage:nil
contactShare:nil];
[self readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[newMessage saveWithTransaction:transaction];
messageId = newMessage.uniqueId;
}];
2015-12-07 03:31:43 +01:00
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:messageId];
XCTAssertEqualObjects(body, fetchedMessage.body);
XCTAssertFalse(fetchedMessage.hasAttachments);
XCTAssertEqual(timestamp, fetchedMessage.timestamp);
XCTAssertFalse(fetchedMessage.wasRead);
XCTAssertEqualObjects(self.thread.uniqueId, fetchedMessage.uniqueThreadId);
2015-12-07 03:31:43 +01:00
}
- (void)testMessagesDeletedOnThreadDeletion
{
2018-09-07 23:01:27 +02:00
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
NSMutableArray<TSIncomingMessage *> *messages = [NSMutableArray new];
for (int i = 0; i < 10; i++) {
2018-09-20 18:53:03 +02:00
TSIncomingMessage *newMessage =
[[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:i
inThread:self.thread
authorId:[self.thread contactIdentifier]
sourceDeviceId:1
messageBody:body
attachmentIds:@[]
expiresInSeconds:0
quotedMessage:nil
contactShare:nil];
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
[messages addObject:newMessage];
2015-12-07 03:31:43 +01:00
[newMessage save];
}
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:message.uniqueId];
XCTAssertEqualObjects(fetchedMessage.body, body, @"Body of incoming message recovered");
XCTAssertEqual(0, fetchedMessage.attachmentIds.count, @"attachments are nil");
XCTAssertEqualObjects(fetchedMessage.uniqueId, message.uniqueId, @"Unique identifier is accurate");
XCTAssertFalse(fetchedMessage.wasRead, @"Message should originally be unread");
XCTAssertEqualObjects(
fetchedMessage.uniqueThreadId, self.thread.uniqueId, @"Isn't stored in the right thread!");
}
2015-12-07 03:31:43 +01:00
[self.thread remove];
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:message.uniqueId];
XCTAssertNil(fetchedMessage, @"Message should be deleted!");
}
2015-12-07 03:31:43 +01:00
}
- (void)testGroupMessagesDeletedOnThreadDeletion
{
2018-09-07 23:01:27 +02:00
NSString *body
= @"A child born today will grow up with no conception of privacy at all. Theyll never know what it means to "
@"have a private moment to themselves an unrecorded, unanalyzed thought. And thats a problem because "
@"privacy matters; privacy is what allows us to determine who we are and who we want to be.";
2015-12-07 03:31:43 +01:00
__block TSGroupThread *thread;
[self readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
thread = [TSGroupThread getOrCreateThreadWithGroupModel:[[TSGroupModel alloc] initWithTitle:@"fdsfsd"
memberIds:[@[] mutableCopy]
image:nil
groupId:[NSData data]]
transaction:transaction];
2015-12-07 03:31:43 +01:00
[thread saveWithTransaction:transaction];
}];
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
NSMutableArray<TSIncomingMessage *> *messages = [NSMutableArray new];
for (uint64_t i = 0; i < 10; i++) {
2018-09-20 18:53:03 +02:00
TSIncomingMessage *newMessage = [[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:i
inThread:thread
authorId:@"Ed"
sourceDeviceId:1
messageBody:body
attachmentIds:@[]
expiresInSeconds:0
quotedMessage:nil
contactShare:nil];
2015-12-07 03:31:43 +01:00
[newMessage save];
Explain send failures for text and media messages Motivation ---------- We were often swallowing errors or yielding generic errors when it would be better to provide specific errors. We also didn't create an attachment when attachments failed to send, making it impossible to show the user what was happening with an in-progress or failed attachment. Primary Changes --------------- - Funnel all message sending through MessageSender, and remove message sending from MessagesManager. - Record most recent sending error so we can expose it in the UI - Can resend attachments. - Update message status for attachments, just like text messages - Extracted UploadingService from MessagesManager - Saving attachment stream before uploading gives uniform API for send vs. resend - update status for downloading transcript attachments - TSAttachments have a local id, separate from the server allocated id This allows us to save the attachment before the allocation request. Which is is good because: 1. can show feedback to user faster. 2. allows us to show an error when allocation fails. Code Cleanup ------------ - Replaced a lot of global singleton access with injected dependencies to make for easier testing. - Never save group meta messages. Rather than checking before (hopefully) every save, do it in the save method. - Don't use callbacks for sync code. - Handle errors on writing attachment data - Fix old long broken tests that weren't even running. =( - Removed dead code - Use constants vs define - Port flaky travis fixes from Signal-iOS // FREEBIE
2016-10-14 23:00:29 +02:00
[messages addObject:newMessage];
}
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:message.uniqueId];
XCTAssertNotNil(fetchedMessage);
XCTAssertEqualObjects(fetchedMessage.body, body, @"Body of incoming message recovered");
XCTAssertEqual(0, fetchedMessage.attachmentIds.count, @"attachments are empty");
XCTAssertEqualObjects(fetchedMessage.uniqueId, message.uniqueId, @"Unique identifier is accurate");
XCTAssertFalse(fetchedMessage.wasRead, @"Message should originally be unread");
XCTAssertEqualObjects(fetchedMessage.uniqueThreadId, thread.uniqueId, @"Isn't stored in the right thread!");
}
[thread remove];
for (TSIncomingMessage *message in messages) {
TSIncomingMessage *fetchedMessage = [TSIncomingMessage fetchObjectWithUniqueID:message.uniqueId];
XCTAssertNil(fetchedMessage, @"Message should be deleted!");
2015-12-07 03:31:43 +01:00
}
}
2018-09-20 18:53:03 +02:00
#endif
2015-12-07 03:31:43 +01:00
@end