chiark / gitweb /
Commit 2.4.5-5 as unpacked
[inn-innduct.git] / tests / lib / qio-t.c
1 /* $Id: qio-t.c 6939 2004-06-10 22:04:58Z hkehoe $ */
2 /* Test suite for the Quick I/O library */
3
4 #include "config.h"
5 #include "clibrary.h"
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
9
10 #include "inn/messages.h"
11 #include "inn/qio.h"
12 #include "libinn.h"
13 #include "libtest.h"
14
15 static void
16 output(int fd, const void *data, size_t size)
17 {
18     if (xwrite(fd, data, size) < 0)
19         sysdie("Can't write to .testout");
20 }
21
22 int
23 main(void)
24 {
25     unsigned char data[256], line[256], out[256];
26     unsigned char c;
27     char *result;
28     int i, count, fd;
29     size_t size = 8192;
30     QIOSTATE *qio;
31     bool success;
32
33 #if HAVE_ST_BLKSIZE
34     struct stat st;
35 #endif
36
37     for (c = 1, i = 0; i < 255; i++, c++)
38         data[i] = c;
39     data[9] = ' ';
40     data[255] = '\255';
41     memcpy(line, data, 255);
42     line[255] = '\n';
43     memcpy(out, data, 255);
44     out[255] = '\0';
45     fd = open(".testout", O_RDWR | O_CREAT | O_TRUNC, 0644);
46     if (fd < 0) sysdie("Can't create .testout");
47
48 #if HAVE_ST_BLKSIZE
49     /* Mostly duplicate the code from qio.c so that we can test with lines
50        exactly as large as the buffer. */
51     if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
52         size = st.st_blksize;
53         if (size > 4 * 8192)
54             size = 8192;
55         else
56             while(size < 8192)
57                 size += st.st_blksize;
58     }
59 #endif /* HAVE_ST_BLKSIZE */
60
61     /* Start with small, equally sized lines exactly equal to the buffer.
62        Then a line equal in size to the buffer, then a short line and
63        another line equal in size to the buffer, then a half line and lines
64        repeated to fill another buffer, then a line that's one character too
65        long. */
66     count = size / 256;
67     for (i = 0; i < count; i++)
68         output(fd, line, 256);
69     for (i = 0; i < count - 1; i++)
70         output(fd, data, 256);
71     output(fd, line, 256);
72     output(fd, "\n", 1);
73     for (i = 0; i < count - 1; i++)
74         output(fd, data, 256);
75     output(fd, line, 256);
76     output(fd, data, 127);
77     output(fd, "\n", 1);
78     for (i = 0; i < count; i++)
79         output(fd, line, 256);
80     for (i = 0; i < count; i++)
81         output(fd, data, 256);
82     output(fd, "\n", 1);
83     close(fd);
84
85     puts("30");
86
87     /* Now make sure we can read all that back correctly. */
88     qio = QIOopen(".testout");
89     ok(1, qio != NULL);
90     ok(2, !QIOerror(qio));
91     ok(3, QIOfileno(qio) > 0);
92     if (unlink(".testout") < 0)
93         sysdie("Can't unlink .testout");
94     for (success = true, i = 0; i < count; i++) {
95         result = QIOread(qio);
96         success = (success && !QIOerror(qio) && (QIOlength(qio) == 255)
97                    && !strcmp(result, (char *) out));
98     }
99     ok(4, success);
100     ok(5, QIOtell(qio) == (off_t) size);
101     result = QIOread(qio);
102     if (strlen(result) < size - 1) {
103         ok(6, false);
104     } else {
105         for (success = true, i = 0; i < count - 1; i++)
106             success = success && !memcmp(result + i * 256, data, 256);
107         success = success && !memcmp(result + i * 256, data, 255);
108         ok(6, success);
109     }
110     ok(7, QIOtell(qio) == (off_t) (2 * size));
111     result = QIOread(qio);
112     ok(8, !QIOerror(qio));
113     ok(9, QIOlength(qio) == 0);
114     ok(10, *result == 0);
115     result = QIOread(qio);
116     if (strlen(result) < size - 1) {
117         ok(11, false);
118     } else {
119         for (success = true, i = 0; i < count - 1; i++)
120             success = success && !memcmp(result + i * 256, data, 256);
121         success = success && !memcmp(result + i * 256, data, 255);
122         ok(11, success);
123     }
124     ok(12, QIOtell(qio) == (off_t) (3 * size + 1));
125     result = QIOread(qio);
126     ok(13, !QIOerror(qio));
127     ok(14, QIOlength(qio) == 127);
128     ok(15, strlen(result) == 127);
129     ok(16, !memcmp(result, data, 127));
130     for (success = true, i = 0; i < count; i++) {
131         result = QIOread(qio);
132         success = (success && !QIOerror(qio) && (QIOlength(qio) == 255)
133                    && !strcmp(result, (char *) out));
134     }
135     ok(17, success);
136     ok(18, QIOtell(qio) == (off_t) (4 * size + 129));
137     result = QIOread(qio);
138     ok(19, !result);
139     ok(20, QIOerror(qio));
140     ok(21, QIOtoolong(qio));
141     ok(22, QIOrewind(qio) == 0);
142     ok(23, QIOtell(qio) == 0);
143     result = QIOread(qio);
144     ok(24, !QIOerror(qio));
145     ok(25, QIOlength(qio) == 255);
146     ok(26, strlen(result) == 255);
147     ok(27, !strcmp(result, (char *) out));
148     ok(28, QIOtell(qio) == 256);
149     fd = QIOfileno(qio);
150     QIOclose(qio);
151     ok(29, close(fd) < 0);
152     ok(30, errno == EBADF);
153
154     return 0;
155 }