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