icookforms 0.1.0

The World's Reference Cookie Audit Software - Complete Security & Compliance Analysis
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Google Ad Tech Provider (ATP) List Management
//!
//! This module manages the official Google Ad Tech Provider (ATP) List, which contains
//! information about all Google advertising technology providers that can be referenced
//! in Additional Consent (AC) Strings.
//!
//! # Overview
//!
//! The Google ATP List is published at:
//! `https://storage.googleapis.com/gac/google-atp-list.json`
//!
//! This list contains:
//! - ATP unique IDs (numeric identifiers)
//! - ATP names (company/product names)
//! - ATP domains (associated web domains)
//!
//! # Usage
//!
//! ```no_run
//! # use icookforms::compliance::google_consent::GoogleATPList;
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Fetch the latest ATP list from Google
//! let atp_list = GoogleATPList::fetch().await?;
//!
//! // Get a provider by ID
//! if let Some(provider) = atp_list.get_provider(35) {
//!     println!("Provider: {}", provider.name);
//! }
//!
//! // Create an index for fast lookups
//! let index = atp_list.create_index();
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Official Google Ad Tech Provider List
///
/// Contains all registered Google Ad Tech Providers with their IDs, names, and domains.
/// This list is used to validate Additional Consent (AC) String ATP IDs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GoogleATPList {
    /// List of all registered Ad Tech Providers
    pub providers: Vec<AdTechProvider>,
}

/// Google Ad Tech Provider information
///
/// Represents a single Ad Tech Provider registered with Google.
///
/// # Fields
/// - `id`: Unique numeric identifier for the ATP
/// - `name`: Company or product name
/// - `domains`: List of associated web domains
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AdTechProvider {
    /// Unique ATP identifier
    pub id: u16,

    /// Provider name (e.g., `Google Advertising Products`)
    pub name: String,

    /// Associated domains (e.g., `["doubleclick.net", "google.com"]`)
    pub domains: Vec<String>,
}

impl GoogleATPList {
    /// Creates a new empty ATP list
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::GoogleATPList;
    /// let list = GoogleATPList::new();
    /// assert!(list.providers.is_empty());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            providers: Vec::new(),
        }
    }

    /// Creates an ATP list with provided providers
    ///
    /// # Arguments
    /// * `providers` - Vector of `AdTechProvider` instances
    #[must_use]
    pub fn with_providers(providers: Vec<AdTechProvider>) -> Self {
        Self { providers }
    }

    /// Fetches the latest ATP list from Google's official source
    ///
    /// # Returns
    /// * `Ok(GoogleATPList)` if the fetch succeeds
    /// * `Err(ATPError)` if the fetch fails or the JSON is invalid
    ///
    /// # Examples
    /// ```no_run
    /// # use icookforms::compliance::google_consent::GoogleATPList;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let atp_list = GoogleATPList::fetch().await?;
    /// println!("Fetched {} providers", atp_list.providers.len());
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "scanner")]
    pub async fn fetch() -> Result<Self, ATPError> {
        use reqwest;

        const ATP_LIST_URL: &str = "https://storage.googleapis.com/gac/google-atp-list.json";

        let response = reqwest::get(ATP_LIST_URL)
            .await
            .map_err(|e| ATPError::FetchError(e.to_string()))?;

        if !response.status().is_success() {
            return Err(ATPError::HttpError(response.status().as_u16()));
        }

        let list: GoogleATPList = response
            .json()
            .await
            .map_err(|e| ATPError::ParseError(e.to_string()))?;

        Ok(list)
    }

    /// Fetches ATP list with a custom timeout
    ///
    /// # Arguments
    /// * `timeout_secs` - Request timeout in seconds
    #[cfg(feature = "scanner")]
    pub async fn fetch_with_timeout(timeout_secs: u64) -> Result<Self, ATPError> {
        use reqwest;
        use std::time::Duration;

        const ATP_LIST_URL: &str = "https://storage.googleapis.com/gac/google-atp-list.json";

        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(timeout_secs))
            .build()
            .map_err(|e| ATPError::FetchError(e.to_string()))?;

        let response = client
            .get(ATP_LIST_URL)
            .send()
            .await
            .map_err(|e| ATPError::FetchError(e.to_string()))?;

        if !response.status().is_success() {
            return Err(ATPError::HttpError(response.status().as_u16()));
        }

        let list: GoogleATPList = response
            .json()
            .await
            .map_err(|e| ATPError::ParseError(e.to_string()))?;

        Ok(list)
    }

    /// Loads ATP list from a JSON string
    ///
    /// Useful for testing or loading cached lists.
    ///
    /// # Arguments
    /// * `json` - JSON string containing the ATP list
    ///
    /// # Returns
    /// * `Ok(GoogleATPList)` if parsing succeeds
    /// * `Err(ATPError)` if the JSON is invalid
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::GoogleATPList;
    /// let json = r#"{"providers": [{"id": 1, "name": "Test", "domains": ["test.com"]}]}"#;
    /// let list = GoogleATPList::from_json(json).unwrap();
    /// assert_eq!(list.providers.len(), 1);
    /// ```
    #[must_use = "JSON parsing failure must be handled"]
    pub fn from_json(json: &str) -> Result<Self, ATPError> {
        serde_json::from_str(json).map_err(|e| ATPError::ParseError(e.to_string()))
    }

    /// Gets a provider by its ID
    ///
    /// # Arguments
    /// * `id` - The ATP ID to search for
    ///
    /// # Returns
    /// * `Some(&AdTechProvider)` if found
    /// * `None` if not found
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::{GoogleATPList, AdTechProvider};
    /// let providers = vec![
    ///     AdTechProvider { id: 1, name: "Test".to_string(), domains: vec![] },
    /// ];
    /// let list = GoogleATPList::with_providers(providers);
    /// assert!(list.get_provider(1).is_some());
    /// assert!(list.get_provider(999).is_none());
    /// ```
    #[must_use]
    pub fn get_provider(&self, id: u16) -> Option<&AdTechProvider> {
        self.providers.iter().find(|p| p.id == id)
    }

    /// Finds a provider by domain
    ///
    /// # Arguments
    /// * `domain` - Domain to search for (e.g., "doubleclick.net")
    ///
    /// # Returns
    /// Vector of providers that include this domain
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::{GoogleATPList, AdTechProvider};
    /// let providers = vec![
    ///     AdTechProvider {
    ///         id: 1,
    ///         name: "Test".to_string(),
    ///         domains: vec!["test.com".to_string()],
    ///     },
    /// ];
    /// let list = GoogleATPList::with_providers(providers);
    /// let found = list.find_by_domain("test.com");
    /// assert_eq!(found.len(), 1);
    /// ```
    #[must_use]
    pub fn find_by_domain(&self, domain: &str) -> Vec<&AdTechProvider> {
        self.providers
            .iter()
            .filter(|p| p.domains.iter().any(|d| d == domain))
            .collect()
    }

    /// Creates a `HashMap` index for fast ID lookups
    ///
    /// # Returns
    /// `HashMap` mapping ATP IDs to providers
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::{GoogleATPList, AdTechProvider};
    /// let providers = vec![
    ///     AdTechProvider { id: 1, name: "Test".to_string(), domains: vec![] },
    /// ];
    /// let list = GoogleATPList::with_providers(providers);
    /// let index = list.create_index();
    /// assert!(index.contains_key(&1));
    /// ```
    #[must_use]
    pub fn create_index(&self) -> HashMap<u16, &AdTechProvider> {
        self.providers.iter().map(|p| (p.id, p)).collect()
    }

    /// Creates a domain-to-provider index
    ///
    /// # Returns
    /// `HashMap` mapping domains to vectors of providers
    #[must_use]
    pub fn create_domain_index(&self) -> HashMap<String, Vec<&AdTechProvider>> {
        let mut index: HashMap<String, Vec<&AdTechProvider>> = HashMap::new();

        for provider in &self.providers {
            for domain in &provider.domains {
                index.entry(domain.clone()).or_default().push(provider);
            }
        }

        index
    }

    /// Returns all ATP IDs in the list
    ///
    /// # Examples
    /// ```
    /// # use icookforms::compliance::google_consent::{GoogleATPList, AdTechProvider};
    /// let providers = vec![
    ///     AdTechProvider { id: 1, name: "Test1".to_string(), domains: vec![] },
    ///     AdTechProvider { id: 35, name: "Test35".to_string(), domains: vec![] },
    /// ];
    /// let list = GoogleATPList::with_providers(providers);
    /// assert_eq!(list.all_ids(), vec![1, 35]);
    /// ```
    #[must_use]
    pub fn all_ids(&self) -> Vec<u16> {
        self.providers.iter().map(|p| p.id).collect()
    }

    /// Returns the number of providers in the list
    #[must_use]
    pub fn len(&self) -> usize {
        self.providers.len()
    }

    /// Checks if the list is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.providers.is_empty()
    }

    /// Validates if an ATP ID exists in the list
    ///
    /// # Arguments
    /// * `id` - ATP ID to validate
    ///
    /// # Returns
    /// `true` if the ID exists, `false` otherwise
    #[must_use]
    pub fn is_valid_id(&self, id: u16) -> bool {
        self.get_provider(id).is_some()
    }
}

impl Default for GoogleATPList {
    fn default() -> Self {
        Self::new()
    }
}

impl AdTechProvider {
    /// Creates a new Ad Tech Provider
    ///
    /// # Arguments
    /// * `id` - Unique ATP identifier
    /// * `name` - Provider name
    /// * `domains` - Associated domains
    #[must_use]
    pub fn new(id: u16, name: String, domains: Vec<String>) -> Self {
        Self { id, name, domains }
    }

    /// Checks if this provider is associated with a specific domain
    #[must_use]
    pub fn has_domain(&self, domain: &str) -> bool {
        self.domains.iter().any(|d| d == domain)
    }
}

/// Error type for ATP list operations
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum ATPError {
    /// Failed to fetch the ATP list from Google
    #[error("Failed to fetch ATP list: {0}")]
    FetchError(String),

    /// HTTP error when fetching the list
    #[error("HTTP error {0} when fetching ATP list")]
    HttpError(u16),

    /// Failed to parse the ATP list JSON
    #[error("Failed to parse ATP list: {0}")]
    ParseError(String),

    /// ATP ID not found in the list
    #[error("ATP ID {0} not found in the list")]
    AtpNotFound(u16),
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // Test Helper Functions
    // ========================================================================

    fn create_test_providers() -> Vec<AdTechProvider> {
        vec![
            AdTechProvider {
                id: 1,
                name: "Google Advertising Products".to_string(),
                domains: vec!["doubleclick.net".to_string(), "google.com".to_string()],
            },
            AdTechProvider {
                id: 35,
                name: "Google Analytics".to_string(),
                domains: vec!["google-analytics.com".to_string()],
            },
            AdTechProvider {
                id: 41,
                name: "YouTube".to_string(),
                domains: vec!["youtube.com".to_string(), "googlevideo.com".to_string()],
            },
            AdTechProvider {
                id: 101,
                name: "DoubleClick".to_string(),
                domains: vec!["doubleclick.net".to_string()],
            },
        ]
    }

    fn create_test_list() -> GoogleATPList {
        GoogleATPList::with_providers(create_test_providers())
    }

    // ========================================================================
    // GoogleATPList Creation Tests
    // ========================================================================

    #[test]
    fn test_new_empty_list() {
        let list = GoogleATPList::new();
        assert!(list.providers.is_empty());
        assert_eq!(list.len(), 0);
        assert!(list.is_empty());
    }

    #[test]
    fn test_with_providers() {
        let providers = create_test_providers();
        let list = GoogleATPList::with_providers(providers);
        assert_eq!(list.len(), 4);
        assert!(!list.is_empty());
    }

    // ========================================================================
    // Provider Lookup Tests
    // ========================================================================

    #[test]
    fn test_get_provider_found() {
        let list = create_test_list();
        let provider = list.get_provider(35).unwrap();
        assert_eq!(provider.id, 35);
        assert_eq!(provider.name, "Google Analytics");
    }

    #[test]
    fn test_get_provider_not_found() {
        let list = create_test_list();
        assert!(list.get_provider(999).is_none());
    }

    #[test]
    fn test_is_valid_id() {
        let list = create_test_list();
        assert!(list.is_valid_id(1));
        assert!(list.is_valid_id(35));
        assert!(list.is_valid_id(41));
        assert!(!list.is_valid_id(999));
    }

    // ========================================================================
    // Domain Lookup Tests
    // ========================================================================

    #[test]
    fn test_find_by_domain_single() {
        let list = create_test_list();
        let found = list.find_by_domain("google-analytics.com");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].id, 35);
    }

    #[test]
    fn test_find_by_domain_multiple() {
        let list = create_test_list();
        let found = list.find_by_domain("doubleclick.net");
        assert_eq!(found.len(), 2); // IDs 1 and 101
        let ids: Vec<u16> = found.iter().map(|p| p.id).collect();
        assert!(ids.contains(&1));
        assert!(ids.contains(&101));
    }

    #[test]
    fn test_find_by_domain_not_found() {
        let list = create_test_list();
        let found = list.find_by_domain("unknown.com");
        assert!(found.is_empty());
    }

    // ========================================================================
    // Index Creation Tests
    // ========================================================================

    #[test]
    fn test_create_index() {
        let list = create_test_list();
        let index = list.create_index();

        assert_eq!(index.len(), 4);
        assert!(index.contains_key(&1));
        assert!(index.contains_key(&35));
        assert!(index.contains_key(&41));
        assert!(index.contains_key(&101));

        let provider = index.get(&35).unwrap();
        assert_eq!(provider.name, "Google Analytics");
    }

    #[test]
    fn test_create_domain_index() {
        let list = create_test_list();
        let index = list.create_domain_index();

        assert!(index.contains_key("doubleclick.net"));
        assert!(index.contains_key("google-analytics.com"));

        let providers = index.get("doubleclick.net").unwrap();
        assert_eq!(providers.len(), 2);
    }

    // ========================================================================
    // Utility Methods Tests
    // ========================================================================

    #[test]
    fn test_all_ids() {
        let list = create_test_list();
        let ids = list.all_ids();
        assert_eq!(ids, vec![1, 35, 41, 101]);
    }

    #[test]
    fn test_len_and_is_empty() {
        let empty_list = GoogleATPList::new();
        assert_eq!(empty_list.len(), 0);
        assert!(empty_list.is_empty());

        let list = create_test_list();
        assert_eq!(list.len(), 4);
        assert!(!list.is_empty());
    }

    // ========================================================================
    // JSON Parsing Tests
    // ========================================================================

    #[test]
    fn test_from_json_valid() {
        let json = r#"{
            "providers": [
                {
                    "id": 1,
                    "name": "Test Provider",
                    "domains": ["test.com"]
                }
            ]
        }"#;

        let list = GoogleATPList::from_json(json).unwrap();
        assert_eq!(list.len(), 1);
        assert_eq!(list.providers[0].id, 1);
        assert_eq!(list.providers[0].name, "Test Provider");
    }

    #[test]
    fn test_from_json_invalid() {
        let json = r#"{"invalid": "json"}"#;
        let result = GoogleATPList::from_json(json);
        assert!(matches!(result, Err(ATPError::ParseError(_))));
    }

    // ========================================================================
    // AdTechProvider Tests
    // ========================================================================

    #[test]
    fn test_provider_new() {
        let provider = AdTechProvider::new(1, "Test".to_string(), vec!["test.com".to_string()]);
        assert_eq!(provider.id, 1);
        assert_eq!(provider.name, "Test");
        assert_eq!(provider.domains.len(), 1);
    }

    #[test]
    fn test_provider_has_domain() {
        let provider = AdTechProvider {
            id: 1,
            name: "Test".to_string(),
            domains: vec!["test.com".to_string(), "example.com".to_string()],
        };

        assert!(provider.has_domain("test.com"));
        assert!(provider.has_domain("example.com"));
        assert!(!provider.has_domain("other.com"));
    }
}