Skip to content

Commit

Permalink
fix: SentryFilemanager will create folder depending on dsn (#229)
Browse files Browse the repository at this point in the history
* fix: SentryFilemanager will create folder depending on dsn

Fixes #216

* ci: Update to new xcode9.2 image

* meta: Remove unused code
  • Loading branch information
HazAT authored Dec 8, 2017
1 parent e9a4f28 commit f4595e7
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode8.3
osx_image: xcode9.2

cache:
- bundler
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ - (_Nullable instancetype)initWithDsn:(NSString *)dsn
self.dsn = [[SentryDsn alloc] initWithString:dsn didFailWithError:error];
self.requestManager = requestManager;
NSLog(@"Sentry Started -- Version: %@", self.class.versionString);
self.fileManager = [[SentryFileManager alloc] initWithError:error];
self.fileManager = [[SentryFileManager alloc] initWithDsn:self.dsn didFailWithError:error];
self.breadcrumbs = [[SentryBreadcrumbStore alloc] initWithFileManager:self.fileManager];
if (nil != error && nil != *error) {
[SentryLog logWithMessage:(*error).localizedDescription andLevel:kSentryLogLevelError];
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/SentryDsn.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2017 Sentry. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>

#if __has_include(<Sentry/Sentry.h>)

#import <Sentry/SentryDsn.h>
Expand Down Expand Up @@ -37,6 +39,17 @@ - (_Nullable instancetype)initWithString:(NSString *)dsnString didFailWithError:
return self;
}

- (NSString *)getHash {
NSData *data = [[self.url absoluteString] dataUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
}
return output;
}

- (NSURL *_Nullable)convertDsnString:(NSString *)dsnString didFailWithError:(NSError *_Nullable *_Nullable)error {
NSString *trimmedDsnString = [dsnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSSet *allowedSchemes = [NSSet setWithObjects:@"http", @"https", nil];
Expand Down
7 changes: 6 additions & 1 deletion Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#import <Sentry/SentryLog.h>
#import <Sentry/SentryEvent.h>
#import <Sentry/SentryBreadcrumb.h>
#import <Sentry/SentryDsn.h>

#else
#import "SentryFileManager.h"
#import "SentryError.h"
#import "SentryLog.h"
#import "SentryEvent.h"
#import "SentryBreadcrumb.h"
#import "SentryDsn.h"
#endif

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -35,12 +37,15 @@ @interface SentryFileManager ()

@implementation SentryFileManager

- (_Nullable instancetype)initWithError:(NSError **)error {
- (_Nullable instancetype)initWithDsn:(SentryDsn *)dsn didFailWithError:(NSError **)error {
self = [super init];
if (self) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;

self.sentryPath = [cachePath stringByAppendingPathComponent:@"io.sentry"];
self.sentryPath = [self.sentryPath stringByAppendingPathComponent:[dsn getHash]];

if (![fileManager fileExistsAtPath:self.sentryPath]) {
[self.class createDirectoryAtPath:self.sentryPath withError:error];
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryDsn.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN

- (_Nullable instancetype)initWithString:(NSString *)dsnString didFailWithError:(NSError *_Nullable *_Nullable)error;

- (NSString *)getHash;

@end

NS_ASSUME_NONNULL_END
4 changes: 2 additions & 2 deletions Sources/Sentry/include/SentryFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

NS_ASSUME_NONNULL_BEGIN

@class SentryEvent, SentryBreadcrumb;
@class SentryEvent, SentryBreadcrumb, SentryDsn;

@interface SentryFileManager : NSObject
SENTRY_NO_INIT

- (_Nullable instancetype)initWithError:(NSError **)error;
- (_Nullable instancetype)initWithDsn:(SentryDsn *)dsn didFailWithError:(NSError **)error;

- (NSString *)storeEvent:(SentryEvent *)event;

Expand Down
3 changes: 2 additions & 1 deletion Tests/SentryTests/SentryBreadcrumbTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SentryBreadcrumbStore.h"
#import "SentryFileManager.h"
#import "NSDate+Extras.h"
#import "SentryDsn.h"

@interface SentryBreadcrumbTests : XCTestCase

Expand All @@ -23,7 +24,7 @@ @implementation SentryBreadcrumbTests
- (void)setUp {
[super setUp];
NSError *error = nil;
self.fileManager = [[SentryFileManager alloc] initWithError:&error];
self.fileManager = [[SentryFileManager alloc] initWithDsn:[[SentryDsn alloc] initWithString:@"https://username:[email protected]/12345" didFailWithError:nil] didFailWithError:&error];
XCTAssertNil(error);
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/SentryTests/SentryFileManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <XCTest/XCTest.h>
#import <Sentry/Sentry.h>
#import "SentryFileManager.h"
#import "SentryDsn.h"

@interface SentryFileManagerTests : XCTestCase

Expand All @@ -22,7 +23,7 @@ - (void)setUp {
[super setUp];
SentryClient.logLevel = kSentryLogLevelDebug;
NSError *error = nil;
self.fileManager = [[SentryFileManager alloc] initWithError:&error];
self.fileManager = [[SentryFileManager alloc] initWithDsn:[[SentryDsn alloc] initWithString:@"https://username:[email protected]/12345" didFailWithError:nil] didFailWithError:&error];
XCTAssertNil(error);
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/SentryInterfacesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ - (void)testBreadcrumb {
}

- (void)testBreadcrumbStore {
SentryBreadcrumbStore *store = [[SentryBreadcrumbStore alloc] initWithFileManager:[[SentryFileManager alloc] initWithError:nil]];
SentryBreadcrumbStore *store = [[SentryBreadcrumbStore alloc] initWithFileManager:[[SentryFileManager alloc] initWithDsn:[[SentryDsn alloc] initWithString:@"https://username:[email protected]/12345" didFailWithError:nil] didFailWithError:nil]];
SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentrySeverityInfo category:@"http"];
[store addBreadcrumb:crumb];
NSDate *date = [NSDate date];
Expand Down
17 changes: 14 additions & 3 deletions Tests/SentryTests/SentryJavaScriptBridgeHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import "SentryJavaScriptBridgeHelper.h"
#import <Sentry/Sentry.h>

NSString *rnReportPath = @"";

@interface SentryJavaScriptBridgeHelper()

+ (NSArray *)parseJavaScriptStacktrace:(NSString *)stacktrace;
Expand Down Expand Up @@ -109,14 +111,22 @@ - (void)testCreateBreadcrumb {
XCTAssertTrue([crumb2.timestamp compare:date]);
}

- (NSDictionary *)getCrashReport {
NSString *jsonPath = [[NSBundle bundleForClass:self.class] pathForResource:rnReportPath ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:jsonPath]];
return [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
}

- (void)testCreateEvent {
SentryEvent *sentryEvent1 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle bundleForClass:self.class] pathForResource:@"raven-sendMessage" ofType:@"json"]]] options:0 error:nil]];
rnReportPath = @"Resources/raven-sendMessage";
SentryEvent *sentryEvent1 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[self getCrashReport]];
XCTAssertEqualObjects(sentryEvent1.message, @"TEST message");
XCTAssertNotNil(sentryEvent1.extra);
XCTAssertNotNil(sentryEvent1.tags);
XCTAssertNotNil(sentryEvent1.user);

SentryEvent *sentryEvent2 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle bundleForClass:self.class] pathForResource:@"raven-rejectedpromise" ofType:@"json"]]] options:0 error:nil]];
rnReportPath = @"Resources/raven-rejectedpromise";
SentryEvent *sentryEvent2 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[self getCrashReport]];
XCTAssertEqualObjects(sentryEvent2.message, @"Boom promise");
XCTAssertEqualObjects(sentryEvent2.platform, @"cocoa");
XCTAssertEqualObjects(sentryEvent2.exceptions.firstObject.type, @"Unhandled Promise Rejection");
Expand All @@ -127,7 +137,8 @@ - (void)testCreateEvent {
XCTAssertNotNil(sentryEvent2.tags);
XCTAssertNotNil(sentryEvent2.user);

SentryEvent *sentryEvent3 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle bundleForClass:self.class] pathForResource:@"raven-throwerror" ofType:@"json"]]] options:0 error:nil]];
rnReportPath = @"Resources/raven-throwerror";
SentryEvent *sentryEvent3 = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:[self getCrashReport]];
XCTAssertEqualObjects(sentryEvent3.exceptions.firstObject.value, @"Sentry: Test throw error");
XCTAssertEqualObjects(sentryEvent3.exceptions.firstObject.type, @"Error");
XCTAssertEqual(sentryEvent3.exceptions.firstObject.thread.stacktrace.frames.count, (NSUInteger)30);
Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/SentryRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ @implementation SentryRequestTests

- (void)clearAllFiles {
NSError *error = nil;
SentryFileManager *fileManager = [[SentryFileManager alloc] initWithError:&error];
SentryFileManager *fileManager = [[SentryFileManager alloc] initWithDsn:[[SentryDsn alloc] initWithString:@"https://username:[email protected]/12345" didFailWithError:nil] didFailWithError:&error];
[fileManager deleteAllStoredEvents];
[fileManager deleteAllStoredBreadcrumbs];
[fileManager deleteAllFolders];
Expand Down
2 changes: 1 addition & 1 deletion Tests/SentryTests/SentrySwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SentrySwiftTests: XCTestCase {

override func setUp() {
super.setUp()
let fileManager = try! SentryFileManager(error: ())
let fileManager = try! SentryFileManager(dsn: SentryDsn(string: "https://username:[email protected]/12345"))
fileManager.deleteAllStoredEvents()
fileManager.deleteAllStoredBreadcrumbs()
fileManager.deleteAllFolders()
Expand Down
4 changes: 3 additions & 1 deletion Tests/SentryTests/SentryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ - (void)testCrashedLastLaunch {

- (void)testBreadCrumbTracking {
NSError *error = nil;
SentryClient *client = [[SentryClient alloc] initWithDsn:@"https://username:[email protected]/12345" didFailWithError:&error];
SentryClient *client = [[SentryClient alloc] initWithDsn:@"https://username:[email protected]/123456" didFailWithError:&error];
[client.breadcrumbs clear];
[client enableAutomaticBreadcrumbTracking];
XCTAssertEqual(client.breadcrumbs.count, (unsigned long)0);
[SentryClient setSharedClient:client];
[SentryClient.sharedClient enableAutomaticBreadcrumbTracking];
XCTAssertEqual(SentryClient.sharedClient.breadcrumbs.count, (unsigned long)1);
[SentryClient setSharedClient:nil];
[client.breadcrumbs clear];
}

- (void)testInstallation {
Expand Down

0 comments on commit f4595e7

Please sign in to comment.