vastlint-core 0.4.21

VAST XML validator, SIMID linter, and VPAID detector — IAB VAST 2.0–4.3, 121 rules, zero runtime dependencies
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use vastlint_core::{fix, fix_with_context, validate, Severity, ValidationContext};

// ── helpers ──────────────────────────────────────────────────────────────────

fn has_issue(result: &vastlint_core::ValidationResult, id: &str) -> bool {
    result.issues.iter().any(|i| i.id == id)
}

/// Assert the fix cycle for a single rule:
///
/// 1. `validate(input)` fires `rule_id`.
/// 2. `fix(input)` applies at least one fix with `rule_id`.
/// 3. `validate(repaired_xml)` does NOT fire `rule_id`.
/// 4. No new Error-severity issues were introduced by the fix.
fn assert_fix_cycle(input: &str, rule_id: &str) {
    // Step 1: confirm the rule fires before fixing.
    let before = validate(input);
    assert!(
        has_issue(&before, rule_id),
        "expected rule '{rule_id}' to fire before fix, issues: {:#?}",
        before.issues
    );

    // Step 2: fix and confirm the rule was applied.
    let result = fix(input);
    assert!(
        result.applied.iter().any(|f| f.rule_id == rule_id),
        "expected fix for rule '{rule_id}' to be applied, applied: {:#?}",
        result.applied
    );

    // Step 3: re-validate and confirm the rule is gone.
    let after = validate(&result.xml);
    assert!(
        !has_issue(&after, rule_id),
        "expected rule '{rule_id}' to be gone after fix, remaining issues: {:#?}",
        after.issues
    );

    // Step 4: no new errors introduced.
    let new_errors: Vec<_> = after
        .issues
        .iter()
        .filter(|i| i.severity == Severity::Error && !has_issue(&before, i.id))
        .collect();
    assert!(
        new_errors.is_empty(),
        "fix introduced new Error-severity issues: {new_errors:#?}"
    );
}

// ── VAST-2.0-mediafile-https ──────────────────────────────────────────────────

const HTTP_MEDIAFILE: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              http://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_mediafile_http_fires_before_fix() {
    let before = validate(HTTP_MEDIAFILE);
    assert!(has_issue(&before, "VAST-2.0-mediafile-https"));
}

#[test]
fn fix_mediafile_http_is_applied() {
    let result = fix(HTTP_MEDIAFILE);
    assert!(result
        .applied
        .iter()
        .any(|f| f.rule_id == "VAST-2.0-mediafile-https"));
}

#[test]
fn fix_mediafile_http_url_is_rewritten() {
    let result = fix(HTTP_MEDIAFILE);
    assert!(result.xml.contains("https://cdn.example.com/ad.mp4"));
    assert!(!result.xml.contains("http://cdn.example.com/ad.mp4"));
}

#[test]
fn fix_mediafile_http_does_not_fire_after_fix() {
    assert_fix_cycle(HTTP_MEDIAFILE, "VAST-2.0-mediafile-https");
}

// ── VAST-2.0-tracking-https (Impression) ─────────────────────────────────────

const HTTP_IMPRESSION: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>http://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_impression_http_full_cycle() {
    assert_fix_cycle(HTTP_IMPRESSION, "VAST-2.0-tracking-https");
}

#[test]
fn fix_impression_http_url_is_rewritten() {
    let result = fix(HTTP_IMPRESSION);
    assert!(result.xml.contains("https://t.example.com/imp"));
    assert!(!result.xml.contains("http://t.example.com/imp"));
}

// ── VAST-2.0-tracking-https (Tracking pixels) ────────────────────────────────

const HTTP_TRACKING: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <TrackingEvents>
            <Tracking event="start">http://track.example.com/start</Tracking>
            <Tracking event="complete">http://track.example.com/complete</Tracking>
          </TrackingEvents>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_tracking_http_full_cycle() {
    assert_fix_cycle(HTTP_TRACKING, "VAST-2.0-tracking-https");
}

#[test]
fn fix_tracking_upgrades_all_tracking_urls() {
    let result = fix(HTTP_TRACKING);
    assert!(result.xml.contains("https://track.example.com/start"));
    assert!(result.xml.contains("https://track.example.com/complete"));
    assert!(!result.xml.contains("http://track.example.com/"));
    // One applied fix for the tracking-https rule (covers all URLs in one pass).
    let tracking_fixes: Vec<_> = result
        .applied
        .iter()
        .filter(|f| f.rule_id == "VAST-2.0-tracking-https")
        .collect();
    assert_eq!(
        tracking_fixes.len(),
        1,
        "expected 1 tracking-https fix entry, got {tracking_fixes:#?}"
    );
}

// ── VAST-2.0-tracking-https (ClickThrough) ───────────────────────────────────

const HTTP_CLICKTHROUGH: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <VideoClicks>
            <ClickThrough>http://click.example.com/land</ClickThrough>
            <ClickTracking>http://click.example.com/track</ClickTracking>
          </VideoClicks>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_clickthrough_http_full_cycle() {
    assert_fix_cycle(HTTP_CLICKTHROUGH, "VAST-2.0-tracking-https");
}

#[test]
fn fix_clickthrough_upgrades_click_urls() {
    let result = fix(HTTP_CLICKTHROUGH);
    assert!(result.xml.contains("https://click.example.com/land"));
    assert!(result.xml.contains("https://click.example.com/track"));
    assert!(!result.xml.contains("http://click.example.com/"));
}

// ── VAST-2.0-tracking-https (Error element) ──────────────────────────────────

const HTTP_ERROR_ELEMENT: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Error>http://error.example.com/[ERRORCODE]</Error>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_error_element_http_full_cycle() {
    assert_fix_cycle(HTTP_ERROR_ELEMENT, "VAST-2.0-tracking-https");
}

#[test]
fn fix_error_element_upgrades_error_url() {
    let result = fix(HTTP_ERROR_ELEMENT);
    assert!(result.xml.contains("https://error.example.com/[ERRORCODE]"));
    assert!(!result.xml.contains("http://error.example.com/"));
}

// ── VAST-4.0-conditionalad ────────────────────────────────────────────────────

const CONDITIONAL_AD: &str = r#"<VAST version="4.1">
  <Ad id="1" conditionalAd="true"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_conditional_ad_full_cycle() {
    assert_fix_cycle(CONDITIONAL_AD, "VAST-4.0-conditionalad");
}

#[test]
fn fix_conditional_ad_attribute_is_removed() {
    let result = fix(CONDITIONAL_AD);
    assert!(!result.xml.contains("conditionalAd"));
}

#[test]
fn fix_conditional_ad_other_attributes_preserved() {
    let result = fix(CONDITIONAL_AD);
    // The id="1" attribute on <Ad> must survive.
    assert!(result.xml.contains("id=\"1\""));
}

// ── Multiple rules fixed in one pass ─────────────────────────────────────────

const MULTIPLE_ISSUES: &str = r#"<VAST version="4.1">
  <Ad id="1" conditionalAd="true"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>http://t.example.com/imp</Impression>
    <Error>http://error.example.com/[ERRORCODE]</Error>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <TrackingEvents>
            <Tracking event="start">http://track.example.com/start</Tracking>
          </TrackingEvents>
          <VideoClicks>
            <ClickThrough>http://click.example.com/land</ClickThrough>
          </VideoClicks>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              http://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_multiple_issues_all_rules_applied() {
    let before = validate(MULTIPLE_ISSUES);
    assert!(has_issue(&before, "VAST-2.0-mediafile-https"));
    assert!(has_issue(&before, "VAST-2.0-tracking-https"));
    assert!(has_issue(&before, "VAST-4.0-conditionalad"));

    let result = fix(MULTIPLE_ISSUES);

    assert!(result
        .applied
        .iter()
        .any(|f| f.rule_id == "VAST-2.0-mediafile-https"));
    assert!(result
        .applied
        .iter()
        .any(|f| f.rule_id == "VAST-2.0-tracking-https"));
    assert!(result
        .applied
        .iter()
        .any(|f| f.rule_id == "VAST-4.0-conditionalad"));

    let after = validate(&result.xml);
    assert!(!has_issue(&after, "VAST-2.0-mediafile-https"));
    assert!(!has_issue(&after, "VAST-2.0-tracking-https"));
    assert!(!has_issue(&after, "VAST-4.0-conditionalad"));
}

#[test]
fn fix_multiple_issues_applied_count_is_correct() {
    let result = fix(MULTIPLE_ISSUES);
    // One entry per rule ID: mediafile-https, tracking-https, conditionalad = 3
    assert_eq!(
        result.applied.len(),
        3,
        "expected 3 applied fixes (one per rule), got: {:#?}",
        result.applied
    );
}

#[test]
fn fix_multiple_issues_repaired_xml_is_clean_of_http() {
    let result = fix(MULTIPLE_ISSUES);
    // No http:// should remain in URL-bearing elements.
    // (The VAST version attribute value "4.1" is not a URL, so "http" there is fine.
    // We check for the scheme+colon+slashes pattern.)
    assert!(
        !result.xml.contains("http://"),
        "repaired XML still contains http:// URLs:\n{}",
        result.xml
    );
}

// ── Idempotency: fixing already-clean XML changes nothing ────────────────────

const CLEAN_VAST: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <AdTitle>Test</AdTitle>
    <AdServingId>sid-1</AdServingId>
    <Impression>https://t.example.com/imp</Impression>
    <Error>https://error.example.com/[ERRORCODE]</Error>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <TrackingEvents>
            <Tracking event="start">https://track.example.com/start</Tracking>
          </TrackingEvents>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_clean_vast_applies_nothing() {
    let result = fix(CLEAN_VAST);
    assert!(
        result.applied.is_empty(),
        "expected no fixes on clean document, got: {:#?}",
        result.applied
    );
}

#[test]
fn fix_clean_vast_remaining_matches_plain_validate() {
    // remaining after fix should equal issues from plain validate (nothing changed).
    let result = fix(CLEAN_VAST);
    let direct = validate(CLEAN_VAST);
    assert_eq!(
        result.remaining.len(),
        direct.issues.len(),
        "remaining count mismatch: fix={} validate={}",
        result.remaining.len(),
        direct.issues.len()
    );
}

// ── Idempotency: fixing twice gives the same result ──────────────────────────

#[test]
fn fix_is_idempotent_on_http_mediafile() {
    let first = fix(HTTP_MEDIAFILE);
    let second = fix(&first.xml);
    assert!(
        second.applied.is_empty(),
        "second fix pass applied changes that should have been done by first pass: {:#?}",
        second.applied
    );
}

#[test]
fn fix_is_idempotent_on_multiple_issues() {
    let first = fix(MULTIPLE_ISSUES);
    let second = fix(&first.xml);
    assert!(
        second.applied.is_empty(),
        "second fix pass applied changes on an already-repaired document: {:#?}",
        second.applied
    );
}

// ── Repaired XML round-trips cleanly through the parser ──────────────────────

#[test]
fn fix_output_parses_without_error_http_mediafile() {
    let result = fix(HTTP_MEDIAFILE);
    let doc = vastlint_core::_test_parse(&result.xml);
    assert!(
        doc.parse_error.is_none(),
        "parse error: {:?}",
        doc.parse_error
    );
}

#[test]
fn fix_output_parses_without_error_multiple_issues() {
    let result = fix(MULTIPLE_ISSUES);
    let doc = vastlint_core::_test_parse(&result.xml);
    assert!(
        doc.parse_error.is_none(),
        "parse error: {:?}",
        doc.parse_error
    );
}

// ── rule_overrides respected by fix_with_context ─────────────────────────────

#[test]
fn fix_with_context_respects_rule_off() {
    use std::collections::HashMap;
    use vastlint_core::RuleLevel;

    let mut overrides = HashMap::new();
    overrides.insert("VAST-2.0-mediafile-https", RuleLevel::Off);

    let ctx = ValidationContext {
        rule_overrides: Some(overrides),
        ..Default::default()
    };

    let result = fix_with_context(HTTP_MEDIAFILE, ctx);
    // The fix for mediafile-https should still be applied — fix passes run
    // unconditionally; rule_overrides only affect what appears in `remaining`.
    assert!(result
        .applied
        .iter()
        .any(|f| f.rule_id == "VAST-2.0-mediafile-https"));
    // mediafile-https should not appear in remaining because it's turned Off.
    assert!(!result
        .remaining
        .iter()
        .any(|i| i.id == "VAST-2.0-mediafile-https"));
}

// ── Unfixable issues land in remaining, not applied ──────────────────────────

const MISSING_REQUIRED: &str = r#"<VAST version="4.2">
  <Ad id="1"><InLine>
    <AdSystem>Demo</AdSystem>
    <Impression>https://t.example.com/imp</Impression>
    <Creatives>
      <Creative>
        <UniversalAdId idRegistry="ad-id.org">UID-1</UniversalAdId>
        <Linear>
          <Duration>00:00:30</Duration>
          <MediaFiles>
            <MediaFile delivery="progressive" type="video/mp4" width="1920" height="1080">
              https://cdn.example.com/ad.mp4
            </MediaFile>
          </MediaFiles>
        </Linear>
      </Creative>
    </Creatives>
  </InLine></Ad>
</VAST>"#;

#[test]
fn fix_does_not_claim_to_fix_missing_required_fields() {
    // Missing AdTitle and AdServingId — these are structural and cannot be auto-fixed.
    let before = validate(MISSING_REQUIRED);
    assert!(has_issue(&before, "VAST-2.0-inline-adtitle"));

    let result = fix(MISSING_REQUIRED);
    // Must NOT appear in applied.
    assert!(
        !result
            .applied
            .iter()
            .any(|f| f.rule_id == "VAST-2.0-inline-adtitle"),
        "fix should not claim to fix missing AdTitle"
    );
    // Must appear in remaining.
    assert!(
        result
            .remaining
            .iter()
            .any(|i| i.id == "VAST-2.0-inline-adtitle"),
        "missing AdTitle should appear in remaining"
    );
}

#[test]
fn fix_remaining_contains_all_unfixable_errors() {
    let before = validate(MISSING_REQUIRED);
    let result = fix(MISSING_REQUIRED);
    // Every error in `before` that isn't a fixable rule should appear in remaining.
    for issue in &before.issues {
        if issue.severity == Severity::Error {
            assert!(
                result.remaining.iter().any(|r| r.id == issue.id),
                "unfixable error '{}' missing from remaining",
                issue.id
            );
        }
    }
}

// ── Wrapper VAST: VASTAdTagURI HTTP upgrade ───────────────────────────────────

const HTTP_WRAPPER: &str = r#"<VAST version="4.2">
  <Ad id="1"><Wrapper>
    <AdSystem>Demo</AdSystem>
    <VASTAdTagURI>http://ads.example.com/vast.xml</VASTAdTagURI>
    <Impression>https://t.example.com/imp</Impression>
  </Wrapper></Ad>
</VAST>"#;

#[test]
fn fix_wrapper_vasttag_uri_http_full_cycle() {
    assert_fix_cycle(HTTP_WRAPPER, "VAST-2.0-tracking-https");
}

#[test]
fn fix_wrapper_vasttag_uri_is_rewritten() {
    let result = fix(HTTP_WRAPPER);
    assert!(result.xml.contains("https://ads.example.com/vast.xml"));
    assert!(!result.xml.contains("http://ads.example.com/vast.xml"));
}