]>
Commit | Line | Data |
---|---|---|
2e7d35d2 SG |
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> | |
756ac0bb | 10 | #include <malloc.h> |
2e7d35d2 SG |
11 | #include <dm/test.h> |
12 | #include <dm/root.h> | |
13 | #include <dm/uclass-internal.h> | |
e721b882 | 14 | #include <test/ut.h> |
2e7d35d2 SG |
15 | |
16 | DECLARE_GLOBAL_DATA_PTR; | |
17 | ||
e721b882 JH |
18 | struct unit_test_state global_dm_test_state; |
19 | static struct dm_test_state _global_priv_dm_test_state; | |
2e7d35d2 SG |
20 | |
21 | /* Get ready for testing */ | |
e721b882 | 22 | static int dm_test_init(struct unit_test_state *uts) |
2e7d35d2 | 23 | { |
e721b882 JH |
24 | struct dm_test_state *dms = uts->priv; |
25 | ||
26 | memset(uts, '\0', sizeof(*uts)); | |
27 | uts->priv = dms; | |
2e7d35d2 SG |
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 */ | |
e721b882 | 39 | static int do_autoprobe(struct unit_test_state *uts) |
2e7d35d2 | 40 | { |
54c5d08a | 41 | struct udevice *dev; |
2e7d35d2 SG |
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 | ||
e721b882 | 53 | static int dm_test_destroy(struct unit_test_state *uts) |
2e7d35d2 SG |
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 | ||
57f54d55 | 73 | int dm_test_main(const char *test_name) |
2e7d35d2 | 74 | { |
e721b882 JH |
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; | |
2e7d35d2 SG |
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" | |
f64000c3 | 87 | " ./u-boot -d arch/sandbox/dts/test.dtb\n"); |
2e7d35d2 SG |
88 | ut_assert(gd->fdt_blob); |
89 | } | |
90 | ||
57f54d55 SG |
91 | if (!test_name) |
92 | printf("Running %d driver model tests\n", n_ents); | |
2e7d35d2 SG |
93 | |
94 | for (test = tests; test < tests + n_ents; test++) { | |
57f54d55 SG |
95 | if (test_name && strcmp(test_name, test->name)) |
96 | continue; | |
2e7d35d2 | 97 | printf("Test: %s\n", test->name); |
e721b882 | 98 | ut_assertok(dm_test_init(uts)); |
2e7d35d2 | 99 | |
e721b882 | 100 | uts->start = mallinfo(); |
2e7d35d2 | 101 | if (test->flags & DM_TESTF_SCAN_PDATA) |
00606d7e | 102 | ut_assertok(dm_scan_platdata(false)); |
2e7d35d2 | 103 | if (test->flags & DM_TESTF_PROBE_TEST) |
e721b882 | 104 | ut_assertok(do_autoprobe(uts)); |
2e7d35d2 | 105 | if (test->flags & DM_TESTF_SCAN_FDT) |
00606d7e | 106 | ut_assertok(dm_scan_fdt(gd->fdt_blob, false)); |
2e7d35d2 | 107 | |
e721b882 | 108 | if (test->func(uts)) |
2e7d35d2 SG |
109 | break; |
110 | ||
e721b882 | 111 | ut_assertok(dm_test_destroy(uts)); |
2e7d35d2 SG |
112 | } |
113 | ||
e721b882 | 114 | printf("Failures: %d\n", uts->fail_count); |
2e7d35d2 SG |
115 | |
116 | return 0; | |
117 | } |