igt/gem_userptr_blits: Fix forked access test
[platform/upstream/intel-gpu-tools.git] / tests / igt_simulation.c
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 <daniel.vetter@ffwll.ch>
25  *
26  */
27
28 #include <stdlib.h>
29 #include <sys/wait.h>
30 #include <sys/types.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #include "drmtest.h"
35 #include "igt_core.h"
36
37 /*
38  * We need to hide assert from the cocci igt test refactor spatch.
39  *
40  * IMPORTANT: Test infrastructure tests are the only valid places where using
41  * assert is allowed.
42  */
43 #define internal_assert assert
44
45 bool simple;
46 bool list_subtests;
47 bool in_fixture;
48 bool in_subtest;
49
50 char test[] = "test";
51 char list[] = "--list-subtests";
52 char *argv_list[] = { test, list };
53 char *argv_run[] = { test };
54
55 static int do_fork(void)
56 {
57         int pid, status;
58
59         switch (pid = fork()) {
60         case -1:
61                 internal_assert(0);
62         case 0:
63                 if (simple) {
64                         igt_simple_init(1, argv_run);
65
66                         igt_skip_on_simulation();
67
68                         exit(0);
69                 } else {
70                         if (list_subtests)
71                                 igt_subtest_init(2, argv_list);
72                         else
73                                 igt_subtest_init(1, argv_run);
74
75                         if (in_fixture) {
76                                 igt_fixture
77                                         igt_skip_on_simulation();
78                         } if (in_subtest) {
79                                 igt_subtest("sim")
80                                         igt_skip_on_simulation();
81                         } else
82                                 igt_skip_on_simulation();
83
84                         if (!in_subtest)
85                                 igt_subtest("foo")
86                                         ;
87
88                         igt_exit();
89                 }
90         default:
91                 while (waitpid(pid, &status, 0) == -1 &&
92                        errno == EINTR)
93                         ;
94
95                 internal_assert(WIFEXITED(status));
96
97                 return WEXITSTATUS(status);
98         }
99 }
100
101 int main(int argc, char **argv)
102 {
103         /* simple tests */
104         simple = true;
105         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
106         internal_assert(do_fork() == IGT_EXIT_SKIP);
107
108         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
109         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
110
111         /* subtests, list mode */
112         simple = false;
113         list_subtests = true;
114
115         in_fixture = false;
116         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
117         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
118
119         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
120         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
121
122         in_fixture = true;
123         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
124         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
125
126         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
127         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
128
129         in_fixture = false;
130         in_subtest = true;
131         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
132         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
133
134         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
135         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
136
137         /* subtest, run mode */
138         simple = false;
139         list_subtests = false;
140
141         in_fixture = false;
142         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
143         internal_assert(do_fork() == IGT_EXIT_SKIP);
144
145         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
146         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
147
148         in_fixture = true;
149         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
150         internal_assert(do_fork() == IGT_EXIT_SKIP);
151
152         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
153         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
154
155         in_fixture = false;
156         in_subtest = true;
157         internal_assert(setenv("INTEL_SIMULATION", "1", 1) == 0);
158         internal_assert(do_fork() == IGT_EXIT_SKIP);
159
160         internal_assert(setenv("INTEL_SIMULATION", "0", 1) == 0);
161         internal_assert(do_fork() == IGT_EXIT_SUCCESS);
162
163         return 0;
164 }