Skip to content

Commit

Permalink
fix: fix for when macos sandbox disabled (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Feb 7, 2024
1 parent 7ed1b8c commit e148734
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Amplitude.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
3E2411F526F9A4E100793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; };
3E2411F626F9A4E200793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; };
3E2411F726F9A4E400793829 /* PlanTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E2411F026F9A4C600793829 /* PlanTests.m */; };
3E6FCF262B71B11900A31DC3 /* AMPUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E6FCF252B71B11900A31DC3 /* AMPUtilsTests.m */; };
3E6FCF272B71B11900A31DC3 /* AMPUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E6FCF252B71B11900A31DC3 /* AMPUtilsTests.m */; };
3E6FCF282B71B11900A31DC3 /* AMPUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E6FCF252B71B11900A31DC3 /* AMPUtilsTests.m */; };
3EF608C92720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; };
3EF608CA2720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; };
3EF608CB2720E74D00133703 /* AMPServerZone.h in Headers */ = {isa = PBXBuildFile; fileRef = 3EF608C82720E74D00133703 /* AMPServerZone.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -372,6 +375,7 @@
3E2411E626F9A40100793829 /* AMPPlan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AMPPlan.h; sourceTree = "<group>"; };
3E2411EB26F9A46500793829 /* AMPPlan.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPPlan.m; sourceTree = "<group>"; };
3E2411F026F9A4C600793829 /* PlanTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PlanTests.m; sourceTree = "<group>"; };
3E6FCF252B71B11900A31DC3 /* AMPUtilsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMPUtilsTests.m; sourceTree = "<group>"; };
3EF608C82720E74D00133703 /* AMPServerZone.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMPServerZone.h; sourceTree = "<group>"; };
3EF608D22720F52400133703 /* AMPServerZoneUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AMPServerZoneUtil.h; sourceTree = "<group>"; };
3EF608D72720F64500133703 /* AMPServerZoneUtil.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AMPServerZoneUtil.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -580,6 +584,7 @@
12C9730F24108DFF00E9CDDB /* Tests */ = {
isa = PBXGroup;
children = (
3E6FCF252B71B11900A31DC3 /* AMPUtilsTests.m */,
D00A34EB2991EBE300BA484F /* IdentifyInterceptorTests.m */,
3E2411F026F9A4C600793829 /* PlanTests.m */,
12C9731824108DFF00E9CDDB /* BaseTestCase.h */,
Expand Down Expand Up @@ -1201,6 +1206,7 @@
3EF608DE27211AC000133703 /* ServerZoneUtilTests.m in Sources */,
58D45C332A4CDC7C00A090A3 /* ScreenViewTests.m in Sources */,
582516F128C075D200ECAD0D /* IngestionMetadataTests.m in Sources */,
3E6FCF272B71B11900A31DC3 /* AMPUtilsTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1257,6 +1263,7 @@
3EF608DD27211AC000133703 /* ServerZoneUtilTests.m in Sources */,
58D45C322A4CDC7C00A090A3 /* ScreenViewTests.m in Sources */,
582516F028C075D100ECAD0D /* IngestionMetadataTests.m in Sources */,
3E6FCF262B71B11900A31DC3 /* AMPUtilsTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1293,6 +1300,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3E6FCF282B71B11900A31DC3 /* AMPUtilsTests.m in Sources */,
12C973B7241244C700E9CDDB /* SetupTests.m in Sources */,
3E2411F726F9A4E400793829 /* PlanTests.m in Sources */,
12C973C6241244F100E9CDDB /* IdentifyTests.m in Sources */,
Expand Down
4 changes: 3 additions & 1 deletion Sources/Amplitude/AMPDatabaseHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName {

if ((self = [super init])) {
NSString *databaseDirectory = [AMPUtils platformDataDirectory];
NSString *databasePath = [databaseDirectory stringByAppendingPathComponent:@"com.amplitude.database"];
NSString *appPathWithBundleIdenitfier = [NSString stringWithFormat:@"com.amplitude.database.%@", [[NSBundle mainBundle] bundleIdentifier]];
NSString *appPath = [AMPUtils isSandboxEnabled] ? @"com.amplitude.database" : appPathWithBundleIdenitfier;
NSString *databasePath = [databaseDirectory stringByAppendingPathComponent:appPath];
if (![instanceName isEqualToString:kAMPDefaultInstance]) {
databasePath = [NSString stringWithFormat:@"%@_%@", databasePath, instanceName];
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Amplitude/AMPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
+ (BOOL)isEmptyString:(NSString *)str;
+ (NSDictionary *)validateGroups:(NSDictionary *)obj;
+ (NSString *)platformDataDirectory;
+ (NSDictionary<NSString *, NSString *> *)getEnvironment;
+ (BOOL)isSandboxEnabled;

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
+ (UIApplication *)getSharedApplication;
Expand Down
17 changes: 17 additions & 0 deletions Sources/Amplitude/AMPUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,21 @@ + (UIWindow *)getKeyWindow {

#endif

// Helper function to get the environment variables
+ (NSDictionary<NSString *, NSString *> *)getEnvironment {
return [[NSProcessInfo processInfo] environment];
}

// Method to check if sandbox is enabled
+ (BOOL)isSandboxEnabled {
#if TARGET_OS_OSX
// Check if macOS app has "App Sandbox" enabled
NSDictionary<NSString *, NSString *> *environment = [self getEnvironment];
return environment[@"APP_SANDBOX_CONTAINER_ID"] != nil;
#else
// Other platforms (iOS, tvOS, watchOS) are sandboxed by default
return YES;
#endif
}

@end
45 changes: 45 additions & 0 deletions Tests/AMPUtilsTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// AMPUtilsTests.m
// Amplitude
//
// Created by Qingzhuo Zhen on 2/5/24.
// Copyright © 2024 Amplitude. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "AMPUtils.h"

@interface AMPUtilTests : XCTestCase

@end

@interface FakeAMPUtils: AMPUtils
@end

@implementation FakeAMPUtils

+ (NSDictionary *)getEnvironment {
return @{@"APP_SANDBOX_CONTAINER_ID": @"test-container-id"};
}

@end

@implementation AMPUtilTests {}

- (void) testIsSandboxEnabled {
BOOL isSandboxEnabled = [AMPUtils isSandboxEnabled];
#if TARGET_OS_OSX
XCTAssertEqual(isSandboxEnabled, NO);
#else
XCTAssertEqual(isSandboxEnabled, YES);
#endif
}

#if TARGET_OS_OSX
- (void) testIsSandboxEnabledWhenMacOSIsSandboxed {
BOOL isSandboxEnabled = [FakeAMPUtils isSandboxEnabled];
XCTAssertEqual(isSandboxEnabled, YES);
}
#endif

@end

0 comments on commit e148734

Please sign in to comment.