FIRCrashlyticsReportTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import <XCTest/XCTest.h>
  16. #import "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
  17. #import "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
  18. #import "Crashlytics/Crashlytics/Helpers/FIRCLSFile.h"
  19. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  20. #import "Crashlytics/Crashlytics/Private/FIRCrashlyticsReport_Private.h"
  21. #import "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRCrashlyticsReport.h"
  22. @interface FIRCrashlyticsReportTests : XCTestCase
  23. @end
  24. @implementation FIRCrashlyticsReportTests
  25. - (void)setUp {
  26. [super setUp];
  27. FIRCLSContextBaseInit();
  28. // these values must be set for the internals of logging to work
  29. _firclsContext.readonly->logging.userKVStorage.maxCount = 64;
  30. _firclsContext.readonly->logging.userKVStorage.maxIncrementalCount =
  31. FIRCLSUserLoggingMaxKVEntries;
  32. _firclsContext.readonly->logging.internalKVStorage.maxCount = 32;
  33. _firclsContext.readonly->logging.internalKVStorage.maxIncrementalCount = 16;
  34. _firclsContext.readonly->logging.logStorage.maxSize = 64 * 1000;
  35. _firclsContext.readonly->logging.logStorage.maxEntries = 0;
  36. _firclsContext.readonly->logging.logStorage.restrictBySize = true;
  37. _firclsContext.readonly->logging.logStorage.entryCount = NULL;
  38. _firclsContext.readonly->initialized = true;
  39. }
  40. - (void)tearDown {
  41. FIRCLSContextBaseDeinit();
  42. [super tearDown];
  43. }
  44. - (NSString *)resourcePath {
  45. return [[NSBundle bundleForClass:[self class]] resourcePath];
  46. }
  47. - (NSString *)pathForResource:(NSString *)name {
  48. return [[self resourcePath] stringByAppendingPathComponent:name];
  49. }
  50. - (FIRCLSInternalReport *)createTempCopyOfInternalReportWithName:(NSString *)name {
  51. NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:name];
  52. // make sure to remove anything that was there previously
  53. [[NSFileManager defaultManager] removeItemAtPath:tempPath error:nil];
  54. NSString *resourcePath = [self pathForResource:name];
  55. [[NSFileManager defaultManager] copyItemAtPath:resourcePath toPath:tempPath error:nil];
  56. return [[FIRCLSInternalReport alloc] initWithPath:tempPath];
  57. }
  58. - (FIRCrashlyticsReport *)createTempCopyOfReportWithName:(NSString *)name {
  59. FIRCLSInternalReport *internalReport = [self createTempCopyOfInternalReportWithName:name];
  60. return [[FIRCrashlyticsReport alloc] initWithInternalReport:internalReport];
  61. }
  62. #pragma mark - Public Getter Methods
  63. - (void)testPropertiesFromMetadatFile {
  64. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"metadata_only_report"];
  65. XCTAssertEqualObjects(@"772929a7f21f4ad293bb644668f257cd", report.reportID);
  66. XCTAssertEqualObjects([NSDate dateWithTimeIntervalSince1970:1423944888], report.dateCreated);
  67. }
  68. #pragma mark - Public Setter Methods
  69. - (void)testSetUserID {
  70. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"metadata_only_report"];
  71. [report setUserID:@"12345-6"];
  72. NSArray *entries = FIRCLSFileReadSections(
  73. [[report.internalReport pathForContentFile:FIRCLSReportInternalIncrementalKVFile]
  74. fileSystemRepresentation],
  75. false, nil);
  76. XCTAssertEqual([entries count], 1, @"");
  77. XCTAssertEqualObjects(entries[0][@"kv"][@"key"],
  78. FIRCLSFileHexEncodeString([FIRCLSUserIdentifierKey UTF8String]), @"");
  79. XCTAssertEqualObjects(entries[0][@"kv"][@"value"], FIRCLSFileHexEncodeString("12345-6"), @"");
  80. }
  81. - (void)testCustomKeysNoExisting {
  82. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"metadata_only_report"];
  83. [report setCustomValue:@"hello" forKey:@"mykey"];
  84. [report setCustomValue:@"goodbye" forKey:@"anotherkey"];
  85. [report setCustomKeysAndValues:@{
  86. @"is_test" : @(YES),
  87. @"test_number" : @(10),
  88. }];
  89. NSArray *entries = FIRCLSFileReadSections(
  90. [[report.internalReport pathForContentFile:FIRCLSReportUserIncrementalKVFile]
  91. fileSystemRepresentation],
  92. false, nil);
  93. XCTAssertEqual([entries count], 4, @"");
  94. XCTAssertEqualObjects(entries[0][@"kv"][@"key"], FIRCLSFileHexEncodeString("mykey"), @"");
  95. XCTAssertEqualObjects(entries[0][@"kv"][@"value"], FIRCLSFileHexEncodeString("hello"), @"");
  96. XCTAssertEqualObjects(entries[1][@"kv"][@"key"], FIRCLSFileHexEncodeString("anotherkey"), @"");
  97. XCTAssertEqualObjects(entries[1][@"kv"][@"value"], FIRCLSFileHexEncodeString("goodbye"), @"");
  98. XCTAssertEqualObjects(entries[2][@"kv"][@"key"], FIRCLSFileHexEncodeString("is_test"), @"");
  99. XCTAssertEqualObjects(entries[2][@"kv"][@"value"], FIRCLSFileHexEncodeString("1"), @"");
  100. XCTAssertEqualObjects(entries[3][@"kv"][@"key"], FIRCLSFileHexEncodeString("test_number"), @"");
  101. XCTAssertEqualObjects(entries[3][@"kv"][@"value"], FIRCLSFileHexEncodeString("10"), @"");
  102. }
  103. - (void)testCustomKeysWithExisting {
  104. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"ios_all_files_crash"];
  105. [report setCustomValue:@"hello" forKey:@"mykey"];
  106. [report setCustomValue:@"goodbye" forKey:@"anotherkey"];
  107. [report setCustomKeysAndValues:@{
  108. @"is_test" : @(YES),
  109. @"test_number" : @(10),
  110. }];
  111. NSArray *entries = FIRCLSFileReadSections(
  112. [[report.internalReport pathForContentFile:FIRCLSReportUserIncrementalKVFile]
  113. fileSystemRepresentation],
  114. false, nil);
  115. XCTAssertEqual([entries count], 5, @"");
  116. XCTAssertEqualObjects(entries[1][@"kv"][@"key"], FIRCLSFileHexEncodeString("mykey"), @"");
  117. XCTAssertEqualObjects(entries[1][@"kv"][@"value"], FIRCLSFileHexEncodeString("hello"), @"");
  118. XCTAssertEqualObjects(entries[2][@"kv"][@"key"], FIRCLSFileHexEncodeString("anotherkey"), @"");
  119. XCTAssertEqualObjects(entries[2][@"kv"][@"value"], FIRCLSFileHexEncodeString("goodbye"), @"");
  120. XCTAssertEqualObjects(entries[3][@"kv"][@"key"], FIRCLSFileHexEncodeString("is_test"), @"");
  121. XCTAssertEqualObjects(entries[3][@"kv"][@"value"], FIRCLSFileHexEncodeString("1"), @"");
  122. XCTAssertEqualObjects(entries[4][@"kv"][@"key"], FIRCLSFileHexEncodeString("test_number"), @"");
  123. XCTAssertEqualObjects(entries[4][@"kv"][@"value"], FIRCLSFileHexEncodeString("10"), @"");
  124. }
  125. - (void)testCustomKeysLimits {
  126. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"ios_all_files_crash"];
  127. // Write a bunch of keys and values
  128. for (int i = 0; i < 120; i++) {
  129. NSString *key = [NSString stringWithFormat:@"key_%i", i];
  130. [report setCustomValue:@"hello" forKey:key];
  131. }
  132. NSArray *entriesI = FIRCLSFileReadSections(
  133. [[report.internalReport pathForContentFile:FIRCLSReportUserIncrementalKVFile]
  134. fileSystemRepresentation],
  135. false, nil);
  136. NSArray *entriesC = FIRCLSFileReadSections(
  137. [[report.internalReport pathForContentFile:FIRCLSReportUserCompactedKVFile]
  138. fileSystemRepresentation],
  139. false, nil);
  140. // One of these should be the max (64), and one should be the number of written keys modulo 64
  141. // (eg. 56 == (120 mod 64))
  142. XCTAssertEqual(entriesI.count, 56, @"");
  143. XCTAssertEqual(entriesC.count, 64, @"");
  144. }
  145. - (void)testLogsNoExisting {
  146. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"metadata_only_report"];
  147. [report log:@"Normal log without formatting"];
  148. [report logWithFormat:@"%@, %@", @"First", @"Second"];
  149. NSArray *entries = FIRCLSFileReadSections(
  150. [[report.internalReport pathForContentFile:FIRCLSReportLogAFile] fileSystemRepresentation],
  151. false, nil);
  152. XCTAssertEqual([entries count], 2, @"");
  153. XCTAssertEqualObjects(entries[0][@"log"][@"msg"],
  154. FIRCLSFileHexEncodeString("Normal log without formatting"), @"");
  155. XCTAssertEqualObjects(entries[1][@"log"][@"msg"], FIRCLSFileHexEncodeString("First, Second"),
  156. @"");
  157. }
  158. - (void)testLogsWithExisting {
  159. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"ios_all_files_crash"];
  160. [report log:@"Normal log without formatting"];
  161. [report logWithFormat:@"%@, %@", @"First", @"Second"];
  162. NSArray *entries = FIRCLSFileReadSections(
  163. [[report.internalReport pathForContentFile:FIRCLSReportLogAFile] fileSystemRepresentation],
  164. false, nil);
  165. XCTAssertEqual([entries count], 8, @"");
  166. XCTAssertEqualObjects(entries[6][@"log"][@"msg"],
  167. FIRCLSFileHexEncodeString("Normal log without formatting"), @"");
  168. XCTAssertEqualObjects(entries[7][@"log"][@"msg"], FIRCLSFileHexEncodeString("First, Second"),
  169. @"");
  170. }
  171. - (void)testLogLimits {
  172. FIRCrashlyticsReport *report = [self createTempCopyOfReportWithName:@"metadata_only_report"];
  173. for (int i = 0; i < 2000; i++) {
  174. [report log:@"0123456789"];
  175. }
  176. unsigned long long sizeA = [[[NSFileManager defaultManager]
  177. attributesOfItemAtPath:[report.internalReport pathForContentFile:FIRCLSReportLogAFile]
  178. error:nil] fileSize];
  179. unsigned long long sizeB = [[[NSFileManager defaultManager]
  180. attributesOfItemAtPath:[report.internalReport pathForContentFile:FIRCLSReportLogBFile]
  181. error:nil] fileSize];
  182. NSArray *entriesA = FIRCLSFileReadSections(
  183. [[report.internalReport pathForContentFile:FIRCLSReportLogAFile] fileSystemRepresentation],
  184. false, nil);
  185. NSArray *entriesB = FIRCLSFileReadSections(
  186. [[report.internalReport pathForContentFile:FIRCLSReportLogBFile] fileSystemRepresentation],
  187. false, nil);
  188. // If these numbers have changed, the goal is to validate that the size of log_a and log_b are
  189. // under the limit, logStorage.maxSize (64 * 1000). These numbers don't need to be exact so if
  190. // they fluctuate then we might just need to accept a range in these tests.
  191. XCTAssertEqual(entriesB.count + entriesA.count, 2000, @"");
  192. XCTAssertEqual(sizeA, 64 * 1000 + 20, @"");
  193. XCTAssertEqual(sizeB, 55980, @"");
  194. }
  195. @end