xref: /illumos-gate/usr/src/test/libc-tests/tests/priv_gettext/priv_gettext.c (revision 843c398e8904ed9d833d2af3103894f909fb4b52)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2015 Joyent, Inc.
14  */
15 
16 #include <string.h>
17 #include <locale.h>
18 #include <assert.h>
19 #include <stdlib.h>
20 #include <priv.h>
21 
22 /*
23  * This tests priv_gettext(). The priv_gettext() function always falls back to
24  * the C locale if it can't find anything. To deal with that, we've defined a
25  * dummy translation for the zz_AA.UTF-8 locale which has a translation for the
26  * 'dtrace_kernel' privilege.
27  *
28  * Normally 'dtrace_kernel' has the following description:
29  *
30  *   Allows DTrace kernel-level tracing.
31  *
32  * In the zz_AA.UTF-8 locale it has the following description:
33  *
34  *   Ah Elbereth Gilthoniel
35  *
36  * We explicitly verify that things respect the global locale and per-thread
37  * locale.
38  */
39 
40 static const char *def = "Allows DTrace kernel-level tracing.\n";
41 static const char *trans = "Ah Elbereth Gilthoniel\n";
42 
43 static void
44 priv_verify(const char *exp)
45 {
46 	char *res = priv_gettext("dtrace_kernel");
47 	assert(res != NULL);
48 	assert(strcmp(res, exp) == 0);
49 	free(res);
50 }
51 
52 int
53 main(void)
54 {
55 	locale_t loc;
56 
57 	(void) setlocale(LC_ALL, "C");
58 	priv_verify(def);
59 
60 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
61 	priv_verify(trans);
62 
63 	(void) setlocale(LC_ALL, "C");
64 	loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
65 	assert(loc != NULL);
66 	priv_verify(def);
67 
68 	(void) uselocale(loc);
69 	priv_verify(trans);
70 
71 	(void) uselocale(LC_GLOBAL_LOCALE);
72 	priv_verify(def);
73 	freelocale(loc);
74 
75 	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
76 	loc = newlocale(LC_MESSAGES_MASK, "C", NULL);
77 	assert(loc != NULL);
78 	priv_verify(trans);
79 
80 	(void) uselocale(loc);
81 	priv_verify(def);
82 
83 	(void) uselocale(LC_GLOBAL_LOCALE);
84 	priv_verify(trans);
85 	freelocale(loc);
86 
87 	return (0);
88 }
89