Skip to content

Commit

Permalink
Enforce size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali committed Jul 22, 2016
1 parent debc6ab commit 47c9985
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
42 changes: 32 additions & 10 deletions Source/BugsnagBreadcrumb.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "Bugsnag.h"

NSString *const BSGBreadcrumbDefaultName = @"manual";
NSUInteger const BSGBreadcrumbMaxByteSize = 4096;

NSString *BSGBreadcrumbTypeValue(BSGBreadcrumbType type) {
switch (type) {
Expand All @@ -52,6 +53,12 @@
@interface BugsnagBreadcrumbs()

@property (nonatomic,readwrite,strong) NSMutableArray* breadcrumbs;

@end

@interface BugsnagBreadcrumb ()

- (NSDictionary *_Nullable)objectValue;
@end

@implementation BugsnagBreadcrumb
Expand All @@ -70,6 +77,21 @@ - (BOOL)isValid {
return self.name.length > 0 && self.timestamp != nil;
}

- (NSDictionary *)objectValue {
NSString* timestamp = [[Bugsnag payloadDateFormatter] stringFromDate:self.timestamp];
if (timestamp && self.name.length > 0) {
NSMutableDictionary *data = @{
@"name": self.name,
@"timestamp": timestamp,
@"type": BSGBreadcrumbTypeValue(self.type)
}.mutableCopy;
if (self.metadata)
data[@"metaData"] = self.metadata;
return data;
}
return nil;
}

+ (instancetype)breadcrumbWithBlock:(BSGBreadcrumbConfiguration)block {
BugsnagBreadcrumb *crumb = [self new];
if (block)
Expand Down Expand Up @@ -145,16 +167,16 @@ - (NSArray *)arrayValue {
}
NSMutableArray* contents = [[NSMutableArray alloc] initWithCapacity:[self count]];
for (BugsnagBreadcrumb* crumb in self.breadcrumbs) {
NSString* timestamp = [[Bugsnag payloadDateFormatter] stringFromDate:crumb.timestamp];
if (timestamp && crumb.name.length > 0) {
NSMutableDictionary *data = @{
@"name": crumb.name,
@"timestamp": timestamp,
@"type": BSGBreadcrumbTypeValue(crumb.type)
}.mutableCopy;
if (crumb.metadata)
data[@"metaData"] = crumb.metadata;
[contents addObject:data];
NSDictionary *objectValue = [crumb objectValue];
NSError *error = nil;
@try {
NSData* data = [NSJSONSerialization dataWithJSONObject:objectValue options:0 error:&error];
if (data.length <= BSGBreadcrumbMaxByteSize)
[contents addObject:objectValue];
else
NSLog(@"Dropping Bugsnag breadcrumb (%@) exceeding %lu byte size limit", crumb.name, (unsigned long)BSGBreadcrumbMaxByteSize);
} @catch (NSException *exception) {
NSLog(@"Unable to serialize breadcrumb for Bugsnag: %@", error);
}
}
return contents;
Expand Down
28 changes: 28 additions & 0 deletions Tests/BugsnagBreadcrumbsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,32 @@ - (void)testArrayValue {
XCTAssertEqualObjects(value[2][@"metaData"][@"message"], @"Close tutorial");
}

- (void)testStateType {
BugsnagBreadcrumbs* crumbs = [BugsnagBreadcrumbs new];
[crumbs addBreadcrumbWithBlock:^(BugsnagBreadcrumb * _Nonnull crumb) {
crumb.type = BSGBreadcrumbTypeState;
crumb.name = @"Rotated Menu";
crumb.metadata = @{ @"direction": @"right" };
}];
NSArray* value = [crumbs arrayValue];
XCTAssertEqualObjects(value[0][@"metaData"][@"direction"], @"right");
XCTAssertEqualObjects(value[0][@"name"], @"Rotated Menu");
XCTAssertEqualObjects(value[0][@"type"], @"state");
}

- (void)testByteSizeLimit {
BugsnagBreadcrumbs* crumbs = [BugsnagBreadcrumbs new];
[crumbs addBreadcrumbWithBlock:^(BugsnagBreadcrumb * _Nonnull crumb) {
crumb.type = BSGBreadcrumbTypeState;
crumb.name = @"Rotated Menu";
NSMutableDictionary *metadata = @{}.mutableCopy;
for (int i = 0; i < 400; i++) {
metadata[[NSString stringWithFormat:@"%d", i]] = @"!!";
}
crumb.metadata = metadata;
}];
NSArray* value = [crumbs arrayValue];
XCTAssertTrue(value.count == 0);
}

@end

0 comments on commit 47c9985

Please sign in to comment.