Skip to content

Commit

Permalink
chore: extract isSandboxEnabled into util class for testing, added so…
Browse files Browse the repository at this point in the history
…me tests - WIP
  • Loading branch information
justin-fiedler committed Feb 1, 2024
1 parent 5aef5ca commit 3b277fd
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 17 deletions.
12 changes: 10 additions & 2 deletions Amplitude-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
BA994B9D2A4F4FCB00D0913F /* legacy_v4.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = BA994B9B2A4F4B7500D0913F /* legacy_v4.sqlite */; };
BA9BEA4B299FB43B00BC0F7C /* IdentifyInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA9BEA4A299FB43B00BC0F7C /* IdentifyInterceptor.swift */; };
BA9BEA4D299FB4BB00BC0F7C /* IdentifyInterceptorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA9BEA4C299FB4BB00BC0F7C /* IdentifyInterceptorTests.swift */; };
D010435F2B6C59EE00F8173C /* SandboxHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D010435E2B6C59EE00F8173C /* SandboxHelper.swift */; };
D01043612B6C5A8500F8173C /* SandboxHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D01043602B6C5A8500F8173C /* SandboxHelperTests.swift */; };
OBJ_100 /* Mediator.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_21 /* Mediator.swift */; };
OBJ_101 /* AmplitudeDestinationPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_23 /* AmplitudeDestinationPlugin.swift */; };
OBJ_102 /* ContextPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_24 /* ContextPlugin.swift */; };
Expand Down Expand Up @@ -123,14 +125,14 @@
isa = PBXContainerItemProxy;
containerPortal = OBJ_1 /* Project object */;
proxyType = 1;
remoteGlobalIDString = amplitude-swift::Amplitude-Swift;
remoteGlobalIDString = "amplitude-swift::Amplitude-Swift";
remoteInfo = "Amplitude-Swift";
};
580FD1F1294A56F60036777B /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = OBJ_1 /* Project object */;
proxyType = 1;
remoteGlobalIDString = amplitude-swift::Amplitude-SwiftTests;
remoteGlobalIDString = "amplitude-swift::Amplitude-SwiftTests";
remoteInfo = "Amplitude-SwiftTests";
};
/* End PBXContainerItemProxy section */
Expand Down Expand Up @@ -176,6 +178,8 @@
BA994B9B2A4F4B7500D0913F /* legacy_v4.sqlite */ = {isa = PBXFileReference; lastKnownFileType = file; path = legacy_v4.sqlite; sourceTree = "<group>"; };
BA9BEA4A299FB43B00BC0F7C /* IdentifyInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentifyInterceptor.swift; sourceTree = "<group>"; };
BA9BEA4C299FB4BB00BC0F7C /* IdentifyInterceptorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IdentifyInterceptorTests.swift; sourceTree = "<group>"; };
D010435E2B6C59EE00F8173C /* SandboxHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SandboxHelper.swift; sourceTree = "<group>"; };
D01043602B6C5A8500F8173C /* SandboxHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SandboxHelperTests.swift; sourceTree = "<group>"; };
OBJ_10 /* ConsoleLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleLogger.swift; sourceTree = "<group>"; };
OBJ_11 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
OBJ_12 /* EventBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventBridge.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -394,6 +398,7 @@
OBJ_52 /* UrlExtension.swift */,
BA9BEA4A299FB43B00BC0F7C /* IdentifyInterceptor.swift */,
8EDEC54AB4DF9E1074C3D6A4 /* Weak.swift */,
D010435E2B6C59EE00F8173C /* SandboxHelper.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -499,6 +504,7 @@
OBJ_74 /* UrlExtensionTests.swift */,
BA9BEA4C299FB4BB00BC0F7C /* IdentifyInterceptorTests.swift */,
8EDEC4F83BFAA664749FAEF0 /* QueueTimeTests.swift */,
D01043602B6C5A8500F8173C /* SandboxHelperTests.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -640,6 +646,7 @@
OBJ_154 /* TypesTests.swift in Sources */,
OBJ_155 /* EventPipelineTests.swift in Sources */,
OBJ_156 /* HttpClientTests.swift in Sources */,
D01043612B6C5A8500F8173C /* SandboxHelperTests.swift in Sources */,
OBJ_157 /* PersistentStorageResponseHandlerTests.swift in Sources */,
OBJ_158 /* UrlExtensionTests.swift in Sources */,
8EDEC4EE0DE1C89889F451B5 /* QueueTimeTests.swift in Sources */,
Expand Down Expand Up @@ -698,6 +705,7 @@
8EDEC8F8DD2CDCD6568512F8 /* RemnantDataMigration.swift in Sources */,
8EDEC977C03AA2676724F436 /* BasePlugins.swift in Sources */,
8EDEC1073A308B12B5CCD975 /* AnalyticsConnectorPlugin.swift in Sources */,
D010435F2B6C59EE00F8173C /* SandboxHelper.swift in Sources */,
8EDEC3283B812D5D34DADF7B /* AnalyticsConnectorIdentityPlugin.swift in Sources */,
8EDEC4D0C0CE07BF211804CC /* DefaultTrackingOptions.swift in Sources */,
8EDEC30C0075E9D92B1B5210 /* UIKitScreenViews.swift in Sources */,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions Sources/Amplitude/Storages/PersistentStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,10 @@ extension PersistentStorage {
return result
}

private func isSandboxEnabled() -> Bool {
let environment = ProcessInfo.processInfo.environment
return environment["APP_SANDBOX_CONTAINER_ID"] != nil
}

internal func getEventsStorageDirectory(createDirectory: Bool = true) -> URL {
let searchPathDirectory = FileManager.SearchPathDirectory.applicationSupportDirectory

// Make sure Amplitude data is sandboxed per app
#if os(iOS)
// iOS is sandboxed by default
let appPath = ""
#else
// macOS/tvOS are not sandboxed automatically
let appPath = isSandboxEnabled() ? "" : "\(Bundle.main.bundleIdentifier!)/"
#endif
let appPath = SandboxHelper.isSandboxEnabled() ? "" : "\(Bundle.main.bundleIdentifier!)/"

let urls = fileManager.urls(for: searchPathDirectory, in: .userDomainMask)
let docUrl = urls[0]
Expand All @@ -267,7 +255,6 @@ extension PersistentStorage {
at: storageUrl, withIntermediateDirectories: true, attributes: nil)
try? storageUrl.setResourceValues(values)
}

return storageUrl
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/Amplitude/Utilities/SandboxHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SandboxHelper.swift
// Amplitude-Swift
//
// Created by Justin Fiedler on 2/1/24.
//

import Foundation

public class SandboxHelper {
static public func isSandboxEnabled() -> Bool {
#if os(iOS)
// iOS is always sandboxed
return true
#else
// this works on macOS (not iOS), need to test on tvOS
let environment = ProcessInfo.processInfo.environment
return environment["APP_SANDBOX_CONTAINER_ID"] != nil
#endif
}
}
21 changes: 21 additions & 0 deletions Tests/AmplitudeTests/Storages/PersistentStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ final class PersistentStorageTests: XCTestCase {
XCTAssertNotEqual(eventFiles?[0].pathExtension, PersistentStorage.TEMP_FILE_EXTENSION)
persistentStorage.reset()
}

func testStorageDirectorySandboxed() {
let persistentStorage = PersistentStorage(storagePrefix: "sandbox-instance")

// e.g. /Library/Developer/CoreSimulator/Devices/AA0CFF70-35A4-4D85-AB9A-C27A8DBF94D7/data/Library/Application%20Support/amplitude/amplitude-swift-sandbox-instance.events.inde
// /Library/Developer/CoreSimulator/Devices/AA0CFF70-35A4-4D85-AB9A-C27A8DBF94D7/data/Containers/Data/Application/06213CC5-0BE3-4822-BF6A-44C711467CB7/Library/Application%20Support/amplitude/amplitude-swift-identify-default_instance.events.index
let iOSSandboxPathRegex = "CoreSimulator/Devices/[A-Z0-9-]*/data/"
let bundleId = Bundle.main.bundleIdentifier!

let storageUrl = persistentStorage.getEventsStorageDirectory(createDirectory: false)

// print("bundleId=\(bundleId)")
// print("storageUrl=\(storageUrl)")

#if os(iOS)
XCTAssertNotNil(storageUrl.absoluteString.range(of: iOSSandboxPathRegex, options: .regularExpression, range: nil, locale: nil))
#else
XCTAssertEqual(storageUrl.absoluteString.contains(bundleId), true)
#endif
persistentStorage.reset()
}
}
24 changes: 24 additions & 0 deletions Tests/AmplitudeTests/Utilities/SandboxHelperTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// SandboxHelperTests.swift
// Amplitude-SwiftTests
//
// Created by Justin Fiedler on 2/1/24.
//

import XCTest

@testable import AmplitudeSwift

final class SandboxHelperTests: XCTestCase {

func testIsSandboxEnabled() {
let isSandboxed = SandboxHelper.isSandboxEnabled()

#if os(iOS)
XCTAssertEqual(isSandboxed, true)
#else
XCTAssertEqual(isSandboxed, false)
#endif
}

}

0 comments on commit 3b277fd

Please sign in to comment.