priv-set: new module and accompanying tests; adapt write-any-file
[gnulib.git] / tests / test-priv-set.c
1 /* Test the priv-set module.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by David Bartley <dtbartle@csclub.uwaterloo.ca>, 2007.  */
18
19 #include <config.h>
20 #include "priv-set.h"
21
22 #if HAVE_GETPPRIV
23 # include <priv.h>
24 #endif
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/types.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           fflush (stderr);                                                   \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 int
44 main (void)
45 {
46 #if HAVE_GETPPRIV
47     priv_set_t *set;
48
49     ASSERT (set = priv_allocset ());
50     ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
51     ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 1);
52
53     /* Do a series of removes and restores making sure that the results are
54        consistent with our ismember function and solaris' priv_ismember.  */
55     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 1);
56         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
57         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 1);
58     ASSERT (priv_set_restore (PRIV_PROC_EXEC) == -1);
59         ASSERT (errno == EINVAL);
60     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 1);
61         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
62         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 1);
63     ASSERT (priv_set_remove (PRIV_PROC_EXEC) == 0);
64     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 0);
65         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
66         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 0);
67     ASSERT (priv_set_remove (PRIV_PROC_EXEC) == -1);
68         ASSERT (errno == EINVAL);
69     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 0);
70         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
71         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 0);
72     ASSERT (priv_set_restore (PRIV_PROC_EXEC) == 0);
73     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 1);
74         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
75         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 1);
76     ASSERT (priv_set_restore (PRIV_PROC_EXEC) == -1);
77         ASSERT (errno == EINVAL);
78     ASSERT (priv_set_ismember (PRIV_PROC_EXEC) == 1);
79         ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
80         ASSERT (priv_ismember (set, PRIV_PROC_EXEC) == 1);
81
82     /* Test the priv_set_linkdir wrappers.  */
83     ASSERT (getppriv (PRIV_EFFECTIVE, set) == 0);
84     if (priv_ismember (set, PRIV_SYS_LINKDIR))
85       {
86         ASSERT (priv_set_restore_linkdir () == -1);
87             ASSERT (errno == EINVAL);
88         ASSERT (priv_set_remove_linkdir () == 0);
89         ASSERT (priv_set_remove_linkdir () == -1);
90             ASSERT (errno == EINVAL);
91         ASSERT (priv_set_restore_linkdir () == 0);
92       }
93 #else
94     ASSERT (priv_set_restore_linkdir () == -1);
95     ASSERT (priv_set_remove_linkdir () == -1);
96 #endif
97
98     return 0;
99 }