Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions Example/Messaging/Tests/FIRMessagingServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,57 +177,6 @@ - (void)testSubscribeWithoutStart {
}];
}

// TODO(chliangGoogle) Investigate why invalid token can't throw assertion but the rest can under
// release build.
- (void)testSubscribeWithInvalidTopic {

XCTestExpectation *exceptionExpectation =
[self expectationWithDescription:@"Should throw exception for invalid token"];
@try {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
[_mockPubSub subscribeWithToken:kFakeToken
topic:nil
options:nil
handler:^(NSError *error) {
XCTFail(@"Should not invoke the handler");
}];
#pragma clang diagnostic pop
}
@catch (NSException *exception) {
[exceptionExpectation fulfill];
}
@finally {
[self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}
}

- (void)testUnsubscribeWithInvalidTopic {
XCTestExpectation *exceptionExpectation =
[self expectationWithDescription:@"Should throw exception for invalid token"];
@try {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
[_mockPubSub unsubscribeWithToken:kFakeToken
topic:nil
options:nil
handler:^(NSError *error) {
XCTFail(@"Should not invoke the handler");
}];
#pragma clang diagnostic pop
}
@catch (NSException *exception) {
[exceptionExpectation fulfill];
}
@finally {
[self waitForExpectationsWithTimeout:0.1 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}
}

- (void)testSubscribeWithNoTopicPrefix {
OCMStub([_mockInstanceID
instanceIDWithHandler:([OCMArg invokeBlockWithArgs:_result, [NSNull null], nil])]);
Expand Down
9 changes: 9 additions & 0 deletions Firebase/Messaging/FIRMMessageCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented = 2023, // I-FCM002023
kFIRMessagingMessageCodeTopicFormatIsDeprecated = 2024,
kFIRMessagingMessageCodeDirectChannelConnectionFailed = 2025,
kFIRMessagingMessageCodeInvalidClient = 2026,
// FIRMessagingClient.m
kFIRMessagingMessageCodeClient000 = 4000, // I-FCM004000
kFIRMessagingMessageCodeClient001 = 4001, // I-FCM004001
Expand All @@ -60,6 +61,9 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
kFIRMessagingMessageCodeClient009 = 4009, // I-FCM004009
kFIRMessagingMessageCodeClient010 = 4010, // I-FCM004010
kFIRMessagingMessageCodeClient011 = 4011, // I-FCM004011
kFIRMessagingMessageCodeClientInvalidState = 4012,
kFIRMessagingMessageCodeClientInvalidStateTimeout = 4013,

// FIRMessagingConnection.m
kFIRMessagingMessageCodeConnection000 = 5000, // I-FCM005000
kFIRMessagingMessageCodeConnection001 = 5001, // I-FCM005001
Expand Down Expand Up @@ -106,6 +110,8 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
kFIRMessagingMessageCodeDataMessageManager010 = 7010, // I-FCM007010
kFIRMessagingMessageCodeDataMessageManager011 = 7011, // I-FCM007011
kFIRMessagingMessageCodeDataMessageManager012 = 7012, // I-FCM007012
kFIRMessagingMessageCodeDataMessageManager013 = 7013,

// FIRMessagingPendingTopicsList.m
kFIRMessagingMessageCodePendingTopicsList000 = 8000, // I-FCM008000
// FIRMessagingPubSub.m
Expand Down Expand Up @@ -196,4 +202,7 @@ typedef NS_ENUM(NSInteger, FIRMessagingMessageCode) {
kFIRMessagingServiceExtensionLocalFileNotCreated = 20002,
kFIRMessagingServiceExtensionImageNotAttached = 20003,

// FIRMessagingCodedInputStream.m
kFIRMessagingCodeInputStreamInvalidParameters = 21000,

};
12 changes: 5 additions & 7 deletions Firebase/Messaging/FIRMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ - (void)setupRmqManager {
}

- (void)setupTopics {
_FIRMessagingDevAssert(self.client, @"Invalid nil client before init pubsub.");
if (!self.client) {
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeInvalidClient, @"Invalid nil client before init pubsub.");
}
self.pubsub = [[FIRMessagingPubSub alloc] initWithClient:self.client];
}

Expand All @@ -364,8 +366,6 @@ - (void)setupSyncMessageManager {
}

- (void)teardown {
_FIRMessagingDevAssert([NSThread isMainThread],
@"FIRMessaging should be called from main thread only.");
[self.client teardown];
self.pubsub = nil;
self.syncMessageManager = nil;
Expand Down Expand Up @@ -823,15 +823,13 @@ - (void)sendMessage:(NSDictionary *)message
to:(NSString *)to
withMessageID:(NSString *)messageID
timeToLive:(int64_t)ttl {
_FIRMessagingDevAssert([to length] != 0, @"Invalid receiver id for FIRMessaging-message");

NSMutableDictionary *fcmMessage = [[self class] createFIRMessagingMessageWithMessage:message
to:to
withID:messageID
timeToLive:ttl
delay:0];
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@",
message, messageID);
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeMessaging013, @"Sending message: %@ with id: %@ to %@.",
message, messageID, to);
[self.dataMessageManager sendDataMessageStanza:fcmMessage];
}

Expand Down
41 changes: 20 additions & 21 deletions Firebase/Messaging/FIRMessagingClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
}

- (void)teardown {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient000, @"");
if (![NSThread isMainThread]) {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient000, @"FIRMessagingClient should be called from main thread only.");
}
self.stayConnected = NO;

// Clear all the handlers
Expand All @@ -140,9 +142,8 @@ - (void)teardown {
[self.connection teardown];

// Stop all subscription requests
[self.registrar stopAllSubscriptionRequests];
[self.registrar stopAllSubscriptionRequests];

_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected, @"Did not disconnect");
[NSObject cancelPreviousPerformRequestsWithTarget:self];

[[NSNotificationCenter defaultCenter] removeObserver:self];
Expand All @@ -166,10 +167,7 @@ - (void)updateSubscriptionWithToken:(NSString *)token
options:(NSDictionary *)options
shouldDelete:(BOOL)shouldDelete
handler:(FIRMessagingTopicOperationCompletion)handler {

_FIRMessagingDevAssert(handler != nil, @"Invalid handler to FIRMessaging subscribe");

FIRMessagingTopicOperationCompletion completion = ^void(NSError *error) {
FIRMessagingTopicOperationCompletion completion = ^void(NSError *error) {
if (error) {
FIRMessagingLoggerError(kFIRMessagingMessageCodeClient001, @"Failed to subscribe to topic %@",
error);
Expand All @@ -182,7 +180,9 @@ - (void)updateSubscriptionWithToken:(NSString *)token
@"Successfully subscribed to topic %@", topic);
}
}
handler(error);
if (handler) {
handler(error);
}
};

if ([[FIRInstanceID instanceID] tryToLoadValidCheckinInfo]) {
Expand Down Expand Up @@ -316,8 +316,6 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {

self.stayConnected = tryToConnectLater;
[self.connection signOut];
_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected,
@"FIRMessaging connection did not disconnect");

// since we can disconnect while still trying to establish the connection it's required to
// cancel all performSelectors else the object might be retained
Expand Down Expand Up @@ -421,8 +419,6 @@ - (void)setupConnectionAndConnect {
- (void)setupConnection {
NSString *host = FIRMessagingServerHost();
NSUInteger port = FIRMessagingServerPort();
_FIRMessagingDevAssert([host length] > 0 && port != 0, @"Invalid port or host");

if (self.connection != nil) {
// if there is an old connection, explicitly sign it off.
[self.connection signOut];
Expand All @@ -447,17 +443,20 @@ - (void)tryToConnect {
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(tryToConnect)
object:nil];

NSString *deviceAuthID = [FIRInstanceID instanceID].deviceAuthID;
NSString *secretToken = [FIRInstanceID instanceID].secretToken;
if (deviceAuthID.length == 0 || secretToken.length == 0 ||
!self.connection) {
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeClientInvalidState,
@"Invalid state to connect, deviceAuthID: %@, secretToken: %@, connection state: %ld",
deviceAuthID, secretToken, (long)self.connection.state);
return;
}
// Do not re-sign in if there is already a connection in progress.
if (self.connection.state != kFIRMessagingConnectionNotConnected) {
return;
}

_FIRMessagingDevAssert([FIRInstanceID instanceID].deviceAuthID.length > 0 &&
[FIRInstanceID instanceID].secretToken.length > 0 &&
self.connection != nil,
@"Invalid state cannot connect");

self.connectRetryCount = MIN(kMaxRetryExponent, self.connectRetryCount + 1);
[self performSelector:@selector(didConnectTimeout)
withObject:nil
Expand All @@ -466,9 +465,9 @@ - (void)tryToConnect {
}

- (void)didConnectTimeout {
_FIRMessagingDevAssert(self.connection.state != kFIRMessagingConnectionSignedIn,
@"Invalid state for MCS connection");

if (self.connection.state == kFIRMessagingConnectionSignedIn) {
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeClientInvalidStateTimeout, @"Invalid state for connection timeout.");
}
if (self.stayConnected) {
[self.connection signOut];
[self scheduleConnectRetry];
Expand Down
14 changes: 9 additions & 5 deletions Firebase/Messaging/FIRMessagingCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

#import "Firebase/Messaging/FIRMessagingCodedInputStream.h"
#import "Firebase/Messaging/FIRMessagingDefines.h"

#import "Firebase/Messaging/FIRMMessageCode.h"
#import "Firebase/Messaging/FIRMessagingLogger.h"

typedef struct {
const void *bytes;
Expand All @@ -32,8 +34,9 @@ static BOOL CheckSize(BufferState *state, size_t size) {
}

static BOOL ReadRawByte(BufferState *state, int8_t *output) {
_FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");

if (state == NULL || output == NULL) {
FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
}
if (CheckSize(state, sizeof(int8_t))) {
*output = ((int8_t *)state->bytes)[state->bufferPos++];
return YES;
Expand All @@ -42,8 +45,9 @@ static BOOL ReadRawByte(BufferState *state, int8_t *output) {
}

static BOOL ReadRawVarInt32(BufferState *state, int32_t *output) {
_FIRMessagingDevAssert(output != NULL && state != NULL, @"Invalid parameters");

if (state == NULL || output == NULL) {
FIRMessagingLoggerDebug(kFIRMessagingCodeInputStreamInvalidParameters, @"Invalid parameters.");
}
int8_t tmp = 0;
if (!ReadRawByte(state, &tmp)) {
return NO;
Expand Down
12 changes: 1 addition & 11 deletions Firebase/Messaging/FIRMessagingConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ - (NSString *)description {
}

- (void)signIn {
_FIRMessagingDevAssert(self.state == kFIRMessagingConnectionNotConnected, @"Invalid connection state.");
if (self.state != kFIRMessagingConnectionNotConnected) {
return;
}
Expand Down Expand Up @@ -201,11 +200,8 @@ - (void)secureSocketDidConnect:(FIRMessagingSecureSocket *)socket {
}

- (void)didDisconnectWithSecureSocket:(FIRMessagingSecureSocket *)socket {
_FIRMessagingDevAssert(self.socket == socket, @"Invalid socket");
_FIRMessagingDevAssert(self.socket.state == kFIRMessagingSecureSocketClosed, @"Socket already closed");

FIRMessagingLoggerDebug(kFIRMessagingMessageCodeConnection002,
@"Secure socket disconnected from FIRMessaging service.");
@"Secure socket disconnected from FIRMessaging service. %ld", (long)self.socket.state);
[self disconnect];
[self.delegate connection:self didCloseForReason:kFIRMessagingConnectionCloseReasonSocketDisconnected];
}
Expand Down Expand Up @@ -295,7 +291,6 @@ - (void)sendProto:(GPBMessage *)proto {
return;
}

_FIRMessagingDevAssert(self.socket != nil, @"Socket shouldn't be nil");
if (self.socket == nil) {
return;
}
Expand Down Expand Up @@ -439,10 +434,6 @@ - (void)didReceiveLoginResponse:(GtalkLoginResponse *)loginResponse {
return;
}
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeConnection012, @"Logged onto MCS service.");
// We sent the persisted list of unack'd messages with login so we can assume they have been ack'd
// by the server.
_FIRMessagingDevAssert(self.unackedS2dIds.count == 0, @"No ids present");
_FIRMessagingDevAssert(self.outStreamId == 1, @"Login should be the first stream id");

self.state = kFIRMessagingConnectionSignedIn;
self.lastLoginServerTimestamp = loginResponse.serverTimestamp;
Expand Down Expand Up @@ -660,7 +651,6 @@ - (void)resetUnconfirmedAcks {
}

- (void)disconnect {
_FIRMessagingDevAssert(self.state != kFIRMessagingConnectionNotConnected, @"Connection already not connected");
// cancel pending timeout tasks.
[self cancelConnectionTimeoutTask];
// cancel pending heartbeat.
Expand Down
17 changes: 6 additions & 11 deletions Firebase/Messaging/FIRMessagingContextManagerService.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,16 @@ + (BOOL)handleContextManagerMessage:(NSDictionary *)message {

+ (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:kLocalTimeFormatString];
NSDate *startDate = [dateFormatter dateFromString:startTimeString];

_FIRMessagingDevAssert(startDate, @"Invalid local start date format %@", startTimeString);
if (!startTimeString) {
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService002,
@"Invalid local start date format %@. Message dropped",
startTimeString);
@"Invalid local start date format %@. Message dropped",
startTimeString);
return NO;
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:kLocalTimeFormatString];
NSDate *startDate = [dateFormatter dateFromString:startTimeString];
NSDate *currentDate = [NSDate date];

if ([currentDate compare:startDate] == NSOrderedAscending) {
Expand All @@ -106,8 +103,6 @@ + (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
}

NSDate *endDate = [dateFormatter dateFromString:endTimeString];

_FIRMessagingDevAssert(endDate, @"Invalid local end date format %@", endTimeString);
if (!endTimeString) {
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService004,
@"Invalid local end date format %@. Message dropped", endTimeString);
Expand Down
Loading