summaryrefslogtreecommitdiffhomepage
path: root/test/nxt_utf8_file_name_test.c
blob: 4481ad38e3d2fb2e289593ed8c64e0305097fd3c (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


extern char  **environ;

static nxt_int_t nxt_utf8_file_name_test(nxt_thread_t *thr);


nxt_module_init_t  nxt_init_modules[0];
nxt_uint_t         nxt_init_modules_n;


int nxt_cdecl
main(int argc, char **argv)
{
    nxt_thread_t     *thr;

    if (nxt_lib_start("utf8_file_name_test", argv, &environ) != NXT_OK) {
        return 1;
    }

    nxt_main_log.level = NXT_LOG_INFO;

    thr = nxt_thread();

    if (nxt_utf8_file_name_test(thr) != NXT_OK) {
        return 1;
    }

    return 0;
}


static nxt_int_t
nxt_utf8_file_name_test(nxt_thread_t *thr)
{
    u_char               *p, test[4], buf[32];
    ssize_t              n;
    uint32_t             uc, lc;
    nxt_int_t            ret;
    nxt_task_t           *task = NULL;
    nxt_file_t           uc_file, lc_file;
    const u_char         *pp;
    nxt_file_name_t      uc_name[10], lc_name[10];
    static const u_char  utf8[4] = "UTF8";

    nxt_thread_time_update(thr);

    uc_name[0] = 'u';
    uc_name[1] = 't';
    uc_name[2] = 'f';
    uc_name[3] = '8';
    uc_name[4] = '_';

    lc_name[0] = 'u';
    lc_name[1] = 't';
    lc_name[2] = 'f';
    lc_name[3] = '8';
    lc_name[4] = '_';

    nxt_memzero(&uc_file, sizeof(nxt_file_t));

    uc_file.name = uc_name;
    uc_file.log_level = NXT_LOG_ALERT;

    nxt_memzero(&lc_file, sizeof(nxt_file_t));

    lc_file.name = lc_name;

    for (uc = 0x41; uc < 0x110000; uc++) {

        p = nxt_utf8_encode(&uc_name[5], uc);

        if (p == NULL) {
            nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", uc);
            return NXT_ERROR;
        }

        *p = '\0';

        pp = &uc_name[5];
        lc = nxt_utf8_lowcase(&pp, p);

        if (lc == 0xffffffff) {
            nxt_log_alert(thr->log, "nxt_utf8_lowcase(%05uxD) failed: %05uxD",
                          uc, lc);
            return NXT_ERROR;
        }

        if (uc == lc) {
            continue;
        }

        p = nxt_utf8_encode(&lc_name[5], lc);

        if (p == NULL) {
            nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", lc);
            return NXT_ERROR;
        }

        *p = '\0';

        ret = nxt_file_open(task, &uc_file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE,
                            NXT_FILE_DEFAULT_ACCESS);
        if (ret != NXT_OK) {
            return NXT_ERROR;
        }

        if (nxt_file_write(&uc_file, utf8, 4, 0) != 4) {
            return NXT_ERROR;
        }

        nxt_file_close(task, &uc_file);

        ret = nxt_file_open(task, &lc_file, NXT_FILE_RDONLY, NXT_FILE_OPEN,
                            NXT_FILE_DEFAULT_ACCESS);

        if (ret == NXT_OK) {
            n = nxt_file_read(&lc_file, test, 4, 0);

            nxt_file_close(task, &lc_file);

            if (n != 4 || nxt_memcmp(utf8, test, 4) != 0) {
                nxt_log_alert(thr->log, "nxt_file_read() mismatch");

                nxt_file_delete(lc_file.name);
            }

            p = nxt_sprintf(buf, buf + 32, "%04uXD; C; %04uXD;%n", uc, lc);

            nxt_fd_write(nxt_stdout, buf, p - buf);
        }

        nxt_file_delete(uc_file.name);
    }

    nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 file name unit test passed");
    return NXT_OK;
}