base_encoding/
lib.rs

1// Copyright 2017 ThetaSinner
2//
3// This file is part of base-encoding.
4//
5// base-encoding is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// base-encoding is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with base-encoding. If not, see <http://www.gnu.org/licenses/>.
17
18pub mod encode;
19pub mod decode;
20
21#[cfg(test)]
22mod tests {
23    use super::encode;
24    use super::decode;
25
26    // base64
27    #[test]
28    fn base_64_section_9_example_1() {
29        let result = encode::base64(&[0x14, 0xfb, 0x9c, 0x3, 0xd9, 0x7e]);
30
31        assert_eq!(String::from("FPucA9l+"), result);
32
33        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3, 0xd9, 0x7e], decode::base64(result.as_str()).as_slice());
34    }
35
36    #[test]
37    fn base_64_section_9_example_2() {
38        let result = encode::base64(&[0x14, 0xfb, 0x9c, 0x3, 0xd9]);
39
40        assert_eq!(String::from("FPucA9k="), result);
41
42        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3, 0xd9], decode::base64(result.as_str()).as_slice());
43    }
44
45    #[test]
46    fn base_64_section_9_example_3() {
47        let result = encode::base64(&[0x14, 0xfb, 0x9c, 0x3]);
48
49        assert_eq!(String::from("FPucAw=="), result);
50
51        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3], decode::base64(result.as_str()).as_slice());
52    }
53
54    #[test]
55    fn base_64_section_10_example_1() {
56        let result = encode::base64(String::from("").as_bytes());
57
58        assert_eq!(String::from(""), result);
59
60        assert_eq!(0, decode::base64(result.as_str()).as_slice().len());
61    }
62
63    #[test]
64    fn base_64_section_10_example_2() {
65        let result = encode::base64(String::from("f").as_bytes());
66
67        assert_eq!(String::from("Zg=="), result);
68
69        assert_eq!(String::from("f"), String::from_utf8(decode::base64(result.as_str())).unwrap());
70    }
71
72    #[test]
73    fn base_64_section_10_example_3() {
74        let result = encode::base64(String::from("fo").as_bytes());
75
76        assert_eq!(String::from("Zm8="), result);
77
78        assert_eq!(String::from("fo"), String::from_utf8(decode::base64(result.as_str())).unwrap());
79    }
80
81    #[test]
82    fn base_64_section_10_example_4() {
83        let result = encode::base64(String::from("foo").as_bytes());
84
85        assert_eq!(String::from("Zm9v"), result);
86
87        assert_eq!(String::from("foo"), String::from_utf8(decode::base64(result.as_str())).unwrap());
88    }
89
90    #[test]
91    fn base_64_section_10_example_5() {
92        let result = encode::base64(String::from("foob").as_bytes());
93
94        assert_eq!(String::from("Zm9vYg=="), result);
95
96        assert_eq!(String::from("foob"), String::from_utf8(decode::base64(result.as_str())).unwrap());
97    }
98
99    #[test]
100    fn base_64_section_10_example_6() {
101        let result = encode::base64(String::from("fooba").as_bytes());
102
103        assert_eq!(String::from("Zm9vYmE="), result);
104
105        assert_eq!(String::from("fooba"), String::from_utf8(decode::base64(result.as_str())).unwrap());
106    }
107
108    #[test]
109    fn base_64_section_10_example_7() {
110        let result = encode::base64(String::from("foobar").as_bytes());
111
112        assert_eq!(String::from("Zm9vYmFy"), result);
113
114        assert_eq!(String::from("foobar"), String::from_utf8(decode::base64(result.as_str())).unwrap());
115    }
116
117    // base64 no padding
118    #[test]
119    fn base_64_section_10_example_5_no_padding() {
120        let result = String::from("Zm9vYg");
121
122        assert_eq!(String::from("foob"), String::from_utf8(decode::base64(result.as_str())).unwrap());
123    }
124
125    #[test]
126    fn base_64_section_10_example_6_no_padding() {
127        let result = String::from("Zm9vYmE");
128
129        assert_eq!(String::from("fooba"), String::from_utf8(decode::base64(result.as_str())).unwrap());
130    }
131
132    // base64url
133    #[test]
134    fn base_64_url_section_9_example_1() {
135        let result = encode::base64url(&[0x14, 0xfb, 0x9c, 0x3, 0xd9, 0x7e]);
136
137        assert_eq!(String::from("FPucA9l-"), result);
138
139        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3, 0xd9, 0x7e], decode::base64url(result.as_str()).as_slice());
140    }
141
142    #[test]
143    fn base_64_url_section_9_example_2() {
144        let result = encode::base64url(&[0x14, 0xfb, 0x9c, 0x3, 0xd9]);
145
146        assert_eq!(String::from("FPucA9k="), result);
147
148        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3, 0xd9], decode::base64url(result.as_str()).as_slice());
149    }
150
151    #[test]
152    fn base_64_url_section_9_example_3() {
153        let result = encode::base64url(&[0x14, 0xfb, 0x9c, 0x3]);
154
155        assert_eq!(String::from("FPucAw=="), result);
156
157        assert_eq!(&[0x14, 0xfb, 0x9c, 0x3], decode::base64url(result.as_str()).as_slice());
158    }
159
160    #[test]
161    fn base_64_url_section_10_example_1() {
162        let result = encode::base64url(String::from("").as_bytes());
163
164        assert_eq!(String::from(""), result);
165
166        assert_eq!(0, decode::base64url(result.as_str()).as_slice().len());
167    }
168
169    #[test]
170    fn base_64_url_section_10_example_2() {
171        let result = encode::base64url(String::from("f").as_bytes());
172
173        assert_eq!(String::from("Zg=="), result);
174
175        assert_eq!(String::from("f"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
176    }
177
178    #[test]
179    fn base_64_url_section_10_example_3() {
180        let result = encode::base64url(String::from("fo").as_bytes());
181
182        assert_eq!(String::from("Zm8="), result);
183
184        assert_eq!(String::from("fo"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
185    }
186
187    #[test]
188    fn base_64_url_section_10_example_4() {
189        let result = encode::base64url(String::from("foo").as_bytes());
190
191        assert_eq!(String::from("Zm9v"), result);
192
193        assert_eq!(String::from("foo"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
194    }
195
196    #[test]
197    fn base_64_url_section_10_example_5() {
198        let result = encode::base64url(String::from("foob").as_bytes());
199
200        assert_eq!(String::from("Zm9vYg=="), result);
201
202        assert_eq!(String::from("foob"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
203    }
204
205    #[test]
206    fn base_64_url_section_10_example_6() {
207        let result = encode::base64url(String::from("fooba").as_bytes());
208
209        assert_eq!(String::from("Zm9vYmE="), result);
210
211        assert_eq!(String::from("fooba"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
212    }
213
214    #[test]
215    fn base_64_url_section_10_example_7() {
216        let result = encode::base64url(String::from("foobar").as_bytes());
217
218        assert_eq!(String::from("Zm9vYmFy"), result);
219
220        assert_eq!(String::from("foobar"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
221    }
222
223    // base64url no padding
224    #[test]
225    fn base_64_url_section_10_example_5_no_padding() {
226        let result = String::from("Zm9vYg");
227
228        assert_eq!(String::from("foob"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
229    }
230
231    #[test]
232    fn base_64_url_section_10_example_6_no_padding() {
233        let result = String::from("Zm9vYmE");
234
235        assert_eq!(String::from("fooba"), String::from_utf8(decode::base64url(result.as_str())).unwrap());
236    }
237}