test_fdt.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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-source2')
  175. prop = node.props['clocks']
  176. self.assertTrue(fdt32_to_cpu(prop.value) > 0)
  177. def _ConvertProp(self, prop_name):
  178. """Helper function to look up a property in self.node and return it
  179. Args:
  180. Property name to find
  181. Return fdt.Prop object for this property
  182. """
  183. p = self.fdt.get_property(self.node.Offset(), prop_name)
  184. return fdt.Prop(self.node, -1, prop_name, p)
  185. def testMakeProp(self):
  186. """Test we can convert all the the types that are supported"""
  187. prop = self._ConvertProp('boolval')
  188. self.assertEqual(fdt.TYPE_BOOL, prop.type)
  189. self.assertEqual(True, prop.value)
  190. prop = self._ConvertProp('intval')
  191. self.assertEqual(fdt.TYPE_INT, prop.type)
  192. self.assertEqual(1, fdt32_to_cpu(prop.value))
  193. prop = self._ConvertProp('intarray')
  194. self.assertEqual(fdt.TYPE_INT, prop.type)
  195. val = [fdt32_to_cpu(val) for val in prop.value]
  196. self.assertEqual([2, 3, 4], val)
  197. prop = self._ConvertProp('byteval')
  198. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  199. self.assertEqual(5, ord(prop.value))
  200. prop = self._ConvertProp('longbytearray')
  201. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  202. val = [ord(val) for val in prop.value]
  203. self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val)
  204. prop = self._ConvertProp('stringval')
  205. self.assertEqual(fdt.TYPE_STRING, prop.type)
  206. self.assertEqual('message', prop.value)
  207. prop = self._ConvertProp('stringarray')
  208. self.assertEqual(fdt.TYPE_STRING, prop.type)
  209. self.assertEqual(['multi-word', 'message'], prop.value)
  210. prop = self._ConvertProp('notstring')
  211. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  212. val = [ord(val) for val in prop.value]
  213. self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
  214. def testGetEmpty(self):
  215. """Tests the GetEmpty() function for the various supported types"""
  216. self.assertEqual(True, fdt.Prop.GetEmpty(fdt.TYPE_BOOL))
  217. self.assertEqual(chr(0), fdt.Prop.GetEmpty(fdt.TYPE_BYTE))
  218. self.assertEqual(chr(0) * 4, fdt.Prop.GetEmpty(fdt.TYPE_INT))
  219. self.assertEqual('', fdt.Prop.GetEmpty(fdt.TYPE_STRING))
  220. def testGetOffset(self):
  221. """Test we can get the offset of a property"""
  222. prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
  223. self.assertEqual(prop.value, value)
  224. def testWiden(self):
  225. """Test widening of values"""
  226. node2 = self.dtb.GetNode('/spl-test2')
  227. prop = self.node.props['intval']
  228. # No action
  229. prop2 = node2.props['intval']
  230. prop.Widen(prop2)
  231. self.assertEqual(fdt.TYPE_INT, prop.type)
  232. self.assertEqual(1, fdt32_to_cpu(prop.value))
  233. # Convert singla value to array
  234. prop2 = self.node.props['intarray']
  235. prop.Widen(prop2)
  236. self.assertEqual(fdt.TYPE_INT, prop.type)
  237. self.assertTrue(isinstance(prop.value, list))
  238. # A 4-byte array looks like a single integer. When widened by a longer
  239. # byte array, it should turn into an array.
  240. prop = self.node.props['longbytearray']
  241. prop2 = node2.props['longbytearray']
  242. self.assertFalse(isinstance(prop2.value, list))
  243. self.assertEqual(4, len(prop2.value))
  244. prop2.Widen(prop)
  245. self.assertTrue(isinstance(prop2.value, list))
  246. self.assertEqual(9, len(prop2.value))
  247. # Similarly for a string array
  248. prop = self.node.props['stringval']
  249. prop2 = node2.props['stringarray']
  250. self.assertFalse(isinstance(prop.value, list))
  251. self.assertEqual(7, len(prop.value))
  252. prop.Widen(prop2)
  253. self.assertTrue(isinstance(prop.value, list))
  254. self.assertEqual(3, len(prop.value))
  255. # Enlarging an existing array
  256. prop = self.node.props['stringarray']
  257. prop2 = node2.props['stringarray']
  258. self.assertTrue(isinstance(prop.value, list))
  259. self.assertEqual(2, len(prop.value))
  260. prop.Widen(prop2)
  261. self.assertTrue(isinstance(prop.value, list))
  262. self.assertEqual(3, len(prop.value))
  263. def testAdd(self):
  264. """Test adding properties"""
  265. self.fdt.pack()
  266. # This function should automatically expand the device tree
  267. self.node.AddZeroProp('one')
  268. self.node.AddZeroProp('two')
  269. self.node.AddZeroProp('three')
  270. # Updating existing properties should be OK, since the device-tree size
  271. # does not change
  272. self.fdt.pack()
  273. self.node.SetInt('one', 1)
  274. self.node.SetInt('two', 2)
  275. self.node.SetInt('three', 3)
  276. # This should fail since it would need to increase the device-tree size
  277. with self.assertRaises(libfdt.FdtException) as e:
  278. self.node.SetInt('four', 4)
  279. self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
  280. class TestFdtUtil(unittest.TestCase):
  281. """Tests for the fdt_util module
  282. This module will likely be mostly replaced at some point, once upstream
  283. libfdt has better Python support. For now, this provides tests for current
  284. functionality.
  285. """
  286. @classmethod
  287. def setUpClass(cls):
  288. tools.PrepareOutputDir(None)
  289. def setUp(self):
  290. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  291. self.node = self.dtb.GetNode('/spl-test')
  292. def testGetInt(self):
  293. self.assertEqual(1, fdt_util.GetInt(self.node, 'intval'))
  294. self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3))
  295. with self.assertRaises(ValueError) as e:
  296. self.assertEqual(3, fdt_util.GetInt(self.node, 'intarray'))
  297. self.assertIn("property 'intarray' has list value: expecting a single "
  298. 'integer', str(e.exception))
  299. def testGetString(self):
  300. self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
  301. self.assertEqual('test', fdt_util.GetString(self.node, 'missing',
  302. 'test'))
  303. with self.assertRaises(ValueError) as e:
  304. self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray'))
  305. self.assertIn("property 'stringarray' has list value: expecting a "
  306. 'single string', str(e.exception))
  307. def testGetBool(self):
  308. self.assertEqual(True, fdt_util.GetBool(self.node, 'boolval'))
  309. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing'))
  310. self.assertEqual(True, fdt_util.GetBool(self.node, 'missing', True))
  311. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing', False))
  312. def testGetByte(self):
  313. self.assertEqual(5, fdt_util.GetByte(self.node, 'byteval'))
  314. self.assertEqual(3, fdt_util.GetByte(self.node, 'missing', 3))
  315. with self.assertRaises(ValueError) as e:
  316. fdt_util.GetByte(self.node, 'longbytearray')
  317. self.assertIn("property 'longbytearray' has list value: expecting a "
  318. 'single byte', str(e.exception))
  319. with self.assertRaises(ValueError) as e:
  320. fdt_util.GetByte(self.node, 'intval')
  321. self.assertIn("property 'intval' has length 4, expecting 1",
  322. str(e.exception))
  323. def testGetDataType(self):
  324. self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int))
  325. self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval',
  326. str))
  327. with self.assertRaises(ValueError) as e:
  328. self.assertEqual(3, fdt_util.GetDatatype(self.node, 'boolval',
  329. bool))
  330. def testFdtCellsToCpu(self):
  331. val = self.node.props['intarray'].value
  332. self.assertEqual(0, fdt_util.fdt_cells_to_cpu(val, 0))
  333. self.assertEqual(2, fdt_util.fdt_cells_to_cpu(val, 1))
  334. dtb2 = fdt.FdtScan('tools/dtoc/dtoc_test_addr64.dts')
  335. node2 = dtb2.GetNode('/test1')
  336. val = node2.props['reg'].value
  337. self.assertEqual(0x1234, fdt_util.fdt_cells_to_cpu(val, 2))
  338. def testEnsureCompiled(self):
  339. """Test a degenerate case of this function"""
  340. dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts')
  341. self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
  342. def testGetPlainBytes(self):
  343. self.assertEqual('fred', fdt_util.get_plain_bytes('fred'))
  344. def RunTestCoverage():
  345. """Run the tests and check that we get 100% coverage"""
  346. test_util.RunTestCoverage('tools/dtoc/test_fdt.py', None,
  347. ['tools/patman/*.py', '*test_fdt.py'], options.build_dir)
  348. def RunTests(args):
  349. """Run all the test we have for the fdt model
  350. Args:
  351. args: List of positional args provided to fdt. This can hold a test
  352. name to execute (as in 'fdt -t testFdt', for example)
  353. """
  354. result = unittest.TestResult()
  355. sys.argv = [sys.argv[0]]
  356. test_name = args and args[0] or None
  357. for module in (TestFdt, TestNode, TestProp, TestFdtUtil):
  358. if test_name:
  359. try:
  360. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  361. except AttributeError:
  362. continue
  363. else:
  364. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  365. suite.run(result)
  366. print result
  367. for _, err in result.errors:
  368. print err
  369. for _, err in result.failures:
  370. print err
  371. if __name__ != '__main__':
  372. sys.exit(1)
  373. parser = OptionParser()
  374. parser.add_option('-B', '--build-dir', type='string', default='b',
  375. help='Directory containing the build output')
  376. parser.add_option('-t', '--test', action='store_true', dest='test',
  377. default=False, help='run tests')
  378. parser.add_option('-T', '--test-coverage', action='store_true',
  379. default=False, help='run tests and check for 100% coverage')
  380. (options, args) = parser.parse_args()
  381. # Run our meagre tests
  382. if options.test:
  383. RunTests(args)
  384. elif options.test_coverage:
  385. RunTestCoverage()