getusershell: Work around musl bugs.
[gnulib.git] / tests / test-immutable.c
blobafb268313737445fd5b3a9f6f6bc81eadddbacee
1 /* Test of immutable data.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2021. */
19 #include <config.h>
21 #include "immutable.h"
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "macros.h"
29 struct data
31 int x;
32 long y;
35 #if IMMUTABLE_EFFECTIVE
37 static _GL_ASYNC_SAFE _Noreturn void
38 segv_handler (int signo)
40 _Exit (0);
43 static void
44 install_segv_handler (void)
46 signal (SIGSEGV, segv_handler);
47 # if (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__
48 signal (SIGBUS, segv_handler);
49 # endif
52 #endif
54 int
55 main (int argc, char *argv[])
57 if (argc != 2)
59 fprintf (stderr, "%s: need 1 argument\n", argv[0]);
60 return 1;
62 int test = atoi (argv[1]);
63 switch (test)
65 case 0:
66 /* Indicates whether the implementation effectively rejects writes to
67 immutable data. */
68 #if !IMMUTABLE_EFFECTIVE
69 fputs ("Skipping test: immutability cannot be enforced\n", stderr);
70 return 77;
71 #else
72 break;
73 #endif
75 case 1:
76 /* Correct use of immmalloc. */
78 struct data *wp;
79 struct data const *p;
81 wp = (struct data *) immmalloc (sizeof (struct data));
82 ASSERT (wp != NULL);
83 wp->x = 7;
84 wp->y = 42;
85 p = immfreeze (wp);
86 ASSERT (p->x == 7);
87 ASSERT (p->y == 42);
88 immfree (p);
90 break;
92 case 2:
93 /* Catch invalid write access. */
95 struct data *wp;
96 struct data const *p;
98 wp = (struct data *) immmalloc (sizeof (struct data));
99 ASSERT (wp != NULL);
100 wp->x = 7;
101 wp->y = 42;
102 p = immfreeze (wp);
103 #if IMMUTABLE_EFFECTIVE
104 install_segv_handler ();
105 #endif
106 /* This assignment should crash. */
107 ((struct data *) p)->y = 77;
108 #if IMMUTABLE_EFFECTIVE
109 return 1;
110 #endif
112 break;
114 case 3:
115 /* Catch invalid write access while another data object is not frozen. */
117 struct data *wp;
118 struct data const *p;
119 struct data *wp2;
121 wp = (struct data *) immmalloc (sizeof (struct data));
122 ASSERT (wp != NULL);
123 wp->x = 7;
124 wp->y = 42;
125 p = immfreeze (wp);
126 ASSERT (p->x == 7);
127 ASSERT (p->y == 42);
129 wp2 = (struct data *) immmalloc (sizeof (struct data));
130 ASSERT (wp2 != NULL);
131 wp2->x = 7;
132 #if IMMUTABLE_EFFECTIVE
133 install_segv_handler ();
134 #endif
135 /* This assignment should crash. */
136 ((struct data *) p)->y = 42;
137 #if IMMUTABLE_EFFECTIVE
138 return 1;
139 #endif
141 break;
143 case 4:
144 /* Correct use of immstrdup. */
146 const char *s = immstrdup ("Hello");
147 ASSERT (strlen (s) == 5);
148 ASSERT (strcmp (s, "Hello") == 0);
149 immfree (s);
151 break;
153 default:
154 ASSERT (false);
156 return test_exit_status;