s4-winreg: fix _winreg_EnumValue behavior
[gd/samba/.git] / nsswitch / b15464-testcase.c
1 #include "replace.h"
2 #include "system/wait.h"
3 #include "system/threads.h"
4 #include <assert.h>
5
6 int main(int argc, const char *argv[])
7 {
8         pid_t pid;
9         int wstatus;
10         pthread_key_t k1;
11         pthread_key_t k2;
12         pthread_key_t k3;
13         char *val = NULL;
14         const char *nss_winbind = (argc >= 2 ? argv[1] : "bin/plugins/libnss_winbind.so.2");
15         void *nss_winbind_handle = NULL;
16         union {
17                 int (*fn)(void);
18                 void *symbol;
19         } nss_winbind_endpwent = { .symbol = NULL, };
20
21         /*
22          * load and invoke something simple like
23          * _nss_winbind_endpwent in order to
24          * get the libnss_winbind internal going
25          */
26         nss_winbind_handle = dlopen(nss_winbind, RTLD_NOW);
27         printf("%d: nss_winbind[%s] nss_winbind_handle[%p]\n",
28                getpid(), nss_winbind, nss_winbind_handle);
29         assert(nss_winbind_handle != NULL);
30
31         nss_winbind_endpwent.symbol = dlsym(nss_winbind_handle,
32                                             "_nss_winbind_endpwent");
33         printf("%d: nss_winbind_handle[%p] _nss_winbind_endpwent[%p]\n",
34                getpid(), nss_winbind_handle, nss_winbind_endpwent.symbol);
35         assert(nss_winbind_endpwent.symbol != NULL);
36         (void)nss_winbind_endpwent.fn();
37
38         val = malloc(1);
39         assert(val != NULL);
40
41         pthread_key_create(&k1, NULL);
42         pthread_setspecific(k1, val);
43         printf("%d: k1=%d\n", getpid(), k1);
44
45         pid = fork();
46         if (pid) {
47                 free(val);
48                 wait(&wstatus);
49                 return WEXITSTATUS(wstatus);
50         }
51
52         pthread_key_create(&k2, NULL);
53         pthread_setspecific(k2, val);
54
55         printf("%d: Hello after fork, k1=%d, k2=%d\n", getpid(), k1, k2);
56
57         pid = fork();
58
59         if (pid) {
60                 free(val);
61                 wait(&wstatus);
62                 return WEXITSTATUS(wstatus);
63         }
64
65         pthread_key_create(&k3, NULL);
66         pthread_setspecific(k3, val);
67
68         printf("%d: Hello after fork2, k1=%d, k2=%d, k3=%d\n", getpid(), k1, k2, k3);
69
70         if (k1 == k2 || k2 == k3) {
71                 printf("%d: FAIL inconsistent keys\n", getpid());
72                 return 1;
73         }
74
75         printf("%d: OK consistent keys\n", getpid());
76         return 0;
77 }