test_fdt.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #!/usr/bin/env python3
  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 shutil
  10. import sys
  11. import tempfile
  12. import unittest
  13. # Bring in the patman libraries
  14. our_path = os.path.dirname(os.path.realpath(__file__))
  15. sys.path.insert(1, os.path.join(our_path, '..'))
  16. from dtoc import fdt
  17. from dtoc import fdt_util
  18. from dtoc.fdt_util import fdt32_to_cpu
  19. from fdt import TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL, BytesToValue
  20. import libfdt
  21. from patman import command
  22. from patman import test_util
  23. from patman import tools
  24. def _GetPropertyValue(dtb, node, prop_name):
  25. """Low-level function to get the property value based on its offset
  26. This looks directly in the device tree at the property's offset to find
  27. its value. It is useful as a check that the property is in the correct
  28. place.
  29. Args:
  30. node: Node to look in
  31. prop_name: Property name to find
  32. Returns:
  33. Tuple:
  34. Prop object found
  35. Value of property as a string (found using property offset)
  36. """
  37. prop = node.props[prop_name]
  38. # Add 12, which is sizeof(struct fdt_property), to get to start of data
  39. offset = prop.GetOffset() + 12
  40. data = dtb.GetContents()[offset:offset + len(prop.value)]
  41. return prop, [tools.ToChar(x) for x in data]
  42. class TestFdt(unittest.TestCase):
  43. """Tests for the Fdt module
  44. This includes unit tests for some functions and functional tests for the fdt
  45. module.
  46. """
  47. @classmethod
  48. def setUpClass(cls):
  49. tools.PrepareOutputDir(None)
  50. @classmethod
  51. def tearDownClass(cls):
  52. tools.FinaliseOutputDir()
  53. def setUp(self):
  54. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  55. def testFdt(self):
  56. """Test that we can open an Fdt"""
  57. self.dtb.Scan()
  58. root = self.dtb.GetRoot()
  59. self.assertTrue(isinstance(root, fdt.Node))
  60. def testGetNode(self):
  61. """Test the GetNode() method"""
  62. node = self.dtb.GetNode('/spl-test')
  63. self.assertTrue(isinstance(node, fdt.Node))
  64. node = self.dtb.GetNode('/i2c@0/pmic@9')
  65. self.assertTrue(isinstance(node, fdt.Node))
  66. self.assertEqual('pmic@9', node.name)
  67. self.assertIsNone(self.dtb.GetNode('/i2c@0/pmic@9/missing'))
  68. node = self.dtb.GetNode('/')
  69. self.assertTrue(isinstance(node, fdt.Node))
  70. self.assertEqual(0, node.Offset())
  71. def testFlush(self):
  72. """Check that we can flush the device tree out to its file"""
  73. fname = self.dtb._fname
  74. with open(fname, 'rb') as fd:
  75. data = fd.read()
  76. os.remove(fname)
  77. with self.assertRaises(IOError):
  78. open(fname, 'rb')
  79. self.dtb.Flush()
  80. with open(fname, 'rb') as fd:
  81. data = fd.read()
  82. def testPack(self):
  83. """Test that packing a device tree works"""
  84. self.dtb.Pack()
  85. def testGetFdt(self):
  86. """Tetst that we can access the raw device-tree data"""
  87. self.assertTrue(isinstance(self.dtb.GetContents(), bytearray))
  88. def testGetProps(self):
  89. """Tests obtaining a list of properties"""
  90. node = self.dtb.GetNode('/spl-test')
  91. props = self.dtb.GetProps(node)
  92. self.assertEqual(['boolval', 'bytearray', 'byteval', 'compatible',
  93. 'intarray', 'intval', 'longbytearray', 'notstring',
  94. 'stringarray', 'stringval', 'u-boot,dm-pre-reloc'],
  95. sorted(props.keys()))
  96. def testCheckError(self):
  97. """Tests the ChecKError() function"""
  98. with self.assertRaises(ValueError) as e:
  99. fdt.CheckErr(-libfdt.NOTFOUND, 'hello')
  100. self.assertIn('FDT_ERR_NOTFOUND: hello', str(e.exception))
  101. def testGetFdt(self):
  102. node = self.dtb.GetNode('/spl-test')
  103. self.assertEqual(self.dtb, node.GetFdt())
  104. def testBytesToValue(self):
  105. self.assertEqual(BytesToValue(b'this\0is\0'),
  106. (TYPE_STRING, ['this', 'is']))
  107. class TestNode(unittest.TestCase):
  108. """Test operation of the Node class"""
  109. @classmethod
  110. def setUpClass(cls):
  111. tools.PrepareOutputDir(None)
  112. @classmethod
  113. def tearDownClass(cls):
  114. tools.FinaliseOutputDir()
  115. def setUp(self):
  116. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  117. self.node = self.dtb.GetNode('/spl-test')
  118. def testOffset(self):
  119. """Tests that we can obtain the offset of a node"""
  120. self.assertTrue(self.node.Offset() > 0)
  121. def testDelete(self):
  122. """Tests that we can delete a property"""
  123. node2 = self.dtb.GetNode('/spl-test2')
  124. offset1 = node2.Offset()
  125. self.node.DeleteProp('intval')
  126. offset2 = node2.Offset()
  127. self.assertTrue(offset2 < offset1)
  128. self.node.DeleteProp('intarray')
  129. offset3 = node2.Offset()
  130. self.assertTrue(offset3 < offset2)
  131. with self.assertRaises(libfdt.FdtException):
  132. self.node.DeleteProp('missing')
  133. def testDeleteGetOffset(self):
  134. """Test that property offset update when properties are deleted"""
  135. self.node.DeleteProp('intval')
  136. prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
  137. self.assertEqual(prop.value, value)
  138. def testFindNode(self):
  139. """Tests that we can find a node using the FindNode() functoin"""
  140. node = self.dtb.GetRoot().FindNode('i2c@0')
  141. self.assertEqual('i2c@0', node.name)
  142. subnode = node.FindNode('pmic@9')
  143. self.assertEqual('pmic@9', subnode.name)
  144. self.assertEqual(None, node.FindNode('missing'))
  145. def testRefreshMissingNode(self):
  146. """Test refreshing offsets when an extra node is present in dtb"""
  147. # Delete it from our tables, not the device tree
  148. del self.dtb._root.subnodes[-1]
  149. with self.assertRaises(ValueError) as e:
  150. self.dtb.Refresh()
  151. self.assertIn('Internal error, offset', str(e.exception))
  152. def testRefreshExtraNode(self):
  153. """Test refreshing offsets when an expected node is missing"""
  154. # Delete it from the device tre, not our tables
  155. self.dtb.GetFdtObj().del_node(self.node.Offset())
  156. with self.assertRaises(ValueError) as e:
  157. self.dtb.Refresh()
  158. self.assertIn('Internal error, node name mismatch '
  159. 'spl-test != spl-test2', str(e.exception))
  160. def testRefreshMissingProp(self):
  161. """Test refreshing offsets when an extra property is present in dtb"""
  162. # Delete it from our tables, not the device tree
  163. del self.node.props['notstring']
  164. with self.assertRaises(ValueError) as e:
  165. self.dtb.Refresh()
  166. self.assertIn("Internal error, property 'notstring' missing, offset ",
  167. str(e.exception))
  168. def testLookupPhandle(self):
  169. """Test looking up a single phandle"""
  170. dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
  171. node = dtb.GetNode('/phandle-source2')
  172. prop = node.props['clocks']
  173. target = dtb.GetNode('/phandle-target')
  174. self.assertEqual(target, dtb.LookupPhandle(fdt32_to_cpu(prop.value)))
  175. class TestProp(unittest.TestCase):
  176. """Test operation of the Prop class"""
  177. @classmethod
  178. def setUpClass(cls):
  179. tools.PrepareOutputDir(None)
  180. @classmethod
  181. def tearDownClass(cls):
  182. tools.FinaliseOutputDir()
  183. def setUp(self):
  184. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  185. self.node = self.dtb.GetNode('/spl-test')
  186. self.fdt = self.dtb.GetFdtObj()
  187. def testMissingNode(self):
  188. self.assertEqual(None, self.dtb.GetNode('missing'))
  189. def testPhandle(self):
  190. dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
  191. node = dtb.GetNode('/phandle-source2')
  192. prop = node.props['clocks']
  193. self.assertTrue(fdt32_to_cpu(prop.value) > 0)
  194. def _ConvertProp(self, prop_name):
  195. """Helper function to look up a property in self.node and return it
  196. Args:
  197. Property name to find
  198. Return fdt.Prop object for this property
  199. """
  200. p = self.fdt.getprop(self.node.Offset(), prop_name)
  201. return fdt.Prop(self.node, -1, prop_name, p)
  202. def testMakeProp(self):
  203. """Test we can convert all the the types that are supported"""
  204. prop = self._ConvertProp('boolval')
  205. self.assertEqual(fdt.TYPE_BOOL, prop.type)
  206. self.assertEqual(True, prop.value)
  207. prop = self._ConvertProp('intval')
  208. self.assertEqual(fdt.TYPE_INT, prop.type)
  209. self.assertEqual(1, fdt32_to_cpu(prop.value))
  210. prop = self._ConvertProp('intarray')
  211. self.assertEqual(fdt.TYPE_INT, prop.type)
  212. val = [fdt32_to_cpu(val) for val in prop.value]
  213. self.assertEqual([2, 3, 4], val)
  214. prop = self._ConvertProp('byteval')
  215. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  216. self.assertEqual(5, ord(prop.value))
  217. prop = self._ConvertProp('longbytearray')
  218. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  219. val = [ord(val) for val in prop.value]
  220. self.assertEqual([9, 10, 11, 12, 13, 14, 15, 16, 17], val)
  221. prop = self._ConvertProp('stringval')
  222. self.assertEqual(fdt.TYPE_STRING, prop.type)
  223. self.assertEqual('message', prop.value)
  224. prop = self._ConvertProp('stringarray')
  225. self.assertEqual(fdt.TYPE_STRING, prop.type)
  226. self.assertEqual(['multi-word', 'message'], prop.value)
  227. prop = self._ConvertProp('notstring')
  228. self.assertEqual(fdt.TYPE_BYTE, prop.type)
  229. val = [ord(val) for val in prop.value]
  230. self.assertEqual([0x20, 0x21, 0x22, 0x10, 0], val)
  231. def testGetEmpty(self):
  232. """Tests the GetEmpty() function for the various supported types"""
  233. self.assertEqual(True, fdt.Prop.GetEmpty(fdt.TYPE_BOOL))
  234. self.assertEqual(chr(0), fdt.Prop.GetEmpty(fdt.TYPE_BYTE))
  235. self.assertEqual(tools.GetBytes(0, 4), fdt.Prop.GetEmpty(fdt.TYPE_INT))
  236. self.assertEqual('', fdt.Prop.GetEmpty(fdt.TYPE_STRING))
  237. def testGetOffset(self):
  238. """Test we can get the offset of a property"""
  239. prop, value = _GetPropertyValue(self.dtb, self.node, 'longbytearray')
  240. self.assertEqual(prop.value, value)
  241. def testWiden(self):
  242. """Test widening of values"""
  243. node2 = self.dtb.GetNode('/spl-test2')
  244. prop = self.node.props['intval']
  245. # No action
  246. prop2 = node2.props['intval']
  247. prop.Widen(prop2)
  248. self.assertEqual(fdt.TYPE_INT, prop.type)
  249. self.assertEqual(1, fdt32_to_cpu(prop.value))
  250. # Convert singla value to array
  251. prop2 = self.node.props['intarray']
  252. prop.Widen(prop2)
  253. self.assertEqual(fdt.TYPE_INT, prop.type)
  254. self.assertTrue(isinstance(prop.value, list))
  255. # A 4-byte array looks like a single integer. When widened by a longer
  256. # byte array, it should turn into an array.
  257. prop = self.node.props['longbytearray']
  258. prop2 = node2.props['longbytearray']
  259. self.assertFalse(isinstance(prop2.value, list))
  260. self.assertEqual(4, len(prop2.value))
  261. prop2.Widen(prop)
  262. self.assertTrue(isinstance(prop2.value, list))
  263. self.assertEqual(9, len(prop2.value))
  264. # Similarly for a string array
  265. prop = self.node.props['stringval']
  266. prop2 = node2.props['stringarray']
  267. self.assertFalse(isinstance(prop.value, list))
  268. self.assertEqual(7, len(prop.value))
  269. prop.Widen(prop2)
  270. self.assertTrue(isinstance(prop.value, list))
  271. self.assertEqual(3, len(prop.value))
  272. # Enlarging an existing array
  273. prop = self.node.props['stringarray']
  274. prop2 = node2.props['stringarray']
  275. self.assertTrue(isinstance(prop.value, list))
  276. self.assertEqual(2, len(prop.value))
  277. prop.Widen(prop2)
  278. self.assertTrue(isinstance(prop.value, list))
  279. self.assertEqual(3, len(prop.value))
  280. def testAdd(self):
  281. """Test adding properties"""
  282. self.fdt.pack()
  283. # This function should automatically expand the device tree
  284. self.node.AddZeroProp('one')
  285. self.node.AddZeroProp('two')
  286. self.node.AddZeroProp('three')
  287. self.dtb.Sync(auto_resize=True)
  288. # Updating existing properties should be OK, since the device-tree size
  289. # does not change
  290. self.fdt.pack()
  291. self.node.SetInt('one', 1)
  292. self.node.SetInt('two', 2)
  293. self.node.SetInt('three', 3)
  294. self.dtb.Sync(auto_resize=False)
  295. # This should fail since it would need to increase the device-tree size
  296. self.node.AddZeroProp('four')
  297. with self.assertRaises(libfdt.FdtException) as e:
  298. self.dtb.Sync(auto_resize=False)
  299. self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
  300. self.dtb.Sync(auto_resize=True)
  301. def testAddNode(self):
  302. self.fdt.pack()
  303. self.node.AddSubnode('subnode')
  304. with self.assertRaises(libfdt.FdtException) as e:
  305. self.dtb.Sync(auto_resize=False)
  306. self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
  307. self.dtb.Sync(auto_resize=True)
  308. offset = self.fdt.path_offset('/spl-test/subnode')
  309. self.assertTrue(offset > 0)
  310. def testAddMore(self):
  311. """Test various other methods for adding and setting properties"""
  312. self.node.AddZeroProp('one')
  313. self.dtb.Sync(auto_resize=True)
  314. data = self.fdt.getprop(self.node.Offset(), 'one')
  315. self.assertEqual(0, fdt32_to_cpu(data))
  316. self.node.SetInt('one', 1)
  317. self.dtb.Sync(auto_resize=False)
  318. data = self.fdt.getprop(self.node.Offset(), 'one')
  319. self.assertEqual(1, fdt32_to_cpu(data))
  320. val = '123' + chr(0) + '456'
  321. self.node.AddString('string', val)
  322. self.dtb.Sync(auto_resize=True)
  323. data = self.fdt.getprop(self.node.Offset(), 'string')
  324. self.assertEqual(tools.ToBytes(val) + b'\0', data)
  325. self.fdt.pack()
  326. self.node.SetString('string', val + 'x')
  327. with self.assertRaises(libfdt.FdtException) as e:
  328. self.dtb.Sync(auto_resize=False)
  329. self.assertIn('FDT_ERR_NOSPACE', str(e.exception))
  330. self.node.SetString('string', val[:-1])
  331. prop = self.node.props['string']
  332. prop.SetData(tools.ToBytes(val))
  333. self.dtb.Sync(auto_resize=False)
  334. data = self.fdt.getprop(self.node.Offset(), 'string')
  335. self.assertEqual(tools.ToBytes(val), data)
  336. self.node.AddEmptyProp('empty', 5)
  337. self.dtb.Sync(auto_resize=True)
  338. prop = self.node.props['empty']
  339. prop.SetData(tools.ToBytes(val))
  340. self.dtb.Sync(auto_resize=False)
  341. data = self.fdt.getprop(self.node.Offset(), 'empty')
  342. self.assertEqual(tools.ToBytes(val), data)
  343. self.node.SetData('empty', b'123')
  344. self.assertEqual(b'123', prop.bytes)
  345. def testFromData(self):
  346. dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
  347. self.assertEqual(dtb2.GetContents(), self.dtb.GetContents())
  348. self.node.AddEmptyProp('empty', 5)
  349. self.dtb.Sync(auto_resize=True)
  350. self.assertTrue(dtb2.GetContents() != self.dtb.GetContents())
  351. def testMissingSetInt(self):
  352. """Test handling of a missing property with SetInt"""
  353. with self.assertRaises(ValueError) as e:
  354. self.node.SetInt('one', 1)
  355. self.assertIn("node '/spl-test': Missing property 'one'",
  356. str(e.exception))
  357. def testMissingSetData(self):
  358. """Test handling of a missing property with SetData"""
  359. with self.assertRaises(ValueError) as e:
  360. self.node.SetData('one', b'data')
  361. self.assertIn("node '/spl-test': Missing property 'one'",
  362. str(e.exception))
  363. def testMissingSetString(self):
  364. """Test handling of a missing property with SetString"""
  365. with self.assertRaises(ValueError) as e:
  366. self.node.SetString('one', 1)
  367. self.assertIn("node '/spl-test': Missing property 'one'",
  368. str(e.exception))
  369. def testGetFilename(self):
  370. """Test the dtb filename can be provided"""
  371. self.assertEqual(tools.GetOutputFilename('source.dtb'),
  372. self.dtb.GetFilename())
  373. class TestFdtUtil(unittest.TestCase):
  374. """Tests for the fdt_util module
  375. This module will likely be mostly replaced at some point, once upstream
  376. libfdt has better Python support. For now, this provides tests for current
  377. functionality.
  378. """
  379. @classmethod
  380. def setUpClass(cls):
  381. tools.PrepareOutputDir(None)
  382. @classmethod
  383. def tearDownClass(cls):
  384. tools.FinaliseOutputDir()
  385. def setUp(self):
  386. self.dtb = fdt.FdtScan('tools/dtoc/dtoc_test_simple.dts')
  387. self.node = self.dtb.GetNode('/spl-test')
  388. def testGetInt(self):
  389. self.assertEqual(1, fdt_util.GetInt(self.node, 'intval'))
  390. self.assertEqual(3, fdt_util.GetInt(self.node, 'missing', 3))
  391. with self.assertRaises(ValueError) as e:
  392. self.assertEqual(3, fdt_util.GetInt(self.node, 'intarray'))
  393. self.assertIn("property 'intarray' has list value: expecting a single "
  394. 'integer', str(e.exception))
  395. def testGetString(self):
  396. self.assertEqual('message', fdt_util.GetString(self.node, 'stringval'))
  397. self.assertEqual('test', fdt_util.GetString(self.node, 'missing',
  398. 'test'))
  399. with self.assertRaises(ValueError) as e:
  400. self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray'))
  401. self.assertIn("property 'stringarray' has list value: expecting a "
  402. 'single string', str(e.exception))
  403. def testGetBool(self):
  404. self.assertEqual(True, fdt_util.GetBool(self.node, 'boolval'))
  405. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing'))
  406. self.assertEqual(True, fdt_util.GetBool(self.node, 'missing', True))
  407. self.assertEqual(False, fdt_util.GetBool(self.node, 'missing', False))
  408. def testGetByte(self):
  409. self.assertEqual(5, fdt_util.GetByte(self.node, 'byteval'))
  410. self.assertEqual(3, fdt_util.GetByte(self.node, 'missing', 3))
  411. with self.assertRaises(ValueError) as e:
  412. fdt_util.GetByte(self.node, 'longbytearray')
  413. self.assertIn("property 'longbytearray' has list value: expecting a "
  414. 'single byte', str(e.exception))
  415. with self.assertRaises(ValueError) as e:
  416. fdt_util.GetByte(self.node, 'intval')
  417. self.assertIn("property 'intval' has length 4, expecting 1",
  418. str(e.exception))
  419. def testGetPhandleList(self):
  420. dtb = fdt.FdtScan('tools/dtoc/dtoc_test_phandle.dts')
  421. node = dtb.GetNode('/phandle-source2')
  422. self.assertEqual([1], fdt_util.GetPhandleList(node, 'clocks'))
  423. node = dtb.GetNode('/phandle-source')
  424. self.assertEqual([1, 2, 11, 3, 12, 13, 1],
  425. fdt_util.GetPhandleList(node, 'clocks'))
  426. self.assertEqual(None, fdt_util.GetPhandleList(node, 'missing'))
  427. def testGetDataType(self):
  428. self.assertEqual(1, fdt_util.GetDatatype(self.node, 'intval', int))
  429. self.assertEqual('message', fdt_util.GetDatatype(self.node, 'stringval',
  430. str))
  431. with self.assertRaises(ValueError) as e:
  432. self.assertEqual(3, fdt_util.GetDatatype(self.node, 'boolval',
  433. bool))
  434. def testFdtCellsToCpu(self):
  435. val = self.node.props['intarray'].value
  436. self.assertEqual(0, fdt_util.fdt_cells_to_cpu(val, 0))
  437. self.assertEqual(2, fdt_util.fdt_cells_to_cpu(val, 1))
  438. dtb2 = fdt.FdtScan('tools/dtoc/dtoc_test_addr64.dts')
  439. node1 = dtb2.GetNode('/test1')
  440. val = node1.props['reg'].value
  441. self.assertEqual(0x1234, fdt_util.fdt_cells_to_cpu(val, 2))
  442. node2 = dtb2.GetNode('/test2')
  443. val = node2.props['reg'].value
  444. self.assertEqual(0x1234567890123456, fdt_util.fdt_cells_to_cpu(val, 2))
  445. self.assertEqual(0x9876543210987654, fdt_util.fdt_cells_to_cpu(val[2:],
  446. 2))
  447. self.assertEqual(0x12345678, fdt_util.fdt_cells_to_cpu(val, 1))
  448. def testEnsureCompiled(self):
  449. """Test a degenerate case of this function (file already compiled)"""
  450. dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts')
  451. self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
  452. def testEnsureCompiledTmpdir(self):
  453. """Test providing a temporary directory"""
  454. try:
  455. old_outdir = tools.outdir
  456. tools.outdir= None
  457. tmpdir = tempfile.mkdtemp(prefix='test_fdt.')
  458. dtb = fdt_util.EnsureCompiled('tools/dtoc/dtoc_test_simple.dts',
  459. tmpdir)
  460. self.assertEqual(tmpdir, os.path.dirname(dtb))
  461. shutil.rmtree(tmpdir)
  462. finally:
  463. tools.outdir= old_outdir
  464. def RunTestCoverage():
  465. """Run the tests and check that we get 100% coverage"""
  466. test_util.RunTestCoverage('tools/dtoc/test_fdt.py', None,
  467. ['tools/patman/*.py', '*test_fdt.py'], options.build_dir)
  468. def RunTests(args):
  469. """Run all the test we have for the fdt model
  470. Args:
  471. args: List of positional args provided to fdt. This can hold a test
  472. name to execute (as in 'fdt -t testFdt', for example)
  473. """
  474. result = unittest.TestResult()
  475. sys.argv = [sys.argv[0]]
  476. test_name = args and args[0] or None
  477. for module in (TestFdt, TestNode, TestProp, TestFdtUtil):
  478. if test_name:
  479. try:
  480. suite = unittest.TestLoader().loadTestsFromName(test_name, module)
  481. except AttributeError:
  482. continue
  483. else:
  484. suite = unittest.TestLoader().loadTestsFromTestCase(module)
  485. suite.run(result)
  486. print(result)
  487. for _, err in result.errors:
  488. print(err)
  489. for _, err in result.failures:
  490. print(err)
  491. if __name__ != '__main__':
  492. sys.exit(1)
  493. parser = OptionParser()
  494. parser.add_option('-B', '--build-dir', type='string', default='b',
  495. help='Directory containing the build output')
  496. parser.add_option('-P', '--processes', type=int,
  497. help='set number of processes to use for running tests')
  498. parser.add_option('-t', '--test', action='store_true', dest='test',
  499. default=False, help='run tests')
  500. parser.add_option('-T', '--test-coverage', action='store_true',
  501. default=False, help='run tests and check for 100% coverage')
  502. (options, args) = parser.parse_args()
  503. # Run our meagre tests
  504. if options.test:
  505. RunTests(args)
  506. elif options.test_coverage:
  507. RunTestCoverage()