Commit 8512ea2e359cf5217183fc87326a015caab1994d

Authored by Simon Glass
1 parent c3f9454103

dtoc: Increase code coverage to 100%

Add more tests to increase dtoc code coverage to 100%.

Correct a whitespace error in some test .dts files at the same time.

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 10 changed files with 285 additions and 2 deletions Side-by-side Diff

tools/dtoc/dtoc_test_add_prop.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + #address-cells = <1>;
  12 + #size-cells = <1>;
  13 + spl-test {
  14 + u-boot,dm-pre-reloc;
  15 + compatible = "sandbox,spl-test";
  16 + intval = <1>;
  17 + };
  18 +
  19 + spl-test2 {
  20 + u-boot,dm-pre-reloc;
  21 + compatible = "sandbox,spl-test";
  22 + intarray = <5>;
  23 + };
  24 +};
tools/dtoc/dtoc_test_addr32_64.dts
... ... @@ -5,7 +5,7 @@
5 5 * Copyright 2017 Google, Inc
6 6 */
7 7  
8   - /dts-v1/;
  8 +/dts-v1/;
9 9  
10 10 / {
11 11 #address-cells = <1>;
tools/dtoc/dtoc_test_addr64_32.dts
... ... @@ -5,7 +5,7 @@
5 5 * Copyright 2017 Google, Inc
6 6 */
7 7  
8   - /dts-v1/;
  8 +/dts-v1/;
9 9  
10 10 / {
11 11 #address-cells = <2>;
tools/dtoc/dtoc_test_bad_reg.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + #address-cells = <1>;
  12 + #size-cells = <1>;
  13 + spl-test {
  14 + compatible = "test";
  15 + reg = "fre";
  16 + };
  17 +};
tools/dtoc/dtoc_test_bad_reg2.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + #address-cells = <1>;
  12 + #size-cells = <1>;
  13 + spl-test {
  14 + compatible = "test";
  15 + reg = <1 2 3>;
  16 + };
  17 +};
tools/dtoc/dtoc_test_phandle_bad.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + phandle-source {
  12 + u-boot,dm-pre-reloc;
  13 + compatible = "source";
  14 + clocks = <20>; /* Invalid phandle */
  15 + };
  16 +};
tools/dtoc/dtoc_test_phandle_bad2.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + phandle: phandle-target {
  12 + u-boot,dm-pre-reloc;
  13 + compatible = "target";
  14 + intval = <0>;
  15 + };
  16 +
  17 + phandle-source2 {
  18 + u-boot,dm-pre-reloc;
  19 + compatible = "source";
  20 + clocks = <&phandle>;
  21 + };
  22 +};
tools/dtoc/dtoc_test_phandle_reorder.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 +
  12 + phandle-source2 {
  13 + u-boot,dm-pre-reloc;
  14 + compatible = "source";
  15 + clocks = <&phandle>;
  16 + };
  17 +
  18 + phandle: phandle-target {
  19 + u-boot,dm-pre-reloc;
  20 + compatible = "target";
  21 + #clock-cells = <0>;
  22 + };
  23 +};
tools/dtoc/dtoc_test_phandle_single.dts
  1 +// SPDX-License-Identifier: GPL-2.0+
  2 +/*
  3 + * Test device tree file for dtoc
  4 + *
  5 + * Copyright 2018 Google, Inc
  6 + */
  7 +
  8 +/dts-v1/;
  9 +
  10 +/ {
  11 + phandle: phandle-target {
  12 + u-boot,dm-pre-reloc;
  13 + compatible = "target";
  14 + intval = <0>;
  15 + #clock-cells = <0>;
  16 + };
  17 +
  18 + phandle-source2 {
  19 + u-boot,dm-pre-reloc;
  20 + compatible = "source";
  21 + clocks = <&phandle>;
  22 + };
  23 +};
tools/dtoc/test_dtoc.py
... ... @@ -20,6 +20,7 @@
20 20 from dtb_platdata import tab_to
21 21 import fdt
22 22 import fdt_util
  23 +import test_util
23 24 import tools
24 25  
25 26 our_path = os.path.dirname(os.path.realpath(__file__))
... ... @@ -335,6 +336,68 @@
335 336  
336 337 ''', data)
337 338  
  339 + def test_phandle_single(self):
  340 + """Test output from a node containing a phandle reference"""
  341 + dtb_file = get_dtb_file('dtoc_test_phandle_single.dts')
  342 + output = tools.GetOutputFilename('output')
  343 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  344 + with open(output) as infile:
  345 + data = infile.read()
  346 + self._CheckStrings(HEADER + '''
  347 +struct dtd_source {
  348 +\tstruct phandle_0_arg clocks[1];
  349 +};
  350 +struct dtd_target {
  351 +\tfdt32_t\t\tintval;
  352 +};
  353 +''', data)
  354 +
  355 + def test_phandle_reorder(self):
  356 + """Test that phandle targets are generated before their references"""
  357 + dtb_file = get_dtb_file('dtoc_test_phandle_reorder.dts')
  358 + output = tools.GetOutputFilename('output')
  359 + dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  360 + with open(output) as infile:
  361 + data = infile.read()
  362 + self._CheckStrings(C_HEADER + '''
  363 +static struct dtd_target dtv_phandle_target = {
  364 +};
  365 +U_BOOT_DEVICE(phandle_target) = {
  366 +\t.name\t\t= "target",
  367 +\t.platdata\t= &dtv_phandle_target,
  368 +\t.platdata_size\t= sizeof(dtv_phandle_target),
  369 +};
  370 +
  371 +static struct dtd_source dtv_phandle_source2 = {
  372 +\t.clocks\t\t\t= {
  373 +\t\t\t{&dtv_phandle_target, {}},},
  374 +};
  375 +U_BOOT_DEVICE(phandle_source2) = {
  376 +\t.name\t\t= "source",
  377 +\t.platdata\t= &dtv_phandle_source2,
  378 +\t.platdata_size\t= sizeof(dtv_phandle_source2),
  379 +};
  380 +
  381 +''', data)
  382 +
  383 + def test_phandle_bad(self):
  384 + """Test a node containing an invalid phandle fails"""
  385 + dtb_file = get_dtb_file('dtoc_test_phandle_bad.dts')
  386 + output = tools.GetOutputFilename('output')
  387 + with self.assertRaises(ValueError) as e:
  388 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  389 + self.assertIn("Cannot parse 'clocks' in node 'phandle-source'",
  390 + str(e.exception))
  391 +
  392 + def test_phandle_bad2(self):
  393 + """Test a phandle target missing its #*-cells property"""
  394 + dtb_file = get_dtb_file('dtoc_test_phandle_bad2.dts')
  395 + output = tools.GetOutputFilename('output')
  396 + with self.assertRaises(ValueError) as e:
  397 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  398 + self.assertIn("Node 'phandle-target' has no '#clock-cells' property",
  399 + str(e.exception))
  400 +
338 401 def test_aliases(self):
339 402 """Test output from a node with multiple compatible strings"""
340 403 dtb_file = get_dtb_file('dtoc_test_aliases.dts')
... ... @@ -560,4 +623,82 @@
560 623 };
561 624  
562 625 ''', data)
  626 +
  627 + def test_bad_reg(self):
  628 + """Test that a reg property with an invalid type generates an error"""
  629 + dtb_file = get_dtb_file('dtoc_test_bad_reg.dts')
  630 + output = tools.GetOutputFilename('output')
  631 + with self.assertRaises(ValueError) as e:
  632 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  633 + self.assertIn("Node 'spl-test' reg property is not an int",
  634 + str(e.exception))
  635 +
  636 + def test_bad_reg2(self):
  637 + """Test that a reg property with an invalid cell count is detected"""
  638 + dtb_file = get_dtb_file('dtoc_test_bad_reg2.dts')
  639 + output = tools.GetOutputFilename('output')
  640 + with self.assertRaises(ValueError) as e:
  641 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  642 + self.assertIn("Node 'spl-test' reg property has 3 cells which is not a multiple of na + ns = 1 + 1)",
  643 + str(e.exception))
  644 +
  645 + def test_add_prop(self):
  646 + """Test that a subequent node can add a new property to a struct"""
  647 + dtb_file = get_dtb_file('dtoc_test_add_prop.dts')
  648 + output = tools.GetOutputFilename('output')
  649 + dtb_platdata.run_steps(['struct'], dtb_file, False, output)
  650 + with open(output) as infile:
  651 + data = infile.read()
  652 + self._CheckStrings(HEADER + '''
  653 +struct dtd_sandbox_spl_test {
  654 +\tfdt32_t\t\tintarray;
  655 +\tfdt32_t\t\tintval;
  656 +};
  657 +''', data)
  658 +
  659 + dtb_platdata.run_steps(['platdata'], dtb_file, False, output)
  660 + with open(output) as infile:
  661 + data = infile.read()
  662 + self._CheckStrings(C_HEADER + '''
  663 +static struct dtd_sandbox_spl_test dtv_spl_test = {
  664 +\t.intval\t\t\t= 0x1,
  665 +};
  666 +U_BOOT_DEVICE(spl_test) = {
  667 +\t.name\t\t= "sandbox_spl_test",
  668 +\t.platdata\t= &dtv_spl_test,
  669 +\t.platdata_size\t= sizeof(dtv_spl_test),
  670 +};
  671 +
  672 +static struct dtd_sandbox_spl_test dtv_spl_test2 = {
  673 +\t.intarray\t\t= 0x5,
  674 +};
  675 +U_BOOT_DEVICE(spl_test2) = {
  676 +\t.name\t\t= "sandbox_spl_test",
  677 +\t.platdata\t= &dtv_spl_test2,
  678 +\t.platdata_size\t= sizeof(dtv_spl_test2),
  679 +};
  680 +
  681 +''', data)
  682 +
  683 + def testStdout(self):
  684 + """Test output to stdout"""
  685 + dtb_file = get_dtb_file('dtoc_test_simple.dts')
  686 + with test_util.capture_sys_output() as (stdout, stderr):
  687 + dtb_platdata.run_steps(['struct'], dtb_file, False, '-')
  688 +
  689 + def testNoCommand(self):
  690 + """Test running dtoc without a command"""
  691 + with self.assertRaises(ValueError) as e:
  692 + dtb_platdata.run_steps([], '', False, '')
  693 + self.assertIn("Please specify a command: struct, platdata",
  694 + str(e.exception))
  695 +
  696 + def testBadCommand(self):
  697 + """Test running dtoc with an invalid command"""
  698 + dtb_file = get_dtb_file('dtoc_test_simple.dts')
  699 + output = tools.GetOutputFilename('output')
  700 + with self.assertRaises(ValueError) as e:
  701 + dtb_platdata.run_steps(['invalid-cmd'], dtb_file, False, output)
  702 + self.assertIn("Unknown command 'invalid-cmd': (use: struct, platdata)",
  703 + str(e.exception))