dcaf 0.4.0

An implementation of the ACE-OAuth framework
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
/*
 * Copyright (c) 2022 The NAMIB Project Developers.
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 *
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

/// Tests for text encoded scopes.
mod text {
    #[cfg(not(feature = "std"))]
    use alloc::{string::ToString, vec, vec::Vec};
    use core::marker::PhantomData;

    use ciborium::value::Value;

    use crate::common::cbor_map::decode_scope;
    use crate::common::scope::{AifRestMethodSet, TextEncodedScope};
    use crate::error::{InvalidTextEncodedScopeError, ScopeFromValueError};
    use crate::{AifEncodedScope, Scope};

    #[test]
    fn test_scope_element_normal() -> Result<(), InvalidTextEncodedScopeError> {
        let simple = TextEncodedScope::try_from("this is a test")?;
        assert!(simple.elements().eq(vec!["this", "is", "a", "test"]));

        let single = TextEncodedScope::try_from("single")?;
        assert!(single.elements().eq(vec!["single"]));

        let third = TextEncodedScope::try_from("another quick test")?;
        assert!(third.elements().eq(vec!["another", "quick", "test"]));

        let array = TextEncodedScope::try_from(vec!["array", "test"])?;
        assert!(array.elements().eq(vec!["array", "test"]));

        let array_single = TextEncodedScope::try_from(vec!["justme"])?;
        assert!(array_single.elements().eq(vec!["justme"]));
        Ok(())
    }

    #[test]
    fn test_scope_elements_empty() {
        let empty_inputs: Vec<&str> = vec!["    ", " ", ""];

        for input in empty_inputs {
            assert!(TextEncodedScope::try_from(input).is_err());
        }

        let empty_arrays: Vec<Vec<&str>> = vec![
            vec![],
            vec![""],
            vec![" "],
            vec!["   "],
            vec!["", ""],
            vec!["", " "],
            vec!["", "   "],
            vec![" ", " "],
            vec![" ", ""],
            vec![" ", "   "],
            vec!["   ", "   "],
            vec!["   ", " "],
            vec!["   ", ""],
        ];

        for input in empty_arrays {
            assert!(!TextEncodedScope::try_from(input)
                .expect_err("must be error")
                .to_string()
                .is_empty());
        }
    }

    #[test]
    fn test_scope_elements_invalid_spaces() {
        let invalid_inputs = vec![
            "space at the end ",
            "spaces at the end   ",
            " space at the start",
            "   spaces at the start",
            " spaces at both ends ",
            "   spaces at both ends    ",
            "spaces   in the       middle",
            "   spaces   wherever  you    look   ",
        ];
        for input in invalid_inputs {
            assert!(!TextEncodedScope::try_from(input)
                .expect_err("must be error")
                .to_string()
                .is_empty());
        }
    }

    #[test]
    fn test_scope_elements_invalid_characters() {
        let invalid_inputs = vec![
            "\"",
            "\\",
            "a \" in between",
            "a \\ in between",
            " \" ",
            " \\ ",
            "within\"word",
            "within\\word",
        ];
        for input in invalid_inputs {
            assert!(!TextEncodedScope::try_from(input)
                .expect_err("must be error")
                .to_string()
                .is_empty());
        }

        let invalid_arrays = vec![
            vec!["space within"],
            vec!["more spaces within"],
            vec!["normal", "array", "but space"],
            vec!["normal", "but space", "array"],
            vec!["but space", "normal", "array"],
            vec!["\""],
            vec!["\\"],
            vec!["\"\\"],
            vec!["\" \\"],
            vec!["\\ \\"],
            vec!["\" \""],
            vec!["\\", "\\"],
            vec!["\"", "\""],
            vec!["\\", "\""],
            vec!["\"", "\\"],
            vec!["normal", "\\", "almost"],
            vec!["normal", "\"", "allowed"],
            vec!["normal", "in\"word\""],
            vec!["normal", "in\\word"],
        ];
        for input in invalid_arrays {
            assert!(!TextEncodedScope::try_from(input)
                .expect_err("must be error")
                .to_string()
                .is_empty());
        }
    }

    #[test]
    fn test_scope_value() -> Result<(), ScopeFromValueError> {
        let scope = Scope::try_from(vec!["one", "two", "three"])?;
        let value = Value::from(scope.clone());
        assert!(value.is_text());
        assert_eq!(value.as_text().expect("not a text"), "one two three");
        assert_eq!(Scope::try_from(value)?, scope);
        Ok(())
    }

    #[test]
    fn test_convert_invalid_value_to_scope() {
        assert!(decode_scope(Value::Bool(false)).is_err());
    }

    #[test]
    fn test_scope_invalid_try_from() {
        let scope = Scope::from(AifEncodedScope::from(vec![(
            "empty".to_string(),
            AifRestMethodSet::empty(),
        )]));
        let error = TextEncodedScope::try_from(scope).expect_err("expected error");
        assert_eq!(error.actual_type, "AifEncoded");
        assert_eq!(error.expected_type, "TextEncoded");
        assert_eq!(error.general_type, PhantomData::<Scope>::default());
    }
}

mod aif {
    use core::marker::PhantomData;

    use ciborium::de::from_reader;
    use ciborium::ser::into_writer;
    use enumflags2::{make_bitflags, BitFlags};

    #[cfg(not(feature = "std"))]
    use {
        alloc::string::{String, ToString},
        alloc::vec,
        alloc::vec::Vec,
    };

    use crate::common::scope::{AifEncodedScopeElement, AifRestMethod, AifRestMethodSet};
    use crate::error::InvalidAifEncodedScopeError;
    use crate::{AifEncodedScope, LibdcafEncodedScope, Scope};

    pub(crate) fn example_elements() -> (
        AifEncodedScopeElement,
        AifEncodedScopeElement,
        AifEncodedScopeElement,
        AifEncodedScopeElement,
    ) {
        let restricted = AifEncodedScopeElement::new("restricted".to_string(), AifRestMethod::Get);
        let dynamic = AifEncodedScopeElement::new(
            "dynamic".to_string(),
            make_bitflags!(AifRestMethod::{DynamicGet | DynamicFetch}),
        );
        let all = AifEncodedScopeElement::new("all".to_string(), BitFlags::all());
        let none = AifEncodedScopeElement::new("none".to_string(), BitFlags::empty());
        (restricted, dynamic, all, none)
    }

    #[test]
    fn test_aif_rest_method_serialize() {
        for method in AifRestMethodSet::all() {
            let mut serialized = Vec::<u8>::new();
            assert!(into_writer(&method, &mut serialized).is_ok());
            assert_eq!(
                method,
                from_reader::<AifRestMethod, &[u8]>(serialized.as_slice())
                    .expect("couldn't deserialize")
            );
        }
    }

    #[test]
    fn test_scope_elements_normal() {
        let (restricted, dynamic, all, none) = example_elements();
        let single = AifEncodedScope::new(vec![restricted.clone()]);
        assert_eq!(single.elements(), &vec![restricted]);

        let multiple = AifEncodedScope::new(vec![dynamic.clone(), all.clone()]);
        assert_eq!(multiple.elements(), &vec![dynamic.clone(), all.clone()]);

        let single_arr = AifEncodedScope::from(vec![("none".to_string(), BitFlags::empty())]);
        assert_eq!(single_arr.elements(), &vec![none]);

        let multi_arr = AifEncodedScope::from(vec![
            (
                "dynamic".to_string(),
                make_bitflags!(AifRestMethod::{DynamicGet | DynamicFetch}),
            ),
            ("all".to_string(), BitFlags::all()),
        ]);
        assert_eq!(multi_arr.to_elements(), vec![dynamic, all]);
    }

    #[test]
    fn test_scope_elements_valid() -> Result<(), InvalidAifEncodedScopeError> {
        let (restricted, dynamic, all, none) = example_elements();
        let multi = AifEncodedScope::try_from(vec![
            ("restricted".to_string(), u64::pow(2, 0)),
            ("all".to_string(), BitFlags::<AifRestMethod>::all().bits()),
            ("none".to_string(), 0),
        ])?;
        assert_eq!(multi.to_elements(), vec![restricted, all, none]);

        let single = AifEncodedScope::try_from(vec![(
            "dynamic".to_string(),
            u64::pow(2, 32) + u64::pow(2, 36),
        )])?;
        assert_eq!(single.to_elements(), vec![dynamic]);
        Ok(())
    }

    #[test]
    fn test_scope_elements_invalid() {
        // String part can't be invalid, so we ignore it here
        let invalids = vec![
            u64::pow(2, 7),
            u64::pow(2, 31),
            u64::pow(2, 39),
            u64::pow(2, 63),
            0xFFFF_FFFF_FFFF_FFFF, // maximum
        ];
        for invalid in invalids {
            assert!(
                !AifEncodedScope::try_from(vec![("whatever".to_string(), invalid)])
                    .expect_err("must be error")
                    .to_string()
                    .is_empty()
            );
        }
    }

    #[test]
    fn test_scope_elements_empty() -> Result<(), String> {
        // Note: Spec doesn't seem to mention anything about emptiness, so we assume it's allowed.
        assert!(AifEncodedScope::try_from(Vec::<(String, u64)>::new()).is_ok());
        let empty = AifEncodedScope::from(Vec::<(String, BitFlags<AifRestMethod>)>::new());
        assert_eq!(empty.elements(), &vec![]);
        let mut serialized = Vec::<u8>::new();
        into_writer(&empty, &mut serialized).map_err(|x| x.to_string())?;
        assert_eq!(&serialized, &Vec::from([0x80]));
        assert_eq!(
            from_reader::<Scope, &[u8]>(serialized.as_slice()).map_err(|x| x.to_string())?,
            Scope::from(empty)
        );
        Ok(())
    }

    #[test]
    fn test_scope_encoding() -> Result<(), String> {
        // This tests the encoding of the scope using the example given in Figure 5 of the AIF RFC.
        let cbor = hex::decode("8382672F732F74656D700182662F612F6C65640582652F64746C7302")
            .map_err(|x| x.to_string())?;
        let expected: Scope = AifEncodedScope::from(vec![
            ("/s/temp", make_bitflags!(AifRestMethod::{Get})),
            ("/a/led", make_bitflags!(AifRestMethod::{Put | Get})),
            ("/dtls", make_bitflags!(AifRestMethod::{Post})),
        ])
        .into();
        assert_eq!(
            expected,
            from_reader::<Scope, &[u8]>(cbor.as_slice()).map_err(|x| x.to_string())?
        );
        Ok(())
    }

    #[test]
    fn test_scope_invalid_try_from() {
        let scope = Scope::from(LibdcafEncodedScope::new("empty", AifRestMethodSet::empty()));
        let error = AifEncodedScope::try_from(scope).expect_err("expected error");
        assert_eq!(error.actual_type, "LibdcafEncoded");
        assert_eq!(error.expected_type, "AifEncoded");
        assert_eq!(error.general_type, PhantomData::<Scope>::default());
    }
}

mod libdcaf {
    use core::marker::PhantomData;

    use ciborium::de::from_reader;

    #[cfg(not(feature = "std"))]
    use {alloc::string::ToString, alloc::vec};

    use crate::error::{InvalidAifEncodedScopeError, ScopeFromValueError};
    use crate::{LibdcafEncodedScope, Scope};

    use super::aif::example_elements;

    #[test]
    fn test_scope_elements_normal() {
        let (restricted, dynamic, all, none) = example_elements();

        for element in vec![restricted, dynamic, all, none] {
            let scope = LibdcafEncodedScope::from_element(element.clone());
            assert_eq!(scope.elements(), vec![&element]);
        }
    }

    #[test]
    fn test_scope_element_valid() -> Result<(), InvalidAifEncodedScopeError> {
        let (restricted, _, _, _) = example_elements();
        let scope = LibdcafEncodedScope::try_from_bits("restricted".to_string(), u64::pow(2, 0))?;
        assert_eq!(scope.to_elements(), vec![restricted]);
        Ok(())
    }

    #[test]
    fn test_scope_element_invalid() {
        // String part can't be invalid, so we ignore it here
        let invalids = vec![
            u64::pow(2, 7),
            u64::pow(2, 31),
            u64::pow(2, 39),
            u64::pow(2, 63),
            0xFFFF_FFFF_FFFF_FFFF, // maximum
        ];
        for invalid in invalids {
            assert!(
                !LibdcafEncodedScope::try_from_bits("whatever".to_string(), invalid)
                    .expect_err("must be error")
                    .to_string()
                    .is_empty()
            );
        }
    }

    #[test]
    fn test_scope_element_empty() {
        // Emptiness isn't allowed here.
        let serialized = vec![0x80]; // empty CBOR array
                                     // That means that this *must not* resolve to a libdcaf scope
        assert!(from_reader::<Scope, &[u8]>(serialized.as_slice())
            .ok()
            .map(LibdcafEncodedScope::try_from)
            .and_then(Result::ok)
            .is_none());
    }

    #[test]
    fn test_scope_invalid_try_from() -> Result<(), ScopeFromValueError> {
        let scope = Scope::try_from(vec![0xDC, 0xAF].as_slice())?;
        let error = LibdcafEncodedScope::try_from(scope).expect_err("expected error");
        assert_eq!(error.actual_type, "BinaryEncoded");
        assert_eq!(error.expected_type, "LibdcafEncoded");
        assert_eq!(error.general_type, PhantomData::<Scope>::default());
        Ok(())
    }
}

/// Tests for binary encoded scopes.
mod binary {
    #[cfg(not(feature = "std"))]
    use alloc::{boxed::Box, string::ToString, vec};
    use core::marker::PhantomData;

    use ciborium::value::{Integer, Value};

    use crate::common::scope::BinaryEncodedScope;
    use crate::error::{InvalidBinaryEncodedScopeError, ScopeFromValueError};
    use crate::Scope;

    #[test]
    fn test_scope_elements_normal() -> Result<(), InvalidBinaryEncodedScopeError> {
        let single = BinaryEncodedScope::try_from(vec![0].as_slice())?;
        assert!(single.elements(Some(0x20))?.eq(&vec![vec![0]]));

        let simple1 = BinaryEncodedScope::try_from(vec![0, 1, 2].as_slice())?;
        assert!(simple1.elements(Some(0x20))?.eq(&vec![vec![0, 1, 2]]));
        assert!(simple1.elements(Some(1))?.eq(&vec![vec![0], vec![2]]));

        let simple2 = BinaryEncodedScope::try_from(vec![0xDC, 0x20, 0xAF].as_slice())?;
        assert!(simple2
            .elements(Some(0x20))?
            .eq(&vec![vec![0xDC], vec![0xAF]]));
        assert!(simple2.elements(Some(0))?.eq(&vec![vec![0xDC, 0x20, 0xAF]]));

        let simple3 = BinaryEncodedScope::try_from(
            vec![0xDE, 0xAD, 0xBE, 0xEF, 0, 0xDC, 0xAF, 0, 1].as_slice(),
        )?;
        assert!(simple3.elements(Some(0))?.eq(&vec![
            vec![0xDE, 0xAD, 0xBE, 0xEF],
            vec![0xDC, 0xAF],
            vec![1],
        ]));
        assert!(simple3
            .elements(Some(0xEF))?
            .eq(&vec![vec![0xDE, 0xAD, 0xBE], vec![0, 0xDC, 0xAF, 0, 1]]));
        assert!(simple3
            .elements(Some(2))?
            .eq(&vec![vec![0xDE, 0xAD, 0xBE, 0xEF, 0, 0xDC, 0xAF, 0, 1]]));
        Ok(())
    }

    #[test]
    fn test_scope_elements_empty() -> Result<(), InvalidBinaryEncodedScopeError> {
        assert!(BinaryEncodedScope::try_from(vec![].as_slice()).is_err());
        // Assuming 0 is separator
        let empty_vecs = vec![vec![0], vec![0, 0], vec![0, 0, 0]];
        for vec in empty_vecs {
            assert!(!BinaryEncodedScope::try_from(vec.as_slice())?
                .elements(Some(0))
                .expect_err("must be error")
                .to_string()
                .is_empty());
            // If the separator is something else, the result should just contain the vec
            // as a single element.
            assert!(BinaryEncodedScope::try_from(vec.as_slice())?
                .elements(Some(1))?
                .eq(&vec![vec]));
        }
        Ok(())
    }

    #[test]
    fn test_scope_elements_invalid_separators() -> Result<(), InvalidBinaryEncodedScopeError> {
        // Assuming 0 is separator
        let invalid = vec![
            vec![0xDC, 0xAF, 0],
            vec![0xDC, 0xAF, 0, 0],
            vec![0, 0xDC, 0xAF],
            vec![0, 0, 0xDC, 0xAF],
            vec![0, 0xDC, 0xAF, 0],
            vec![0, 0, 0xDC, 0xAF, 0, 0],
            vec![0, 0, 0xDC, 0xAF, 0, 0],
            vec![0xDC, 0, 0, 0xAF],
            vec![0, 0xDC, 0, 0xAF, 0],
            vec![0, 0, 0xDC, 0, 0xAF, 0],
            vec![0, 0xDC, 0, 0, 0xAF, 0],
            vec![0, 0xDC, 0, 0xAF, 0, 0],
            vec![0, 0, 0xDC, 0, 0, 0xAF, 0, 0],
        ];
        for vec in invalid {
            assert!(!BinaryEncodedScope::try_from(vec.as_slice())?
                .elements(Some(0))
                .expect_err("must be error")
                .to_string()
                .is_empty());
            // If the separator is something else, the result should just contain the vec
            // as a single element.
            assert!(BinaryEncodedScope::try_from(vec.as_slice())?
                .elements(Some(1))?
                .eq(&vec![vec]));
        }
        Ok(())
    }

    #[test]
    fn test_scope_value() -> Result<(), ScopeFromValueError> {
        let scope = Scope::try_from(vec![0xDC, 0xAF].as_slice())?;
        let value = Value::from(scope.clone());
        assert!(value.is_bytes());
        assert_eq!(
            value.as_bytes().expect("not bytes"),
            vec![0xDC, 0xAF].as_slice()
        );
        assert_eq!(Scope::try_from(value)?, scope);
        Ok(())
    }

    #[test]
    fn test_scope_invalid_try_from() -> Result<(), ScopeFromValueError> {
        let scope = Scope::try_from(vec!["first", "second"])?;
        let error = BinaryEncodedScope::try_from(scope).expect_err("expected error");
        assert_eq!(error.actual_type, "TextEncoded");
        assert_eq!(error.expected_type, "BinaryEncoded");
        assert_eq!(error.general_type, PhantomData::<Scope>::default());

        let invalid_values = vec![
            Value::Bool(true),
            Value::Integer(Integer::from(u8::MIN)),
            Value::Float(0.5),
            Value::Null,
            Value::Tag(0, Box::new(Value::Null)),
            Value::Map(vec![]),
        ];
        for value in invalid_values {
            assert!(!Scope::try_from(value)
                .expect_err("must be error")
                .to_string()
                .is_empty());
        }
        Ok(())
    }
}