-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UserDefaults misbehavior monitoring (#3510)
Task/Issue URL: https://app.asana.com/0/1206226850447395/1208659072736427/f Tech Design URL: https://app.asana.com/0/481882893211075/1208618515043198/f CC: **Description**: Attempt to validate a hypothesis about unreliable/inaccessible UserDefaults data during app launch. **Steps to test this PR**: #### Statistics loader 1. Launch the app 2. Stop and put a breakpoint in `StatisticsLoader.swift:50` 3. Run the app again. On breakpoint run a debugger command: `expr statisticsStore.atb = nil` 4. Continue execution. 5. Verify proper pixel is fired. 6. On assertion go to `StatisticsLoader.load()` frame in the stack and run: `expr atbPresenceFileMarker?.unmark()` or remove the app. This will prevent assertion for next scenario. #### Ad attribution reporter 1. Enable `adAttributionReporting` feature flag. 1. Put a breakpoint in `AdAttributionPixelReporter.swift:60` 3. Run the app. On breakpoint run a debugger command: `expr attributionReportSuccessfulFileMarker?.mark()` 4. Continue execution 5. Verify proper pixel is fired. 6. On assertion go to `AdAttributionPixelReporter.reportAttributionIfNeeded()` frame in the stack and run: `expr attributionReportSuccessfulFileMarker?.unmark()` or remove the app. <!-- Before submitting a PR, please ensure you have tested the combinations you expect the reviewer to test, then delete configurations you *know* do not need explicit testing. Using a simulator where a physical device is unavailable is acceptable. --> **Definition of Done (Internal Only)**: * [ ] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)? --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943)
- Loading branch information
Showing
11 changed files
with
312 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// BoolFileMarker.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
public struct BoolFileMarker { | ||
let fileManager = FileManager.default | ||
private let url: URL | ||
|
||
public var isPresent: Bool { | ||
fileManager.fileExists(atPath: url.path) | ||
} | ||
|
||
public func mark() { | ||
if !isPresent { | ||
fileManager.createFile(atPath: url.path, contents: nil, attributes: [.protectionKey: FileProtectionType.none]) | ||
} | ||
} | ||
|
||
public func unmark() { | ||
if isPresent { | ||
try? fileManager.removeItem(at: url) | ||
} | ||
} | ||
|
||
public init?(name: Name) { | ||
guard let applicationSupportDirectory = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first else { | ||
return nil | ||
} | ||
|
||
self.url = applicationSupportDirectory.appendingPathComponent(name.rawValue) | ||
} | ||
|
||
public struct Name: RawRepresentable { | ||
public let rawValue: String | ||
|
||
public init(rawValue: String) { | ||
self.rawValue = "\(rawValue).marker" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// BoolFileMarkerTests.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import XCTest | ||
@testable import Core | ||
|
||
final class BoolFileMarkerTests: XCTestCase { | ||
|
||
private let marker = BoolFileMarker(name: .init(rawValue: "test"))! | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
|
||
marker.unmark() | ||
} | ||
|
||
private var testFileURL: URL? { | ||
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?.appendingPathComponent("test.marker") | ||
} | ||
|
||
func testMarkCreatesCorrectFile() throws { | ||
|
||
marker.mark() | ||
|
||
let fileURL = try XCTUnwrap(testFileURL) | ||
|
||
let attributes = try FileManager.default.attributesOfItem(atPath: fileURL.path) | ||
XCTAssertNil(attributes[.protectionKey]) | ||
XCTAssertTrue(FileManager.default.fileExists(atPath: fileURL.path)) | ||
XCTAssertEqual(marker.isPresent, true) | ||
} | ||
|
||
func testUnmarkRemovesFile() throws { | ||
marker.mark() | ||
marker.unmark() | ||
|
||
let fileURL = try XCTUnwrap(testFileURL) | ||
|
||
XCTAssertFalse(marker.isPresent) | ||
XCTAssertFalse(FileManager.default.fileExists(atPath: fileURL.path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// StorageInconsistencyMonitor.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol AppActivationInconsistencyMonitoring { | ||
/// See `StorageInconsistencyMonitor` for details | ||
func didBecomeActive(isProtectedDataAvailable: Bool) | ||
} | ||
|
||
public protocol StatisticsStoreInconsistencyMonitoring { | ||
/// See `StorageInconsistencyMonitor` for details | ||
func statisticsDidLoad(hasFileMarker: Bool, hasInstallStatistics: Bool) | ||
} | ||
|
||
public protocol AdAttributionReporterInconsistencyMonitoring { | ||
/// See `StorageInconsistencyMonitor` for details | ||
func addAttributionReporter(hasFileMarker: Bool, hasCompletedFlag: Bool) | ||
} | ||
|
||
/// Takes care of reporting inconsistency in storage availability and/or state. | ||
/// See https://app.asana.com/0/481882893211075/1208618515043198/f for details. | ||
public struct StorageInconsistencyMonitor: AppActivationInconsistencyMonitoring & StatisticsStoreInconsistencyMonitoring & AdAttributionReporterInconsistencyMonitoring { | ||
|
||
public init() { } | ||
|
||
/// Reports a pixel if data is not available while app is active | ||
public func didBecomeActive(isProtectedDataAvailable: Bool) { | ||
if !isProtectedDataAvailable { | ||
Pixel.fire(pixel: .protectedDataUnavailableWhenBecomeActive) | ||
assertionFailure("This is unexpected state, debug if possible") | ||
} | ||
} | ||
|
||
/// Reports a pixel if file marker exists but installStatistics are missing | ||
public func statisticsDidLoad(hasFileMarker: Bool, hasInstallStatistics: Bool) { | ||
if hasFileMarker == true && hasInstallStatistics == false { | ||
Pixel.fire(pixel: .statisticsLoaderATBStateMismatch) | ||
assertionFailure("This is unexpected state, debug if possible") | ||
} | ||
} | ||
|
||
/// Reports a pixel if file marker exists but completion flag is false | ||
public func addAttributionReporter(hasFileMarker: Bool, hasCompletedFlag: Bool) { | ||
if hasFileMarker == true && hasCompletedFlag == false { | ||
Pixel.fire(pixel: .adAttributionReportStateMismatch) | ||
assertionFailure("This is unexpected state, debug if possible") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.