FIRMessagingFakeConnection.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Example/Messaging/Tests/FIRMessagingFakeConnection.h"
  17. #import <OCMock/OCMock.h>
  18. #import "Firebase/Messaging/Protos/GtalkCore.pbobjc.h"
  19. #import "Firebase/Messaging/FIRMessagingSecureSocket.h"
  20. #import "Firebase/Messaging/FIRMessagingUtilities.h"
  21. static NSString *const kHost = @"localhost";
  22. static const int kPort = 6234;
  23. @interface FIRMessagingSecureSocket ()
  24. @property(nonatomic, readwrite, assign) FIRMessagingSecureSocketState state;
  25. @end
  26. @interface FIRMessagingConnection ()
  27. @property(nonatomic, readwrite, strong) FIRMessagingSecureSocket *socket;
  28. - (void)setupConnectionSocket;
  29. - (void)connectToSocket:(FIRMessagingSecureSocket *)socket;
  30. - (NSTimeInterval)connectionTimeoutInterval;
  31. - (void)sendHeartbeatPing;
  32. - (void)secureSocket:(FIRMessagingSecureSocket *)socket
  33. didReceiveData:(NSData *)data
  34. withTag:(int8_t)tag;
  35. @end
  36. @implementation FIRMessagingFakeConnection
  37. - (void)signIn {
  38. // use this if you don't really want to mock/stub the login behaviour. In case
  39. // you want to stub the login behavoiur you should do these things manually in
  40. // your test and add custom logic in between as required for your testing.
  41. [self setupConnectionSocket];
  42. id socketMock = OCMPartialMock(self.socket);
  43. self.socket = socketMock;
  44. [[[socketMock stub] andDo:^(NSInvocation *invocation) {
  45. if (self.shouldFakeSuccessLogin) {
  46. [self willFakeSuccessfulLoginToFCM];
  47. }
  48. self.socket.state = kFIRMessagingSecureSocketOpen;
  49. [self.socket.delegate secureSocketDidConnect:self.socket];
  50. }] connectToHost:kHost port:kPort onRunLoop:[OCMArg any]];
  51. [self connectToSocket:socketMock];
  52. }
  53. - (NSTimeInterval)connectionTimeoutInterval {
  54. if (self.fakeConnectionTimeout) {
  55. return self.fakeConnectionTimeout;
  56. } else {
  57. return 0.5; // 0.5s
  58. }
  59. }
  60. - (void)mockSocketDisconnect {
  61. id mockSocket = self.socket;
  62. [[[mockSocket stub] andDo:^(NSInvocation *invocation) {
  63. self.socket.state = kFIRMessagingSecureSocketClosed;
  64. }] disconnect];
  65. }
  66. - (void)disconnectNow {
  67. [self.socket disconnect];
  68. [self.socket.delegate didDisconnectWithSecureSocket:self.socket];
  69. }
  70. + (NSString *)fakeHost {
  71. return @"localhost";
  72. }
  73. + (int)fakePort {
  74. return 6234;
  75. }
  76. - (void)willFakeSuccessfulLoginToFCM {
  77. id mockSocket = self.socket;
  78. [[[mockSocket stub] andDo:^(NSInvocation *invocation) {
  79. // mock successful login
  80. GtalkLoginResponse *response = [[GtalkLoginResponse alloc] init];
  81. [response setId_p:@""];
  82. [self secureSocket:self.socket
  83. didReceiveData:[response data]
  84. withTag:kFIRMessagingProtoTagLoginResponse];
  85. }] sendData:[OCMArg any] withTag:kFIRMessagingProtoTagLoginRequest rmqId:[OCMArg isNil]];
  86. }
  87. @end
  88. @implementation FIRMessagingFakeFailConnection
  89. - (void)signIn {
  90. self.signInRequests++;
  91. [self setupConnectionSocket];
  92. id mockSocket = OCMPartialMock(self.socket);
  93. self.socket = mockSocket;
  94. [[[mockSocket stub] andDo:^(NSInvocation *invocation) {
  95. [self mockSocketDisconnect];
  96. if (self.signInRequests <= self.failCount) {
  97. // do nothing -- should timeout
  98. } else {
  99. // since we will always fail once we would disconnect the socket before
  100. // we ever try again thus mock the disconnect to change the state and
  101. // prevent any assertions
  102. [self willFakeSuccessfulLoginToFCM];
  103. self.socket.state = kFIRMessagingSecureSocketOpen;
  104. [self.socket.delegate secureSocketDidConnect:self.socket];
  105. }
  106. }] connectToHost:kHost port:kPort onRunLoop:[OCMArg any]];
  107. [self connectToSocket:mockSocket];
  108. }
  109. @end