Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use none-deprecated archive functions when possible #394

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@
children = (
9679F4A027CD8D0600800797 /* CLLocation+Radar.h */,
9679F4A227CD8DE200800797 /* CLLocation+Radar.m */,
53CCD782275E579800F79CC8 /* RadarLogBuffer.m */,
);
name = Util;
sourceTree = "<group>";
Expand Down Expand Up @@ -497,6 +496,7 @@
96A5A11527ADA02E007B960B /* RadarLog.h */,
96A5A11427ADA02E007B960B /* RadarLog.m */,
96A5A11627ADA02E007B960B /* RadarLogBuffer.h */,
53CCD782275E579800F79CC8 /* RadarLogBuffer.m */,
DD236D66230A0D6700EB88F9 /* RadarLogger.h */,
DD236D67230A0D6700EB88F9 /* RadarLogger.m */,
9683FD6127B36C26009EBB6B /* RadarMeta.h */,
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
Represents a debug log.
*/
@interface RadarLog : NSObject <NSCoding>
@interface RadarLog : NSObject <NSSecureCoding>

/**
The levels for debug logs.
Expand Down
4 changes: 4 additions & 0 deletions RadarSDK/RadarLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,8 @@ - (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:_createdAt forKey:@"createdAt"];
}

+ (BOOL)supportsSecureCoding {
return YES;
}

@end
17 changes: 15 additions & 2 deletions RadarSDK/RadarLogBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ - (void)persistLogs {
for (NSString *file in files) {
NSString *filePath = [self.logFileDir stringByAppendingPathComponent:file];
NSData *fileData = [self.fileHandler readFileAtPath:filePath];
RadarLog *log = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
RadarLog *log;
if (@available(iOS 11.0, *)) {
log = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithObjects:[NSString class], [RadarLog class], [NSDate class], nil] fromData:fileData error:nil];
} else {
log = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
}

if (log && log.message) {
[logs addObject:log];
}
Expand All @@ -122,7 +128,14 @@ - (void)persistLogs {

- (void)writeToFileStorage:(NSArray <RadarLog *> *)logs {
for (RadarLog *log in logs) {
NSData *logData = [NSKeyedArchiver archivedDataWithRootObject:log];

NSData *logData;
if (@available(iOS 11.0, *)) {
logData = [NSKeyedArchiver archivedDataWithRootObject:log requiringSecureCoding:YES error:nil];
} else {
logData = [NSKeyedArchiver archivedDataWithRootObject:log];
}

NSTimeInterval unixTimestamp = [log.createdAt timeIntervalSince1970];
// logs may be created in the same millisecond, so we append a counter to the end of the timestamp to "tiebreak"
NSString *unixTimestampString = [NSString stringWithFormat:@"%lld_%04d", (long long)unixTimestamp, fileCounter++];
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarReplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#import "Radar.h"

@interface RadarReplay : NSObject <NSCoding>
@interface RadarReplay : NSObject <NSSecureCoding>

@property (nonnull, copy, nonatomic, readonly) NSDictionary *replayParams;

Expand Down
4 changes: 4 additions & 0 deletions RadarSDK/RadarReplay.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ - (NSUInteger)hash {
return [self.replayParams hash];
}

+ (BOOL)supportsSecureCoding {
return YES;
}

@end
27 changes: 23 additions & 4 deletions RadarSDK/RadarReplayBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ - (void)writeNewReplayToBuffer:(NSMutableDictionary *)replayParams {
[prunedBuffer addObject:mutableReplayBuffer[i]];
}
}
replaysData = [NSKeyedArchiver archivedDataWithRootObject:prunedBuffer];
if (@available(iOS 11.0, *)) {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:prunedBuffer requiringSecureCoding:true error:nil];
} else {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:prunedBuffer];
}
} else {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer];
if (@available(iOS 11.0, *)) {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer requiringSecureCoding:true error:nil];
} else {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer];
}
}

[[NSUserDefaults standardUserDefaults] setObject:replaysData forKey:@"radar-replays"];
Expand Down Expand Up @@ -150,14 +158,25 @@ - (void)removeReplaysFromBuffer:(NSArray<RadarReplay *> *)replays {
[mutableReplayBuffer removeObjectsInArray:replays];

// persist the updated buffer
NSData *replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer];
NSData *replaysData;
if (@available(iOS 11.0, *)) {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer requiringSecureCoding:true error:nil];
} else {
replaysData = [NSKeyedArchiver archivedDataWithRootObject:mutableReplayBuffer];
}
[[NSUserDefaults standardUserDefaults] setObject:replaysData forKey:@"radar-replays"];
}

- (void)loadReplaysFromPersistentStore {
NSData *replaysData = [[NSUserDefaults standardUserDefaults] objectForKey:@"radar-replays"];
if (replaysData) {
NSArray *replays = [NSKeyedUnarchiver unarchiveObjectWithData:replaysData];
NSArray *replays;
if (@available(iOS 11.0, *)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused about iOS 11 version gate since NSSecureCoding is available from iOS 6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the archivedDataWithRootObject: requireingSecureCoding: function is only introduced in iOS 11.0. the function that we were using (without the second param) is the one raising security alerts.

NSSet *allowedClasses = [NSSet setWithObjects:[NSArray class], [RadarReplay class], [NSDictionary class], [NSString class], [NSNumber class], nil];
replays = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowedClasses fromData:replaysData error:nil];
} else {
replays = [NSKeyedUnarchiver unarchiveObjectWithData:replaysData];
}
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelDebug message:[NSString stringWithFormat:@"Loaded replays | length = %lu", (unsigned long)[replays count]]];
mutableReplayBuffer = [NSMutableArray arrayWithArray:replays];
}
Expand Down
21 changes: 19 additions & 2 deletions RadarSDKTests/RadarSDKTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#import "RadarTestUtils.h"
#import "RadarTripOptions.h"
#import "RadarFileStorage.h"

#import "RadarReplayBuffer.h"

@interface RadarSDKTests : XCTestCase

Expand All @@ -30,6 +30,7 @@ @interface RadarSDKTests : XCTestCase
@property (nonatomic, strong) RadarFileStorage *fileSystem;
@property (nonatomic, strong) NSString *testFilePath;
@property (nonatomic, strong) RadarLogBuffer *logBuffer;
@property (nonatomic, strong) RadarReplayBuffer *replayBuffer;
@end

@implementation RadarSDKTests
Expand Down Expand Up @@ -299,7 +300,7 @@ - (void)setUp {
self.testFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"testfile"];
[[RadarLogBuffer sharedInstance]clearBuffer];
[[RadarLogBuffer sharedInstance]setPersistentLogFeatureFlag:YES];

[[RadarReplayBuffer sharedInstance]clearBuffer];
}

- (void)tearDown {
Expand Down Expand Up @@ -1489,6 +1490,22 @@ - (void)test_RadarLogBuffer_purge {
[[RadarLogBuffer sharedInstance]clearBuffer];
}

- (void)test_RadarReplayBuffer_writeAndRead {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for adding this test

RadarSdkConfiguration *sdkConfiguration = [RadarSettings sdkConfiguration];
sdkConfiguration.usePersistence = true;
[RadarSettings setSdkConfiguration:sdkConfiguration];

CLLocation *location = [[CLLocation alloc] initWithLatitude:0.1 longitude:0.1];
NSMutableDictionary * params = [RadarTestUtils createTrackParamWithLocation:location stopped:YES foreground:YES source:RadarLocationSourceGeofenceEnter replayed:YES beacons:[NSArray arrayWithObject:[RadarBeacon alloc]] verified:YES attestationString:@"attestationString" keyId:@"keyID" attestationError:@"attestationError" encrypted:YES expectedCountryCode:@"CountryCode" expectedStateCode:@"StateCode"];

[[RadarReplayBuffer sharedInstance] writeNewReplayToBuffer:params];
[[RadarReplayBuffer sharedInstance] setValue:NULL forKey:@"mutableReplayBuffer"];
[[RadarReplayBuffer sharedInstance] loadReplaysFromPersistentStore];
NSMutableArray<RadarReplay *> *mutableReplayBuffer = [[RadarReplayBuffer sharedInstance] valueForKey:@"mutableReplayBuffer"];
XCTAssertEqual(mutableReplayBuffer.count, 1);
XCTAssertEqualObjects(mutableReplayBuffer.firstObject.replayParams, params);
}

- (void)test_RadarSdkConfiguration {
RadarSdkConfiguration *sdkConfiguration = [[RadarSdkConfiguration alloc] initWithDict:@{
@"logLevel": @"warning",
Expand Down
26 changes: 26 additions & 0 deletions RadarSDKTests/RadarTestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,38 @@

#import <Foundation/Foundation.h>

#import "CLLocation+Radar.h"
#import "RadarLocationManager.h"
#import "RadarSettings.h"
#import "RadarState.h"
#import "RadarUtils.h"
#import "RadarTripOptions.h"
#import "RadarVerificationManager.h"

NS_ASSUME_NONNULL_BEGIN

@interface RadarTestUtils : NSObject

+ (NSDictionary *)jsonDictionaryFromResource:(NSString *)resource;

/**
Construct a track param dict which can be used to test replays from paramters of the track call.
uses the stored values from `RadarSettings`, `RadarUtils`, and `RadarState`
*/
+ (NSMutableDictionary *)createTrackParamWithLocation:(CLLocation *_Nonnull)location
stopped:(BOOL)stopped
foreground:(BOOL)foreground
source:(RadarLocationSource)source
replayed:(BOOL)replayed
beacons:(NSArray<RadarBeacon *> *_Nullable)beacons
verified:(BOOL)verified
attestationString:(NSString *_Nullable)attestationString
keyId:(NSString *_Nullable)keyId
attestationError:(NSString *_Nullable)attestationError
encrypted:(BOOL)encrypted
expectedCountryCode:(NSString * _Nullable)expectedCountryCode
expectedStateCode:(NSString * _Nullable)expectedStateCode;

@end

NS_ASSUME_NONNULL_END
Loading
Loading