summaryrefslogtreecommitdiff
path: root/usr/src/test/libc-tests/tests/strcoll-strxfrm-6907.c
blob: 3ae1ea6cb561b65bc638528a8fe85d028bbe76c1 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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 2016 Tom Lane <tgl@sss.pgh.pa.us>
 * Copyright 2017 Nexenta Systems, Inc.
 */

#include <err.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*
 * #6907: generate random UTF8 strings, strxfrm'ing them in process.
 * Walk through comparing each string with all strings, and checking
 * that strcoll() and strcmp() for strxfrm'ed data produce same results.
 */
#define	NSTRINGS 2000
#define	MAXSTRLEN 20
#define	MAXXFRMLEN (MAXSTRLEN * 20)

typedef struct {
	char	sval[MAXSTRLEN];
	char	xval[MAXXFRMLEN];
} cstr;

int
main(void)
{
	cstr	data[NSTRINGS];
	char	*curloc;
	int	i, j;

	if ((curloc = setlocale(LC_ALL, "")) == NULL)
		err(1, "setlocale");

	/* Ensure new random() values on every run */
	srandom((unsigned int) time(NULL));

	/* Generate random UTF8 strings of length less than MAXSTRLEN bytes */
	for (i = 0; i < NSTRINGS; i++) {
		char	*p;
		int	len;

again:
		p = data[i].sval;
		len = 1 + (random() % (MAXSTRLEN - 1));
		while (len > 0) {
			int c;

			/*
			 * Generate random printable char in ISO8859-1 range.
			 * Bias towards producing a lot of spaces.
			 */
			if ((random() % 16) < 3) {
				c = ' ';
			} else {
				do {
					c = random() & 0xFF;
				} while (!((c >= ' ' && c <= 127) ||
				    (c >= 0xA0 && c <= 0xFF)));
			}

			if (c <= 127) {
				*p++ = c;
				len--;
			} else {
				if (len < 2)
					break;
				/* Poor man's utf8-ification */
				*p++ = 0xC0 + (c >> 6);
				len--;
				*p++ = 0x80 + (c & 0x3F);
				len--;
			}
		}
		*p = '\0';

		/* strxfrm() each string as we produce it */
		errno = 0;
		if (strxfrm(data[i].xval, data[i].sval,
		    MAXXFRMLEN) >= MAXXFRMLEN) {
			errx(1, "strxfrm() result for %d-length string "
			    "exceeded %d bytes", (int)strlen(data[i].sval),
			    MAXXFRMLEN);
		}
		/* Amend strxfrm() failing for certain characters (#7962) */
		if (errno != 0)
			goto again;
	}

	for (i = 0; i < NSTRINGS; i++) {
		for (j = 0; j < NSTRINGS; j++) {
			int sr = strcoll(data[i].sval, data[j].sval);
			int sx = strcmp(data[i].xval, data[j].xval);

			if ((sr * sx < 0) || (sr * sx == 0 && sr + sx != 0)) {
				errx(1, "%s: diff for \"%s\" and \"%s\"",
				    curloc, data[i].sval, data[j].sval);
			}
		}
	}

	return (0);
}