summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_with/tests/base64.rs
blob: 5b84c0c6f0e932b03d3a292e9ff4943fe27abf80 (plain)
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
#![allow(
    // clippy is broken and shows wrong warnings
    // clippy on stable does not know yet about the lint name
    unknown_lints,
    // https://github.com/rust-lang/rust-clippy/issues/8867
    clippy::derive_partial_eq_without_eq,
    // This allows the tests to be written more uniform and not have to special case the last clone().
    clippy::redundant_clone,
)]

mod utils;

use crate::utils::{check_deserialization, check_error_deserialization, is_equal};
use expect_test::expect;
use serde::{Deserialize, Serialize};
use serde_with::{
    base64::{Base64, Bcrypt, BinHex, Crypt, ImapMutf7, Standard, UrlSafe},
    formats::{Padded, Unpadded},
    serde_as,
};

#[test]
fn base64_vec() {
    let check_equal = vec![vec![0, 1, 2, 13], vec![14, 5, 6, 7]];
    let check_deser = vec![vec![0xaa, 0xbc, 0xff], vec![0xe0, 0x7d], vec![0xe0, 0x7d]];
    let check_deser_from = r#"["qrz/","4H0=","4H0"]"#;

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct BDefault(#[serde_as(as = "Vec<Base64>")] Vec<Vec<u8>>);

    is_equal(
        BDefault(check_equal.clone()),
        expect![[r#"
            [
              "AAECDQ==",
              "DgUGBw=="
            ]"#]],
    );

    // Check mixed padding deserialization
    check_deserialization(BDefault(check_deser.clone()), check_deser_from);

    check_error_deserialization::<BDefault>(
        r#"["0"]"#,
        expect![[r#"Encoded text cannot have a 6-bit remainder. at line 1 column 5"#]],
    );
    check_error_deserialization::<BDefault>(
        r#"["zz"]"#,
        expect![[r#"Invalid last symbol 122, offset 1. at line 1 column 6"#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct BPadded(#[serde_as(as = "Vec<Base64<Standard, Padded>>")] Vec<Vec<u8>>);

    is_equal(
        BPadded(check_equal.clone()),
        expect![[r#"
            [
              "AAECDQ==",
              "DgUGBw=="
            ]"#]],
    );
    check_deserialization(BPadded(check_deser.clone()), check_deser_from);

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct BUnpadded(#[serde_as(as = "Vec<Base64<Standard, Unpadded>>")] Vec<Vec<u8>>);

    is_equal(
        BUnpadded(check_equal.clone()),
        expect![[r#"
            [
              "AAECDQ",
              "DgUGBw"
            ]"#]],
    );
    check_deserialization(BUnpadded(check_deser.clone()), check_deser_from);
}

#[test]
fn base64_different_charsets() {
    let bytes = [
        0x69_u8, 0xb7, 0x1d, 0x79, 0xf8, 0x21, 0x8a, 0x39, 0x25, 0x9a, 0x7a, 0x29, 0xaa, 0xbb,
        0x2d, 0xba, 0xfc, 0x31, 0xcb, 0x30, 0x01, 0x08, 0x31, 0x05, 0x18, 0x72, 0x09, 0x28, 0xb3,
        0x0d, 0x38, 0xf4, 0x11, 0x49, 0x35, 0x15, 0x59, 0x76, 0x19, 0xd3, 0x5d, 0xb7, 0xe3, 0x9e,
        0xbb, 0xf3, 0xdf, 0xbf, 0x00,
    ];

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64Standard(#[serde_as(as = "Base64<Standard, Padded>")] Vec<u8>);

    is_equal(
        B64Standard(bytes.to_vec()),
        expect![[r#""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/AA==""#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64UrlSafe(#[serde_as(as = "Base64<UrlSafe, Padded>")] Vec<u8>);

    is_equal(
        B64UrlSafe(bytes.to_vec()),
        expect![[r#""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_AA==""#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64Crypt(#[serde_as(as = "Base64<Crypt, Padded>")] Vec<u8>);

    is_equal(
        B64Crypt(bytes.to_vec()),
        expect![[r#""OPQRSTUVWXYZabcdefghijklmn./0123456789ABCDEFGHIJKLMNopqrstuvwxyz..==""#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64Bcrypt(#[serde_as(as = "Base64<Bcrypt, Padded>")] Vec<u8>);

    is_equal(
        B64Bcrypt(bytes.to_vec()),
        expect![[r#""YZabcdefghijklmnopqrstuvwx./ABCDEFGHIJKLMNOPQRSTUVWXyz0123456789..==""#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64ImapMutf7(#[serde_as(as = "Base64<ImapMutf7, Padded>")] Vec<u8>);

    is_equal(
        B64ImapMutf7(bytes.to_vec()),
        expect![[r#""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+,AA==""#]],
    );

    #[serde_as]
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    pub struct B64BinHex(#[serde_as(as = "Base64<BinHex, Padded>")] Vec<u8>);

    is_equal(
        B64BinHex(bytes.to_vec()),
        expect![[r##""CDEFGHIJKLMNPQRSTUVXYZ[`ab!\"#$%&'()*+,-0123456789@ABcdehijklmpqr!!==""##]],
    );
}