summaryrefslogtreecommitdiff
path: root/usr/src/test/libc-tests/tests/strerror/strerror.c
blob: 78dce8e04fe77b07c3780e07c67b2ba5b7695429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2015 Joyent, Inc.
 */

#include <string.h>
#include <locale.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>

/*
 * This is designed to test strerorr and strerror_l's ability to react properly
 * to being in various locales. This also serves as a regression test for
 * illumos#6133.
 *
 * For this test, we utilize the poorly named 'zz_AA.UTF_8' locale which
 * was created because it actually has a translation! It 'translates'
 * the string:
 *
 * "No such file or directory" -> "It's a trap!"
 *
 * It's otherwise a boring en_US.UTF-8 locale under the hood.
 *
 * We explicitly want to verify the following cases:
 *
 * + strerror() honors the global locale before uselocale
 * + strerror() honors the per-thread locale
 * + strerror_l() always reflects the chosen locale
 */

static int err = ENOENT;
static const char *en = "No such file or directory";
static const char *trans = "It's a trap!";

static void
strerror_verify(const char *exp)
{
	const char *r;
	errno = 0;
	r = strerror(err);
	assert(errno == 0);
	assert(strcmp(r, exp) == 0);
}

static void
strerror_l_verify(locale_t loc, const char *exp)
{
	const char *r;
	errno = 0;
	r = strerror_l(err, loc);
	assert(errno == 0);
	assert(strcmp(r, exp) == 0);
}

int
main(void)
{
	locale_t loc;

	(void) setlocale(LC_ALL, "C");
	strerror_verify(en);

	(void) setlocale(LC_ALL, "zz_AA.UTF-8");
	strerror_verify(trans);

	(void) setlocale(LC_MESSAGES, "C");
	strerror_verify(en);

	(void) setlocale(LC_ALL, "C");
	loc = newlocale(LC_MESSAGES_MASK, "zz_AA.UTF-8", NULL);
	assert(loc != NULL);

	strerror_verify(en);
	strerror_l_verify(NULL, en);
	strerror_l_verify(loc, trans);

	(void) uselocale(loc);
	strerror_verify(trans);
	strerror_l_verify(NULL, trans);
	strerror_l_verify(loc, trans);

	(void) uselocale(LC_GLOBAL_LOCALE);
	strerror_verify(en);
	strerror_l_verify(NULL, en);
	strerror_l_verify(loc, trans);

	freelocale(loc);
	return (0);
}