test_fdt.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2018 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. from optparse import OptionParser
  7. import glob
  8. import os
  9. import sys
  10. import unittest
  11. # Bring in the patman libraries
  12. our_path = os.path.dirname(os.path.realpath(__file__))
  13. for dirname in ['../patman', '..']:
  14. sys.path.insert(0, os.path.join(our_path, dirname))
  15. import command
  16. import fdt
  17. from fdt import TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL
  18. import fdt_util
  19. from fdt_util import fdt32_to_cpu
  20. import libfdt
  21. import test_util
  22. import tools
  23. def _GetPropertyValue(dtb, node, prop_name):
  24. """Low-level function to get the property value based on its offset
  25. This looks directly in the device tree at the property's offset to find
  26. its value. It is useful as a check that the property is in the correct
  27. place.
  28. Args:
  29. node: Node to look in
  30. prop_name: Property name to find
  31. Returns:
  32. Tuple:
  33. Prop object found
  34. Value of property as a string (found using property offset)
  35. """
  36. prop = node.props[prop_name]
  37. # Add 12, which is sizeof(struct fdt_property), to get to start of data
  38. offset = prop.GetOffset() + 12
  39. data = dtb.GetContents()[offset:offset + len(prop.value)]
  40. return prop, [chr(x) for x in data]
  41. class TestFdt(unittest.TestCase):
  42. """Tests for the Fdt module
  43. This includes unit tests for some functions and functional tests for the fdt
  44. module.
  45. """
  46. @classmethod
  47. def setUpClass(cls):
  48. tools.PrepareOutputDir(None)
  49. @classmethod
  50. def tearDownClass(cls):
  51. tools._FinaliseForTest()
  52. def setUp(self):
  53. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  54. def testFdt(self):
  55. """Test that we can open an Fdt"""
  56. self.dtb.Scan()
  57. root = self.dtb.GetRoot()
  58. self.assertTrue(isinstance(root, fdt.Node))
  59. def testGetNode(self):
  60. """Test the GetNode() method"""
  61. node = self.dtb.GetNode('/spl-test')
  62. self.assertTrue(isinstance(node, fdt.Node))
  63. node = self.dtb.GetNode('/i2c@0/pmic@9')
  64. self.assertTrue(isinstance(node, fdt.Node))
  65. self.assertEqual('pmic@9', node.name)
  66. self.assertIsNone(self.dtb.GetNode('/i2c@0/pmic@9/missing'))
  67. def testFlush(self):
  68. """Check that we can flush the device tree out to its file"""
  69. fname = self.dtb._fname
  70. with open(fname) as fd:
  71. data = fd.read()
  72. os.remove(fname)
  73. with self.assertRaises(IOError):
  74. open(fname)
  75. self.dtb.Flush()
  76. with open(fname) as fd:
  77. data = fd.read()
  78. def testPack(self):
  79. """Test that packing a device tree works"""
  80. self.dtb.Pack()
  81. def testGetFdt(self):
  82. """Tetst that we can access the raw device-tree data"""
  83. self.assertTrue(isinstance(self.dtb.GetContents(), bytearray))
  84. def testGetProps(self):
  85. """Tests obtaining a list of properties"""
  86. node = self.dtb.GetNode('/spl-test')
  87. props = self.dtb.GetProps(node)
  88. self.assertEqual(['boolval', 'bytearray', 'byteval', 'compatible',
  89. 'intarray', 'intval', 'longbytearray', 'notstring',
  90. 'stringarray', 'stringval', 'u-boot,dm-pre-reloc'],
  91. sorted(props.keys()))
  92. def testCheckError(self):
  93. """Tests the ChecKError() function"""
  94. with self.assertRaises(ValueError) as e:
  95. fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
  96. self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
  97. class TestNode(unittest.TestCase):
  98. """Test operation of the Node class"""
  99. @classmethod
  100. def setUpClass(cls):
  101. tools.PrepareOutputDir(None)
  102. @classmethod
  103. def tearDownClass(cls):
  104. tools._FinaliseForTest()
  105. def setUp(self):
  106. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  107. self.node = self.dtb.GetNode('/spl-test')
  108. def testOffset(self):
  109. """Tests that we can obtain the offset of a node"""
  110. self.assertTrue(self.node.Offset() > 0)
  111. def testDelete(self):
  112. """Tests that we can delete a property"""
  113. node2 = self.dtb.GetNode('/spl-test2')
  114. offset1 = node2.Offset()
  115. self.node.DeleteProp('intval')
  116. offset2 = node2.Offset()
  117. self.assertTrue(offset2 < offset1)
  118. self.node.DeleteProp('intarray')
  119. offset3 = node2.Offset()
  120. self.assertTrue(offset3 < offset2)
  121. with self.assertRaises(libfdt.FdtException):
  122. self.node.DeleteProp('missing')
  123. def testDeleteGetOffset(self):
  124. """Test that property offset update when properties are deleted"""
  125. self.node.DeleteProp('intval')
  126. prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
  127. self.assertEqual(prop.value, value)
  128. def testFindNode(self):
  129. """Tests that we can find a node using the _FindNode() functoin"""
  130. node = self.dtb.GetRoot()._FindNode('i2c@0')
  131. self.assertEqual('i2c@0', node.name)
  132. subnode = node._FindNode('pmic@9')
  133. self.assertEqual('pmic@9', subnode.name)
  134. self.assertEqual(None, node._FindNode('missing'))
  135. def testRefreshMissingNode(self):
  136. """Test refreshing offsets when an extra node is present in dtb"""
  137. # Delete it from our tables, not the device tree
  138. del self.dtb._root.subnodes[-1]
  139. with self.assertRaises(ValueError) as e:
  140. self.dtb.Refresh()
  141. self.assertIn('Internal error, offset', str(e.exception))
  142. def testRefreshExtraNode(self):
  143. """Test refreshing offsets when an expected node is missing"""
  144. # Delete it from the device tre, not our tables
  145. self.dtb.GetFdtObj().del_node(self.node.Offset())
  146. with self.assertRaises(ValueError) as e:
  147. self.dtb.Refresh()
  148. self.assertIn('Internal error, node name mismatch '
  149. 'spl-test != spl-test2', str(e.exception))
  150. def testRefreshMissingProp(self):
  151. """Test refreshing offsets when an extra property is present in dtb"""
  152. # Delete it from our tables, not the device tree
  153. del self.node.props['notstring']
  154. with self.assertRaises(ValueError) as e:
  155. self.dtb.Refresh()
  156. self.assertIn("Internal error, property 'notstring' missing, offset ",
  157. str(e.exception))
  158. class TestProp(unittest.TestCase):
  159. """Test operation of the Prop class"""
  160. @classmethod
  161. def setUpClass(cls):
  162. tools.PrepareOutputDir(None)
  163. @classmethod
  164. def tearDownClass(cls):
  165. tools._FinaliseForTest()
  166. def setUp(self):
  167. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  168. self.node = self.dtb.GetNode('/spl-test')
  169. self.fdt = self.dtb.GetFdtObj()
  170. def testMissingNode(self):
  171. self.assertEqual(None, self.dtb.GetNode('missing'))
  172. def testPhandle(self):
  173. dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
  174. node = dtb.GetNode('/phandle-source')
  175. def _ConvertProp(self, prop_name):
  176. """Helper function to look up a property in self.node and return it
  177. Args:
  178. Property name to find
  179. Return fdt.Prop object for this property
  180. """
  181. p = self.fdt.get_property(self.node.Offset(), prop_name)
  182. return fdt.Prop(self.node, -1, prop_name, p)
  183. def testMakeProp(self):
  184. """Test we can convert all the the types that are supported"""
  185. prop = self._ConvertProp('boolval')
  186. self.assertEqual(fdt.TYPE_BOOL, prop.type)
  187. self.assertEqual(True, prop.value)
  188. prop = self._ConvertProp('intval')
  189. self.assertEqual(fdt.TYPE_INT, prop.type)
  190. self.assertEqual(1, fdt32_to_cpu(prop.value))
  191. prop = self._ConvertProp('intarray')
  192. self.assertEqual(fdt.TYPE_INT, prop.type)
  193. val = [fdt32_to_cpu(val) for val in prop.value]
  194. self.assertEqual([2, 3, 4], val)
  195. prop = self._ConvertProp('byteval')
  196. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  197. self.assertEqual(5, ord(prop.value))
  198. prop = self._ConvertProp('longbytearray')
  199. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  200. val = [ord(val) for val in prop.value]
  201. self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val)
  202. prop = self._ConvertProp('stringval')
  203. self.assertEqual(fdt.TYPE_STRING, prop.type)
  204. self.assertEqual('message', prop.value)
  205. prop = self._ConvertProp('stringarray')
  206. self.assertEqual(fdt.TYPE_STRING, prop.type)
  207. self.assertEqual(['multi-word', 'message'], prop.value)
  208. prop = self._ConvertProp('notstring')
  209. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  210. val = [ord(val) for val in prop.value]
  211. self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
  212. def testGetEmpty(self):
  213. """Tests the GetEmpty() function for the various supported types"""
  214. self.assertEqual(True, fdt.Prop.GetEmpty(fdt.TYPE_BOOL))
  215. self.assertEqual(chr(0), fdt.Prop.GetEmpty(fdt.TYPE_BYTE))
  216. self.assertEqual(chr(0) * 4, fdt.Prop.GetEmpty(fdt.TYPE_INT))
  217. self.assertEqual('', fdt.Prop.GetEmpty(fdt.TYPE_STRING))
  218. def testGetOffset(self):
  219. """Test we can get the offset of a property"""
  220. prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
  221. self.assertEqual(prop.value, value)
  222. def testWiden(self):
  223. """Test widening of values"""
  224. node2 = self.dtb.GetNode('/spl-test2')
  225. prop = self.node.props['intval']
  226. # No action
  227. prop2 = node2.props['intval']
  228. prop.Widen(prop2)
  229. self.assertEqual(fdt.TYPE_INT, prop.type)
  230. self.assertEqual(1, fdt32_to_cpu(prop.value))
  231. # Convert singla value to array
  232. prop2 = self.node.props['intarray']
  233. prop.Widen(prop2)
  234. self.assertEqual(fdt.TYPE_INT, prop.type)
  235. self.assertTrue(isinstance(prop.value, list))
  236. # A 4-byte array looks like a single integer. When widened by a longer
  237. # byte array, it should turn into an array.
  238. prop = self.node.props['longbytearray']
  239. prop2 = node2.props['longbytearray']
  240. self.assertFalse(isinstance(prop2.value, list))
  241. self.assertEqual(4, len(prop2.value))
  242. prop2.Widen(prop)
  243. self.assertTrue(isinstance(prop2.value, list))
  244. self.assertEqual(9, len(prop2.value))
  245. # Similarly for a string array
  246. prop = self.node.props['stringval']
  247. prop2 = node2.props['stringarray']
  248. self.assertFalse(isinstance(prop.value, list))
  249. self.assertEqual(7, len(prop.value))
  250. prop.Widen(prop2)
  251. self.assertTrue(isinstance(prop.value, list))
  252. self.assertEqual(3, len(prop.value))
  253. # Enlarging an existing array
  254. prop = self.node.props['stringarray']
  255. prop2 = node2.props['stringarray']
  256. self.assertTrue(isinstance(prop.value, list))
  257. self.assertEqual(2, len(prop.value))
  258. prop.Widen(prop2)
  259. self.assertTrue(isinstance(prop.value, list))
  260. self.assertEqual(3, len(prop.value))
  261. class TestFdtUtil(unittest.TestCase):
  262. """Tests for the fdt_util module
  263. This module will likely be mostly replaced at some point, once upstream
  264. libfdt has better Python support. For now, this provides tests for current
  265. functionality.
  266. """
  267. @classmethod
  268. def setUpClass(cls):
  269. tools.PrepareOutputDir(None)
  270. def setUp(self):
  271. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  272. self.node = self.dtb.GetNode('/spl-test')
  273. def testGetInt(self):
  274. self.assertEqual(1, fdt_util.GetInt(self.node, 'intval'))
  275. self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3))
  276. with self.assertRaises(ValueError) as e:
  277. self.assertEqual(3, fdt_util.GetInt(self.node, 'intarray'))
  278. self.assertIn("property 'intarray' has list value: expecting a single "
  279. 'integer', str(e.exception))
  280. def testGetString(self):
  281. self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
  282. self.assertEqual('test', fdt_util.GetString(self.node, 'missing',
  283. 'test'))
  284. with self.assertRaises(ValueError) as e:
  285. self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray'))
  286. self.assertIn("property 'stringarray' has list value: expecting a "
  287. 'single string', str(e.exception))
  288. def testGetBool(self):
  289. self.assertEqual(True, fdt_util.GetBool(self.node, 'boolval'))
  290. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing'))
  291. self.assertEqual(True, fdt_util.GetBool(self.node, 'missing', True))
  292. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing', False))
  293. def testFdtCellsToCpu(self):
  294. val = self.node.props['intarray'].value
  295. self.assertEqual(0, fdt_util.fdt_cells_to_cpu(val, 0))
  296. self.assertEqual(2, fdt_util.fdt_cells_to_cpu(val, 1))
  297. dtb2 = fdt.FdtScan('tools/dtoc/dtoc_test_addr64.dts')
  298. node2 = dtb2.GetNode('/test1')
  299. val = node2.props['reg'].value
  300. self.assertEqual(0x1234, fdt_util.fdt_cells_to_cpu(val, 2))
  301. def testEnsureCompiled(self):
  302. """Test a degenerate case of this function"""
  303. dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts')
  304. self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
  305. def testGetPlainBytes(self):
  306. self.assertEqual('fred', fdt_util.get_plain_bytes('fred'))
  307. def RunTestCoverage():
  308. """Run the tests and check that we get 100% coverage"""
  309. test_util.RunTestCoverage('tools/dtoc/test_fdt.py', None,
  310. ['tools/patman/*.py', '*test_fdt.py'], options.build_dir)
  311. def RunTests(args):
  312. """Run all the test we have for the fdt model
  313. Args:
  314. args: List of positional args provided to fdt. This can hold a test
  315. name to execute (as in 'fdt -t testFdt', for example)
  316. """
  317. result = unittest.TestResult()
  318. sys.argv = [sys.argv[0]]
  319. test_name = args and args[0] or None
  320. for module in (TestFdt, TestNode, TestProp, TestFdtUtil):
  321. if test_name:
  322. try:
  323. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  324. except AttributeError:
  325. continue
  326. else:
  327. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  328. suite.run(result)
  329. print result
  330. for _, err in result.errors:
  331. print err
  332. for _, err in result.failures:
  333. print err
  334. if __name__ != '__main__':
  335. sys.exit(1)
  336. parser = OptionParser()
  337. parser.add_option('-B', '--build-dir', type='string', default='b',
  338. help='Directory containing the build output')
  339. parser.add_option('-t', '--test', action='store_true', dest='test',
  340. default=False, help='run tests')
  341. parser.add_option('-T', '--test-coverage', action='store_true',
  342. default=False, help='run tests and check for 100% coverage')
  343. (options, args) = parser.parse_args()
  344. # Run our meagre tests
  345. if options.test:
  346. RunTests(args)
  347. elif options.test_coverage:
  348. RunTestCoverage()