|
1 |
| -//! Tests that ensure that the compiler can compile the code. |
2 |
| -
|
3 |
| -#![allow(unused)] |
4 |
| - |
5 | 1 | pub mod fail;
|
6 | 2 | pub mod succeed;
|
7 |
| - |
8 |
| -use veil::*; |
9 |
| - |
10 |
| -#[derive(Redact)] |
11 |
| -struct CreditCard { |
12 |
| - #[redact] |
13 |
| - cvv: String, |
14 |
| - |
15 |
| - #[redact(partial)] |
16 |
| - number: String, |
17 |
| - |
18 |
| - expiration: String, |
19 |
| - |
20 |
| - #[redact(with = 'X')] |
21 |
| - name: String, |
22 |
| - |
23 |
| - billing_address: Address, |
24 |
| - |
25 |
| - issuer: CreditCardIssuer, |
26 |
| - |
27 |
| - country: Country, |
28 |
| -} |
29 |
| - |
30 |
| -#[derive(Redact)] |
31 |
| -struct Address { |
32 |
| - #[redact(partial)] |
33 |
| - line1: String, |
34 |
| - |
35 |
| - #[redact(partial)] |
36 |
| - line2: String, |
37 |
| - |
38 |
| - #[redact] |
39 |
| - house_or_flat_number: Option<u32>, |
40 |
| - |
41 |
| - #[redact] |
42 |
| - postcode: String, |
43 |
| - |
44 |
| - #[redact(partial)] |
45 |
| - city: String, |
46 |
| -} |
47 |
| - |
48 |
| -#[derive(Redact)] |
49 |
| -#[redact(all)] |
50 |
| -struct RedactAll { |
51 |
| - field: String, |
52 |
| - field2: String, |
53 |
| - field3: String, |
54 |
| -} |
55 |
| - |
56 |
| -#[derive(Redact)] |
57 |
| -#[redact(all, partial, with = 'X')] |
58 |
| -struct RedactAllWithFlags { |
59 |
| - field: String, |
60 |
| - |
61 |
| - #[redact(skip)] |
62 |
| - field2: String, |
63 |
| - |
64 |
| - field3: String, |
65 |
| -} |
66 |
| - |
67 |
| -#[derive(Redact)] |
68 |
| -enum CreditCardIssuer { |
69 |
| - #[redact(variant)] |
70 |
| - Visa { |
71 |
| - #[redact(partial)] |
72 |
| - visa_data_1: String, |
73 |
| - |
74 |
| - #[redact(partial)] |
75 |
| - visa_data_2: String, |
76 |
| - }, |
77 |
| - |
78 |
| - #[redact(variant, partial)] |
79 |
| - MasterCard, |
80 |
| - |
81 |
| - #[redact(variant)] |
82 |
| - #[redact(all, fixed = 6, with = '$')] |
83 |
| - SecretAgentCard { |
84 |
| - secret_data_1: String, |
85 |
| - secret_data_2: String, |
86 |
| - }, |
87 |
| -} |
88 |
| - |
89 |
| -#[derive(Redact)] |
90 |
| -#[redact(all, variant, partial)] |
91 |
| -enum Country { |
92 |
| - #[doc = "hello world!"] // to test mixing attributes works ok |
93 |
| - #[redact(variant)] |
94 |
| - UnitedKingdom, |
95 |
| - Italy, |
96 |
| -} |
97 |
| - |
98 |
| -#[derive(Redact)] |
99 |
| -struct TupleStruct(#[redact] u32, #[redact(partial)] u32); |
100 |
| - |
101 |
| -#[derive(Redact)] |
102 |
| -struct GenericStruct<Foo: std::fmt::Debug, Bar: std::fmt::Debug>(Foo, #[redact] Bar); |
103 |
| - |
104 |
| -#[derive(Redact)] |
105 |
| -struct GenericWhereStruct<Foo, Bar>(Foo, #[redact] Bar) |
106 |
| -where |
107 |
| - Foo: std::fmt::Debug, |
108 |
| - Bar: std::fmt::Debug; |
109 |
| - |
110 |
| -#[derive(Redact)] |
111 |
| -enum GenericWhereEnum<Foo, Bar> |
112 |
| -where |
113 |
| - Foo: std::fmt::Debug, |
114 |
| - Bar: std::fmt::Debug, |
115 |
| -{ |
116 |
| - FooVariant(Foo), |
117 |
| - BarVariant(#[redact] Bar), |
118 |
| -} |
119 |
| - |
120 |
| -#[derive(Redact)] |
121 |
| -enum GenericEnum<Foo: std::fmt::Debug, Bar: std::fmt::Debug> { |
122 |
| - FooVariant(Foo), |
123 |
| - BarVariant(#[redact] Bar), |
124 |
| -} |
125 |
| - |
126 |
| -#[test] |
127 |
| -fn test_credit_card_redacting() { |
128 |
| - println!( |
129 |
| - "{:#?}", |
130 |
| - CreditCard { |
131 |
| - cvv: "098".to_string(), |
132 |
| - number: "1234 5678 9012 3456".to_string(), |
133 |
| - expiration: "12/34".to_string(), |
134 |
| - name: "John Doe".to_string(), |
135 |
| - billing_address: Address { |
136 |
| - line1: "123 Fake Street".to_string(), |
137 |
| - line2: "Apt. 1".to_string(), |
138 |
| - house_or_flat_number: Some(64), |
139 |
| - postcode: "12345".to_string(), |
140 |
| - city: "London".to_string(), |
141 |
| - }, |
142 |
| - issuer: CreditCardIssuer::Visa { |
143 |
| - visa_data_1: "Hello".to_string(), |
144 |
| - visa_data_2: "World".to_string() |
145 |
| - }, |
146 |
| - country: Country::UnitedKingdom, |
147 |
| - } |
148 |
| - ); |
149 |
| - |
150 |
| - println!( |
151 |
| - "{:#?}", |
152 |
| - CreditCard { |
153 |
| - cvv: "098".to_string(), |
154 |
| - number: "1234 5678 9012 3456".to_string(), |
155 |
| - expiration: "12/34".to_string(), |
156 |
| - name: "John Doe".to_string(), |
157 |
| - billing_address: Address { |
158 |
| - line1: "123 Fake Street".to_string(), |
159 |
| - line2: "Apt. 1".to_string(), |
160 |
| - house_or_flat_number: Some(64), |
161 |
| - postcode: "12345".to_string(), |
162 |
| - city: "London".to_string(), |
163 |
| - }, |
164 |
| - issuer: CreditCardIssuer::SecretAgentCard { |
165 |
| - secret_data_1: "Hello".to_string(), |
166 |
| - secret_data_2: "World".to_string() |
167 |
| - }, |
168 |
| - country: Country::Italy, |
169 |
| - } |
170 |
| - ); |
171 |
| -} |
172 |
| - |
173 |
| -#[test] |
174 |
| -fn test_redact_all() { |
175 |
| - println!( |
176 |
| - "{:#?}", |
177 |
| - RedactAll { |
178 |
| - field: "Hello".to_string(), |
179 |
| - field2: "World".to_string(), |
180 |
| - field3: "!".to_string(), |
181 |
| - } |
182 |
| - ); |
183 |
| -} |
184 |
| - |
185 |
| -#[test] |
186 |
| -fn test_redact_all_with_flags() { |
187 |
| - println!( |
188 |
| - "{:#?}", |
189 |
| - RedactAllWithFlags { |
190 |
| - field: "Hello".to_string(), |
191 |
| - field2: "World".to_string(), |
192 |
| - field3: "!".to_string(), |
193 |
| - } |
194 |
| - ); |
195 |
| -} |
196 |
| - |
197 |
| -#[test] |
198 |
| -fn test_redact_tuple_struct() { |
199 |
| - println!("{:#?}", TupleStruct(100, 2000000)); |
200 |
| -} |
201 |
| - |
202 |
| -#[test] |
203 |
| -fn test_redact_multiple_attributes() { |
204 |
| - use rand_derive2::RandGen; |
205 |
| - use serde::{Deserialize, Serialize}; |
206 |
| - |
207 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
208 |
| - struct MultipleAttributes { |
209 |
| - #[redact] |
210 |
| - #[serde(default)] |
211 |
| - foo: bool, |
212 |
| - bar: bool, |
213 |
| - } |
214 |
| - |
215 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
216 |
| - #[serde(rename_all = "camelCase")] |
217 |
| - #[redact(all)] |
218 |
| - struct MultipleAttributesAll { |
219 |
| - #[serde(default)] |
220 |
| - #[redact(partial)] |
221 |
| - foo: bool, |
222 |
| - bar: bool, |
223 |
| - } |
224 |
| - |
225 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
226 |
| - struct MultipleAttributesTuple( |
227 |
| - #[redact] |
228 |
| - #[serde(default)] |
229 |
| - bool, |
230 |
| - bool, |
231 |
| - ); |
232 |
| - |
233 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
234 |
| - #[serde(rename_all = "camelCase")] |
235 |
| - #[redact(all)] |
236 |
| - struct MultipleAttributesAllTuple( |
237 |
| - #[serde(default)] |
238 |
| - #[redact(partial)] |
239 |
| - bool, |
240 |
| - bool, |
241 |
| - ); |
242 |
| - |
243 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
244 |
| - #[serde(rename_all = "camelCase")] |
245 |
| - enum MultipleAttributesEnum { |
246 |
| - #[serde(rename = "foo")] |
247 |
| - #[redact(variant)] |
248 |
| - Foo, |
249 |
| - |
250 |
| - #[serde(rename = "bar")] |
251 |
| - #[redact(all)] |
252 |
| - Bar { |
253 |
| - #[serde(default)] |
254 |
| - #[redact(partial)] |
255 |
| - foo: bool, |
256 |
| - bar: bool, |
257 |
| - }, |
258 |
| - |
259 |
| - #[redact(all)] |
260 |
| - #[serde(rename = "baz")] |
261 |
| - Baz( |
262 |
| - #[serde(default)] |
263 |
| - #[redact(partial)] |
264 |
| - bool, |
265 |
| - bool, |
266 |
| - ), |
267 |
| - } |
268 |
| - |
269 |
| - #[derive(Serialize, Deserialize, Redact, RandGen)] |
270 |
| - #[serde(rename_all = "camelCase")] |
271 |
| - #[redact(all, variant)] |
272 |
| - enum MultipleAttributesEnumAll { |
273 |
| - #[serde(rename = "foo")] |
274 |
| - Foo, |
275 |
| - |
276 |
| - #[serde(rename = "bar")] |
277 |
| - #[redact(all)] |
278 |
| - Bar { foo: bool, bar: bool }, |
279 |
| - |
280 |
| - #[redact(all)] |
281 |
| - #[serde(rename = "baz")] |
282 |
| - Baz(bool, bool), |
283 |
| - |
284 |
| - #[serde(rename = "qux")] |
285 |
| - #[redact(all)] |
286 |
| - Qux { |
287 |
| - #[serde(default)] |
288 |
| - #[redact(partial)] |
289 |
| - foo: bool, |
290 |
| - bar: bool, |
291 |
| - }, |
292 |
| - |
293 |
| - #[serde(rename = "quux")] |
294 |
| - #[redact(all)] |
295 |
| - Quux( |
296 |
| - #[serde(default)] |
297 |
| - #[redact(partial)] |
298 |
| - bool, |
299 |
| - bool, |
300 |
| - ), |
301 |
| - } |
302 |
| - |
303 |
| - let json = serde_json::json!({ |
304 |
| - "bar": true |
305 |
| - }); |
306 |
| - let attributes: MultipleAttributesAll = serde_json::from_value(json).unwrap(); |
307 |
| - assert!(!attributes.foo); |
308 |
| - assert!(attributes.bar); |
309 |
| - |
310 |
| - macro_rules! test_serde_attributes { |
311 |
| - {$($ty:ty),*} => { |
312 |
| - $({ |
313 |
| - let random = <$ty>::generate_random(); |
314 |
| - let json = serde_json::to_string(&random).unwrap(); |
315 |
| - serde_json::from_str::<$ty>(&json).unwrap(); |
316 |
| - })* |
317 |
| - }; |
318 |
| - } |
319 |
| - test_serde_attributes! { |
320 |
| - MultipleAttributes, |
321 |
| - MultipleAttributesAll, |
322 |
| - MultipleAttributesEnum, |
323 |
| - MultipleAttributesEnumAll, |
324 |
| - MultipleAttributesTuple, |
325 |
| - MultipleAttributesAllTuple |
326 |
| - } |
327 |
| -} |
0 commit comments