ic_http_certification/tree/
certification_tree.rs

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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use super::{
    certification_tree_entry::HttpCertificationTreeEntry,
    certification_tree_path::CertificationTreePathSegment,
};
use crate::{
    tree::HttpCertificationPathType,
    utils::{more_specific_wildcards_for, PATH_PREFIX_BYTES},
    HttpCertificationError, HttpCertificationPath, HttpCertificationResult,
};
use ic_certification::{labeled, labeled_hash, merge_hash_trees, AsHashTree, HashTree, NestedTree};
use ic_representation_independent_hash::Sha256Digest;
use std::fmt::{Debug, Formatter};

type CertificationTree = NestedTree<CertificationTreePathSegment, Vec<u8>>;

/// A certification tree for generic HTTP requests.
#[derive(Clone)]
pub struct HttpCertificationTree {
    tree: CertificationTree,
}

impl Debug for HttpCertificationTree {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "tree: {:#?}", self.tree)
    }
}

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

impl HttpCertificationTree {
    /// Creates a new empty [HttpCertificationTree] from a given [CertificationTree](ic_certification::NestedTree).
    /// The [default](HttpCertificationTree::default) implementation should be used in most cases.
    pub fn new(tree: CertificationTree) -> Self {
        Self { tree }
    }

    /// Returns the root hash of the tree.
    /// This hash can be used as the canister's certified variable.
    pub fn root_hash(&self) -> Sha256Digest {
        labeled_hash(PATH_PREFIX_BYTES, &self.tree.root_hash())
    }

    /// Inserts a given [HttpCertificationTreeEntry] into the tree.
    /// After performing this operation, the canister's certified variable will need to be updated
    /// with the new [root hash](HttpCertificationTree::root_hash) of the tree.
    pub fn insert(&mut self, entry: &HttpCertificationTreeEntry) {
        let tree_path = entry.to_tree_path();
        self.tree.insert(&tree_path, vec![]);
    }

    /// Deletes a given [HttpCertificationTreeEntry] from the tree.
    /// After performing this operation, the canister's certified variable will need to be updated
    /// with the new [root hash](HttpCertificationTree::root_hash) of the tree.
    pub fn delete(&mut self, entry: &HttpCertificationTreeEntry) {
        let tree_path = entry.to_tree_path();
        self.tree.delete(&tree_path);
    }

    /// Deletes all [HttpCertificationTreeEntry]s that match a given [HttpCertificationPath].
    /// After performing this operation, the canister's certified variable will need to be updated
    /// with the new [root hash](HttpCertificationTree::root_hash) of the tree.
    pub fn delete_by_path(&mut self, path: &HttpCertificationPath) {
        let tree_path = path.to_tree_path();
        self.tree.delete(&tree_path);
    }

    /// Clears the tree of all [HttpCertificationTreeEntry].
    /// After performing this operation, the canister's certified variable will need to be updated
    /// with the new [root hash](HttpCertificationTree::root_hash) of the tree.
    pub fn clear(&mut self) {
        self.tree.clear();
    }

    /// Returns a pruned [HashTree] that will prove the presence of a given [HttpCertificationTreeEntry]
    /// in the full [HttpCertificationTree], without needing to return the full tree.
    ///
    /// `request_url` is required so that the witness can be generated with respect to the request URL.
    pub fn witness(
        &self,
        entry: &HttpCertificationTreeEntry,
        request_url: &str,
    ) -> HttpCertificationResult<HashTree> {
        let witness = match entry.path.get_type() {
            HttpCertificationPathType::Exact(_) => self.tree.witness(&entry.to_tree_path()),

            HttpCertificationPathType::Wildcard(wildcard_path) => {
                let requested_tree_path = HttpCertificationPath::exact(request_url).to_tree_path();
                let responding_tree_path = entry.path.to_tree_path();

                if responding_tree_path.len() > requested_tree_path.len() {
                    return Err(HttpCertificationError::WildcardPathNotValidForRequestPath {
                        wildcard_path: wildcard_path.to_string(),
                        request_path: request_url.to_string(),
                    });
                }

                // For wildcards we start by witnessing the path that was requested so the verifier can be sure
                // that there is no exact match in the tree.
                let witness = self.tree.witness(&requested_tree_path);

                // Then we witness the wildcard path that we will be responding with.
                let witness = merge_hash_trees(witness, self.tree.witness(&responding_tree_path));

                // Then we need to prove that there is not a more specific wildcard in the tree that
                // matches the request URL.
                more_specific_wildcards_for(&requested_tree_path, &responding_tree_path)
                    .iter()
                    .fold(witness, |acc, path| {
                        merge_hash_trees(acc, self.tree.witness(path))
                    })
            }
        };

        Ok(labeled(PATH_PREFIX_BYTES, witness))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        DefaultCelBuilder, DefaultResponseCertification, HttpCertification, HttpRequest,
        HttpResponse, StatusCode, CERTIFICATE_EXPRESSION_HEADER_NAME,
    };
    use assert_matches::assert_matches;
    use ic_certification::SubtreeLookupResult;
    use rstest::*;

    #[rstest]
    fn test_witness() {
        let mut tree = HttpCertificationTree::default();

        let cel_expr = DefaultCelBuilder::full_certification()
            .with_response_certification(DefaultResponseCertification::response_header_exclusions(
                vec![],
            ))
            .build();

        let not_found_request = HttpRequest::get("/assets/js/not-found.js").build();
        let not_found_response = HttpResponse::not_found(
            br#"404 Not Found"#,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();

        let hello_world_request = HttpRequest::get("/assets/js/hello-world.js").build();
        let hello_world_response = HttpResponse::not_found(
            br#"console.log("Hello, World!")"#,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();

        let not_found_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::wildcard("/assets/js"),
            HttpCertification::full(&cel_expr, &not_found_request, &not_found_response, None)
                .unwrap(),
        );
        tree.insert(&not_found_entry);

        let hello_world_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(hello_world_request.url()),
            HttpCertification::full(&cel_expr, &hello_world_request, &hello_world_response, None)
                .unwrap(),
        );
        tree.insert(&hello_world_entry);

        let not_found_witness = tree.witness(&not_found_entry, "/assets/js/0.js").unwrap();

        assert_eq!(
            not_found_witness.lookup_subtree(["http_expr", "<*>"]),
            SubtreeLookupResult::Absent
        );
        assert_eq!(
            not_found_witness.lookup_subtree(["http_expr", "", "<*>"]),
            SubtreeLookupResult::Absent
        );
        assert_eq!(
            not_found_witness.lookup_subtree(["http_expr", "assets", "<*>"]),
            SubtreeLookupResult::Absent
        );
        assert_eq!(
            not_found_witness.lookup_subtree(["http_expr", "assets", "", "<*>"]),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            not_found_witness.lookup_subtree(["http_expr", "assets", "js", "<*>"]),
            SubtreeLookupResult::Found(_)
        );
        assert_eq!(
            not_found_witness.lookup_subtree(["http_expr", "assets", "js", "0.js", "<*>"]),
            SubtreeLookupResult::Absent
        );

        let hello_world_witness = tree
            .witness(&hello_world_entry, "/assets/js/hello-world.js")
            .unwrap();

        assert_matches!(
            hello_world_witness.lookup_subtree([
                "http_expr",
                "assets",
                "js",
                "hello-world.js",
                "<$>"
            ]),
            SubtreeLookupResult::Found(_)
        );
    }

    #[rstest]
    fn test_delete_by_path() {
        let mut http_tree = HttpCertificationTree::default();
        let cel_expr = DefaultCelBuilder::full_certification()
            .with_response_certification(DefaultResponseCertification::response_header_exclusions(
                vec![],
            ))
            .build();
        let req_one_url = "/assets/js/hello.js";
        let req_two_url = "/assets/js/world.js";

        // arrange four paths in the tree,
        // two requests, with two responses each
        //
        // The tree should look like this:
        // -- "assets" -- "js" -- "hello.js"
        //                 |            |-- ${cel_expr_hash}
        //                 |                |-- ${req_one_hash}
        //                 |                    |-- ${res_hash}
        //                 |                    |-- ${alt_res_hash}
        //                 | ---- "world.js"
        //                              |-- ${cel_expr_hash}
        //                                  |-- ${req_two_hash}
        //                                      |-- ${res_hash}
        //                                      |-- ${res_hash}
        //
        // Resulting in the following paths (number labels are referenced in comments below):
        // (1) "assets" -- "js" -- "hello.js" -- ${cel_expr_hash} -- ${req_one_hash} -- ${res_hash}
        // (2) "assets" -- "js" -- "hello.js" -- ${cel_expr_hash} -- ${req_one_hash} -- ${alt_res_hash}
        // (3) "assets" -- "js" -- "world.js" -- ${cel_expr_hash} -- ${req_two_hash} -- ${res_hash}
        // (4) "assets" -- "js" -- "world.js" -- ${cel_expr_hash} -- ${req_two_hash} -- ${alt_res_hash}

        let req_one = HttpRequest::get(req_one_url).build();
        let req_two = HttpRequest::get(req_two_url).build();

        let res = HttpResponse::builder()
            .with_status_code(StatusCode::OK)
            .with_body(br#"console.log("Hello, World!")"#)
            .with_headers(vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )])
            .build();
        let alt_res = HttpResponse::builder()
            .with_status_code(StatusCode::OK)
            .with_body(br#"console.log("Hello, ALT World!")"#)
            .with_headers(vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )])
            .build();

        let req_one_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(req_one.url()),
            HttpCertification::full(&cel_expr, &req_one, &res, None).unwrap(),
        );
        http_tree.insert(&req_one_entry);

        let req_one_alt_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(req_one.url()),
            HttpCertification::full(&cel_expr, &req_one, &alt_res, None).unwrap(),
        );
        http_tree.insert(&req_one_alt_entry);

        let req_two_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(req_two.url()),
            HttpCertification::full(&cel_expr, &req_two, &res, None).unwrap(),
        );
        http_tree.insert(&req_two_entry);

        let req_two_alt_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(req_two.url()),
            HttpCertification::full(&cel_expr, &req_two, &alt_res, None).unwrap(),
        );
        http_tree.insert(&req_two_alt_entry);

        assert_matches!(
            http_tree
                .witness(&req_one_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&req_one_alt_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_alt_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert_matches!(
            http_tree
                .witness(&req_two_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&req_two_alt_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_alt_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_one_url).to_tree_path()));
        assert!(http_tree.tree.contains_path(&req_one_entry.to_tree_path()));
        assert!(http_tree
            .tree
            .contains_path(&req_one_alt_entry.to_tree_path()));

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_two_url).to_tree_path()));
        assert!(http_tree.tree.contains_path(&req_two_entry.to_tree_path()));
        assert!(http_tree
            .tree
            .contains_path(&req_two_alt_entry.to_tree_path()));

        // delete the (1) and (2) paths, all other paths should remain in the tree
        http_tree.delete_by_path(&HttpCertificationPath::exact(req_one.url()));

        assert_matches!(
            http_tree
                .witness(&req_one_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&req_one_alt_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_alt_entry)),
            SubtreeLookupResult::Absent
        );

        assert_matches!(
            http_tree
                .witness(&req_two_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&req_two_alt_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_alt_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(!http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_one_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&req_one_entry.to_tree_path()));
        assert!(!http_tree
            .tree
            .contains_path(&req_one_alt_entry.to_tree_path()));

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_two_url).to_tree_path()));
        assert!(http_tree.tree.contains_path(&req_two_entry.to_tree_path()));
        assert!(http_tree
            .tree
            .contains_path(&req_two_alt_entry.to_tree_path()));

        // delete the (3) and (4) paths, now the tree should be empty
        http_tree.delete_by_path(&HttpCertificationPath::exact(req_two.url()));

        assert_matches!(
            http_tree
                .witness(&req_one_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&req_one_alt_entry, req_one_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_one_alt_entry)),
            SubtreeLookupResult::Absent
        );

        assert_matches!(
            http_tree
                .witness(&req_two_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&req_two_alt_entry, req_two_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&req_two_alt_entry)),
            SubtreeLookupResult::Absent
        );

        assert!(!http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_one_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&req_one_entry.to_tree_path()));
        assert!(!http_tree
            .tree
            .contains_path(&req_one_alt_entry.to_tree_path()));

        assert!(!http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_two_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&req_two_entry.to_tree_path()));
        assert!(!http_tree
            .tree
            .contains_path(&req_two_alt_entry.to_tree_path()));
    }

    #[rstest]
    fn delete_removes_empty_subpaths() {
        let mut http_tree = HttpCertificationTree::default();
        let cel_expr = DefaultCelBuilder::full_certification()
            .with_response_certification(DefaultResponseCertification::response_header_exclusions(
                vec![],
            ))
            .build();
        let req_url = "/assets/js/hello-world.js";

        // arrange four paths in the tree,
        // two requests, with two responses each
        //
        // The tree should look like this:
        // -- "assets" -- "js" -- "hello-world.js"
        //                              |-- ${cel_expr_hash}
        //                                  |-- ${get_request_hash}
        //                                  |   |-- ${response_hash}
        //                                  |   |-- ${alt_response_hash}
        //                                  |-- ${post_request_hash}
        //                                      |-- ${response_hash}
        //                                      |-- ${alt_response_hash}
        //
        // Resulting in the following paths (number labels are referenced in comments below):
        // (1) "assets" -- "js" -- "hello-world.js" -- ${cel_expr_hash} -- ${get_request_hash} -- ${response_hash}
        // (2) "assets" -- "js" -- "hello-world.js" -- ${cel_expr_hash} -- ${get_request_hash} -- ${alt_response_hash}
        // (3) "assets" -- "js" -- "hello-world.js" -- ${cel_expr_hash} -- ${post_request_hash} -- ${response_hash}
        // (4) "assets" -- "js" -- "hello-world.js" -- ${cel_expr_hash} -- ${post_request_hash} -- ${alt_response_hash}

        let get_request = HttpRequest::get(req_url).build();
        let post_request = HttpRequest::post(req_url).build();

        let response = HttpResponse::ok(
            br#"console.log("Hello, World!")"#,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();
        let alt_response = HttpResponse::ok(
            br#"console.log("Hello, ALT World!")"#,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();

        let get_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(get_request.url()),
            HttpCertification::full(&cel_expr, &get_request, &response, None).unwrap(),
        );
        http_tree.insert(&get_entry);

        let post_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(post_request.url()),
            HttpCertification::full(&cel_expr, &post_request, &response, None).unwrap(),
        );
        http_tree.insert(&post_entry);

        let alt_get_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(get_request.url()),
            HttpCertification::full(&cel_expr, &get_request, &alt_response, None).unwrap(),
        );
        http_tree.insert(&alt_get_entry);

        let alt_post_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::exact(post_request.url()),
            HttpCertification::full(&cel_expr, &post_request, &alt_response, None).unwrap(),
        );
        http_tree.insert(&alt_post_entry);

        assert_matches!(
            http_tree
                .witness(&get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&get_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&post_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&alt_get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_get_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&alt_post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_post_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_url).to_tree_path()));
        assert!(http_tree.tree.contains_path(&get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&post_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_post_entry.to_tree_path()));

        // delete the (1) path, all other paths should remain in the tree
        http_tree.delete(&get_entry);

        assert_matches!(
            http_tree
                .witness(&get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&post_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&alt_get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_get_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&alt_post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_post_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&post_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_post_entry.to_tree_path()));

        // delete the (3) path, now only (2) and (4) should remain in the tree
        http_tree.delete(&post_entry);

        assert_matches!(
            http_tree
                .witness(&get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&post_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&alt_get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_get_entry)),
            SubtreeLookupResult::Found(_)
        );
        assert_matches!(
            http_tree
                .witness(&alt_post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_post_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&get_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&post_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_post_entry.to_tree_path()));

        // delete the (2) path, now only (4) should remain in the tree
        http_tree.delete(&alt_get_entry);

        assert_matches!(
            http_tree
                .witness(&get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&post_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&alt_get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&alt_post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_post_entry)),
            SubtreeLookupResult::Found(_)
        );

        assert!(http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&get_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&post_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&alt_get_entry.to_tree_path()));
        assert!(http_tree.tree.contains_path(&alt_post_entry.to_tree_path()));

        // delete the (4) path, now the tree should be empty
        http_tree.delete(&alt_post_entry);

        assert_matches!(
            http_tree
                .witness(&get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&post_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&alt_get_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_get_entry)),
            SubtreeLookupResult::Absent
        );
        assert_matches!(
            http_tree
                .witness(&alt_post_entry, req_url)
                .unwrap()
                .lookup_subtree(&lookup_path_from_entry(&alt_post_entry)),
            SubtreeLookupResult::Absent
        );

        assert!(!http_tree
            .tree
            .contains_path(&HttpCertificationPath::exact(req_url).to_tree_path()));
        assert!(!http_tree.tree.contains_path(&get_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&post_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&alt_get_entry.to_tree_path()));
        assert!(!http_tree.tree.contains_path(&alt_post_entry.to_tree_path()));
    }

    #[rstest]
    fn test_witness_wildcard_too_long() {
        let mut tree = HttpCertificationTree::default();

        let cel_expr = DefaultCelBuilder::full_certification()
            .with_response_certification(DefaultResponseCertification::response_header_exclusions(
                vec![],
            ))
            .build();

        let not_found_request = HttpRequest::get("/assets/js/not-found.js").build();
        let not_found_response = HttpResponse::not_found(
            br#"404 Not Found"#,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();

        let not_found_entry = HttpCertificationTreeEntry::new(
            HttpCertificationPath::wildcard("/assets/js"),
            HttpCertification::full(&cel_expr, &not_found_request, &not_found_response, None)
                .unwrap(),
        );
        tree.insert(&not_found_entry);

        let witness = tree.witness(&not_found_entry, "/assets");

        assert_matches!(
            witness,
            Err(HttpCertificationError::WildcardPathNotValidForRequestPath { .. })
        );
    }

    #[rstest]
    fn test_witness_wildcard_matches_asset() {
        let mut tree = HttpCertificationTree::default();

        let cel_expr = DefaultCelBuilder::full_certification()
            .with_response_certification(DefaultResponseCertification::response_header_exclusions(
                vec![],
            ))
            .build();

        let index_html_request = HttpRequest::get("/").build();

        let index_html_body = b"<html><body><h1>Hello World!</h1></body></html>".to_vec();
        let index_html_response = HttpResponse::not_found(
            index_html_body,
            vec![(
                CERTIFICATE_EXPRESSION_HEADER_NAME.into(),
                cel_expr.to_string(),
            )],
        )
        .build();

        let certification =
            HttpCertification::full(&cel_expr, &index_html_request, &index_html_response, None)
                .unwrap();
        let index_html_entry =
            HttpCertificationTreeEntry::new(HttpCertificationPath::wildcard("/"), certification);
        tree.insert(&index_html_entry);

        let witness = tree.witness(&index_html_entry, "/").unwrap();

        let mut path = index_html_entry.to_tree_path();
        path.insert(0, b"http_expr".to_vec());

        assert_matches!(witness.lookup_subtree(&path), SubtreeLookupResult::Found(_));
    }

    fn lookup_path_from_entry(entry: &HttpCertificationTreeEntry) -> Vec<Vec<u8>> {
        let mut lookup_path = entry.to_tree_path();
        lookup_path.insert(0, b"http_expr".to_vec());
        lookup_path
    }
}