tests.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // This Source Code Form is subject to the terms of the Mozilla Public License,
  2. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  3. // obtain one at https://mozilla.org/MPL/2.0/.
  4. extern crate std;
  5. use crate::*;
  6. static TEST: &[u8] = include_bytes!("../test.dtb");
  7. #[test]
  8. fn returns_fdt() {
  9. assert!(Fdt::new(TEST).is_ok());
  10. }
  11. #[test]
  12. fn finds_root_node() {
  13. let fdt = Fdt::new(TEST).unwrap();
  14. assert!(fdt.find_node("/").is_some(), "couldn't find root node");
  15. }
  16. #[test]
  17. fn finds_root_node_properties() {
  18. let fdt = Fdt::new(TEST).unwrap();
  19. let prop = fdt
  20. .find_node("/")
  21. .unwrap()
  22. .properties()
  23. .any(|p| p.name == "compatible" && p.value == b"riscv-virtio\0");
  24. assert!(prop);
  25. }
  26. #[test]
  27. fn finds_child_of_root_node() {
  28. let fdt = Fdt::new(TEST).unwrap();
  29. assert!(fdt.find_node("/cpus").is_some(), "couldn't find cpus node");
  30. }
  31. #[test]
  32. fn correct_flash_regions() {
  33. let fdt = Fdt::new(TEST).unwrap();
  34. let regions = fdt.find_node("/soc/flash").unwrap().reg().unwrap().collect::<std::vec::Vec<_>>();
  35. assert_eq!(
  36. regions,
  37. &[
  38. MemoryRegion { starting_address: 0x20000000 as *const u8, size: Some(0x2000000) },
  39. MemoryRegion { starting_address: 0x22000000 as *const u8, size: Some(0x2000000) }
  40. ]
  41. );
  42. }
  43. #[test]
  44. fn finds_with_addr() {
  45. let fdt = Fdt::new(TEST).unwrap();
  46. assert_eq!(fdt.find_node("/soc/virtio_mmio@10004000").unwrap().name, "virtio_mmio@10004000");
  47. }
  48. #[test]
  49. fn compatibles() {
  50. let fdt = Fdt::new(TEST).unwrap();
  51. let res = fdt
  52. .find_node("/soc/test")
  53. .unwrap()
  54. .compatible()
  55. .unwrap()
  56. .all()
  57. .all(|s| ["sifive,test1", "sifive,test0", "syscon"].contains(&s));
  58. assert!(res);
  59. }
  60. #[test]
  61. fn parent_cell_sizes() {
  62. let fdt = Fdt::new(TEST).unwrap();
  63. let regions = fdt.find_node("/memory").unwrap().reg().unwrap().collect::<std::vec::Vec<_>>();
  64. assert_eq!(
  65. regions,
  66. &[MemoryRegion { starting_address: 0x80000000 as *const u8, size: Some(0x20000000) }]
  67. );
  68. }
  69. #[test]
  70. fn no_properties() {
  71. let fdt = Fdt::new(TEST).unwrap();
  72. let regions = fdt.find_node("/emptyproptest").unwrap();
  73. assert_eq!(regions.properties().count(), 0);
  74. }
  75. #[test]
  76. fn finds_all_nodes() {
  77. let fdt = Fdt::new(TEST).unwrap();
  78. let mut all_nodes: std::vec::Vec<_> = fdt.all_nodes().map(|n| n.name).collect();
  79. all_nodes.sort_unstable();
  80. assert_eq!(
  81. all_nodes,
  82. &[
  83. "/",
  84. "chosen",
  85. "clint@2000000",
  86. "cluster0",
  87. "core0",
  88. "cpu-map",
  89. "cpu@0",
  90. "cpus",
  91. "emptyproptest",
  92. "flash@20000000",
  93. "interrupt-controller",
  94. "memory@80000000",
  95. "pci@30000000",
  96. "plic@c000000",
  97. "poweroff",
  98. "reboot",
  99. "rtc@101000",
  100. "soc",
  101. "test@100000",
  102. "uart@10000000",
  103. "virtio_mmio@10001000",
  104. "virtio_mmio@10002000",
  105. "virtio_mmio@10003000",
  106. "virtio_mmio@10004000",
  107. "virtio_mmio@10005000",
  108. "virtio_mmio@10006000",
  109. "virtio_mmio@10007000",
  110. "virtio_mmio@10008000"
  111. ]
  112. )
  113. }
  114. #[test]
  115. fn required_nodes() {
  116. let fdt = Fdt::new(TEST).unwrap();
  117. fdt.cpus().next().unwrap();
  118. fdt.memory();
  119. fdt.chosen();
  120. }
  121. #[test]
  122. fn doesnt_exist() {
  123. let fdt = Fdt::new(TEST).unwrap();
  124. assert!(fdt.find_node("/this/doesnt/exist").is_none());
  125. }