ax_tree_fuzzer_util.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_ACCESSIBILITY_AX_TREE_FUZZER_UTIL_H_
  5. #define UI_ACCESSIBILITY_AX_TREE_FUZZER_UTIL_H_
  6. #include "ui/accessibility/ax_tree.h"
  7. #include "ui/accessibility/ax_tree_data.h"
  8. #include "ui/accessibility/ax_tree_id.h"
  9. #include "ui/accessibility/ax_tree_update.h"
  10. #include "ui/accessibility/test_ax_tree_manager.h"
  11. // TODO(janewman): Replace usage with ...FuzzedDataProvider...
  12. class FuzzerData {
  13. public:
  14. FuzzerData(const unsigned char* data, size_t size);
  15. size_t RemainingBytes();
  16. unsigned char NextByte();
  17. const unsigned char* NextBytes(size_t amount);
  18. private:
  19. const unsigned char* data_;
  20. const size_t data_size_;
  21. size_t data_index_;
  22. };
  23. class AXTreeFuzzerGenerator {
  24. public:
  25. AXTreeFuzzerGenerator() = default;
  26. ~AXTreeFuzzerGenerator() = default;
  27. ui::AXTree* GetTree();
  28. void GenerateInitialUpdate(FuzzerData& fuzz_data, int node_count);
  29. bool GenerateTreeUpdate(FuzzerData& fuzz_data, size_t node_count);
  30. ui::AXNodeID GetMaxAssignedID() const;
  31. // This must be kept in sync with the minimum amount of data needed to create
  32. // any node. Any optional node data should check to ensure there is space.
  33. static constexpr size_t kMinimumNewNodeFuzzDataSize = 5;
  34. static constexpr size_t kMinTextFuzzDataSize = 10;
  35. static constexpr size_t kMaxTextFuzzDataSize = 200;
  36. // When creating a node, we allow for the next node to be a sibling of an
  37. // ancestor, this constant determines the maximum nodes we will pop when
  38. // building the tree.
  39. static constexpr size_t kMaxAncestorPopCount = 3;
  40. private:
  41. enum NextNodeRelationship {
  42. // Next node is a child of this node. (This node is a parent.)
  43. kChild,
  44. // Next node is sibling to this node. (This node is a leaf.)
  45. kSibling,
  46. // Next node is sibling to an ancestor. (This node is a leaf.)
  47. kSiblingToAncestor,
  48. };
  49. enum TreeUpdateOperation {
  50. kAddChild,
  51. kRemoveNode,
  52. kTextChange,
  53. kNoOperation
  54. };
  55. void RecursiveGenerateUpdate(const ui::AXNode* node,
  56. ui::AXTreeUpdate& tree_update,
  57. FuzzerData& fuzz_data,
  58. std::set<ui::AXNodeID>& updated_nodes);
  59. // TODO(janewman): Many of these can be made static.
  60. ui::AXNodeData CreateChildNodeData(ui::AXNodeData& parent,
  61. ui::AXNodeID new_node_id);
  62. NextNodeRelationship DetermineNextNodeRelationship(ax::mojom::Role role,
  63. unsigned char byte);
  64. TreeUpdateOperation DetermineTreeUpdateOperation(const ui::AXNode* node,
  65. unsigned char byte);
  66. void AddRoleSpecificProperties(FuzzerData& fuzz_data,
  67. ui::AXNodeData& node,
  68. const std::string& parentName,
  69. size_t extra_data_size);
  70. ax::mojom::Role GetInterestingRole(unsigned char byte,
  71. ax::mojom::Role parent_role);
  72. bool CanHaveChildren(ax::mojom::Role role);
  73. bool CanHaveText(ax::mojom::Role role);
  74. std::u16string GenerateInterestingText(const unsigned char* data,
  75. size_t size);
  76. ui::AXNodeID max_assigned_node_id_;
  77. ui::TestAXTreeManager tree_manager_;
  78. };
  79. #endif // UI_ACCESSIBILITY_AX_TREE_FUZZER_UTIL_H_