FIRMessagingCodedInputStreamTest.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <XCTest/XCTest.h>
  17. #import "FirebaseMessaging/Sources/FIRMessagingCodedInputStream.h"
  18. @interface FIRMessagingCodedInputStreamTest : XCTestCase
  19. @end
  20. @implementation FIRMessagingCodedInputStreamTest
  21. - (void)testReadingSmallDataStream {
  22. FIRMessagingCodedInputStream *stream =
  23. [[FIRMessagingCodedInputStream alloc] initWithData:[[self class] sampleData1]];
  24. int8_t actualTag = 2;
  25. int8_t tag;
  26. XCTAssertTrue([stream readTag:&tag]);
  27. XCTAssertEqual(actualTag, tag);
  28. // test length
  29. int32_t actualLength = 4;
  30. int32_t length;
  31. XCTAssertTrue([stream readLength:&length]);
  32. XCTAssertEqual(actualLength, length);
  33. NSData *actualData = [[self class] packetDataForSampleData1];
  34. NSData *data = [stream readDataWithLength:length];
  35. XCTAssertTrue([actualData isEqualToData:data]);
  36. }
  37. - (void)testReadingLargeDataStream {
  38. FIRMessagingCodedInputStream *stream =
  39. [[FIRMessagingCodedInputStream alloc] initWithData:[[self class] sampleData2]];
  40. int8_t actualTag = 5;
  41. int8_t tag;
  42. XCTAssertTrue([stream readTag:&tag]);
  43. XCTAssertEqual(actualTag, tag);
  44. int32_t actualLength = 257;
  45. int32_t length;
  46. XCTAssertTrue([stream readLength:&length]);
  47. XCTAssertEqual(actualLength, length);
  48. NSData *actualData = [[self class] packetDataForSampleData2];
  49. NSData *data = [stream readDataWithLength:length];
  50. XCTAssertTrue([actualData isEqualToData:data]);
  51. }
  52. - (void)testReadingInvalidDataStream {
  53. FIRMessagingCodedInputStream *stream =
  54. [[FIRMessagingCodedInputStream alloc] initWithData:[[self class] invalidData]];
  55. int8_t actualTag = 7;
  56. int8_t tag;
  57. XCTAssertTrue([stream readTag:&tag]);
  58. XCTAssertEqual(actualTag, tag);
  59. int32_t actualLength = 2;
  60. int32_t length;
  61. XCTAssertTrue([stream readLength:&length]);
  62. XCTAssertEqual(actualLength, length);
  63. XCTAssertNil([stream readDataWithLength:length]);
  64. }
  65. + (NSData *)sampleData1 {
  66. // tag = 2,
  67. // length = 4,
  68. // data = integer 255
  69. const char data[] = {0x02, 0x04, 0x80, 0x00, 0x00, 0xff};
  70. return [NSData dataWithBytes:data length:6];
  71. }
  72. + (NSData *)packetDataForSampleData1 {
  73. const char data[] = {0x80, 0x00, 0x00, 0xff};
  74. return [NSData dataWithBytes:data length:4];
  75. }
  76. + (NSData *)sampleData2 {
  77. // test reading varint properly
  78. // tag = 5,
  79. // length = 257,
  80. // data = length 257
  81. const char tagAndLength[] = {0x05, 0x81, 0x02};
  82. NSMutableData *data = [NSMutableData dataWithBytes:tagAndLength length:3];
  83. [data appendData:[self packetDataForSampleData2]];
  84. return data;
  85. }
  86. + (NSData *)packetDataForSampleData2 {
  87. char packetData[257] = {0xff, 0xff, 0xff};
  88. return [NSData dataWithBytes:packetData length:257];
  89. }
  90. + (NSData *)invalidData {
  91. // tag = 7,
  92. // length = 2,
  93. // data = (length 1)
  94. const char data[] = {0x07, 0x02, 0xff};
  95. return [NSData dataWithBytes:data length:3];
  96. }
  97. @end