xref: /openbmc/u-boot/test/dm/test-main.c (revision e721b882e9daf3ad3599eef5a9ccf3847b694228)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <malloc.h>
11 #include <dm/test.h>
12 #include <dm/root.h>
13 #include <dm/uclass-internal.h>
14 #include <test/ut.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 struct unit_test_state global_dm_test_state;
19 static struct dm_test_state _global_priv_dm_test_state;
20 
21 /* Get ready for testing */
22 static int dm_test_init(struct unit_test_state *uts)
23 {
24 	struct dm_test_state *dms = uts->priv;
25 
26 	memset(uts, '\0', sizeof(*uts));
27 	uts->priv = dms;
28 	memset(dms, '\0', sizeof(*dms));
29 	gd->dm_root = NULL;
30 	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
31 
32 	ut_assertok(dm_init());
33 	dms->root = dm_root();
34 
35 	return 0;
36 }
37 
38 /* Ensure all the test devices are probed */
39 static int do_autoprobe(struct unit_test_state *uts)
40 {
41 	struct udevice *dev;
42 	int ret;
43 
44 	/* Scanning the uclass is enough to probe all the devices */
45 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
46 	     dev;
47 	     ret = uclass_next_device(&dev))
48 		;
49 
50 	return ret;
51 }
52 
53 static int dm_test_destroy(struct unit_test_state *uts)
54 {
55 	int id;
56 
57 	for (id = 0; id < UCLASS_COUNT; id++) {
58 		struct uclass *uc;
59 
60 		/*
61 		 * If the uclass doesn't exist we don't want to create it. So
62 		 * check that here before we call uclass_find_device()/
63 		 */
64 		uc = uclass_find(id);
65 		if (!uc)
66 			continue;
67 		ut_assertok(uclass_destroy(uc));
68 	}
69 
70 	return 0;
71 }
72 
73 int dm_test_main(const char *test_name)
74 {
75 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
76 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
77 	struct unit_test_state *uts = &global_dm_test_state;
78 	uts->priv = &_global_priv_dm_test_state;
79 	struct unit_test *test;
80 
81 	/*
82 	 * If we have no device tree, or it only has a root node, then these
83 	 * tests clearly aren't going to work...
84 	 */
85 	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
86 		puts("Please run with test device tree:\n"
87 		     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
88 		ut_assert(gd->fdt_blob);
89 	}
90 
91 	if (!test_name)
92 		printf("Running %d driver model tests\n", n_ents);
93 
94 	for (test = tests; test < tests + n_ents; test++) {
95 		if (test_name && strcmp(test_name, test->name))
96 			continue;
97 		printf("Test: %s\n", test->name);
98 		ut_assertok(dm_test_init(uts));
99 
100 		uts->start = mallinfo();
101 		if (test->flags & DM_TESTF_SCAN_PDATA)
102 			ut_assertok(dm_scan_platdata(false));
103 		if (test->flags & DM_TESTF_PROBE_TEST)
104 			ut_assertok(do_autoprobe(uts));
105 		if (test->flags & DM_TESTF_SCAN_FDT)
106 			ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
107 
108 		if (test->func(uts))
109 			break;
110 
111 		ut_assertok(dm_test_destroy(uts));
112 	}
113 
114 	printf("Failures: %d\n", uts->fail_count);
115 
116 	return 0;
117 }
118