~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/test/internal/testucs.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "apr.h"
 
18
#include "arch/win32/apr_arch_utf8.h"
 
19
#include <wchar.h>
 
20
#include <string.h>
 
21
 
 
22
struct testval {
 
23
    unsigned char n[8];
 
24
    wchar_t w[4];
 
25
    int nl;
 
26
    int wl;
 
27
};
 
28
 
 
29
void displaynw(struct testval *f, struct testval *l)
 
30
{
 
31
    char x[80], *t = x;
 
32
    int i;
 
33
    for (i = 0; i < f->nl; ++i)
 
34
        t += sprintf(t, "%02X ", f->n[i]);
 
35
    *(t++) = '-';
 
36
    for (i = 0; i < l->nl; ++i)
 
37
        t += sprintf(t, " %02X", l->n[i]);
 
38
    *(t++) = ' ';
 
39
    *(t++) = '=';
 
40
    *(t++) = ' '; 
 
41
    for (i = 0; i < f->wl; ++i)
 
42
        t += sprintf(t, "%04X ", f->w[i]);
 
43
    *(t++) = '-';
 
44
    for (i = 0; i < l->wl; ++i)
 
45
        t += sprintf(t, " %04X", l->w[i]);
 
46
    puts(x);
 
47
}
 
48
 
 
49
/*
 
50
 *  Test every possible byte value. 
 
51
 *  If the test passes or fails at this byte value we are done.
 
52
 *  Otherwise iterate test_nrange again, appending another byte.
 
53
 */
 
54
void test_nrange(struct testval *p)
 
55
{
 
56
    struct testval f, l, s;
 
57
    apr_status_t rc;
 
58
    int success = 0;
 
59
    
 
60
    memcpy (&s, p, sizeof(s));
 
61
    ++s.nl;    
 
62
    
 
63
    do {
 
64
        apr_size_t nl = s.nl, wl = sizeof(s.w) / 2;
 
65
        rc = apr_conv_utf8_to_ucs2(s.n, &nl, s.w, &wl);
 
66
        s.wl = (sizeof(s.w) / 2) - wl;
 
67
        if (!nl && rc == APR_SUCCESS) {
 
68
            if (!success) {
 
69
                memcpy(&f, &s, sizeof(s));
 
70
                success = -1;
 
71
            }
 
72
            else {
 
73
                if (s.wl != l.wl 
 
74
                 || memcmp(s.w, l.w, (s.wl - 1) * 2) != 0
 
75
                 || s.w[s.wl - 1] != l.w[l.wl - 1] + 1) {
 
76
                    displaynw(&f, &l);
 
77
                    memcpy(&f, &s, sizeof(s));
 
78
                }
 
79
            }            
 
80
            memcpy(&l, &s, sizeof(s));
 
81
        }
 
82
        else {
 
83
            if (success) {
 
84
                displaynw(&f, &l);
 
85
                success = 0;
 
86
            }
 
87
            if (rc == APR_INCOMPLETE) {
 
88
                test_nrange(&s);
 
89
            }
 
90
        }
 
91
    } while (++s.n[s.nl - 1]);
 
92
 
 
93
    if (success) {
 
94
        displaynw(&f, &l);
 
95
        success = 0;
 
96
    }
 
97
}
 
98
 
 
99
/* 
 
100
 *  Test every possible word value. 
 
101
 *  Once we are finished, retest every possible word value.
 
102
 *  if the test fails on the following null word, iterate test_nrange 
 
103
 *  again, appending another word.
 
104
 *  This assures the output order of the two tests are in sync.
 
105
 */
 
106
void test_wrange(struct testval *p)
 
107
{
 
108
    struct testval f, l, s;
 
109
    apr_status_t rc;
 
110
    int success = 0;
 
111
    
 
112
    memcpy (&s, p, sizeof(s));
 
113
    ++s.wl;    
 
114
    
 
115
    do {
 
116
        apr_size_t nl = sizeof(s.n), wl = s.wl;        
 
117
        rc = apr_conv_ucs2_to_utf8(s.w, &wl, s.n, &nl);
 
118
        s.nl = sizeof(s.n) - nl;
 
119
        if (!wl && rc == APR_SUCCESS) {
 
120
            if (!success) {
 
121
                memcpy(&f, &s, sizeof(s));
 
122
                success = -1;
 
123
            }
 
124
            else {
 
125
                if (s.nl != l.nl 
 
126
                 || memcmp(s.n, l.n, s.nl - 1) != 0
 
127
                 || s.n[s.nl - 1] != l.n[l.nl - 1] + 1) {
 
128
                    displaynw(&f, &l);
 
129
                    memcpy(&f, &s, sizeof(s));
 
130
                }
 
131
            }            
 
132
            memcpy(&l, &s, sizeof(s));
 
133
        }
 
134
        else {
 
135
            if (success) {
 
136
                displaynw(&f, &l);
 
137
                success = 0;
 
138
            }
 
139
        }
 
140
    } while (++s.w[s.wl - 1]);
 
141
 
 
142
    if (success) {
 
143
        displaynw(&f, &l);
 
144
        success = 0;
 
145
    }
 
146
 
 
147
    do {
 
148
        int wl = s.wl, nl = sizeof(s.n);
 
149
        rc = apr_conv_ucs2_to_utf8(s.w, &wl, s.n, &nl);
 
150
        s.nl = sizeof(s.n) - s.nl;
 
151
        if (rc == APR_INCOMPLETE) {
 
152
            test_wrange(&s);
 
153
        }
 
154
    } while (++s.w[s.wl - 1]);
 
155
}
 
156
 
 
157
/*
 
158
 *  Syntax: testucs [w|n]
 
159
 *
 
160
 *  If arg is not recognized, run both tests.
 
161
 */
 
162
int main(int argc, char **argv)
 
163
{
 
164
    struct testval s;
 
165
    memset (&s, 0, sizeof(s));
 
166
 
 
167
    if (argc < 2 || apr_tolower(*argv[1]) != 'w') {
 
168
        printf ("\n\nTesting Narrow Char Ranges\n");
 
169
        test_nrange(&s);
 
170
    }
 
171
    if (argc < 2 || apr_tolower(*argv[1]) != 'n') {
 
172
        printf ("\n\nTesting Wide Char Ranges\n");
 
173
        test_wrange(&s);
 
174
    }
 
175
    return 0;
 
176
}