Skip to content

Commit 6c6486f

Browse files
authored
Merge c770a41 into 916759e
2 parents 916759e + c770a41 commit 6c6486f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public final class HeartbeatController {
2222
private let heartbeatsStorageCapacity: Int = 30
2323
/// Current date provider. It is used for testability.
2424
private let dateProvider: () -> Date
25-
// TODO: Maybe share config with HeartbeatsPayload's DateFormatter?
2625
/// Used for standardizing dates for calendar-day comparision.
2726
static let dateStandardizer: (Date) -> (Date) = {
2827
var calendar = Calendar(identifier: .iso8601)

FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatStorage.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ final class HeartbeatStorage: HeartbeatStorageProtocol {
9090

9191
// MARK: - HeartbeatStorageProtocol
9292

93-
// TODO: Document.
93+
/// Synchronously reads from and writes to storage using the given transform block.
94+
/// - Parameter transform: A block to transform the currently stored heartbeats bundle to a new
95+
/// heartbeats bundle value.
9496
func readAndWriteSync(using transform: (HeartbeatsBundle?) -> HeartbeatsBundle?) {
9597
queue.sync {
9698
let oldHeartbeatsBundle = try? load(from: storage)

FirebaseCore/Internal/Sources/HeartbeatLogging/StorageFactory.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ extension FileStorage: StorageFactory {
3232
static func makeStorage(id: String) -> Storage {
3333
let rootDirectory = FileManager.default.applicationSupportDirectory
3434
let heartbeatDirectoryPath = Constants.heartbeatFileStorageDirectoryPath
35-
let heartbeatFilePath = "heartbeats-\(id)"
35+
36+
// Sanitize the `id` so the heartbeat file name does not include a ":".
37+
let sanitizedID = id.replacingOccurrences(of: ":", with: "_")
38+
let heartbeatFilePath = "heartbeats-\(sanitizedID)"
3639

3740
let storageURL = rootDirectory
3841
.appendingPathComponent(heartbeatDirectoryPath, isDirectory: true)

FirebaseCore/Internal/Tests/Integration/HeartbeatLoggingIntegrationTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,30 @@ class HeartbeatLoggingIntegrationTests: XCTestCase {
364364
XCTAssertNotNil(try Data(contentsOf: heartbeatsFileURL), "Data should not be nil.")
365365
#endif
366366
}
367+
368+
#if !os(tvOS)
369+
// Do not run on tvOS because tvOS uses UserDefaults to store heartbeats.
370+
func testControllerCreatesHeartbeatStorageWithSanitizedFileName() throws {
371+
// Given
372+
let appID = "1:123456789000:ios:abcdefghijklmnop"
373+
let sanitizedAppID = appID.replacingOccurrences(of: ":", with: "_")
374+
let controller = HeartbeatController(id: appID)
375+
// When
376+
// - Trigger the controller to write to the file system.
377+
controller.log("dummy_agent")
378+
_ = XCTWaiter.wait(for: [expectation(description: "Wait for async log.")], timeout: 0.1)
379+
// Then
380+
let heartbeatsDirectoryURL = FileManager.default
381+
.applicationSupportDirectory
382+
.appendingPathComponent(
383+
HeartbeatLoggingTestUtils.Constants.heartbeatFileStorageDirectoryPath,
384+
isDirectory: true
385+
)
386+
387+
let directoryContents = try FileManager.default
388+
.contentsOfDirectory(atPath: heartbeatsDirectoryURL.path)
389+
390+
XCTAssertEqual(directoryContents, ["heartbeats-\(sanitizedAppID)"])
391+
}
392+
#endif // !os(tvOS)
367393
}

0 commit comments

Comments
 (0)