ic-response-verification 3.2.0

Client side response verification for the Internet Computer
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
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Various error types for response verification failure scenarios

#[cfg(all(target_arch = "wasm32", feature = "js"))]
use wasm_bindgen::prelude::*;

use crate::cel;

/// Convenience type that represents the Result of performing response verification
pub type ResponseVerificationResult<T = ()> = Result<T, ResponseVerificationError>;

/// The primary container for response verification errors
#[derive(thiserror::Error, Debug, Clone)]
pub enum ResponseVerificationError {
    /// Error converting UTF-8 string
    #[error(r#"IO error: "{0}""#)]
    IoError(String),

    /// An unsupported verification version was requested
    #[error(r#"The requested verification version {requested_version:?} is not supported, the current supported range is {min_supported_version:?}-{max_supported_version:?}"#)]
    UnsupportedVerificationVersion {
        /// The minimum supported verification version
        min_supported_version: u8,
        /// The maximum supported verification version
        max_supported_version: u8,
        /// The actual requested verification version
        requested_version: u8,
    },

    /// Mismatch between the minimum requested version and the actual requested version
    #[error(r#"The requested verification version {requested_version:?} is lower than the minimum requested version {min_requested_verification_version:?}"#)]
    RequestedVerificationVersionMismatch {
        /// The minimum version that will be requested
        min_requested_verification_version: u8,
        /// The actual requested version
        requested_version: u8,
    },

    /// Error parsing CEL expression
    #[error("Cel parser error")]
    CelError(#[from] cel::CelParserError),

    /// Error decoding base64
    #[error("Base64 decoding error")]
    Base64DecodingError(#[from] base64::DecodeError),

    /// Error parsing int
    #[error("Error parsing int")]
    ParseIntError(#[from] std::num::ParseIntError),

    /// The tree has different root hash from the expected value in the certified variables
    #[error("Invalid tree root hash")]
    InvalidTreeRootHash,

    /// The certificate provided by the "IC-Certificate" response header is missing the
    /// certified data witness for the canister
    #[error(r#"The certificate provided by the "IC-Certificate" response header is missing the certified data witness for the canister with ID {canister_id}"#)]
    CertificateMissingCertifiedData {
        /// The ID of the canister that returned the response
        canister_id: String,
    },

    /// The expression path provided by the "IC-Certificate" response header
    /// has an unexpected suffix and should end with "<$>" or "<*>"
    #[error(r#"The expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) has an unexpected prefix and should start with "http_expr"#)]
    UnexpectedExpressionPathPrefix {
        /// The invalid expression path
        provided_expr_path: Vec<String>,
    },

    /// The expression path provided by the "IC-Certificate" response header
    /// has an unexpected suffix and should end with "<$>" or "<*>"
    #[error(r#"The expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) has an unexpected suffix and should end with "<$>" or "<*>"#)]
    UnexpectedExpressionPathSuffix {
        /// The invalid expression path
        provided_expr_path: Vec<String>,
    },

    /// The exact expression path provided by the "IC-Certificate" response header
    /// was not found in the tree
    #[error(r#"The exact expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) was not found in the tree"#)]
    ExactExpressionPathNotFoundInTree {
        /// The missing expression path
        provided_expr_path: Vec<String>,
    },

    /// The exact expression path provided by the "IC-Certificate" response header
    /// is not valid for the request path
    #[error(r#"The exact expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) is not valid for the request path ({request_path:?})"#)]
    ExactExpressionPathMismatch {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
        /// The request path
        request_path: String,
    },

    /// A wildcard expression path was provided by the "IC-Certificate" response header
    /// but a potential exact expression path is valid for the request path and might
    /// exist in the tree
    #[error(r#"A wildcard expression path was provided by the "IC-Certificate" response header ({provided_expr_path:?}), but a potential exact expression path ({potential_expr_path:?}) is valid for the request path ({request_path:?}) and might exist in the tree"#)]
    ExactExpressionPathMightExistInTree {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
        /// The expected expression path
        potential_expr_path: Vec<String>,
        /// The request path
        request_path: String,
    },

    /// The wildcard expression path provided by the "IC-Certificate" response
    /// was not found in the tree
    #[error(r#"The wildcard expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) is valid for the request path ({request_path:?}), but was not found in the tree"#)]
    WildcardExpressionPathNotFoundInTree {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
        /// The request path
        request_path: String,
    },

    /// The wildcard expression path provided by the "IC-Certificate" response
    /// header is not valid for the request path
    #[error(r#"The wildcard expression path provided by the "IC-Certificate" response header ({provided_expr_path:?}) is not valid for the request path ({request_path:?})"#)]
    WildcardExpressionPathMismatch {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
        /// The request path
        request_path: String,
    },

    /// A more specific wildcard expression path than the one provided by the
    /// "IC-Certificate" response header that is valid for the request path might
    /// exist in the tree
    #[error(r#"A more specific wildcard expression path ({more_specific_expr_path:?}) than the one provided by the "IC-Certificate" response header ({provided_expr_path:?}) that is valid for the request path ({request_path:?}) might exist in the tree"#)]
    MoreSpecificWildcardExpressionMightExistInTree {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
        /// The more specific expression path that might exist in the tree
        more_specific_expr_path: Vec<String>,
        /// The request path
        request_path: String,
    },

    /// The hash of the CEL expression provided by the "IC-Certificate-Expression"
    /// response header does not exist at the expression path provided by the
    /// "IC-Certificate" response header
    #[error(r#"The hash of the CEL expression provided by the "IC-Certificate-Expression" response header does not exist at the path provided by the "IC-Certificate" response header ({provided_expr_path:?})"#)]
    InvalidExpressionHash {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
    },

    /// The hash of the request and response was not found in the tree at the
    /// expression path provided by the "IC-Certificate" response header
    #[error(r#"The hash of the request and response was not found in the tree at the expression path provided by the "IC-Certificate" response header ({provided_expr_path:?})"#)]
    InvalidRequestAndResponseHashes {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
    },

    /// The required empty leaf node was not found in the tree at the expression
    /// path provided by the "IC-Certificate" response header
    #[error(r#"The required empty leaf node was not found in the tree at the expression path provided by the "IC-Certificate" response header ({provided_expr_path:?})"#)]
    MissingLeafNode {
        /// The expression path provided by the "IC-Certificate" response header
        provided_expr_path: Vec<String>,
    },

    /// The response body was a mismatch from the expected values in the tree
    #[error("Invalid response body")]
    InvalidResponseBody,

    /// The certificate was missing from the certification header
    #[error("Certificate not found")]
    HeaderMissingCertificate,

    /// The tree was missing from the certification header
    #[error("Tree not found")]
    HeaderMissingTree,

    /// The certificate expression path was missing from the certification header
    #[error("Certificate expression path not found")]
    HeaderMissingCertificateExpressionPath,

    /// The certificate expression was missing from the response headers
    #[error("Certificate expression not found")]
    HeaderMissingCertificateExpression,

    /// The certification values could not be found in the response headers
    #[error("Certification values not found")]
    HeaderMissingCertification,

    /// Failed to decode CBOR
    #[error("CBOR decoding failed")]
    CborDecodingFailed(#[from] ic_cbor::CborError),

    /// Failed to verify certificate
    #[error("Certificate verification failed")]
    CertificateVerificationFailed(
        #[from] ic_certificate_verification::CertificateVerificationError,
    ),

    /// HTTP Certification error
    #[error(r#"HTTP Certification error: "{0}""#)]
    HttpCertificationError(#[from] ic_http_certification::HttpCertificationError),
}

impl From<std::io::Error> for ResponseVerificationError {
    fn from(error: std::io::Error) -> Self {
        ResponseVerificationError::IoError(error.to_string())
    }
}

/// JS Representation of the ResponseVerificationError code
#[cfg(all(target_arch = "wasm32", feature = "js"))]
#[wasm_bindgen(js_name = ResponseVerificationErrorCode)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ResponseVerificationJsErrorCode {
    /// Error converting UTF-8 string
    IoError,
    /// An unsupported verification version was requested
    UnsupportedVerificationVersion,
    /// Mismatch between the minimum requested version and the actual requested version
    RequestedVerificationVersionMismatch,
    /// Error parsing CEL expression
    CelError,
    /// Error decoding base64
    Base64DecodingError,
    /// Error parsing int
    ParseIntError,
    /// The tree has different root hash from the expected value in the certified variables
    InvalidTreeRootHash,
    /// The certificate provided by the "IC-Certificate" response header is missing the
    /// certified data witness for the canister
    CertificateMissingCertifiedData,
    /// The expression path provided by the "IC-Certificate" response header
    /// has an unexpected suffix and should end with "<$>" or "<*>"
    UnexpectedExpressionPathPrefix,
    /// The expression path provided by the "IC-Certificate" response header
    /// has an unexpected suffix and should end with "<$>" or "<*>"
    UnexpectedExpressionPathSuffix,
    /// The exact expression path provided by the "IC-Certificate" response header
    /// was not found in the tree
    ExactExpressionPathNotFoundInTree,
    /// The exact expression path provided by the "IC-Certificate" response header
    /// is not valid for the request path
    ExactExpressionPathMismatch,
    /// A wildcard expression path was provided by the "IC-Certificate" response header
    /// but a potential exact expression path is valid for the request path and might
    /// exist in the tree
    ExactExpressionPathMightExistInTree,
    /// The wildcard expression path provided by the "IC-Certificate" response
    /// was not found in the tree
    WildcardExpressionPathNotFoundInTree,
    /// The wildcard expression path provided by the "IC-Certificate" response
    /// header is not valid for the request path
    WildcardExpressionPathMismatch,
    /// A more specific wildcard expression path than the one provided by the
    /// "IC-Certificate" response header that is valid for the request path might
    /// exist in the tree
    MoreSpecificWildcardExpressionMightExistInTree,
    /// The hash of the CEL expression provided by the "IC-Certificate-Expression"
    /// response header does not exist at the expression path provided by the
    /// "IC-Certificate" response header
    InvalidExpressionHash,
    /// The hash of the request and response was not found in the tree at the
    /// expression path provided by the "IC-Certificate" response header
    InvalidRequestAndResponseHashes,
    /// The required empty leaf node was not found in the tree at the expression
    /// path provided by the "IC-Certificate" response header
    MissingLeafNode,
    /// The response body was a mismatch from the expected values in the tree
    InvalidResponseBody,
    /// The certificate was missing from the certification header
    HeaderMissingCertificate,
    /// The tree was missing from the certification header
    HeaderMissingTree,
    /// The certificate expression path was missing from the certification header
    HeaderMissingCertificateExpression,
    /// The certificate expression was missing from the response headers
    MissingCertificateExpression,
    /// The certification values could not be found in the response headers
    HeaderMissingCertification,
    /// Failed to decode CBOR
    CborDecodingFailed,
    /// Failed to verify certificate
    CertificateVerificationFailed,
    /// HTTP Certification error
    HttpCertificationError,
}

/// JS Representation of the ResponseVerificationError
#[cfg(all(target_arch = "wasm32", feature = "js"))]
#[wasm_bindgen(inspectable, js_name = ResponseVerificationError)]
#[derive(Debug, Eq, PartialEq)]
pub struct ResponseVerificationJsError {
    /// Error code as an enum
    #[wasm_bindgen(readonly)]
    pub code: ResponseVerificationJsErrorCode,

    /// Stringified error message
    #[wasm_bindgen(getter_with_clone, readonly)]
    pub message: String,
}

#[cfg(all(target_arch = "wasm32", feature = "js"))]
impl From<ResponseVerificationError> for ResponseVerificationJsError {
    fn from(error: ResponseVerificationError) -> ResponseVerificationJsError {
        let code = match error {
            ResponseVerificationError::IoError(_) => ResponseVerificationJsErrorCode::IoError,
            ResponseVerificationError::UnsupportedVerificationVersion { .. } => {
                ResponseVerificationJsErrorCode::UnsupportedVerificationVersion
            }
            ResponseVerificationError::RequestedVerificationVersionMismatch { .. } => {
                ResponseVerificationJsErrorCode::RequestedVerificationVersionMismatch
            }
            ResponseVerificationError::CelError(_) => ResponseVerificationJsErrorCode::CelError,
            ResponseVerificationError::Base64DecodingError(_) => {
                ResponseVerificationJsErrorCode::Base64DecodingError
            }
            ResponseVerificationError::ParseIntError(_) => {
                ResponseVerificationJsErrorCode::ParseIntError
            }
            ResponseVerificationError::InvalidTreeRootHash => {
                ResponseVerificationJsErrorCode::InvalidTreeRootHash
            }
            ResponseVerificationError::CertificateMissingCertifiedData { .. } => {
                ResponseVerificationJsErrorCode::CertificateMissingCertifiedData
            }
            ResponseVerificationError::UnexpectedExpressionPathPrefix { .. } => {
                ResponseVerificationJsErrorCode::UnexpectedExpressionPathPrefix
            }
            ResponseVerificationError::UnexpectedExpressionPathSuffix { .. } => {
                ResponseVerificationJsErrorCode::UnexpectedExpressionPathSuffix
            }
            ResponseVerificationError::ExactExpressionPathNotFoundInTree { .. } => {
                ResponseVerificationJsErrorCode::ExactExpressionPathNotFoundInTree
            }
            ResponseVerificationError::ExactExpressionPathMismatch { .. } => {
                ResponseVerificationJsErrorCode::ExactExpressionPathMismatch
            }
            ResponseVerificationError::ExactExpressionPathMightExistInTree { .. } => {
                ResponseVerificationJsErrorCode::ExactExpressionPathMightExistInTree
            }
            ResponseVerificationError::WildcardExpressionPathNotFoundInTree { .. } => {
                ResponseVerificationJsErrorCode::WildcardExpressionPathNotFoundInTree
            }
            ResponseVerificationError::WildcardExpressionPathMismatch { .. } => {
                ResponseVerificationJsErrorCode::WildcardExpressionPathMismatch
            }
            ResponseVerificationError::MoreSpecificWildcardExpressionMightExistInTree {
                ..
            } => ResponseVerificationJsErrorCode::MoreSpecificWildcardExpressionMightExistInTree,
            ResponseVerificationError::InvalidExpressionHash { .. } => {
                ResponseVerificationJsErrorCode::InvalidExpressionHash
            }
            ResponseVerificationError::InvalidRequestAndResponseHashes { .. } => {
                ResponseVerificationJsErrorCode::InvalidRequestAndResponseHashes
            }
            ResponseVerificationError::MissingLeafNode { .. } => {
                ResponseVerificationJsErrorCode::MissingLeafNode
            }
            ResponseVerificationError::InvalidResponseBody => {
                ResponseVerificationJsErrorCode::InvalidResponseBody
            }
            ResponseVerificationError::HeaderMissingCertificate => {
                ResponseVerificationJsErrorCode::HeaderMissingCertificate
            }
            ResponseVerificationError::HeaderMissingTree => {
                ResponseVerificationJsErrorCode::HeaderMissingTree
            }
            ResponseVerificationError::HeaderMissingCertificateExpressionPath => {
                ResponseVerificationJsErrorCode::HeaderMissingCertificateExpression
            }
            ResponseVerificationError::HeaderMissingCertificateExpression => {
                ResponseVerificationJsErrorCode::MissingCertificateExpression
            }
            ResponseVerificationError::HeaderMissingCertification => {
                ResponseVerificationJsErrorCode::HeaderMissingCertification
            }
            ResponseVerificationError::CborDecodingFailed(_) => {
                ResponseVerificationJsErrorCode::CborDecodingFailed
            }
            ResponseVerificationError::CertificateVerificationFailed(_) => {
                ResponseVerificationJsErrorCode::CertificateVerificationFailed
            }
            ResponseVerificationError::HttpCertificationError(_) => {
                ResponseVerificationJsErrorCode::HttpCertificationError
            }
        };
        let message = error.to_string();

        ResponseVerificationJsError {
            code: code.into(),
            message,
        }
    }
}

#[cfg(all(target_arch = "wasm32", feature = "js", test))]
mod tests {
    use super::*;
    use crate::cel::CelParserError;
    use base64::{engine::general_purpose, Engine as _};
    use ic_cbor::CborError;
    use ic_certificate_verification::CertificateVerificationError;
    use ic_http_certification::HttpCertificationError;
    use ic_response_verification_test_utils::hex_decode;
    use wasm_bindgen_test::wasm_bindgen_test;

    #[wasm_bindgen_test]
    fn error_into_http_certification_error() {
        let error = ResponseVerificationError::HttpCertificationError(
            HttpCertificationError::MalformedUrl("https://internetcomputer.org".into()),
        );
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HttpCertificationError,
                message: r#"HTTP Certification error: "Failed to parse url: "https://internetcomputer.org"""#.into(),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_io_error() {
        let inner_error = std::fs::File::open("foo.txt").expect_err("Expected error");
        let error_msg = inner_error.to_string();

        let error = ResponseVerificationError::IoError(inner_error.to_string());

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::IoError,
                message: format!(r#"IO error: "{}""#, error_msg),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_utf8_conversion_error() {
        let invalid_utf_bytes = hex_decode("fca1a1a1a1a1");
        let inner_error = String::from_utf8(invalid_utf_bytes).expect_err("Expected error");

        let error = ResponseVerificationError::HttpCertificationError(
            HttpCertificationError::Utf8ConversionError(inner_error.clone()),
        );

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HttpCertificationError,
                message: format!(
                    r#"HTTP Certification error: "Error converting UTF8 string bytes: "{0}"""#,
                    inner_error.to_string()
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_unsupported_verification_version() {
        let error = ResponseVerificationError::UnsupportedVerificationVersion {
            min_supported_version: 1,
            max_supported_version: 2,
            requested_version: 42,
        };

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::UnsupportedVerificationVersion,
                message: r#"The requested verification version 42 is not supported, the current supported range is 1-2"#.into(),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_verification_version_mismatch() {
        let error = ResponseVerificationError::RequestedVerificationVersionMismatch {
            min_requested_verification_version: 2,
            requested_version: 1,
        };

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::RequestedVerificationVersionMismatch,
                message: r#"The requested verification version 1 is lower than the minimum requested version 2"#.into(),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_cel_error() {
        let inner_error = CelParserError::CelSyntaxException(
            "Garbage is not allowed in the CEL expression!".into(),
        );
        let error = ResponseVerificationError::from(inner_error);

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::CelError,
                message: r#"Cel parser error"#.into(),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_base64_decoding_error() {
        let invalid_base64 = hex_decode("fca1a1a1a1a1");
        let inner_error = general_purpose::STANDARD
            .decode(invalid_base64)
            .expect_err("Expected error");

        let error = ResponseVerificationError::Base64DecodingError(inner_error);

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::Base64DecodingError,
                message: format!(r#"Base64 decoding error"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_parse_int_error() {
        let invalid_int = "fortytwo";
        let inner_error = invalid_int.parse::<u8>().expect_err("Expected error");

        let error = ResponseVerificationError::ParseIntError(inner_error);

        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::ParseIntError,
                message: format!(r#"Error parsing int"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_tree_error() {
        let error = ResponseVerificationError::InvalidTreeRootHash;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::InvalidTreeRootHash,
                message: format!(r#"Invalid tree root hash"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_unexpected_expression_path_prefix_error() {
        let error = ResponseVerificationError::UnexpectedExpressionPathPrefix {
            provided_expr_path: vec!["http_expr".into()],
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::UnexpectedExpressionPathPrefix,
                message: format!(
                    r#"The expression path provided by the "IC-Certificate" response header (["http_expr"]) has an unexpected prefix and should start with "http_expr"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_unexpected_expression_path_suffix_error() {
        let error = ResponseVerificationError::UnexpectedExpressionPathSuffix {
            provided_expr_path: vec!["http_expr".into()],
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::UnexpectedExpressionPathSuffix,
                message: format!(
                    r#"The expression path provided by the "IC-Certificate" response header (["http_expr"]) has an unexpected suffix and should end with "<$>" or "<*>"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_exact_expression_path_not_found_in_tree_error() {
        let error = ResponseVerificationError::ExactExpressionPathNotFoundInTree {
            provided_expr_path: vec!["http_expr".into()],
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::ExactExpressionPathNotFoundInTree,
                message: format!(
                    r#"The exact expression path provided by the "IC-Certificate" response header (["http_expr"]) was not found in the tree"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_exact_expression_path_mismatch_error() {
        let error = ResponseVerificationError::ExactExpressionPathMismatch {
            provided_expr_path: vec!["http_expr".into()],
            request_path: "/path".into(),
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::ExactExpressionPathMismatch,
                message: format!(
                    r#"The exact expression path provided by the "IC-Certificate" response header (["http_expr"]) is not valid for the request path ("/path")"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_exact_expression_path_might_exist_in_tree_error() {
        let error = ResponseVerificationError::ExactExpressionPathMightExistInTree {
            provided_expr_path: vec!["http_expr".into()],
            potential_expr_path: vec!["http_expr".into()],
            request_path: "/path".into(),
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::ExactExpressionPathMightExistInTree,
                message: format!(
                    r#"A wildcard expression path was provided by the "IC-Certificate" response header (["http_expr"]), but a potential exact expression path (["http_expr"]) is valid for the request path ("/path") and might exist in the tree"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_wildcard_expression_path_not_found_in_tree_error() {
        let error = ResponseVerificationError::WildcardExpressionPathNotFoundInTree {
            provided_expr_path: vec!["http_expr".into()],
            request_path: "/path".into(),
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::WildcardExpressionPathNotFoundInTree,
                message: format!(
                    r#"The wildcard expression path provided by the "IC-Certificate" response header (["http_expr"]) is valid for the request path ("/path"), but was not found in the tree"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_wildcard_expression_path_mismatch_error() {
        let error = ResponseVerificationError::WildcardExpressionPathMismatch {
            provided_expr_path: vec!["http_expr".into()],
            request_path: "/path".into(),
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::WildcardExpressionPathMismatch,
                message: format!(
                    r#"The wildcard expression path provided by the "IC-Certificate" response header (["http_expr"]) is not valid for the request path ("/path")"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_more_specific_wildcard_expression_might_exist_in_tree_error() {
        let error = ResponseVerificationError::MoreSpecificWildcardExpressionMightExistInTree {
            provided_expr_path: vec!["http_expr".into()],
            more_specific_expr_path: vec!["http_expr".into()],
            request_path: "/path".into(),
        };
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code:
                    ResponseVerificationJsErrorCode::MoreSpecificWildcardExpressionMightExistInTree,
                message: format!(
                    r#"A more specific wildcard expression path (["http_expr"]) than the one provided by the "IC-Certificate" response header (["http_expr"]) that is valid for the request path ("/path") might exist in the tree"#
                ),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_response_body_error() {
        let error = ResponseVerificationError::InvalidResponseBody;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::InvalidResponseBody,
                message: format!(r#"Invalid response body"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_missing_certificate_error() {
        let error = ResponseVerificationError::HeaderMissingCertificate;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HeaderMissingCertificate,
                message: format!(r#"Certificate not found"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_missing_tree_error() {
        let error = ResponseVerificationError::HeaderMissingTree;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HeaderMissingTree,
                message: format!(r#"Tree not found"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_missing_certificate_expr_path_error() {
        let error = ResponseVerificationError::HeaderMissingCertificateExpressionPath;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HeaderMissingCertificateExpression,
                message: format!(r#"Certificate expression path not found"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_missing_certificate_expr_error() {
        let error = ResponseVerificationError::HeaderMissingCertificateExpression;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::MissingCertificateExpression,
                message: format!(r#"Certificate expression not found"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_invalid_missing_certification_error() {
        let error = ResponseVerificationError::HeaderMissingCertification;
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::HeaderMissingCertification,
                message: format!(r#"Certification values not found"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_cbor_decoding_failed_error() {
        let error = ResponseVerificationError::CborDecodingFailed(CborError::MalformedCbor(
            "HashTree CBOR is malformed".into(),
        ));
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::CborDecodingFailed,
                message: format!(r#"CBOR decoding failed"#),
            }
        )
    }

    #[wasm_bindgen_test]
    fn error_into_certificate_verification_failed_error() {
        let error = ResponseVerificationError::CertificateVerificationFailed(
            CertificateVerificationError::MissingTimePathInTree {
                path: vec![b"time".to_vec()],
            },
        );
        let result = ResponseVerificationJsError::from(error);

        assert_eq!(
            result,
            ResponseVerificationJsError {
                code: ResponseVerificationJsErrorCode::CertificateVerificationFailed,
                message: format!(r#"Certificate verification failed"#),
            }
        )
    }
}