Skip to content

Commit

Permalink
Ignore rules without a supported action type (#16)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1200194497630846/1203790344085047/f

Description:
This is just a change on the parser itself to ignore rules with action types we don't support. Besides that, there should be no changes in behavior.
  • Loading branch information
Bunn authored Feb 14, 2023
1 parent b87f31f commit 4684440
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Sources/TrackerRadarKit/TrackerData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ public struct KnownTracker: Codable, Equatable {
self.rules = rules
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
domain = try? container.decode(String.self, forKey: .domain)
defaultAction = try? container.decode(KnownTracker.ActionType.self, forKey: .defaultAction)
owner = try? container.decode(KnownTracker.Owner.self, forKey: .owner)
prevalence = try? container.decode(Double.self, forKey: .prevalence)
subdomains = try? container.decode([String]?.self, forKey: .subdomains)
categories = try? container.decode([String].self, forKey: .categories)

let customRules = try? container.decode([OptionalValue<KnownTracker.Rule>].self, forKey: .rules)
rules = customRules?.compactMap { $0.value } as? [KnownTracker.Rule]
}
}

extension KnownTracker {
Expand Down Expand Up @@ -205,3 +217,16 @@ public struct Entity: Codable, Hashable {
}

}

private struct OptionalValue<Value: Decodable>: Decodable {
public let value: Value?

public init(from decoder: Decoder) throws {
do {
let container = try decoder.singleValueContainer()
self.value = try container.decode(Value.self)
} catch {
self.value = nil
}
}
}
23 changes: 23 additions & 0 deletions Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ class ContentBlockerRulesBuilderTests: XCTestCase {
}
}

func testLoadingUnsupportedRules() throws {
let data = JSONTestDataLoader.mockTrackerData
guard let mockData = try? JSONDecoder().decode(TrackerData.self, from: data) else {
XCTFail("Failed to decode tracker data")
return
}

guard let tracker = mockData.findTracker(byCname: "tracker-4.com") else {
XCTFail("Failed to find tracker")
return
}

let expectedNumberOfRules = 1
XCTAssertEqual(tracker.rules?.count, expectedNumberOfRules)
}

func testTrackerDataParserPerformance () {
let data = JSONTestDataLoader.trackerData
measure {
_ = try? JSONDecoder().decode(TrackerData.self, from: data)
}
}

func testLoadingRulesIsDeterministic() {
let firstGeneration = ContentBlockerRulesBuilder(trackerData: trackerData).buildRules(
withExceptions: [],
Expand Down
35 changes: 34 additions & 1 deletion Tests/TrackerRadarKitTests/Resources/mockTrackerData.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@
"cookies": 0.01
}
]
},
"tracker-4.com": {
"domain": "tracker-4.com",
"owner": {
"name": "Test Site for Tracker Blocking",
"displayName": "Bad Third Party Site",
"privacyPolicy": "",
"url": "http://tracker.test"
},
"prevalence": 0.1,
"fingerprinting": 3,
"cookies": 0.1,
"categories": [],
"default": "block",
"rules": [
{
"action": "ignore",
"rule": "tracker-4\\.com\\/breakage"
},
{
"action": "unsupported-action",
"rule": "tracker-4\\.com\\/unsupported-action"
}
]
}
},
"entities": {
Expand All @@ -109,16 +133,25 @@
],
"prevalence": 1,
"displayName": "Tracker 3"
},
"Tracker 4, Inc.": {
"domains": [
"tracker-4.com"
],
"prevalence": 1,
"displayName": "Tracker 4"
}
},
"domains": {
"tracker-1.com": "Tracker 1",
"tracker-2.com": "Tracker 2",
"tracker-3.com": "Tracker 3"
"tracker-3.com": "Tracker 3",
"tracker-4.com": "Tracker 4"
},
"cnames": {
"tracker-1.com": "cname.tracker-1.com",
"tracker-2.com": "cname.tracker-2.com",
"tracker-3.com": "cname.tracker-3.com",
"tracker-4.com": "cname.tracker-4.com"
}
}

0 comments on commit 4684440

Please sign in to comment.