xref: /aosp_15_r20/external/igt-gpu-tools/lib/tests/igt_simulation.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Daniel Vetter <[email protected]>
25  *
26  */
27 
28 #include <stdlib.h>
29 #include <sys/wait.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 
33 #include "drmtest.h"
34 #include "igt_core.h"
35 
36 #include "igt_tests_common.h"
37 
38 bool simple;
39 bool list_subtests;
40 bool in_fixture;
41 bool in_subtest;
42 
43 char test[] = "test";
44 char list[] = "--list-subtests";
45 char *argv_list[] = { test, list };
46 char *argv_run[] = { test };
47 
do_fork(void)48 static int do_fork(void)
49 {
50 	int pid, status;
51 	int argc;
52 
53 	switch (pid = fork()) {
54 	case -1:
55 		internal_assert(0);
56 	case 0:
57 		if (simple) {
58 			argc = 1;
59 			igt_simple_init(argc, argv_run);
60 
61 			igt_skip_on_simulation();
62 
63 			igt_exit();
64 		} else {
65 			if (list_subtests) {
66 				argc = 2;
67 				igt_subtest_init(argc, argv_list);
68 			} else {
69 				argc = 1;
70 				igt_subtest_init(argc, argv_run);
71 			}
72 
73 			if (in_fixture) {
74 				igt_fixture
75 					igt_skip_on_simulation();
76 			} if (in_subtest) {
77 				igt_subtest("sim")
78 					igt_skip_on_simulation();
79 			} else
80 				igt_skip_on_simulation();
81 
82 			if (!in_subtest)
83 				igt_subtest("foo")
84 					;
85 
86 			igt_exit();
87 		}
88 	default:
89 		while (waitpid(pid, &status, 0) == -1 &&
90 		       errno == EINTR)
91 			;
92 
93 		internal_assert(WIFEXITED(status));
94 
95 		return status;
96 	}
97 }
98 
main(int argc,char ** argv)99 int main(int argc, char **argv)
100 {
101 	/* simple tests */
102 	simple = true;
103 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
104 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SKIP);
105 
106 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
107 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
108 
109 	/* subtests, list mode */
110 	simple = false;
111 	list_subtests = true;
112 
113 	in_fixture = false;
114 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
115 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
116 
117 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
118 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
119 
120 	in_fixture = true;
121 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
122 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
123 
124 
125 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
126 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
127 
128 
129 	in_fixture = false;
130 	in_subtest = true;
131 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
132 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
133 
134 
135 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
136 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
137 
138 
139 	/* subtest, run mode */
140 	simple = false;
141 	list_subtests = false;
142 
143 	in_fixture = false;
144 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
145 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SKIP);
146 
147 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
148 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
149 
150 
151 	in_fixture = true;
152 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
153 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SKIP);
154 
155 
156 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
157 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
158 
159 
160 	in_fixture = false;
161 	in_subtest = true;
162 	internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
163 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SKIP);
164 
165 
166 	internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
167 	internal_assert(WEXITSTATUS(do_fork()) == IGT_EXIT_SUCCESS);
168 
169 
170 	return 0;
171 }
172