_testing.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for testing purposes. Not used in real images.
  6. #
  7. from collections import OrderedDict
  8. from binman.entry import Entry, EntryArg
  9. from dtoc import fdt_util
  10. from patman import tools
  11. class Entry__testing(Entry):
  12. """A fake entry used for testing
  13. This entry should not be used in normal images. It is a special entry with
  14. strange features used for testing.
  15. Properties / Entry arguments
  16. test-str-fdt: Test string, normally in the node
  17. test-int-fdt: Test integer, normally in the node
  18. test-str-arg: Test string, normally in the entry arguments
  19. test-int-arg: Test integer, normally in the entry arguments
  20. The entry has a single 'a' byte as its contents. Operation is controlled by
  21. a number of properties in the node, as follows:
  22. Properties:
  23. return-invalid-entry: Return an invalid entry from GetOffsets()
  24. return-unknown-contents: Refuse to provide any contents (to cause a
  25. failure)
  26. bad-update-contents: Return a larger size in ProcessContents
  27. bad-shrink-contents: Return a larger size in ProcessContents
  28. never-complete-process-fdt: Refund to process the FDT (to cause a
  29. failure)
  30. require-args: Require that all used args are present (generating an
  31. error if not)
  32. force-bad-datatype: Force a call to GetEntryArgsOrProps() with a bad
  33. data type (generating an error)
  34. """
  35. def __init__(self, section, etype, node):
  36. super().__init__(section, etype, node)
  37. def ReadNode(self):
  38. super().ReadNode()
  39. self.return_invalid_entry = fdt_util.GetBool(self._node,
  40. 'return-invalid-entry')
  41. self.return_unknown_contents = fdt_util.GetBool(self._node,
  42. 'return-unknown-contents')
  43. self.bad_update_contents = fdt_util.GetBool(self._node,
  44. 'bad-update-contents')
  45. self.bad_shrink_contents = fdt_util.GetBool(self._node,
  46. 'bad-shrink-contents')
  47. self.return_contents_once = fdt_util.GetBool(self._node,
  48. 'return-contents-once')
  49. self.bad_update_contents_twice = fdt_util.GetBool(self._node,
  50. 'bad-update-contents-twice')
  51. self.return_contents_later = fdt_util.GetBool(self._node,
  52. 'return-contents-later')
  53. # Set to True when the entry is ready to process the FDT.
  54. self.process_fdt_ready = False
  55. self.never_complete_process_fdt = fdt_util.GetBool(self._node,
  56. 'never-complete-process-fdt')
  57. self.require_args = fdt_util.GetBool(self._node, 'require-args')
  58. # This should be picked up by GetEntryArgsOrProps()
  59. self.test_existing_prop = 'existing'
  60. self.force_bad_datatype = fdt_util.GetBool(self._node,
  61. 'force-bad-datatype')
  62. (self.test_str_fdt, self.test_str_arg, self.test_int_fdt,
  63. self.test_int_arg, existing) = self.GetEntryArgsOrProps([
  64. EntryArg('test-str-fdt', str),
  65. EntryArg('test-str-arg', str),
  66. EntryArg('test-int-fdt', int),
  67. EntryArg('test-int-arg', int),
  68. EntryArg('test-existing-prop', str)], self.require_args)
  69. if self.force_bad_datatype:
  70. self.GetEntryArgsOrProps([EntryArg('test-bad-datatype-arg', bool)])
  71. self.return_contents = True
  72. self.contents = b'aa'
  73. def ObtainContents(self):
  74. if self.return_unknown_contents or not self.return_contents:
  75. return False
  76. if self.return_contents_later:
  77. self.return_contents_later = False
  78. return False
  79. self.data = self.contents
  80. self.contents_size = len(self.data)
  81. if self.return_contents_once:
  82. self.return_contents = False
  83. return True
  84. def GetOffsets(self):
  85. if self.return_invalid_entry :
  86. return {'invalid-entry': [1, 2]}
  87. return {}
  88. def ProcessContents(self):
  89. data = self.contents
  90. if self.bad_update_contents:
  91. # Request to update the contents with something larger, to cause a
  92. # failure.
  93. if self.bad_update_contents_twice:
  94. data = self.data + b'a'
  95. else:
  96. data = b'aaa'
  97. return self.ProcessContentsUpdate(data)
  98. if self.bad_shrink_contents:
  99. # Request to update the contents with something smaller, to cause a
  100. # failure.
  101. data = b'a'
  102. return self.ProcessContentsUpdate(data)
  103. if self.bad_shrink_contents:
  104. # Request to update the contents with something smaller, to cause a
  105. # failure.
  106. data = b'a'
  107. return self.ProcessContentsUpdate(data)
  108. return True
  109. def ProcessFdt(self, fdt):
  110. """Force reprocessing the first time"""
  111. ready = self.process_fdt_ready
  112. if not self.never_complete_process_fdt:
  113. self.process_fdt_ready = True
  114. return ready