fuzzing.rs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. use dioxus::prelude::Props;
  2. use dioxus_core::*;
  3. use std::cell::Cell;
  4. fn random_ns() -> Option<&'static str> {
  5. let namespace = rand::random::<u8>() % 2;
  6. match namespace {
  7. 0 => None,
  8. 1 => Some(Box::leak(
  9. format!("ns{}", rand::random::<usize>()).into_boxed_str(),
  10. )),
  11. _ => unreachable!(),
  12. }
  13. }
  14. fn create_random_attribute(attr_idx: &mut usize) -> TemplateAttribute<'static> {
  15. match rand::random::<u8>() % 2 {
  16. 0 => TemplateAttribute::Static {
  17. name: Box::leak(format!("attr{}", rand::random::<usize>()).into_boxed_str()),
  18. value: Box::leak(format!("value{}", rand::random::<usize>()).into_boxed_str()),
  19. namespace: random_ns(),
  20. },
  21. 1 => TemplateAttribute::Dynamic {
  22. id: {
  23. let old_idx = *attr_idx;
  24. *attr_idx += 1;
  25. old_idx
  26. },
  27. },
  28. _ => unreachable!(),
  29. }
  30. }
  31. fn create_random_template_node(
  32. template_idx: &mut usize,
  33. attr_idx: &mut usize,
  34. depth: usize,
  35. ) -> TemplateNode<'static> {
  36. match rand::random::<u8>() % 3 {
  37. 0 => {
  38. let attrs = {
  39. let attrs: Vec<_> = (0..(rand::random::<usize>() % 10))
  40. .map(|_| create_random_attribute(attr_idx))
  41. .collect();
  42. Box::leak(attrs.into_boxed_slice())
  43. };
  44. TemplateNode::Element {
  45. tag: Box::leak(format!("tag{}", rand::random::<usize>()).into_boxed_str()),
  46. namespace: random_ns(),
  47. attrs,
  48. children: {
  49. if depth > 10 {
  50. &[]
  51. } else {
  52. let children: Vec<_> = (0..(rand::random::<usize>() % 3))
  53. .map(|_| create_random_template_node(template_idx, attr_idx, depth + 1))
  54. .collect();
  55. Box::leak(children.into_boxed_slice())
  56. }
  57. },
  58. }
  59. }
  60. 1 => TemplateNode::Text {
  61. text: Box::leak(format!("{}", rand::random::<usize>()).into_boxed_str()),
  62. },
  63. 2 => TemplateNode::DynamicText {
  64. id: {
  65. let old_idx = *template_idx;
  66. *template_idx += 1;
  67. old_idx
  68. },
  69. },
  70. 2 => TemplateNode::Dynamic {
  71. id: {
  72. let old_idx = *template_idx;
  73. *template_idx += 1;
  74. old_idx
  75. },
  76. },
  77. _ => unreachable!(),
  78. }
  79. }
  80. fn generate_paths(
  81. node: &TemplateNode<'static>,
  82. current_path: &Vec<u8>,
  83. node_paths: &mut Vec<Vec<u8>>,
  84. attr_paths: &mut Vec<Vec<u8>>,
  85. ) {
  86. match node {
  87. TemplateNode::Element { children, attrs, .. } => {
  88. for attr in *attrs {
  89. match attr {
  90. TemplateAttribute::Static { .. } => {}
  91. TemplateAttribute::Dynamic { .. } => {
  92. attr_paths.push(current_path.clone());
  93. }
  94. }
  95. }
  96. for (i, child) in children.iter().enumerate() {
  97. let mut current_path = current_path.clone();
  98. current_path.push(i as u8);
  99. generate_paths(child, &current_path, node_paths, attr_paths);
  100. }
  101. }
  102. TemplateNode::Text { .. } => {}
  103. TemplateNode::DynamicText { .. } => {
  104. node_paths.push(current_path.clone());
  105. }
  106. TemplateNode::Dynamic { .. } => {
  107. node_paths.push(current_path.clone());
  108. }
  109. }
  110. }
  111. fn create_random_template(name: &'static str) -> Template<'static> {
  112. let mut template_idx = 0;
  113. let mut attr_idx = 0;
  114. let roots = (0..(1 + rand::random::<usize>() % 5))
  115. .map(|_| create_random_template_node(&mut template_idx, &mut attr_idx, 0))
  116. .collect::<Vec<_>>();
  117. assert!(roots.len() > 0);
  118. let roots = Box::leak(roots.into_boxed_slice());
  119. let mut node_paths = Vec::new();
  120. let mut attr_paths = Vec::new();
  121. for (i, root) in roots.iter().enumerate() {
  122. generate_paths(root, &vec![i as u8], &mut node_paths, &mut attr_paths);
  123. }
  124. let node_paths = Box::leak(
  125. node_paths
  126. .into_iter()
  127. .map(|v| &*Box::leak(v.into_boxed_slice()))
  128. .collect::<Vec<_>>()
  129. .into_boxed_slice(),
  130. );
  131. let attr_paths = Box::leak(
  132. attr_paths
  133. .into_iter()
  134. .map(|v| &*Box::leak(v.into_boxed_slice()))
  135. .collect::<Vec<_>>()
  136. .into_boxed_slice(),
  137. );
  138. Template { name, roots, node_paths, attr_paths }
  139. }
  140. fn create_random_dynamic_node<'a>(cx: &'a ScopeState, depth: usize) -> DynamicNode<'a> {
  141. let range = if depth > 10 { 2 } else { 4 };
  142. match rand::random::<u8>() % range {
  143. 0 => DynamicNode::Text(VText {
  144. value: Box::leak(format!("{}", rand::random::<usize>()).into_boxed_str()),
  145. id: Default::default(),
  146. }),
  147. 1 => DynamicNode::Placeholder(Default::default()),
  148. 2 => cx.make_node((0..(rand::random::<u8>() % 5)).map(|_| VNode {
  149. key: None,
  150. parent: Default::default(),
  151. template: Cell::new(Template {
  152. name: concat!(file!(), ":", line!(), ":", column!(), ":0"),
  153. roots: &[TemplateNode::Dynamic { id: 0 }],
  154. node_paths: &[&[0]],
  155. attr_paths: &[],
  156. }),
  157. root_ids: Default::default(),
  158. dynamic_nodes: cx.bump().alloc([cx.component(
  159. create_random_element,
  160. DepthProps { depth: depth },
  161. "create_random_element",
  162. )]),
  163. dynamic_attrs: &[],
  164. })),
  165. 3 => cx.component(
  166. create_random_element,
  167. DepthProps { depth: depth },
  168. "create_random_element",
  169. ),
  170. _ => unreachable!(),
  171. }
  172. }
  173. fn create_random_dynamic_attr<'a>(cx: &'a ScopeState) -> Attribute<'a> {
  174. let value = match rand::random::<u8>() % 6 {
  175. 0 => AttributeValue::Text(Box::leak(
  176. format!("{}", rand::random::<usize>()).into_boxed_str(),
  177. )),
  178. 1 => AttributeValue::Float(rand::random()),
  179. 2 => AttributeValue::Int(rand::random()),
  180. 3 => AttributeValue::Bool(rand::random()),
  181. 4 => cx.any_value(rand::random::<usize>()),
  182. 5 => AttributeValue::None,
  183. // Listener(RefCell<Option<ListenerCb<'a>>>),
  184. _ => unreachable!(),
  185. };
  186. Attribute {
  187. name: Box::leak(format!("attr{}", rand::random::<usize>()).into_boxed_str()),
  188. value,
  189. namespace: random_ns(),
  190. mounted_element: Default::default(),
  191. volatile: rand::random(),
  192. }
  193. }
  194. #[test]
  195. fn debugging() {
  196. let template = create_random_template("test");
  197. println!("{:#?}", template)
  198. }
  199. static mut TEMPLATE_COUNT: usize = 0;
  200. #[derive(PartialEq, Props)]
  201. struct DepthProps {
  202. depth: usize,
  203. }
  204. fn create_random_element<'a>(cx: Scope<'a, DepthProps>) -> Element<'a> {
  205. cx.needs_update();
  206. match rand::random::<usize>() % 3 {
  207. 0 | 1 => {
  208. // This is dynamicly created every render to trigger the pointer equality check on the template path
  209. // In the future this check should be changed to something more obvious
  210. let template = create_random_template(Box::leak(
  211. format!(
  212. "{}{}",
  213. concat!(file!(), ":", line!(), ":", column!(), ":"),
  214. {
  215. unsafe {
  216. let old = TEMPLATE_COUNT;
  217. TEMPLATE_COUNT += 1;
  218. old
  219. }
  220. }
  221. )
  222. .into_boxed_str(),
  223. ));
  224. println!("{:#?}", template);
  225. let node = VNode {
  226. key: None,
  227. parent: None,
  228. template: Cell::new(template),
  229. root_ids: Default::default(),
  230. dynamic_nodes: {
  231. let dynamic_nodes: Vec<_> = (0..template.node_paths.len())
  232. .map(|_| create_random_dynamic_node(cx, cx.props.depth + 1))
  233. .collect();
  234. cx.bump().alloc(dynamic_nodes)
  235. },
  236. dynamic_attrs: cx.bump().alloc(
  237. (0..template.attr_paths.len())
  238. .map(|_| create_random_dynamic_attr(cx))
  239. .collect::<Vec<_>>(),
  240. ),
  241. };
  242. println!("{:#?}", node);
  243. Some(node)
  244. }
  245. _ => None,
  246. }
  247. }
  248. // test for panics when creating random nodes and templates
  249. #[test]
  250. fn create() {
  251. for _ in 0..1000 {
  252. let mut vdom = VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0 });
  253. vdom.rebuild();
  254. }
  255. }
  256. // test for panics when diffing random nodes
  257. // This test will change the template every render which is not very realistic, but it helps stress the system
  258. #[test]
  259. fn diff() {
  260. for _ in 0..100 {
  261. let mut vdom = VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0 });
  262. vdom.rebuild();
  263. for _ in 0..10 {
  264. vdom.render_immediate();
  265. }
  266. }
  267. }