xref: /unit/src/test/nxt_utf8_file_name_test.c (revision 611:323e11065f83)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 extern char  **environ;
11 
12 static nxt_int_t nxt_utf8_file_name_test(nxt_thread_t *thr);
13 
14 
15 nxt_module_init_t  nxt_init_modules[1];
16 nxt_uint_t         nxt_init_modules_n;
17 
18 
19 int nxt_cdecl
main(int argc,char ** argv)20 main(int argc, char **argv)
21 {
22     nxt_thread_t     *thr;
23 
24     if (nxt_lib_start("utf8_file_name_test", argv, &environ) != NXT_OK) {
25         return 1;
26     }
27 
28     nxt_main_log.level = NXT_LOG_INFO;
29 
30     thr = nxt_thread();
31 
32     if (nxt_utf8_file_name_test(thr) != NXT_OK) {
33         return 1;
34     }
35 
36     return 0;
37 }
38 
39 
40 static nxt_int_t
nxt_utf8_file_name_test(nxt_thread_t * thr)41 nxt_utf8_file_name_test(nxt_thread_t *thr)
42 {
43     u_char               *p, test[4], buf[32];
44     ssize_t              n;
45     uint32_t             uc, lc;
46     nxt_int_t            ret;
47     nxt_task_t           task;
48     nxt_file_t           uc_file, lc_file;
49     const u_char         *pp;
50     nxt_file_name_t      uc_name[10], lc_name[10];
51     static const u_char  utf8[4] = "UTF8";
52 
53     nxt_thread_time_update(thr);
54 
55     uc_name[0] = 'u';
56     uc_name[1] = 't';
57     uc_name[2] = 'f';
58     uc_name[3] = '8';
59     uc_name[4] = '_';
60 
61     lc_name[0] = 'u';
62     lc_name[1] = 't';
63     lc_name[2] = 'f';
64     lc_name[3] = '8';
65     lc_name[4] = '_';
66 
67     nxt_memzero(&uc_file, sizeof(nxt_file_t));
68 
69     uc_file.name = uc_name;
70     uc_file.log_level = NXT_LOG_ALERT;
71 
72     nxt_memzero(&lc_file, sizeof(nxt_file_t));
73 
74     lc_file.name = lc_name;
75 
76     task.thread = thr;
77     task.log = thr->log;
78 
79     for (uc = 0x41; uc < 0x110000; uc++) {
80 
81         p = nxt_utf8_encode(&uc_name[5], uc);
82 
83         if (p == NULL) {
84             nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", uc);
85             return NXT_ERROR;
86         }
87 
88         *p = '\0';
89 
90         pp = &uc_name[5];
91         lc = nxt_utf8_lowcase(&pp, p);
92 
93         if (lc == 0xFFFFFFFF) {
94             nxt_log_alert(thr->log, "nxt_utf8_lowcase(%05uxD) failed: %05uxD",
95                           uc, lc);
96             return NXT_ERROR;
97         }
98 
99         if (uc == lc) {
100             continue;
101         }
102 
103         p = nxt_utf8_encode(&lc_name[5], lc);
104 
105         if (p == NULL) {
106             nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", lc);
107             return NXT_ERROR;
108         }
109 
110         *p = '\0';
111 
112         ret = nxt_file_open(&task, &uc_file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE,
113                             NXT_FILE_DEFAULT_ACCESS);
114         if (ret != NXT_OK) {
115             return NXT_ERROR;
116         }
117 
118         if (nxt_file_write(&uc_file, utf8, 4, 0) != 4) {
119             return NXT_ERROR;
120         }
121 
122         nxt_file_close(&task, &uc_file);
123 
124         ret = nxt_file_open(&task, &lc_file, NXT_FILE_RDONLY, NXT_FILE_OPEN,
125                             NXT_FILE_DEFAULT_ACCESS);
126 
127         if (ret == NXT_OK) {
128             n = nxt_file_read(&lc_file, test, 4, 0);
129 
130             nxt_file_close(&task, &lc_file);
131 
132             if (n != 4 || nxt_memcmp(utf8, test, 4) != 0) {
133                 nxt_log_alert(thr->log, "nxt_file_read() mismatch");
134 
135                 nxt_file_delete(lc_file.name);
136             }
137 
138             p = nxt_sprintf(buf, buf + 32, "%04uXD; C; %04uXD;%n", uc, lc);
139 
140             nxt_fd_write(nxt_stdout, buf, p - buf);
141         }
142 
143         nxt_file_delete(uc_file.name);
144     }
145 
146     nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 file name test passed");
147     return NXT_OK;
148 }
149