xref: /aosp_15_r20/external/dtc/tests/pylibfdt_tests.py (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1*cd60bc56SAndroid Build Coastguard Worker# SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2*cd60bc56SAndroid Build Coastguard Worker# pylibfdt - Tests for Flat Device Tree manipulation in Python
3*cd60bc56SAndroid Build Coastguard Worker# Copyright (C) 2017 Google, Inc.
4*cd60bc56SAndroid Build Coastguard Worker# Written by Simon Glass <[email protected]>
5*cd60bc56SAndroid Build Coastguard Worker#
6*cd60bc56SAndroid Build Coastguard Worker
7*cd60bc56SAndroid Build Coastguard Workerimport struct
8*cd60bc56SAndroid Build Coastguard Workerimport sys
9*cd60bc56SAndroid Build Coastguard Workerimport types
10*cd60bc56SAndroid Build Coastguard Workerimport unittest
11*cd60bc56SAndroid Build Coastguard Worker
12*cd60bc56SAndroid Build Coastguard Workersys.path.insert(0, '../pylibfdt')
13*cd60bc56SAndroid Build Coastguard Workerimport libfdt
14*cd60bc56SAndroid Build Coastguard Workerfrom libfdt import Fdt, FdtSw, FdtException, QUIET_NOTFOUND, QUIET_ALL
15*cd60bc56SAndroid Build Coastguard Worker
16*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_1H = 0xdeadbeef
17*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_1L = 0x00000000
18*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_1 = (TEST_ADDR_1H << 32) | TEST_ADDR_1L
19*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_1 = 0x8000000000000000
20*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_1H = 0x00000000
21*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_1L = 0x00100000
22*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_1 = (TEST_SIZE_1H << 32) | TEST_SIZE_1L
23*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_2H = 0
24*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_2L = 123456789
25*cd60bc56SAndroid Build Coastguard WorkerTEST_ADDR_2 = (TEST_ADDR_2H << 32) | TEST_ADDR_2L
26*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_2H = 0
27*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_2L = 0o10000
28*cd60bc56SAndroid Build Coastguard WorkerTEST_SIZE_2 = (TEST_SIZE_2H << 32) | TEST_SIZE_2L
29*cd60bc56SAndroid Build Coastguard Worker
30*cd60bc56SAndroid Build Coastguard WorkerTEST_VALUE_1 = 0xdeadbeef
31*cd60bc56SAndroid Build Coastguard WorkerTEST_VALUE_2 = 123456789
32*cd60bc56SAndroid Build Coastguard Worker
33*cd60bc56SAndroid Build Coastguard WorkerTEST_VALUE64_1H = 0xdeadbeef
34*cd60bc56SAndroid Build Coastguard WorkerTEST_VALUE64_1L = 0x01abcdef
35*cd60bc56SAndroid Build Coastguard WorkerTEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
36*cd60bc56SAndroid Build Coastguard Worker
37*cd60bc56SAndroid Build Coastguard WorkerPHANDLE_1 = 0x2000
38*cd60bc56SAndroid Build Coastguard WorkerPHANDLE_2 = 0x2001
39*cd60bc56SAndroid Build Coastguard Worker
40*cd60bc56SAndroid Build Coastguard WorkerTEST_BYTES_1 = b'hello world'
41*cd60bc56SAndroid Build Coastguard Worker
42*cd60bc56SAndroid Build Coastguard WorkerTEST_STRING_1 = 'hello world'
43*cd60bc56SAndroid Build Coastguard WorkerTEST_STRING_2 = 'hi world'
44*cd60bc56SAndroid Build Coastguard WorkerTEST_STRING_3 = u'unicode \u01d3'
45*cd60bc56SAndroid Build Coastguard Worker
46*cd60bc56SAndroid Build Coastguard Worker
47*cd60bc56SAndroid Build Coastguard Workerdef get_err(err_code):
48*cd60bc56SAndroid Build Coastguard Worker    """Convert an error code into an error message
49*cd60bc56SAndroid Build Coastguard Worker
50*cd60bc56SAndroid Build Coastguard Worker    Args:
51*cd60bc56SAndroid Build Coastguard Worker        err_code: Error code value (FDT_ERR_...)
52*cd60bc56SAndroid Build Coastguard Worker
53*cd60bc56SAndroid Build Coastguard Worker    Returns:
54*cd60bc56SAndroid Build Coastguard Worker        String error code
55*cd60bc56SAndroid Build Coastguard Worker    """
56*cd60bc56SAndroid Build Coastguard Worker    return 'pylibfdt error %d: %s' % (-err_code, libfdt.strerror(-err_code))
57*cd60bc56SAndroid Build Coastguard Worker
58*cd60bc56SAndroid Build Coastguard Workerdef _ReadFdt(fname):
59*cd60bc56SAndroid Build Coastguard Worker    """Read a device tree file into an Fdt object, ready for use
60*cd60bc56SAndroid Build Coastguard Worker
61*cd60bc56SAndroid Build Coastguard Worker    Args:
62*cd60bc56SAndroid Build Coastguard Worker        fname: Filename to read from
63*cd60bc56SAndroid Build Coastguard Worker
64*cd60bc56SAndroid Build Coastguard Worker    Returns:
65*cd60bc56SAndroid Build Coastguard Worker        Fdt bytearray suitable for passing to libfdt functions
66*cd60bc56SAndroid Build Coastguard Worker    """
67*cd60bc56SAndroid Build Coastguard Worker    with open(fname, mode='rb') as f:
68*cd60bc56SAndroid Build Coastguard Worker        return libfdt.Fdt(f.read())
69*cd60bc56SAndroid Build Coastguard Worker
70*cd60bc56SAndroid Build Coastguard Workerclass PyLibfdtBasicTests(unittest.TestCase):
71*cd60bc56SAndroid Build Coastguard Worker    """Test class for basic pylibfdt access functions
72*cd60bc56SAndroid Build Coastguard Worker
73*cd60bc56SAndroid Build Coastguard Worker    Properties:
74*cd60bc56SAndroid Build Coastguard Worker        fdt: Device tree file used for testing
75*cd60bc56SAndroid Build Coastguard Worker    """
76*cd60bc56SAndroid Build Coastguard Worker
77*cd60bc56SAndroid Build Coastguard Worker    def setUp(self):
78*cd60bc56SAndroid Build Coastguard Worker        """Read in the device tree we use for testing"""
79*cd60bc56SAndroid Build Coastguard Worker        self.fdt = _ReadFdt('test_tree1.dtb')
80*cd60bc56SAndroid Build Coastguard Worker        self.fdt2 = _ReadFdt('test_props.dtb')
81*cd60bc56SAndroid Build Coastguard Worker        self.fdt3 = _ReadFdt('aliases.dtb')
82*cd60bc56SAndroid Build Coastguard Worker
83*cd60bc56SAndroid Build Coastguard Worker    def GetPropList(self, node_path):
84*cd60bc56SAndroid Build Coastguard Worker        """Read a list of properties from a node
85*cd60bc56SAndroid Build Coastguard Worker
86*cd60bc56SAndroid Build Coastguard Worker        Args:
87*cd60bc56SAndroid Build Coastguard Worker            node_path: Full path to node, e.g. '/subnode@1/subsubnode'
88*cd60bc56SAndroid Build Coastguard Worker
89*cd60bc56SAndroid Build Coastguard Worker        Returns:
90*cd60bc56SAndroid Build Coastguard Worker            List of property names for that node, e.g. ['compatible', 'reg']
91*cd60bc56SAndroid Build Coastguard Worker        """
92*cd60bc56SAndroid Build Coastguard Worker        prop_list = []
93*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset(node_path)
94*cd60bc56SAndroid Build Coastguard Worker        poffset = self.fdt.first_property_offset(node, QUIET_NOTFOUND)
95*cd60bc56SAndroid Build Coastguard Worker        while poffset > 0:
96*cd60bc56SAndroid Build Coastguard Worker            prop = self.fdt.get_property_by_offset(poffset)
97*cd60bc56SAndroid Build Coastguard Worker            prop_list.append(prop.name)
98*cd60bc56SAndroid Build Coastguard Worker            poffset = self.fdt.next_property_offset(poffset, QUIET_NOTFOUND)
99*cd60bc56SAndroid Build Coastguard Worker        return prop_list
100*cd60bc56SAndroid Build Coastguard Worker
101*cd60bc56SAndroid Build Coastguard Worker    def GetSubnodes(self, node_path):
102*cd60bc56SAndroid Build Coastguard Worker        """Read a list of subnodes from a node
103*cd60bc56SAndroid Build Coastguard Worker
104*cd60bc56SAndroid Build Coastguard Worker        Args:
105*cd60bc56SAndroid Build Coastguard Worker            node_path: Full path to node, e.g. '/subnode@1/subsubnode'
106*cd60bc56SAndroid Build Coastguard Worker
107*cd60bc56SAndroid Build Coastguard Worker        Returns:
108*cd60bc56SAndroid Build Coastguard Worker            List of subnode names for that node, e.g. ['subsubnode', 'ss1']
109*cd60bc56SAndroid Build Coastguard Worker        """
110*cd60bc56SAndroid Build Coastguard Worker        subnode_list = []
111*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset(node_path)
112*cd60bc56SAndroid Build Coastguard Worker        offset = self.fdt.first_subnode(node, QUIET_NOTFOUND)
113*cd60bc56SAndroid Build Coastguard Worker        while offset > 0:
114*cd60bc56SAndroid Build Coastguard Worker            name = self.fdt.get_name(offset)
115*cd60bc56SAndroid Build Coastguard Worker            subnode_list.append(name)
116*cd60bc56SAndroid Build Coastguard Worker            offset = self.fdt.next_subnode(offset, QUIET_NOTFOUND)
117*cd60bc56SAndroid Build Coastguard Worker        return subnode_list
118*cd60bc56SAndroid Build Coastguard Worker
119*cd60bc56SAndroid Build Coastguard Worker    def testImport(self):
120*cd60bc56SAndroid Build Coastguard Worker        """Check that we can import the library correctly"""
121*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(type(libfdt), types.ModuleType)
122*cd60bc56SAndroid Build Coastguard Worker
123*cd60bc56SAndroid Build Coastguard Worker    def testBadFdt(self):
124*cd60bc56SAndroid Build Coastguard Worker        """Check that a filename provided accidentally is not accepted"""
125*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
126*cd60bc56SAndroid Build Coastguard Worker            fdt = libfdt.Fdt(b'a string')
127*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADMAGIC)
128*cd60bc56SAndroid Build Coastguard Worker
129*cd60bc56SAndroid Build Coastguard Worker    def testSubnodeOffset(self):
130*cd60bc56SAndroid Build Coastguard Worker        """check that we can locate a subnode by name"""
131*cd60bc56SAndroid Build Coastguard Worker        node1 = self.fdt.path_offset('/subnode@1')
132*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.subnode_offset(0, 'subnode@1'), node1)
133*cd60bc56SAndroid Build Coastguard Worker
134*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
135*cd60bc56SAndroid Build Coastguard Worker            self.fdt.subnode_offset(0, 'missing')
136*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
137*cd60bc56SAndroid Build Coastguard Worker
138*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
139*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.subnode_offset(node1, 'subsubnode'), node2)
140*cd60bc56SAndroid Build Coastguard Worker
141*cd60bc56SAndroid Build Coastguard Worker    def testPathOffset(self):
142*cd60bc56SAndroid Build Coastguard Worker        """Check that we can find the offset of a node"""
143*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.path_offset('/'), 0)
144*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(self.fdt.path_offset('/subnode@1') > 0)
145*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
146*cd60bc56SAndroid Build Coastguard Worker            self.fdt.path_offset('/wibble')
147*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
148*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.path_offset('/wibble', QUIET_NOTFOUND),
149*cd60bc56SAndroid Build Coastguard Worker                          -libfdt.NOTFOUND)
150*cd60bc56SAndroid Build Coastguard Worker
151*cd60bc56SAndroid Build Coastguard Worker    def testPropertyOffset(self):
152*cd60bc56SAndroid Build Coastguard Worker        """Walk through all the properties in the root node"""
153*cd60bc56SAndroid Build Coastguard Worker        offset = self.fdt.first_property_offset(0)
154*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(offset > 0)
155*cd60bc56SAndroid Build Coastguard Worker        for i in range(5):
156*cd60bc56SAndroid Build Coastguard Worker            next_offset = self.fdt.next_property_offset(offset)
157*cd60bc56SAndroid Build Coastguard Worker            self.assertTrue(next_offset > offset)
158*cd60bc56SAndroid Build Coastguard Worker            offset = next_offset
159*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.next_property_offset(offset, QUIET_NOTFOUND),
160*cd60bc56SAndroid Build Coastguard Worker                          -libfdt.NOTFOUND)
161*cd60bc56SAndroid Build Coastguard Worker
162*cd60bc56SAndroid Build Coastguard Worker    def testPropertyOffsetExceptions(self):
163*cd60bc56SAndroid Build Coastguard Worker        """Check that exceptions are raised as expected"""
164*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
165*cd60bc56SAndroid Build Coastguard Worker            self.fdt.first_property_offset(107)
166*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
167*cd60bc56SAndroid Build Coastguard Worker
168*cd60bc56SAndroid Build Coastguard Worker        # Quieten the NOTFOUND exception and check that a BADOFFSET
169*cd60bc56SAndroid Build Coastguard Worker        # exception is still raised.
170*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
171*cd60bc56SAndroid Build Coastguard Worker            self.fdt.first_property_offset(107, QUIET_NOTFOUND)
172*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
173*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
174*cd60bc56SAndroid Build Coastguard Worker            self.fdt.next_property_offset(107, QUIET_NOTFOUND)
175*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
176*cd60bc56SAndroid Build Coastguard Worker
177*cd60bc56SAndroid Build Coastguard Worker        # Check that NOTFOUND can be quietened.
178*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/ss1')
179*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.first_property_offset(node, QUIET_NOTFOUND),
180*cd60bc56SAndroid Build Coastguard Worker                          -libfdt.NOTFOUND)
181*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
182*cd60bc56SAndroid Build Coastguard Worker            self.fdt.first_property_offset(node)
183*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
184*cd60bc56SAndroid Build Coastguard Worker
185*cd60bc56SAndroid Build Coastguard Worker    def testGetName(self):
186*cd60bc56SAndroid Build Coastguard Worker        """Check that we can get the name of a node"""
187*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.get_name(0), '')
188*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/subsubnode')
189*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.get_name(node), 'subsubnode')
190*cd60bc56SAndroid Build Coastguard Worker
191*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
192*cd60bc56SAndroid Build Coastguard Worker            self.fdt.get_name(-2)
193*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
194*cd60bc56SAndroid Build Coastguard Worker
195*cd60bc56SAndroid Build Coastguard Worker    def testGetPropertyByOffset(self):
196*cd60bc56SAndroid Build Coastguard Worker        """Check that we can read the name and contents of a property"""
197*cd60bc56SAndroid Build Coastguard Worker        root = 0
198*cd60bc56SAndroid Build Coastguard Worker        poffset = self.fdt.first_property_offset(root)
199*cd60bc56SAndroid Build Coastguard Worker        prop = self.fdt.get_property_by_offset(poffset)
200*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(prop.name, 'compatible')
201*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(prop, b'test_tree1\0')
202*cd60bc56SAndroid Build Coastguard Worker
203*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
204*cd60bc56SAndroid Build Coastguard Worker            self.fdt.get_property_by_offset(-2)
205*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
206*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(
207*cd60bc56SAndroid Build Coastguard Worker                -libfdt.BADOFFSET,
208*cd60bc56SAndroid Build Coastguard Worker                self.fdt.get_property_by_offset(-2, [libfdt.BADOFFSET]))
209*cd60bc56SAndroid Build Coastguard Worker
210*cd60bc56SAndroid Build Coastguard Worker    def testGetProp(self):
211*cd60bc56SAndroid Build Coastguard Worker        """Check that we can read the contents of a property by name"""
212*cd60bc56SAndroid Build Coastguard Worker        root = self.fdt.path_offset('/')
213*cd60bc56SAndroid Build Coastguard Worker        value = self.fdt.getprop(root, "compatible")
214*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(value, b'test_tree1\0')
215*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOTFOUND, self.fdt.getprop(root, 'missing',
216*cd60bc56SAndroid Build Coastguard Worker                                                             QUIET_NOTFOUND))
217*cd60bc56SAndroid Build Coastguard Worker
218*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
219*cd60bc56SAndroid Build Coastguard Worker            self.fdt.getprop(root, 'missing')
220*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
221*cd60bc56SAndroid Build Coastguard Worker
222*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/subsubnode')
223*cd60bc56SAndroid Build Coastguard Worker        value = self.fdt.getprop(node, "compatible")
224*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(value, b'subsubnode1\0subsubnode\0')
225*cd60bc56SAndroid Build Coastguard Worker
226*cd60bc56SAndroid Build Coastguard Worker    def testStrError(self):
227*cd60bc56SAndroid Build Coastguard Worker        """Check that we can get an error string"""
228*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(libfdt.strerror(-libfdt.NOTFOUND),
229*cd60bc56SAndroid Build Coastguard Worker                          'FDT_ERR_NOTFOUND')
230*cd60bc56SAndroid Build Coastguard Worker
231*cd60bc56SAndroid Build Coastguard Worker    def testNextNodeOffset(self):
232*cd60bc56SAndroid Build Coastguard Worker        """Check that we can walk through nodes"""
233*cd60bc56SAndroid Build Coastguard Worker        node_list = []
234*cd60bc56SAndroid Build Coastguard Worker        node = 0
235*cd60bc56SAndroid Build Coastguard Worker        depth = 0
236*cd60bc56SAndroid Build Coastguard Worker        while depth >= 0:
237*cd60bc56SAndroid Build Coastguard Worker            node_list.append([depth, self.fdt.get_name(node)])
238*cd60bc56SAndroid Build Coastguard Worker            node, depth = self.fdt.next_node(node, depth, (libfdt.BADOFFSET,))
239*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(node_list, [
240*cd60bc56SAndroid Build Coastguard Worker            [0, ''],
241*cd60bc56SAndroid Build Coastguard Worker            [1, 'subnode@1'],
242*cd60bc56SAndroid Build Coastguard Worker            [2, 'subsubnode'],
243*cd60bc56SAndroid Build Coastguard Worker            [2, 'ss1'],
244*cd60bc56SAndroid Build Coastguard Worker            [1, 'subnode@2'],
245*cd60bc56SAndroid Build Coastguard Worker            [2, 'subsubnode@0'],
246*cd60bc56SAndroid Build Coastguard Worker            [2, 'ss2'],
247*cd60bc56SAndroid Build Coastguard Worker            ])
248*cd60bc56SAndroid Build Coastguard Worker
249*cd60bc56SAndroid Build Coastguard Worker    def testFirstNextSubnodeOffset(self):
250*cd60bc56SAndroid Build Coastguard Worker        """Check that we can walk through subnodes"""
251*cd60bc56SAndroid Build Coastguard Worker        node_list = []
252*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.first_subnode(0, QUIET_NOTFOUND)
253*cd60bc56SAndroid Build Coastguard Worker        while node >= 0:
254*cd60bc56SAndroid Build Coastguard Worker            node_list.append(self.fdt.get_name(node))
255*cd60bc56SAndroid Build Coastguard Worker            node = self.fdt.next_subnode(node, QUIET_NOTFOUND)
256*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(node_list, ['subnode@1', 'subnode@2'])
257*cd60bc56SAndroid Build Coastguard Worker
258*cd60bc56SAndroid Build Coastguard Worker    def testFirstNextSubnodeOffsetExceptions(self):
259*cd60bc56SAndroid Build Coastguard Worker        """Check except handling for first/next subnode functions"""
260*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/subsubnode', QUIET_NOTFOUND)
261*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.first_subnode(node, QUIET_NOTFOUND),
262*cd60bc56SAndroid Build Coastguard Worker                          -libfdt.NOTFOUND)
263*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
264*cd60bc56SAndroid Build Coastguard Worker            self.fdt.first_subnode(node)
265*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
266*cd60bc56SAndroid Build Coastguard Worker
267*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/ss1', QUIET_NOTFOUND)
268*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.next_subnode(node, QUIET_NOTFOUND),
269*cd60bc56SAndroid Build Coastguard Worker                          -libfdt.NOTFOUND)
270*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
271*cd60bc56SAndroid Build Coastguard Worker            self.fdt.next_subnode(node)
272*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
273*cd60bc56SAndroid Build Coastguard Worker
274*cd60bc56SAndroid Build Coastguard Worker    def testDeleteProperty(self):
275*cd60bc56SAndroid Build Coastguard Worker        """Test that we can delete a property"""
276*cd60bc56SAndroid Build Coastguard Worker        node_name = '/subnode@1'
277*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.GetPropList(node_name),
278*cd60bc56SAndroid Build Coastguard Worker                          ['compatible', 'reg', 'prop-int'])
279*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/%s' % node_name)
280*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.delprop(node, 'reg'), 0)
281*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.GetPropList(node_name),
282*cd60bc56SAndroid Build Coastguard Worker                          ['compatible', 'prop-int'])
283*cd60bc56SAndroid Build Coastguard Worker
284*cd60bc56SAndroid Build Coastguard Worker    def testHeader(self):
285*cd60bc56SAndroid Build Coastguard Worker        """Test that we can access the header values"""
286*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.magic(), 0xd00dfeed)
287*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.totalsize(), len(self.fdt._fdt))
288*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.off_dt_struct(), 88)
289*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.off_dt_strings(), 652)
290*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.off_mem_rsvmap(), 40)
291*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.version(), 17)
292*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.last_comp_version(), 16)
293*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.boot_cpuid_phys(), 0)
294*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.size_dt_strings(), 105)
295*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.size_dt_struct(), 564)
296*cd60bc56SAndroid Build Coastguard Worker
297*cd60bc56SAndroid Build Coastguard Worker    def testPack(self):
298*cd60bc56SAndroid Build Coastguard Worker        """Test that we can pack the tree after deleting something"""
299*cd60bc56SAndroid Build Coastguard Worker        orig_size = self.fdt.totalsize()
300*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@2', QUIET_NOTFOUND)
301*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.delprop(node, 'prop-int'), 0)
302*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(orig_size, self.fdt.totalsize())
303*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.pack(), 0)
304*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(self.fdt.totalsize() < orig_size)
305*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.totalsize(), len(self.fdt.as_bytearray()))
306*cd60bc56SAndroid Build Coastguard Worker
307*cd60bc56SAndroid Build Coastguard Worker    def testBadPropertyOffset(self):
308*cd60bc56SAndroid Build Coastguard Worker        """Test that bad property offsets are detected"""
309*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
310*cd60bc56SAndroid Build Coastguard Worker            self.fdt.get_property_by_offset(13)
311*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
312*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
313*cd60bc56SAndroid Build Coastguard Worker            self.fdt.first_property_offset(3)
314*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
315*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
316*cd60bc56SAndroid Build Coastguard Worker            self.fdt.next_property_offset(3)
317*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
318*cd60bc56SAndroid Build Coastguard Worker
319*cd60bc56SAndroid Build Coastguard Worker    def testBadPathOffset(self):
320*cd60bc56SAndroid Build Coastguard Worker        """Test that bad path names are detected"""
321*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaisesRegex(FdtException, get_err(libfdt.BADPATH)):
322*cd60bc56SAndroid Build Coastguard Worker            self.fdt.path_offset('not-present')
323*cd60bc56SAndroid Build Coastguard Worker
324*cd60bc56SAndroid Build Coastguard Worker    def testQuietAll(self):
325*cd60bc56SAndroid Build Coastguard Worker        """Check that exceptions can be masked by QUIET_ALL"""
326*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOTFOUND,
327*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.path_offset('/missing', QUIET_ALL))
328*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.BADOFFSET,
329*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.get_property_by_offset(13, QUIET_ALL))
330*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.BADPATH,
331*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.path_offset('missing', QUIET_ALL))
332*cd60bc56SAndroid Build Coastguard Worker
333*cd60bc56SAndroid Build Coastguard Worker    def testIntegers(self):
334*cd60bc56SAndroid Build Coastguard Worker        """Check that integers can be passed and returned"""
335*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0, libfdt.fdt_get_phandle(self.fdt._fdt, 0))
336*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@2')
337*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0x2000, libfdt.fdt_get_phandle(self.fdt._fdt, node2))
338*cd60bc56SAndroid Build Coastguard Worker
339*cd60bc56SAndroid Build Coastguard Worker    def testGetPhandle(self):
340*cd60bc56SAndroid Build Coastguard Worker        """Test for the get_phandle() method"""
341*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0, self.fdt.get_phandle(0))
342*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@2')
343*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0x2000, self.fdt.get_phandle(node2))
344*cd60bc56SAndroid Build Coastguard Worker
345*cd60bc56SAndroid Build Coastguard Worker    def testGetAlias(self):
346*cd60bc56SAndroid Build Coastguard Worker        """Test for the get_alias() method"""
347*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1", self.fdt3.get_alias('s1'))
348*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1'))
349*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))
350*cd60bc56SAndroid Build Coastguard Worker
351*cd60bc56SAndroid Build Coastguard Worker    def testGetPath(self):
352*cd60bc56SAndroid Build Coastguard Worker        """Test for the get_path() method"""
353*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1')
354*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
355*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1", self.fdt.get_path(node))
356*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2))
357*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2, size_hint=1))
358*cd60bc56SAndroid Build Coastguard Worker
359*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
360*cd60bc56SAndroid Build Coastguard Worker            self.fdt.get_path(-1)
361*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
362*cd60bc56SAndroid Build Coastguard Worker
363*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.BADOFFSET, self.fdt.get_path(-1, quiet=(libfdt.BADOFFSET,)))
364*cd60bc56SAndroid Build Coastguard Worker
365*cd60bc56SAndroid Build Coastguard Worker    def testParentOffset(self):
366*cd60bc56SAndroid Build Coastguard Worker        """Test for the parent_offset() method"""
367*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOTFOUND,
368*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.parent_offset(0, QUIET_NOTFOUND))
369*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(FdtException) as e:
370*cd60bc56SAndroid Build Coastguard Worker            self.fdt.parent_offset(0)
371*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(e.exception.err, -libfdt.NOTFOUND)
372*cd60bc56SAndroid Build Coastguard Worker
373*cd60bc56SAndroid Build Coastguard Worker        node1 = self.fdt.path_offset('/subnode@2')
374*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0, self.fdt.parent_offset(node1))
375*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
376*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(node1, self.fdt.parent_offset(node2))
377*cd60bc56SAndroid Build Coastguard Worker
378*cd60bc56SAndroid Build Coastguard Worker    def testNodeOffsetByPhandle(self):
379*cd60bc56SAndroid Build Coastguard Worker        """Test for the node_offset_by_phandle() method"""
380*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOTFOUND,
381*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.node_offset_by_phandle(1, QUIET_NOTFOUND))
382*cd60bc56SAndroid Build Coastguard Worker        node1 = self.fdt.path_offset('/subnode@2')
383*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(node1, self.fdt.node_offset_by_phandle(0x2000))
384*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@2/subsubnode@0')
385*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(node2, self.fdt.node_offset_by_phandle(0x2001))
386*cd60bc56SAndroid Build Coastguard Worker
387*cd60bc56SAndroid Build Coastguard Worker    def get_prop(self, name):
388*cd60bc56SAndroid Build Coastguard Worker        return self.fdt2.getprop(0, name)
389*cd60bc56SAndroid Build Coastguard Worker
390*cd60bc56SAndroid Build Coastguard Worker    def testGetIntProperties(self):
391*cd60bc56SAndroid Build Coastguard Worker        """Test that we can access properties as integers"""
392*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0xdeadbeef, self.get_prop("prop-hex32").as_uint32())
393*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(123, self.get_prop("prop-uint32").as_uint32())
394*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-2, self.get_prop("prop-int32").as_int32())
395*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(9223372036854775807,
396*cd60bc56SAndroid Build Coastguard Worker                          self.get_prop("prop-uint64").as_uint64())
397*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-2, self.get_prop("prop-int64").as_int64())
398*cd60bc56SAndroid Build Coastguard Worker
399*cd60bc56SAndroid Build Coastguard Worker    def testGetIntListProperties(self):
400*cd60bc56SAndroid Build Coastguard Worker        """Test that we can access properties as integer lists"""
401*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([128, -16, -2],
402*cd60bc56SAndroid Build Coastguard Worker                         self.get_prop("prop-int32-array").as_int32_list())
403*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([0x1, 0x98765432, 0xdeadbeef],
404*cd60bc56SAndroid Build Coastguard Worker                         self.get_prop("prop-uint32-array").as_uint32_list())
405*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([0x100000000, -2],
406*cd60bc56SAndroid Build Coastguard Worker                         self.get_prop("prop-int64-array").as_int64_list())
407*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([0x100000000, 0x1],
408*cd60bc56SAndroid Build Coastguard Worker                         self.get_prop("prop-uint64-array").as_uint64_list())
409*cd60bc56SAndroid Build Coastguard Worker
410*cd60bc56SAndroid Build Coastguard Worker    def testGetStringlistProperties(self):
411*cd60bc56SAndroid Build Coastguard Worker        """Test that we can access properties as string list"""
412*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1/subsubnode')
413*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(["subsubnode1", "subsubnode"],
414*cd60bc56SAndroid Build Coastguard Worker                         self.fdt.getprop(node, "compatible").as_stringlist())
415*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(["this is a placeholder string", "string2"],
416*cd60bc56SAndroid Build Coastguard Worker                         self.fdt.getprop(node, "placeholder").as_stringlist())
417*cd60bc56SAndroid Build Coastguard Worker
418*cd60bc56SAndroid Build Coastguard Worker    def testReserveMap(self):
419*cd60bc56SAndroid Build Coastguard Worker        """Test that we can access the memory reserve map"""
420*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(2, self.fdt.num_mem_rsv())
421*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([ 0xdeadbeef00000000, 0x100000],
422*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.get_mem_rsv(0))
423*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([123456789, 0o10000], self.fdt.get_mem_rsv(1))
424*cd60bc56SAndroid Build Coastguard Worker
425*cd60bc56SAndroid Build Coastguard Worker    def testEmpty(self):
426*cd60bc56SAndroid Build Coastguard Worker        """Test that we can create an empty tree"""
427*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOSPACE,
428*cd60bc56SAndroid Build Coastguard Worker                          Fdt.create_empty_tree(1, (libfdt.NOSPACE,)))
429*cd60bc56SAndroid Build Coastguard Worker        fdt = Fdt.create_empty_tree(128)
430*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(128, fdt.totalsize())
431*cd60bc56SAndroid Build Coastguard Worker
432*cd60bc56SAndroid Build Coastguard Worker    def testOpenInto(self):
433*cd60bc56SAndroid Build Coastguard Worker        """Test that we can resize a tree"""
434*cd60bc56SAndroid Build Coastguard Worker        fdt = Fdt.create_empty_tree(128)
435*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(128, fdt.totalsize())
436*cd60bc56SAndroid Build Coastguard Worker        fdt.resize(256)
437*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(256, fdt.totalsize())
438*cd60bc56SAndroid Build Coastguard Worker        fdt.pack()
439*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(fdt.totalsize() < 128)
440*cd60bc56SAndroid Build Coastguard Worker
441*cd60bc56SAndroid Build Coastguard Worker    def testSetProp(self):
442*cd60bc56SAndroid Build Coastguard Worker        """Test that we can update and create properties"""
443*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1')
444*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop(node, 'compatible', TEST_BYTES_1)
445*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(TEST_BYTES_1, self.fdt.getprop(node, 'compatible'))
446*cd60bc56SAndroid Build Coastguard Worker
447*cd60bc56SAndroid Build Coastguard Worker        # Check that this property is missing, and that we don't have space to
448*cd60bc56SAndroid Build Coastguard Worker        # add it
449*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOTFOUND,
450*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.getprop(node, 'missing', QUIET_NOTFOUND))
451*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(-libfdt.NOSPACE,
452*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.setprop(node, 'missing', TEST_BYTES_1,
453*cd60bc56SAndroid Build Coastguard Worker                                           quiet=(libfdt.NOSPACE,)))
454*cd60bc56SAndroid Build Coastguard Worker
455*cd60bc56SAndroid Build Coastguard Worker        # Expand the device tree so we now have room
456*cd60bc56SAndroid Build Coastguard Worker        self.fdt.resize(self.fdt.totalsize() + 50)
457*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop(node, 'missing', TEST_BYTES_1)
458*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(TEST_BYTES_1, self.fdt.getprop(node, 'missing'))
459*cd60bc56SAndroid Build Coastguard Worker
460*cd60bc56SAndroid Build Coastguard Worker    def testSetPropU32(self):
461*cd60bc56SAndroid Build Coastguard Worker        """Test that we can update and create integer properties"""
462*cd60bc56SAndroid Build Coastguard Worker        node = 0
463*cd60bc56SAndroid Build Coastguard Worker        prop = 'prop-int'
464*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop_u32(node, prop, TEST_VALUE_1)
465*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(struct.pack('>I', TEST_VALUE_1),
466*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.getprop(node, prop))
467*cd60bc56SAndroid Build Coastguard Worker
468*cd60bc56SAndroid Build Coastguard Worker    def testSetPropU64(self):
469*cd60bc56SAndroid Build Coastguard Worker        """Test that we can update and create integer properties"""
470*cd60bc56SAndroid Build Coastguard Worker        node = 0
471*cd60bc56SAndroid Build Coastguard Worker        prop = 'prop-int64'
472*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop_u64(node, prop, TEST_VALUE64_1)
473*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(struct.pack('>Q', TEST_VALUE64_1),
474*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.getprop(node, prop))
475*cd60bc56SAndroid Build Coastguard Worker
476*cd60bc56SAndroid Build Coastguard Worker    def testSetPropStr(self):
477*cd60bc56SAndroid Build Coastguard Worker        """Test that we can set a property to a particular string"""
478*cd60bc56SAndroid Build Coastguard Worker        node = 0
479*cd60bc56SAndroid Build Coastguard Worker        prop = 'prop-str'
480*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(TEST_STRING_1, self.fdt.getprop(node, prop).as_str())
481*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop_str(node, prop, TEST_STRING_2)
482*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(TEST_STRING_2, self.fdt.getprop(node, prop).as_str())
483*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(ValueError) as e:
484*cd60bc56SAndroid Build Coastguard Worker            self.fdt.getprop(node, 'prop-int').as_str()
485*cd60bc56SAndroid Build Coastguard Worker        self.assertIn('lacks nul termination', str(e.exception))
486*cd60bc56SAndroid Build Coastguard Worker
487*cd60bc56SAndroid Build Coastguard Worker        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
488*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(ValueError) as e:
489*cd60bc56SAndroid Build Coastguard Worker            self.fdt.getprop(node2, 'compatible').as_str()
490*cd60bc56SAndroid Build Coastguard Worker        self.assertIn('embedded nul', str(e.exception))
491*cd60bc56SAndroid Build Coastguard Worker
492*cd60bc56SAndroid Build Coastguard Worker        # Expand the device tree so we now have room
493*cd60bc56SAndroid Build Coastguard Worker        self.fdt.resize(self.fdt.totalsize() + 50)
494*cd60bc56SAndroid Build Coastguard Worker        prop = 'prop-unicode'
495*cd60bc56SAndroid Build Coastguard Worker        self.fdt.setprop_str(node, prop, TEST_STRING_3)
496*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(TEST_STRING_3,
497*cd60bc56SAndroid Build Coastguard Worker                          self.fdt.getprop(node, prop).as_str())
498*cd60bc56SAndroid Build Coastguard Worker
499*cd60bc56SAndroid Build Coastguard Worker    def testSetName(self):
500*cd60bc56SAndroid Build Coastguard Worker        """Test that we can update a node name"""
501*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1')
502*cd60bc56SAndroid Build Coastguard Worker        old_val = self.fdt.get_name(node)
503*cd60bc56SAndroid Build Coastguard Worker        self.fdt.set_name(node, 'test')
504*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual('test', self.fdt.get_name(node))
505*cd60bc56SAndroid Build Coastguard Worker
506*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(ValueError) as e:
507*cd60bc56SAndroid Build Coastguard Worker            self.fdt.set_name(node, 'some\0name')
508*cd60bc56SAndroid Build Coastguard Worker        self.assertIn('embedded nul', str(e.exception))
509*cd60bc56SAndroid Build Coastguard Worker
510*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaises(ValueError) as e:
511*cd60bc56SAndroid Build Coastguard Worker            self.fdt.set_name(node, 'name\0')
512*cd60bc56SAndroid Build Coastguard Worker        self.assertIn('embedded nul', str(e.exception))
513*cd60bc56SAndroid Build Coastguard Worker
514*cd60bc56SAndroid Build Coastguard Worker    def testAddDeleteNodes(self):
515*cd60bc56SAndroid Build Coastguard Worker        """Test that we can add and delete nodes"""
516*cd60bc56SAndroid Build Coastguard Worker        node_name = '/subnode@1'
517*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.GetSubnodes(node_name), ['subsubnode', 'ss1'])
518*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('%s/subsubnode' % node_name)
519*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.fdt.del_node(node, 'subsubnode'), 0)
520*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.GetSubnodes(node_name), ['ss1'])
521*cd60bc56SAndroid Build Coastguard Worker
522*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset(node_name)
523*cd60bc56SAndroid Build Coastguard Worker        offset = self.fdt.add_subnode(node, 'more')
524*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(offset > 0)
525*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(self.GetSubnodes(node_name), ['more', 'ss1'])
526*cd60bc56SAndroid Build Coastguard Worker
527*cd60bc56SAndroid Build Coastguard Worker
528*cd60bc56SAndroid Build Coastguard Workerclass PyLibfdtSwTests(unittest.TestCase):
529*cd60bc56SAndroid Build Coastguard Worker    """Test class for pylibfdt sequential-write DT creation
530*cd60bc56SAndroid Build Coastguard Worker    """
531*cd60bc56SAndroid Build Coastguard Worker    def assertOk(self, err_code):
532*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(0, err_code)
533*cd60bc56SAndroid Build Coastguard Worker
534*cd60bc56SAndroid Build Coastguard Worker    def testCreate(self):
535*cd60bc56SAndroid Build Coastguard Worker        # First check the minimum size and also the FdtSw() constructor
536*cd60bc56SAndroid Build Coastguard Worker        with self.assertRaisesRegex(FdtException, get_err(libfdt.NOSPACE)):
537*cd60bc56SAndroid Build Coastguard Worker            self.assertEqual(-libfdt.NOSPACE, FdtSw(3))
538*cd60bc56SAndroid Build Coastguard Worker
539*cd60bc56SAndroid Build Coastguard Worker        sw = FdtSw()
540*cd60bc56SAndroid Build Coastguard Worker        sw.add_reservemap_entry(TEST_ADDR_1, TEST_SIZE_1)
541*cd60bc56SAndroid Build Coastguard Worker        sw.add_reservemap_entry(TEST_ADDR_2, TEST_SIZE_2)
542*cd60bc56SAndroid Build Coastguard Worker        sw.finish_reservemap()
543*cd60bc56SAndroid Build Coastguard Worker
544*cd60bc56SAndroid Build Coastguard Worker        sw.begin_node('')
545*cd60bc56SAndroid Build Coastguard Worker        sw.property_string('compatible', 'test_tree1')
546*cd60bc56SAndroid Build Coastguard Worker        sw.property_u32('prop-int', TEST_VALUE_1)
547*cd60bc56SAndroid Build Coastguard Worker
548*cd60bc56SAndroid Build Coastguard Worker        sw.property_u32('prop-int', TEST_VALUE_1)
549*cd60bc56SAndroid Build Coastguard Worker        sw.property_u64('prop-int64', TEST_VALUE64_1)
550*cd60bc56SAndroid Build Coastguard Worker        sw.property_string('prop-str', TEST_STRING_1)
551*cd60bc56SAndroid Build Coastguard Worker        sw.property_u32('#address-cells', 1)
552*cd60bc56SAndroid Build Coastguard Worker        sw.property_u32('#size-cells', 0)
553*cd60bc56SAndroid Build Coastguard Worker
554*cd60bc56SAndroid Build Coastguard Worker        sw.begin_node('subnode@1')
555*cd60bc56SAndroid Build Coastguard Worker        sw.property_string('compatible', 'subnode1')
556*cd60bc56SAndroid Build Coastguard Worker        sw.property_u32('reg', 1)
557*cd60bc56SAndroid Build Coastguard Worker        sw.property_cell('prop-int', TEST_VALUE_1)
558*cd60bc56SAndroid Build Coastguard Worker        sw.property('data', b'\x00data\x01')
559*cd60bc56SAndroid Build Coastguard Worker        sw.begin_node('subsubnode')
560*cd60bc56SAndroid Build Coastguard Worker        sw.property('compatible', b'subsubnode1\0subsubnode')
561*cd60bc56SAndroid Build Coastguard Worker        sw.property_cell('prop-int', TEST_VALUE_1)
562*cd60bc56SAndroid Build Coastguard Worker        sw.end_node()
563*cd60bc56SAndroid Build Coastguard Worker        sw.begin_node('ss1')
564*cd60bc56SAndroid Build Coastguard Worker        sw.end_node()
565*cd60bc56SAndroid Build Coastguard Worker        sw.end_node()
566*cd60bc56SAndroid Build Coastguard Worker
567*cd60bc56SAndroid Build Coastguard Worker        for i in range(2, 11):
568*cd60bc56SAndroid Build Coastguard Worker            with sw.add_node('subnode@%d' % i):
569*cd60bc56SAndroid Build Coastguard Worker                sw.property_u32('reg', 2)
570*cd60bc56SAndroid Build Coastguard Worker                sw.property_cell('linux,phandle', PHANDLE_1)
571*cd60bc56SAndroid Build Coastguard Worker                sw.property_cell('prop-int', TEST_VALUE_2)
572*cd60bc56SAndroid Build Coastguard Worker                sw.property_u32('#address-cells', 1)
573*cd60bc56SAndroid Build Coastguard Worker                sw.property_u32('#size-cells', 0)
574*cd60bc56SAndroid Build Coastguard Worker                with sw.add_node('subsubnode@0'):
575*cd60bc56SAndroid Build Coastguard Worker                    sw.property_u32('reg', 0)
576*cd60bc56SAndroid Build Coastguard Worker                    sw.property_cell('phandle', PHANDLE_2)
577*cd60bc56SAndroid Build Coastguard Worker                    sw.property('compatible', b'subsubnode2\0subsubnode')
578*cd60bc56SAndroid Build Coastguard Worker                    sw.property_cell('prop-int', TEST_VALUE_2)
579*cd60bc56SAndroid Build Coastguard Worker                with sw.add_node('ss2'):
580*cd60bc56SAndroid Build Coastguard Worker                    pass
581*cd60bc56SAndroid Build Coastguard Worker        sw.end_node()
582*cd60bc56SAndroid Build Coastguard Worker
583*cd60bc56SAndroid Build Coastguard Worker        fdt = sw.as_fdt()
584*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(2, fdt.num_mem_rsv())
585*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual([TEST_ADDR_1, TEST_SIZE_1], fdt.get_mem_rsv(0))
586*cd60bc56SAndroid Build Coastguard Worker
587*cd60bc56SAndroid Build Coastguard Worker        # Make sure we can add a few more things
588*cd60bc56SAndroid Build Coastguard Worker        with sw.add_node('another'):
589*cd60bc56SAndroid Build Coastguard Worker            sw.property_u32('reg', 3)
590*cd60bc56SAndroid Build Coastguard Worker
591*cd60bc56SAndroid Build Coastguard Worker        # Make sure we can read from the tree too
592*cd60bc56SAndroid Build Coastguard Worker        node = sw.path_offset('/subnode@1')
593*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(b'subnode1\0', sw.getprop(node, 'compatible'))
594*cd60bc56SAndroid Build Coastguard Worker
595*cd60bc56SAndroid Build Coastguard Worker        # Make sure we did at least two resizes
596*cd60bc56SAndroid Build Coastguard Worker        self.assertTrue(len(fdt.as_bytearray()) > FdtSw.INC_SIZE * 2)
597*cd60bc56SAndroid Build Coastguard Worker
598*cd60bc56SAndroid Build Coastguard Worker
599*cd60bc56SAndroid Build Coastguard Workerclass PyLibfdtRoTests(unittest.TestCase):
600*cd60bc56SAndroid Build Coastguard Worker    """Test class for read-only pylibfdt access functions
601*cd60bc56SAndroid Build Coastguard Worker
602*cd60bc56SAndroid Build Coastguard Worker    This just tests a few simple cases. Most of the tests are in
603*cd60bc56SAndroid Build Coastguard Worker    PyLibfdtBasicTests.
604*cd60bc56SAndroid Build Coastguard Worker
605*cd60bc56SAndroid Build Coastguard Worker    Properties:
606*cd60bc56SAndroid Build Coastguard Worker        fdt: Device tree file used for testing
607*cd60bc56SAndroid Build Coastguard Worker    """
608*cd60bc56SAndroid Build Coastguard Worker
609*cd60bc56SAndroid Build Coastguard Worker    def setUp(self):
610*cd60bc56SAndroid Build Coastguard Worker        """Read in the device tree we use for testing"""
611*cd60bc56SAndroid Build Coastguard Worker        with open('test_tree1.dtb', mode='rb') as f:
612*cd60bc56SAndroid Build Coastguard Worker            self.fdt = libfdt.FdtRo(f.read())
613*cd60bc56SAndroid Build Coastguard Worker
614*cd60bc56SAndroid Build Coastguard Worker    def testAccess(self):
615*cd60bc56SAndroid Build Coastguard Worker        """Basic sanity check for the FdtRo class"""
616*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.path_offset('/subnode@1')
617*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(b'subnode1\0',
618*cd60bc56SAndroid Build Coastguard Worker                         self.fdt.getprop(node, 'compatible'))
619*cd60bc56SAndroid Build Coastguard Worker        node = self.fdt.first_subnode(node)
620*cd60bc56SAndroid Build Coastguard Worker        self.assertEqual(b'this is a placeholder string\0string2\0',
621*cd60bc56SAndroid Build Coastguard Worker                         self.fdt.getprop(node, 'placeholder'))
622*cd60bc56SAndroid Build Coastguard Worker
623*cd60bc56SAndroid Build Coastguard Worker
624*cd60bc56SAndroid Build Coastguard Workerif __name__ == "__main__":
625*cd60bc56SAndroid Build Coastguard Worker    unittest.main()
626