tests.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #[test]
  15. fn test_input_from() { let _ = untrusted::Input::from(b"foo"); }
  16. #[test]
  17. fn test_input_is_empty() {
  18. let input = untrusted::Input::from(b"");
  19. assert!(input.is_empty());
  20. let input = untrusted::Input::from(b"foo");
  21. assert!(!input.is_empty());
  22. }
  23. #[test]
  24. fn test_input_len() {
  25. let input = untrusted::Input::from(b"foo");
  26. assert_eq!(input.len(), 3);
  27. }
  28. #[test]
  29. fn test_input_read_all() {
  30. let input = untrusted::Input::from(b"foo");
  31. let result = input.read_all(untrusted::EndOfInput, |input| {
  32. assert_eq!(b'f', input.read_byte()?);
  33. assert_eq!(b'o', input.read_byte()?);
  34. assert_eq!(b'o', input.read_byte()?);
  35. assert!(input.at_end());
  36. Ok(())
  37. });
  38. assert_eq!(result, Ok(()));
  39. }
  40. #[test]
  41. fn test_input_read_all_unconsume() {
  42. let input = untrusted::Input::from(b"foo");
  43. let result = input.read_all(untrusted::EndOfInput, |input| {
  44. assert_eq!(b'f', input.read_byte()?);
  45. assert!(!input.at_end());
  46. Ok(())
  47. });
  48. assert_eq!(result, Err(untrusted::EndOfInput));
  49. }
  50. #[test]
  51. fn test_input_as_slice_less_safe() {
  52. let slice = b"foo";
  53. let input = untrusted::Input::from(slice);
  54. assert_eq!(input.as_slice_less_safe(), slice);
  55. }
  56. #[test]
  57. fn using_reader_after_skip_and_get_error_returns_error_must_not_panic() {
  58. let input = untrusted::Input::from(&[]);
  59. let r = input.read_all(untrusted::EndOfInput, |input| {
  60. let r = input.read_bytes(1);
  61. assert_eq!(r, Err(untrusted::EndOfInput));
  62. Ok(input.read_bytes_to_end())
  63. });
  64. let _ = r; // "Use" r. The value of `r` is undefined here.
  65. }
  66. #[test]
  67. fn size_assumptions() {
  68. // Assume that a pointer can address any point in the address space, and
  69. // infer that this implies that a byte slice will never be
  70. // `core::usize::MAX` bytes long.
  71. assert_eq!(core::mem::size_of::<*const u8>(), core::mem::size_of::<usize>());
  72. }
  73. #[test]
  74. fn const_fn() {
  75. const _INPUT: untrusted::Input<'static> = untrusted::Input::from(&[]);
  76. }
  77. #[test]
  78. fn test_vec_into() {
  79. extern crate std;
  80. let vec = vec![0u8; 0];
  81. let _x: untrusted::Input = (&vec[..]).into();
  82. }
  83. #[test]
  84. fn test_from_slice() {
  85. let slice: &[u8] = &[0u8];
  86. let _x: untrusted::Input = slice.into();
  87. }