summaryrefslogtreecommitdiff
path: root/DomainDigTests/Fixtures/SnapshotFixture.swift
blob: 4129a306181728a703ccc4bbd45cc00467f859b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import Foundation
@testable import DomainDig

/// Deterministic builders for the deep `LookupSnapshot` / `DomainReport` models
/// so the core unit tests can construct inputs without wiring up every one of the
/// ~75 snapshot fields at each call site.
///
/// `snapshot(...)` exposes only the fields the tests actually vary; everything
/// else defaults to an empty/absent value. `report(...)` runs a snapshot through
/// the real `DomainReportBuilder`, which is both the natural constructor for the
/// otherwise-unconstructable `DomainReport` and, for the builder tests, the unit
/// under test.
enum SnapshotFixture {
    /// Fixed instant so timestamp-derived output (diff ranges, export headers) is
    /// stable across runs.
    static let referenceDate = Date(timeIntervalSince1970: 1_700_000_000)

    static let defaultResolverURL = "https://resolver.example/dns-query"

    static func snapshot(
        domain: String = "example.com",
        timestamp: Date = referenceDate,
        resolverURLString: String = defaultResolverURL,
        resultSource: LookupResultSource = .live,
        availability: DomainAvailabilityStatus? = nil,
        ownership: DomainOwnership? = nil,
        dnsSections: [DNSSection] = [],
        ptrRecord: String? = nil,
        sslInfo: SSLCertificateInfo? = nil,
        httpHeaders: [HTTPHeader] = [],
        httpStatusCode: Int? = nil,
        httpSecurityGrade: String? = nil,
        subdomains: [DiscoveredSubdomain] = [],
        extendedSubdomains: [DiscoveredSubdomain] = [],
        isPartialSnapshot: Bool = false,
        validationIssues: [String] = [],
        changeSummary: DomainChangeSummary? = nil
    ) -> LookupSnapshot {
        LookupSnapshot(
            historyEntryID: nil,
            domain: domain,
            timestamp: timestamp,
            trackedDomainID: nil,
            note: nil,
            appVersion: "test",
            resolverDisplayName: "Test Resolver",
            resolverURLString: resolverURLString,
            dataSources: [],
            provenanceBySection: [:],
            availabilityConfidence: nil,
            ownershipConfidence: nil,
            subdomainConfidence: nil,
            emailSecurityConfidence: nil,
            geolocationConfidence: nil,
            errorDetails: [:],
            isPartialSnapshot: isPartialSnapshot,
            validationIssues: validationIssues,
            totalLookupDurationMs: nil,
            snapshotIndex: nil,
            previousSnapshotID: nil,
            changeCount: 0,
            severitySummary: nil,
            dnsSections: dnsSections,
            dnsError: nil,
            availabilityResult: availability.map { DomainAvailabilityResult(domain: domain, status: $0) },
            suggestions: [],
            sslInfo: sslInfo,
            sslError: nil,
            hstsPreloaded: nil,
            httpHeaders: httpHeaders,
            httpSecurityGrade: httpSecurityGrade,
            httpStatusCode: httpStatusCode,
            httpResponseTimeMs: nil,
            httpProtocol: nil,
            http3Advertised: false,
            httpHeadersError: nil,
            reachabilityResults: [],
            reachabilityError: nil,
            ipGeolocation: nil,
            ipGeolocationError: nil,
            emailSecurity: nil,
            emailSecurityError: nil,
            ownership: ownership,
            ownershipError: nil,
            ownershipHistory: [],
            ownershipHistoryError: nil,
            inferredProvider: nil,
            priorProviders: [],
            domainClassification: nil,
            ownershipTransitions: [],
            hostingTransitions: [],
            subdomainHistory: [],
            riskSignals: [],
            intelligenceTimeline: [],
            ptrRecord: ptrRecord,
            ptrError: nil,
            redirectChain: [],
            redirectChainError: nil,
            subdomains: subdomains,
            subdomainsError: nil,
            extendedSubdomains: extendedSubdomains,
            extendedSubdomainsError: nil,
            dnsHistory: [],
            dnsHistoryError: nil,
            domainPricing: nil,
            domainPricingError: nil,
            reputation: nil,
            reputationError: nil,
            portScanResults: [],
            portScanError: nil,
            changeSummary: changeSummary,
            resultSource: resultSource,
            cachedSections: [],
            statusMessage: nil
        )
    }

    static func report(
        domain: String = "example.com",
        timestamp: Date = referenceDate,
        resolverURLString: String = defaultResolverURL,
        resultSource: LookupResultSource = .live,
        availability: DomainAvailabilityStatus? = nil,
        ownership: DomainOwnership? = nil,
        dnsSections: [DNSSection] = [],
        ptrRecord: String? = nil,
        sslInfo: SSLCertificateInfo? = nil,
        httpHeaders: [HTTPHeader] = [],
        httpStatusCode: Int? = nil,
        httpSecurityGrade: String? = nil,
        subdomains: [DiscoveredSubdomain] = [],
        extendedSubdomains: [DiscoveredSubdomain] = [],
        isPartialSnapshot: Bool = false,
        validationIssues: [String] = []
    ) -> DomainReport {
        DomainReportBuilder().build(
            from: snapshot(
                domain: domain,
                timestamp: timestamp,
                resolverURLString: resolverURLString,
                resultSource: resultSource,
                availability: availability,
                ownership: ownership,
                dnsSections: dnsSections,
                ptrRecord: ptrRecord,
                sslInfo: sslInfo,
                httpHeaders: httpHeaders,
                httpStatusCode: httpStatusCode,
                httpSecurityGrade: httpSecurityGrade,
                subdomains: subdomains,
                extendedSubdomains: extendedSubdomains,
                isPartialSnapshot: isPartialSnapshot,
                validationIssues: validationIssues
            ),
            deriveChangeSummary: false
        )
    }

    // MARK: - Nested model conveniences

    static func dnsSection(
        type: DNSRecordType,
        values: [String],
        ttl: Int = 300,
        dnssecSigned: Bool? = nil,
        wildcards: [String] = []
    ) -> DNSSection {
        DNSSection(
            recordType: type,
            records: values.map { DNSRecord(value: $0, ttl: ttl) },
            wildcardRecords: wildcards.map { DNSRecord(value: $0, ttl: ttl) },
            dnssecSigned: dnssecSigned
        )
    }

    static func certificate(
        commonName: String = "example.com",
        issuer: String = "Test CA",
        daysUntilExpiry: Int = 90
    ) -> SSLCertificateInfo {
        SSLCertificateInfo(
            commonName: commonName,
            subjectAltNames: [commonName],
            issuer: issuer,
            validFrom: referenceDate,
            validUntil: referenceDate.addingTimeInterval(Double(daysUntilExpiry) * 86_400),
            daysUntilExpiry: daysUntilExpiry,
            chainDepth: 1
        )
    }
}