1 // Copyright 2015-2019 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
10 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 #[test]
test_input_from()16 fn test_input_from() { let _ = untrusted::Input::from(b"foo"); }
17 
18 #[test]
test_input_is_empty()19 fn test_input_is_empty() {
20     let input = untrusted::Input::from(b"");
21     assert!(input.is_empty());
22     let input = untrusted::Input::from(b"foo");
23     assert!(!input.is_empty());
24 }
25 
26 #[test]
test_input_len()27 fn test_input_len() {
28     let input = untrusted::Input::from(b"foo");
29     assert_eq!(input.len(), 3);
30 }
31 
32 #[test]
test_input_read_all()33 fn test_input_read_all() {
34     let input = untrusted::Input::from(b"foo");
35     let result = input.read_all(untrusted::EndOfInput, |input| {
36         assert_eq!(b'f', input.read_byte()?);
37         assert_eq!(b'o', input.read_byte()?);
38         assert_eq!(b'o', input.read_byte()?);
39         assert!(input.at_end());
40         Ok(())
41     });
42     assert_eq!(result, Ok(()));
43 }
44 
45 #[test]
test_input_read_all_unconsume()46 fn test_input_read_all_unconsume() {
47     let input = untrusted::Input::from(b"foo");
48     let result = input.read_all(untrusted::EndOfInput, |input| {
49         assert_eq!(b'f', input.read_byte()?);
50         assert!(!input.at_end());
51         Ok(())
52     });
53     assert_eq!(result, Err(untrusted::EndOfInput));
54 }
55 
56 #[test]
test_input_as_slice_less_safe()57 fn test_input_as_slice_less_safe() {
58     let slice = b"foo";
59     let input = untrusted::Input::from(slice);
60     assert_eq!(input.as_slice_less_safe(), slice);
61 }
62 
63 #[test]
using_reader_after_skip_and_get_error_returns_error_must_not_panic()64 fn using_reader_after_skip_and_get_error_returns_error_must_not_panic() {
65     let input = untrusted::Input::from(&[]);
66     let r = input.read_all(untrusted::EndOfInput, |input| {
67         let r = input.read_bytes(1);
68         assert_eq!(r, Err(untrusted::EndOfInput));
69         Ok(input.read_bytes_to_end())
70     });
71     let _ = r; // "Use" r. The value of `r` is undefined here.
72 }
73 
74 #[test]
size_assumptions()75 fn size_assumptions() {
76     // Assume that a pointer can address any point in the address space, and
77     // infer that this implies that a byte slice will never be
78     // `core::usize::MAX` bytes long.
79     assert_eq!(core::mem::size_of::<*const u8>(), core::mem::size_of::<usize>());
80 }
81 
82 #[test]
const_fn()83 fn const_fn() {
84     const _INPUT: untrusted::Input<'static> = untrusted::Input::from(&[]);
85 }
86 
87 #[test]
test_vec_into()88 fn test_vec_into() {
89     extern crate std;
90     let vec = vec![0u8; 0];
91     let _x: untrusted::Input = (&vec[..]).into();
92 }
93 
94 #[test]
test_from_slice()95 fn test_from_slice() {
96     let slice: &[u8] = &[0u8];
97     let _x: untrusted::Input = slice.into();
98 }