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

« back to all changes in this revision

Viewing changes to srclib/apr-util/test/testxml.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 2000-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 "apr_general.h"
 
19
#include "apr_xml.h"
 
20
 
 
21
#if APR_HAVE_STDLIB_H
 
22
#include <stdlib.h>  /* for exit() */
 
23
#endif
 
24
 
 
25
static const char *progname;
 
26
static const char *usage = "%s [xmlfile]\nIt will create "
 
27
                           "a dummy XML file if none is supplied";
 
28
/*
 
29
 * If our platform knows about the tmpnam() external buffer size, create
 
30
 * a buffer to pass in.  This is needed in a threaded environment, or
 
31
 * one that thinks it is (like HP-UX).
 
32
 */
 
33
 
 
34
#ifdef L_tmpnam
 
35
static char tname_buf[L_tmpnam];
 
36
#else
 
37
static char *tname_buf = NULL;
 
38
#endif
 
39
 
 
40
static apr_status_t create_dummy_file_error(apr_pool_t *p, apr_file_t **fd)
 
41
{
 
42
    apr_status_t rv;
 
43
    char *tmpfile;
 
44
    int i;
 
45
    apr_off_t off = 0L;
 
46
    tmpfile = tmpnam(tname_buf);
 
47
 
 
48
    if ((tmpfile == NULL) || (*tmpfile == '\0')) {
 
49
        fprintf(stderr, "unable to generate temporary filename\n");
 
50
        if (errno == 0) {
 
51
            errno = ENOENT;
 
52
        }
 
53
        perror("tmpnam");
 
54
        return APR_ENOENT;
 
55
    }
 
56
    rv = apr_file_open(fd, tmpfile, APR_CREATE|APR_TRUNCATE|APR_DELONCLOSE|
 
57
                       APR_READ|APR_WRITE|APR_EXCL, APR_OS_DEFAULT, p);
 
58
 
 
59
    if (rv != APR_SUCCESS)
 
60
        return rv;
 
61
    rv = apr_file_puts("<?xml version=\"1.0\" ?>\n<maryx>"
 
62
                       "<had a=\"little\"/><lamb its='fleece "
 
63
                       "was white as snow' />\n", *fd);
 
64
    if (rv != APR_SUCCESS)
 
65
        return rv;
 
66
 
 
67
    for (i = 0; i < 5000; i++) {
 
68
        rv = apr_file_puts("<hmm roast=\"lamb\" "
 
69
                           "for=\"dinner\">yummy</hmm>\n", *fd);
 
70
        if (rv != APR_SUCCESS)
 
71
            return rv;
 
72
    }
 
73
    rv = apr_file_puts("</mary>\n", *fd);
 
74
    if (rv != APR_SUCCESS)
 
75
        return rv;
 
76
 
 
77
    return apr_file_seek(*fd, APR_SET, &off);
 
78
}
 
79
 
 
80
static apr_status_t create_dummy_file(apr_pool_t *p, apr_file_t **fd)
 
81
{
 
82
    apr_status_t rv;
 
83
    char *tmpfile;
 
84
    int i;
 
85
    apr_off_t off = 0L;
 
86
    tmpfile = tmpnam(tname_buf);
 
87
 
 
88
    if ((tmpfile == NULL) || (*tmpfile == '\0')) {
 
89
        fprintf(stderr, "unable to generate temporary filename\n");
 
90
        if (errno == 0) {
 
91
            errno = ENOENT;
 
92
        }
 
93
        perror("tmpnam");
 
94
        return APR_ENOENT;
 
95
    }
 
96
    rv = apr_file_open(fd, tmpfile, APR_CREATE|APR_TRUNCATE|APR_DELONCLOSE|
 
97
                       APR_READ|APR_WRITE|APR_EXCL, APR_OS_DEFAULT, p);
 
98
 
 
99
    if (rv != APR_SUCCESS)
 
100
        return rv;
 
101
    rv = apr_file_puts("<?xml version=\"1.0\" ?>\n<mary>"
 
102
                       "<had a=\"little\"/><lamb its='fleece "
 
103
                       "was white as snow' />\n", *fd);
 
104
    if (rv != APR_SUCCESS)
 
105
        return rv;
 
106
 
 
107
    for (i = 0; i < 5000; i++) {
 
108
        rv = apr_file_puts("<hmm roast=\"lamb\" "
 
109
                           "for=\"dinner\">yummy</hmm>\n", *fd);
 
110
        if (rv != APR_SUCCESS)
 
111
            return rv;
 
112
    }
 
113
    rv = apr_file_puts("</mary>\n", *fd);
 
114
    if (rv != APR_SUCCESS)
 
115
        return rv;
 
116
 
 
117
    rv = apr_file_seek(*fd, APR_SET, &off);
 
118
    return rv;
 
119
}
 
120
 
 
121
static void dump_xml(apr_xml_elem *e, int level)
 
122
{
 
123
    apr_xml_attr *a;
 
124
    apr_xml_elem *ec;
 
125
 
 
126
    printf("%d: element %s\n", level, e->name);
 
127
    if (e->attr) {
 
128
        a = e->attr;
 
129
        printf("%d:\tattrs\t", level);
 
130
        while (a) {
 
131
            printf("%s=%s\t", a->name, a->value);
 
132
            a = a->next;
 
133
        }
 
134
        printf("\n");
 
135
    }
 
136
    if (e->first_child) {
 
137
        ec = e->first_child;
 
138
        while (ec) {
 
139
            dump_xml(ec, level + 1);
 
140
            ec = ec->next;
 
141
        }
 
142
    }
 
143
}
 
144
 
 
145
static void oops(const char *s1, const char *s2, apr_status_t rv)
 
146
{
 
147
    if (progname)
 
148
        fprintf(stderr, "%s: ", progname);
 
149
    fprintf(stderr, s1, s2);
 
150
    if (rv != APR_SUCCESS) {
 
151
        char buf[120];
 
152
 
 
153
        fprintf(stderr, " (%s)", apr_strerror(rv, buf, sizeof buf));
 
154
    }
 
155
    fprintf(stderr, "\n");
 
156
    exit(1);
 
157
}
 
158
 
 
159
int main(int argc, const char *const * argv)
 
160
{
 
161
    apr_pool_t *pool;
 
162
    apr_file_t *fd;
 
163
    apr_xml_parser *parser;
 
164
    apr_xml_doc *doc;
 
165
    apr_status_t rv;
 
166
    char errbuf[2000];
 
167
    char errbufXML[2000];
 
168
 
 
169
    (void) apr_initialize();
 
170
    apr_pool_create(&pool, NULL);
 
171
    progname = argv[0];
 
172
    if (argc == 1) {
 
173
        rv = create_dummy_file(pool, &fd);
 
174
        if (rv != APR_SUCCESS) {
 
175
            oops("cannot create dummy file", "oops", rv);
 
176
        }
 
177
    }
 
178
    else {
 
179
        if (argc == 2) {
 
180
            rv = apr_file_open(&fd, argv[1], APR_READ, APR_OS_DEFAULT, pool);
 
181
            if (rv != APR_SUCCESS) {
 
182
                oops("cannot open: %s", argv[1], rv);
 
183
            }
 
184
        }
 
185
        else {
 
186
            oops("usage: %s", usage, 0);
 
187
        }
 
188
    }
 
189
    rv = apr_xml_parse_file(pool, &parser, &doc, fd, 2000);
 
190
    if (rv != APR_SUCCESS) {
 
191
        fprintf(stderr, "APR Error %s\nXML Error: %s\n",
 
192
                apr_strerror(rv, errbuf, sizeof(errbuf)),
 
193
             apr_xml_parser_geterror(parser, errbufXML, sizeof(errbufXML)));
 
194
        return rv;
 
195
    }
 
196
    dump_xml(doc->root, 0);
 
197
    apr_file_close(fd);
 
198
    if (argc == 1) {
 
199
        rv = create_dummy_file_error(pool, &fd);
 
200
        if (rv != APR_SUCCESS) {
 
201
            oops("cannot create error dummy file", "oops", rv);
 
202
        }
 
203
        rv = apr_xml_parse_file(pool, &parser, &doc, fd, 2000);
 
204
        if (rv != APR_SUCCESS) {
 
205
            fprintf(stdout, "APR Error %s\nXML Error: %s "
 
206
                            "(EXPECTED) This is good.\n",
 
207
                    apr_strerror(rv, errbuf, sizeof(errbuf)),
 
208
             apr_xml_parser_geterror(parser, errbufXML, sizeof(errbufXML)));
 
209
             rv = APR_SUCCESS; /* reset the return code, as the test is supposed to get this error */
 
210
        }
 
211
        else {
 
212
            fprintf(stderr, "Expected an error, but didn't get one ;( ");
 
213
            return APR_EGENERAL;
 
214
        }
 
215
    }
 
216
    apr_pool_destroy(pool);
 
217
    apr_terminate();
 
218
    return rv;
 
219
}