annotate test/cookies.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents 20e683b693c0
children
rev   line source
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
1 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
2 * Dillo cookies test
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
3 *
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
6 * the Free Software Foundation; either version 3 of the License, or
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
7 * (at your option) any later version.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
8 *
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
12 * GNU General Public License for more details.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
13 *
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
16 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
17
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
18 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
19 * This has a big blob of the current src/IO/dpi.c in it.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
20 * I hope there's a better way.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
21 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
22
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
23 #include <stdlib.h> /* malloc, etc. */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
24 #include <unistd.h> /* read, etc. */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
25 #include <stdio.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
26 #include <stdarg.h> /* va_list */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
27 #include <string.h> /* strchr */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
28 #include <errno.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
29 #include <ctype.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
30 #include <time.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
31 /* net */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
32 #include <sys/types.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
33 #include <sys/socket.h>
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
34 #include <arpa/inet.h>
1529
e01bbd700e83 fix building of new cookies test program on *BSD
Johannes Hofmann <Johannes.Hofmann@gmx.de>
parents: 1528
diff changeset
35 #include <netinet/in.h>
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
36
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
37
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
38 #define _MSG(...)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
39
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
40 #define MSG_INNARDS(prefix, ...) \
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
41 D_STMT_START { \
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
42 printf(prefix __VA_ARGS__); \
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
43 fflush (stdout); \
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
44 } D_STMT_END
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
45
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
46 #define MSG(...) MSG_INNARDS("", __VA_ARGS__)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
47 #define MSG_ERR(...) MSG_INNARDS("** ERROR **: ", __VA_ARGS__)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
48
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
49
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
50 #include "../dlib/dlib.h"
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
51 #include "../dpip/dpip.h"
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
52
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
53 static uint_t failed = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
54 static uint_t passed = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
55
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
56 static char SharedKey[32];
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
57
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
58 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
59 * Read all the available data from a filedescriptor.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
60 * This is intended for short answers, i.e. when we know the server
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
61 * will write it all before being preempted. For answers that may come
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
62 * as an stream with delays, non-blocking is better.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
63 * Return value: read data, or NULL on error and no data.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
64 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
65 static char *Dpi_blocking_read(int fd)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
66 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
67 int st;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
68 const int buf_sz = 8*1024;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
69 char buf[buf_sz], *msg = NULL;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
70 Dstr *dstr = dStr_sized_new(buf_sz);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
71
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
72 do {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
73 st = read(fd, buf, buf_sz);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
74 if (st < 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
75 if (errno == EINTR) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
76 continue;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
77 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
78 MSG_ERR("[Dpi_blocking_read] %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
79 break;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
80 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
81 } else if (st > 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
82 dStr_append_l(dstr, buf, st);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
83 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
84 } while (st == buf_sz);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
85
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
86 msg = (dstr->len > 0) ? dstr->str : NULL;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
87 dStr_free(dstr, (dstr->len > 0) ? FALSE : TRUE);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
88 return msg;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
89 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
90
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
91 static void Dpi_close_fd(int fd)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
92 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
93 int st;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
94
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
95 dReturn_if (fd < 0);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
96 do
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
97 st = close(fd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
98 while (st < 0 && errno == EINTR);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
99 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
100
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
101 static int Dpi_make_socket_fd()
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
102 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
103 int fd, ret = -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
104
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
105 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
106 ret = fd;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
107 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
108 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
109 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
110
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
111 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
112 * Read dpid's communication keys from its saved file.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
113 * Return value: 1 on success, -1 on error.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
114 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
115 static int Dpi_read_comm_keys(int *port)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
116 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
117 FILE *In;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
118 char *fname, *rcline = NULL, *tail;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
119 int i, ret = -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
120
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
121 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
122 if ((In = fopen(fname, "r")) == NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
123 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
124 } else if ((rcline = dGetline(In)) == NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
125 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
126 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
127 *port = strtol(rcline, &tail, 10);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
128 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
129 SharedKey[i] = tail[i+1];
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
130 SharedKey[i] = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
131 ret = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
132 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
133 if (In)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
134 fclose(In);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
135 dFree(rcline);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
136 dFree(fname);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
137
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
138 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
139 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
140
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
141 static int Dpi_check_dpid_ids()
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
142 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
143 struct sockaddr_in sin;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
144 const socklen_t sin_sz = sizeof(sin);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
145 int sock_fd, dpid_port, ret = -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
146
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
147 /* socket connection test */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
148 memset(&sin, 0, sizeof(sin));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
149 sin.sin_family = AF_INET;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
150 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
151
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
152 if (Dpi_read_comm_keys(&dpid_port) != -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
153 sin.sin_port = htons(dpid_port);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
154 if ((sock_fd = Dpi_make_socket_fd()) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
155 MSG("Dpi_check_dpid_ids: sock_fd=%d %s\n", sock_fd, dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
156 } else if (connect(sock_fd, (struct sockaddr *)&sin, sin_sz) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
157 MSG("Dpi_check_dpid_ids: %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
158 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
159 Dpi_close_fd(sock_fd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
160 ret = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
161 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
162 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
163 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
164 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
165
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
166 static int Dpi_blocking_write(int fd, const char *msg, int msg_len)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
167 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
168 int st, sent = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
169
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
170 while (sent < msg_len) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
171 st = write(fd, msg + sent, msg_len - sent);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
172 if (st < 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
173 if (errno == EINTR) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
174 continue;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
175 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
176 MSG_ERR("[Dpi_blocking_write] %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
177 break;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
178 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
179 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
180 sent += st;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
181 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
182
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
183 return (sent == msg_len) ? 1 : -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
184 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
185
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
186 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
187 * Start dpid.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
188 * Return: 0 starting now, 1 Error.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
189 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
190 static int Dpi_start_dpid(void)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
191 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
192 pid_t pid;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
193 int st_pipe[2], ret = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
194 char *answer;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
195
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
196 /* create a pipe to track our child's status */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
197 if (pipe(st_pipe))
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
198 return 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
199
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
200 pid = fork();
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
201 if (pid == 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
202 /* This is the child process. Execute the command. */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
203 char *path1 = dStrconcat(dGethomedir(), "/.dillo/dpid", NULL);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
204 Dpi_close_fd(st_pipe[0]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
205 if (execl(path1, "dpid", (char*)NULL) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
206 dFree(path1);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
207 if (execlp("dpid", "dpid", (char*)NULL) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
208 MSG("Dpi_start_dpid (child): %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
209 if (Dpi_blocking_write(st_pipe[1], "ERROR", 5) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
210 MSG("Dpi_start_dpid (child): can't write to pipe.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
211 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
212 Dpi_close_fd(st_pipe[1]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
213 _exit (EXIT_FAILURE);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
214 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
215 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
216 } else if (pid < 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
217 /* The fork failed. Report failure. */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
218 MSG("Dpi_start_dpid: %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
219 /* close the unused pipe */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
220 Dpi_close_fd(st_pipe[0]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
221 Dpi_close_fd(st_pipe[1]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
222
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
223 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
224 /* This is the parent process, check our child status... */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
225 Dpi_close_fd(st_pipe[1]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
226 if ((answer = Dpi_blocking_read(st_pipe[0])) != NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
227 MSG("Dpi_start_dpid: can't start dpid\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
228 dFree(answer);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
229 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
230 ret = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
231 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
232 Dpi_close_fd(st_pipe[0]);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
233 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
234
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
235 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
236 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
237
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
238 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
239 * Confirm that the dpid is running. If not, start it.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
240 * Return: 0 running OK, 1 starting (EAGAIN), 2 Error.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
241 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
242 static int Dpi_check_dpid(int num_tries)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
243 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
244 static int starting = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
245 int check_st = 1, ret = 2;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
246
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
247 check_st = Dpi_check_dpid_ids();
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
248 _MSG("Dpi_check_dpid: check_st=%d\n", check_st);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
249
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
250 if (check_st == 1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
251 /* connection test with dpi server passed */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
252 starting = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
253 ret = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
254 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
255 if (!starting) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
256 /* start dpid */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
257 if (Dpi_start_dpid() == 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
258 starting = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
259 ret = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
260 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
261 } else if (++starting < num_tries) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
262 /* starting */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
263 ret = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
264 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
265 /* we waited too much, report an error... */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
266 starting = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
267 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
268 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
269
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
270 _MSG("Dpi_check_dpid:: %s\n",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
271 (ret == 0) ? "OK" : (ret == 1 ? "EAGAIN" : "ERROR"));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
272 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
273 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
274
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
275
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
276 static int Dpi_blocking_start_dpid(void)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
277 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
278 int cst, try = 0,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
279 n_tries = 12; /* 3 seconds */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
280
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
281 /* test the dpid, and wait a bit for it to start if necessary */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
282 while ((cst = Dpi_check_dpid(n_tries)) == 1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
283 MSG("Dpi_blocking_start_dpid: try %d\n", ++try);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
284 usleep(250000); /* 1/4 sec */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
285 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
286 return cst;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
287 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
288
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
289
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
290 /*
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
291 * Return the dpi server's port number, or -1 on error.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
292 * (A query is sent to dpid and then its answer parsed)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
293 * note: as the available servers and/or the dpi socket directory can
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
294 * change at any time, we'll ask each time. If someday we find
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
295 * that connecting each time significantly degrades performance,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
296 * an optimized approach can be tried.
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
297 */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
298 static int Dpi_get_server_port(const char *server_name)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
299 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
300 int sock_fd = -1, dpi_port = -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
301 int dpid_port, ok = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
302 struct sockaddr_in sin;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
303 char *cmd, *request, *rply = NULL, *port_str;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
304 socklen_t sin_sz;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
305
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
306 dReturn_val_if_fail (server_name != NULL, dpi_port);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
307 _MSG("Dpi_get_server_port:: server_name = [%s]\n", server_name);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
308
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
309 /* Read dpid's port from saved file */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
310 if (Dpi_read_comm_keys(&dpid_port) != -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
311 ok = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
312 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
313 if (ok) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
314 /* Connect a socket with dpid */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
315 ok = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
316 sin_sz = sizeof(sin);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
317 memset(&sin, 0, sizeof(sin));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
318 sin.sin_family = AF_INET;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
319 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
320 sin.sin_port = htons(dpid_port);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
321 if ((sock_fd = Dpi_make_socket_fd()) == -1 ||
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
322 connect(sock_fd, (struct sockaddr *)&sin, sin_sz) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
323 MSG("Dpi_get_server_port: %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
324 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
325 ok = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
326 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
327 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
328 if (ok) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
329 /* ask dpid to check the dpi and send its port number back */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
330 ok = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
331 request = a_Dpip_build_cmd("cmd=%s msg=%s", "check_server", server_name);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
332 _MSG("[%s]\n", request);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
333
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
334 if (Dpi_blocking_write(sock_fd, request, strlen(request)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
335 MSG("Dpi_get_server_port: %s\n", dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
336 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
337 ok = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
338 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
339 dFree(request);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
340 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
341 if (ok) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
342 /* Get the reply */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
343 ok = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
344 if ((rply = Dpi_blocking_read(sock_fd)) == NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
345 MSG("Dpi_get_server_port: can't read server port from dpid.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
346 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
347 ok = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
348 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
349 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
350 if (ok) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
351 /* Parse reply */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
352 ok = 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
353 cmd = a_Dpip_get_attr(rply, "cmd");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
354 if (strcmp(cmd, "send_data") == 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
355 port_str = a_Dpip_get_attr(rply, "msg");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
356 _MSG("Dpi_get_server_port: rply=%s\n", rply);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
357 _MSG("Dpi_get_server_port: port_str=%s\n", port_str);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
358 dpi_port = strtol(port_str, NULL, 10);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
359 dFree(port_str);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
360 ok = 1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
361 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
362 dFree(cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
363 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
364 dFree(rply);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
365 Dpi_close_fd(sock_fd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
366
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
367 return ok ? dpi_port : -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
368 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
369
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
370
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
371 static int Dpi_connect_socket(const char *server_name, int retry)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
372 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
373 struct sockaddr_in sin;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
374 int sock_fd, err, dpi_port, ret=-1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
375 char *cmd = NULL;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
376
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
377 /* Query dpid for the port number for this server */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
378 if ((dpi_port = Dpi_get_server_port(server_name)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
379 _MSG("Dpi_connect_socket:: can't get port number for %s\n", server_name);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
380 return -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
381 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
382 _MSG("Dpi_connect_socket: server=%s port=%d\n", server_name, dpi_port);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
383
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
384 /* connect with this server's socket */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
385 memset(&sin, 0, sizeof(sin));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
386 sin.sin_family = AF_INET;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
387 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
388 sin.sin_port = htons(dpi_port);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
389
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
390 if ((sock_fd = Dpi_make_socket_fd()) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
391 perror("[dpi::socket]");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
392 } else if (connect(sock_fd, (void*)&sin, sizeof(sin)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
393 err = errno;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
394 sock_fd = -1;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
395 MSG("[dpi::connect] errno:%d %s\n", errno, dStrerror(errno));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
396 if (retry) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
397 switch (err) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
398 case ECONNREFUSED: case EBADF: case ENOTSOCK: case EADDRNOTAVAIL:
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
399 sock_fd = Dpi_connect_socket(server_name, FALSE);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
400 break;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
401 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
402 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
403
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
404 /* send authentication Key (the server closes sock_fd on error) */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
405 } else if (!(cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "auth", SharedKey))) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
406 MSG_ERR("[Dpi_connect_socket] Can't make auth message.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
407 } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
408 MSG_ERR("[Dpi_connect_socket] Can't send auth message.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
409 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
410 ret = sock_fd;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
411 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
412 dFree(cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
413
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
414 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
415 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
416
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
417
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
418 char *a_Dpi_send_blocking_cmd(const char *server_name, const char *cmd)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
419 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
420 int cst, sock_fd;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
421 char *ret = NULL;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
422
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
423 /* test the dpid, and wait a bit for it to start if necessary */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
424 if ((cst = Dpi_blocking_start_dpid()) != 0) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
425 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
426 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
427
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
428 if ((sock_fd = Dpi_connect_socket(server_name, TRUE)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
429 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't connect to server.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
430 } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
431 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't send message.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
432 } if ((ret = Dpi_blocking_read(sock_fd)) == NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
433 MSG_ERR("[a_Dpi_send_blocking_cmd] Can't read message.\n");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
434 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
435 Dpi_close_fd(sock_fd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
436
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
437 return ret;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
438 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
439
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
440
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
441
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
442 void a_Cookies_set(const char *cookie, const char *host, const char *path,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
443 const char *date)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
444 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
445 char *cmd, *dpip_tag;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
446
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
447 if (date)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
448 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s date=%s",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
449 "set_cookie", cookie,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
450 host, path, date);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
451 else
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
452 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s host=%s path=%s",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
453 "set_cookie", cookie,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
454 host, path);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
455
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
456 dpip_tag = a_Dpi_send_blocking_cmd("cookies", cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
457 _MSG("a_Cookies_set: dpip_tag = {%s}\n", dpip_tag);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
458 dFree(dpip_tag);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
459 dFree(cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
460 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
461
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
462
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
463 char *a_Cookies_get_query(const char *scheme, const char *host,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
464 const char *path)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
465 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
466 char *cmd, *dpip_tag, *query;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
467
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
468 cmd = a_Dpip_build_cmd("cmd=%s scheme=%s host=%s path=%s",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
469 "get_cookie", scheme,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
470 host, path);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
471
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
472 /* Get the answer from cookies.dpi */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
473 _MSG("cookies.c: a_Dpi_send_blocking_cmd cmd = {%s}\n", cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
474 dpip_tag = a_Dpi_send_blocking_cmd("cookies", cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
475 _MSG("cookies.c: after a_Dpi_send_blocking_cmd resp={%s}\n", dpip_tag);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
476 dFree(cmd);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
477
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
478 if (dpip_tag != NULL) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
479 query = a_Dpip_get_attr(dpip_tag, "cookie");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
480 dFree(dpip_tag);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
481 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
482 query = dStrdup("");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
483 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
484
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
485 return query;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
486 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
487
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
488 static void expect(int lineno, const char *exp_reply,
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
489 const char *scheme, const char *host, const char *path)
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
490 {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
491 char *reply = a_Cookies_get_query(scheme, host, path);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
492
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
493 if (strcmp(reply, exp_reply)) {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
494 MSG("line %d: EXPECTED: %s GOT: %s\n", lineno, exp_reply, reply);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
495 failed++;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
496 } else {
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
497 passed++;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
498 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
499 }
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
500
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
501 static void toomany()
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
502 {
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
503 a_Cookies_set("1=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
504 a_Cookies_set("2=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
505 a_Cookies_set("3=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
506 a_Cookies_set("4=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
507 a_Cookies_set("5=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
508 a_Cookies_set("6=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
509 a_Cookies_set("7=1", "toomany.com", "/path/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
510 a_Cookies_set("8=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
511 a_Cookies_set("9=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
512 a_Cookies_set("10=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
513 a_Cookies_set("11=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
514 a_Cookies_set("12=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
515 a_Cookies_set("13=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
516 a_Cookies_set("14=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
517 a_Cookies_set("15=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
518 a_Cookies_set("16=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
519 a_Cookies_set("17=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
520 a_Cookies_set("18=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
521 a_Cookies_set("19=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
522 a_Cookies_set("20=1", "toomany.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
523 a_Cookies_set("21=1", "toomany.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
524 /* 1 was oldest and discarded */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
525 expect(__LINE__, "Cookie: 7=1; 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
526 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
527 "20=1; 21=1\r\n", "http", "toomany.com", "/path/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
528 sleep(1);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
529 /* touch all of them except #7 (path matching) */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
530 expect(__LINE__, "Cookie: 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
531 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
532 "20=1; 21=1\r\n", "http", "toomany.com", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
533 a_Cookies_set("22=1", "toomany.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
534 /* 7 was oldest and discarded */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
535 expect(__LINE__, "Cookie: 2=1; 3=1; 4=1; 5=1; 6=1; 8=1; 9=1; 10=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
536 "11=1; 12=1; 13=1; 14=1; 15=1; 16=1; 17=1; 18=1; 19=1; "
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
537 "20=1; 21=1; 22=1\r\n", "http", "toomany.com", "/path/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
538 }
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
539
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
540 static void maxage()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
541 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
542 time_t t = time(NULL)+1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
543 char *server_date = dStrdup(ctime(&t));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
544
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
545 a_Cookies_set("name=val; max-age=0", "maxage0.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
546 expect(__LINE__, "", "http", "maxage0.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
547
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
548 a_Cookies_set("name=val; max-age=100", "maxage100.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
549 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage100.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
550
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
551 a_Cookies_set("name=val; max-age=-100", "maxage-100.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
552 expect(__LINE__, "", "http", "maxage-100.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
553
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1529
diff changeset
554 a_Cookies_set("name=val; max-age=2000000000", "maxage-huge.com", "/", NULL);
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1529
diff changeset
555 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage-huge.com", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
556 /* just having a server date shouldn't matter */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
557
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
558 a_Cookies_set("name=val; max-age=0", "maxage0s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
559 expect(__LINE__, "", "http", "maxage0s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
560
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
561 a_Cookies_set("name=val; max-age=100", "maxage100s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
562 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxage100s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
563
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
564 a_Cookies_set("name=val; max-age=-100", "maxage-100s.com", "/",server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
565 expect(__LINE__, "", "http", "maxage-100s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
566
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
567 /* MAX-AGE and EXPIRES */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
568 a_Cookies_set("name=val; max-age=90; expires=Wed Jan 20 01:26:32 2010",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
569 "maxagelater.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
570 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxagelater.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
571
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
572 a_Cookies_set("name=val; max-age=90; expires=Wed Jan 20 01:26:32 2010",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
573 "maxagelaters.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
574 expect(__LINE__, "Cookie: name=val\r\n", "http", "maxagelaters.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
575
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
576 dFree(server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
577 }
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
578
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
579 static void expires_server_ahead()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
580 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
581 char *string;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
582 time_t t = time(NULL)+1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
583 char *server_date = dStrdup(ctime(&t));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
584 time_t expt = t + 1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
585 char *exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
586
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
587 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
588 a_Cookies_set(string, "e2000s1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
589 expect(__LINE__, "Cookie: name=val\r\n", "http", "e2000s1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
590
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
591 a_Cookies_set(string, "e2000s1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
592 expect(__LINE__, "Cookie: name=val\r\n", "http", "e2000s1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
593
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
594 expt = t - 500; /* past for the server, future for us */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
595 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
596 exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
597
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
598 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
599 a_Cookies_set(string, "e500s1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
600 expect(__LINE__, "Cookie: name=val\r\n", "http", "e500s1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
601
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
602 a_Cookies_set(string, "e500s1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
603 expect(__LINE__, "", "http", "e500s1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
604
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
605 expt = t; /* expire at future-for-us server date */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
606 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
607 exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
608
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
609 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
610 a_Cookies_set(string, "e1000s1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
611 expect(__LINE__, "Cookie: name=val\r\n", "http", "e1000s1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
612
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
613 a_Cookies_set(string, "e1000s1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
614 expect(__LINE__, "", "http", "e1000s1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
615
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
616 expt = time(NULL); /* now */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
617 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
618 exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
619
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
620 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
621 a_Cookies_set(string, "e0s1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
622 expect(__LINE__, "", "http", "e0s1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
623
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
624 a_Cookies_set(string, "e0s1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
625 expect(__LINE__, "", "http", "e0s1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
626
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
627 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
628 dFree(server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
629 }
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
630
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
631 static void expires_server_behind()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
632 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
633 char *string;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
634 time_t t = time(NULL)-1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
635 char *server_date = dStrdup(ctime(&t));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
636
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
637 time_t expt = t + 1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
638 char *exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
639
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
640 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
641 a_Cookies_set(string, "e0s-1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
642 expect(__LINE__, "", "http", "e0s-1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
643
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
644 a_Cookies_set(string, "e0s-1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
645 expect(__LINE__, "Cookie: name=val\r\n", "http", "e0s-1000s.com","/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
646
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
647 expt = t + 500; /* future for the server, past for us */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
648 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
649 exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
650
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
651 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
652 a_Cookies_set(string, "e-500s-1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
653 expect(__LINE__, "", "http", "e-500s-1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
654
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
655 a_Cookies_set(string, "e-500s-1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
656 expect(__LINE__, "Cookie: name=val\r\n", "http", "e-500s-1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
657
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
658 expt = t; /* expire at past-for-us server date */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
659 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
660 exp_date = dStrdup(ctime(&expt));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
661
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
662 string = dStrconcat("name=val; expires=", exp_date, NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
663 a_Cookies_set(string, "e-1000s-1000.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
664 expect(__LINE__, "", "http", "e-1000s-1000.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
665
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
666 a_Cookies_set(string, "e-1000s-1000s.com", "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
667 expect(__LINE__, "", "http", "e-1000s-1000s.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
668
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
669 dFree(server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
670 dFree(exp_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
671 }
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
672
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
673 static void expires_extremes()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
674 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
675 time_t t;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
676 char *server_date;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
677
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
678 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expmin.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
679 "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
680 expect(__LINE__, "", "http", "expmin.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
681
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
682 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expneg.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
683 "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
684 expect(__LINE__, "", "http", "expneg.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
685
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
686 a_Cookies_set("name=val; expires=Thu Jan 1 00:00:00 1970", "expepoch.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
687 "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
688 expect(__LINE__, "", "http", "expepoch.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
689
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
690 /* TODO: revisit these tests in a few decades */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
691 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmax.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
692 "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
693 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmax.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
694
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
695 a_Cookies_set("name=val; expires=Sun Jan 1 00:00:00 2040", "pastmax.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
696 "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
697 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmax.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
698
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
699 t = time(NULL)+1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
700 server_date = dStrdup(ctime(&t));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
701
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
702 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expmina.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
703 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
704 expect(__LINE__, "", "http", "expmina.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
705
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
706 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expnega.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
707 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
708 expect(__LINE__, "", "http", "expnega.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
709
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
710 a_Cookies_set("name=val; expires=Thu Jan 1 00:00:00 1970", "expepocha.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
711 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
712 expect(__LINE__, "", "http", "expepocha.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
713
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
714 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmaxa.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
715 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
716 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmaxa.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
717
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
718 a_Cookies_set("name=val; expires=Sun Jan 1 00:00:00 2040", "pastmaxa.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
719 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
720 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmaxa.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
721
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
722 t = time(NULL)-1000;
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
723 dFree(server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
724 server_date = dStrdup(ctime(&t));
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
725
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
726 a_Cookies_set("name=val; expires=Fri Dec 13 20:45:52 1901", "expminb.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
727 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
728 expect(__LINE__, "", "http", "expminb.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
729
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
730 a_Cookies_set("name=val; expires=Wed Dec 31 23:59:59 1969", "expnegb.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
731 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
732 expect(__LINE__, "", "http", "expnegb.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
733
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
734 a_Cookies_set("name=val; expires=Thu Jan 1 00:00:00 1970", "expepochb.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
735 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
736 expect(__LINE__, "", "http", "expepochb.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
737
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
738 a_Cookies_set("name=val; expires=Tue Jan 19 03:14:07 2038", "expmaxb.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
739 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
740 expect(__LINE__, "Cookie: name=val\r\n", "http", "expmaxb.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
741
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
742 a_Cookies_set("name=val; expires=Sun Jan 1 00:00:00 2040", "pastmaxb.com",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
743 "/", server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
744 expect(__LINE__, "Cookie: name=val\r\n", "http", "pastmaxb.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
745
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
746 dFree(server_date);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
747 }
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
748
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
749 static void path()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
750 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
751 a_Cookies_set("name=val; path=/", "p1.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
752 expect(__LINE__, "Cookie: name=val\r\n", "http", "p1.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
753
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
754 a_Cookies_set("name=val; path=/dir1", "p2.com", "/dir2", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
755 expect(__LINE__, "", "http", "p2.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
756 expect(__LINE__, "", "http", "p2.com", "/d");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
757 expect(__LINE__, "Cookie: name=val\r\n", "http", "p2.com", "/dir1");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
758 expect(__LINE__, "Cookie: name=val\r\n", "http", "p2.com", "/dir1/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
759 expect(__LINE__, "", "http", "p2.com", "/dir2");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
760 expect(__LINE__, "", "http", "p2.com", "/dir11");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
761
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
762 a_Cookies_set("name=val; path=dir1", "p3.com", "/dir2", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
763 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
764 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/dir1");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
765 expect(__LINE__, "Cookie: name=val\r\n", "http", "p3.com", "/dir2");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
766
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
767 a_Cookies_set("name=val; path=/dir1/", "p4.com", "/dir2", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
768 expect(__LINE__, "", "http", "p4.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
769 /* this next one strikes me as a bit odd, personally, but I suppose it's not
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
770 * a big deal */
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
771 expect(__LINE__, "", "http", "p4.com", "/dir1");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
772 expect(__LINE__, "", "http", "p4.com", "/dir11");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
773 expect(__LINE__, "Cookie: name=val\r\n", "http", "p4.com", "/dir1/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
774 expect(__LINE__, "Cookie: name=val\r\n", "http", "p4.com", "/dir1/sub");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
775
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
776 a_Cookies_set("name=val", "p5.com", "/dir/subdir", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
777 expect(__LINE__, "", "http", "p5.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
778 expect(__LINE__, "", "http", "p5.com", "/bir");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
779 expect(__LINE__, "Cookie: name=val\r\n", "http", "p5.com", "/dir");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
780 expect(__LINE__, "Cookie: name=val\r\n", "http", "p5.com", "/dir/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
781
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
782 a_Cookies_set("name=val", "p6.com", "/dir/subdir/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
783 expect(__LINE__, "", "http", "p6.com", "/dir/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
784 expect(__LINE__, "Cookie: name=val\r\n", "http", "p6.com", "/dir/subdir");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
785 expect(__LINE__, "Cookie: name=val\r\n", "http", "p6.com", "/dir/subdir/s");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
786 }
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
787
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
788 int main()
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
789 {
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
790 a_Cookies_set("name=val", "ordinary.com", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
791 expect(__LINE__, "Cookie: name=val\r\n", "http", "ordinary.com", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
792
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
793 toomany();
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
794 maxage();
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
795 expires_server_ahead();
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
796 expires_server_behind();
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
797 expires_extremes();
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
798
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
799 a_Cookies_set("name=val; expires=\"Sun Jan 10 00:00:00 2038\"",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
800 "quoted-date.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
801 expect(__LINE__, "Cookie: name=val\r\n", "http", "quoted-date.org", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
802
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
803 a_Cookies_set("name=val; expires=\"Sun Jan 11 00:00:00 1970\"",
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
804 "quoted-pastdate.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
805 expect(__LINE__, "", "http", "quoted-pastdate.org", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
806
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
807 path();
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
808
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
809 /* LEADING/TRAILING DOTS AND A LITTLE PUBLIC SUFFIX */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
810 a_Cookies_set("name=val; domain=co.uk", "www.co.uk", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
811 expect(__LINE__, "", "http", "www.co.uk", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
812
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
813 a_Cookies_set("name=val; domain=.co.uk", "www.co.uk", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
814 expect(__LINE__, "", "http", "www.co.uk", "/");
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
815
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
816 a_Cookies_set("name=val; domain=co.uk.", "www.co.uk.", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
817 expect(__LINE__, "", "http", "www.co.uk.", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
818
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
819 a_Cookies_set("name=val; domain=.co.uk.", "www.co.uk.", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
820 expect(__LINE__, "", "http", ".www.co.uk.", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
821
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
822 a_Cookies_set("name=val; domain=co.org", "www.co.org", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
823 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.co.org", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
824
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
825 a_Cookies_set("name=val; domain=.cp.org", "www.cp.org", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
826 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.cp.org", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
827
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
828
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
829 /* DOTDOMAIN */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
830 a_Cookies_set("name=val; domain=.dotdomain.org", "dotdomain.org", "/",
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
831 NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
832 expect(__LINE__, "Cookie: name=val\r\n", "http", "dotdomain.org", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
833 expect(__LINE__, "Cookie: name=val\r\n", "http", "www.dotdomain.org", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
834
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
835 /* HOST_ONLY */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
836 a_Cookies_set("name=val; domain=.hostonly.org", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
837 a_Cookies_set("name2=val2", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
838 a_Cookies_set("name3=val3; domain=hostonly.org", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
839 expect(__LINE__, "Cookie: name=val; name2=val2; name3=val3\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
840 "hostonly.org", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
841 a_Cookies_set("name=new; domain=.hostonly.org", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
842 expect(__LINE__, "Cookie: name=new; name2=val2; name3=val3\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
843 "hostonly.org", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
844 a_Cookies_set("name2=new2", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
845 expect(__LINE__, "Cookie: name=new; name2=new2; name3=val3\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
846 "hostonly.org", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
847 a_Cookies_set("name3=new3; domain=hostonly.org", "hostonly.org", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
848 expect(__LINE__, "Cookie: name=new; name2=new2; name3=new3\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
849 "hostonly.org", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
850
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
851 /* SUBDOMAIN */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
852 a_Cookies_set("name=val; domain=www.subdomain.com", "subdomain.com", "/",
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
853 NULL);
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
854 a_Cookies_set("name=val; domain=.www.subdomain.com", "subdomain.com", "/",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
855 NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
856 expect(__LINE__, "", "http", "subdomain.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
857 expect(__LINE__, "", "http", "www.subdomain.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
858
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
859 /* SUPERDOMAIN(?) */
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
860 a_Cookies_set("name=val; domain=.supdomain.com", "www.supdomain.com", "/",
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
861 NULL);
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
862 a_Cookies_set("name2=val2; domain=supdomain.com", "www.supdomain.com", "/",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
863 NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
864 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
865 "sub2.sub.supdomain.com", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
866 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
867 "www.supdomain.com", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
868 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
869 "supdomain.com", "/");
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
870
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
871 /* UNRELATED */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
872 a_Cookies_set("name=val; domain=another.com", "unrelated.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
873 expect(__LINE__, "", "http", "another.com", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
874 a_Cookies_set("name=val; domain=another.com", "a.org", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
875 expect(__LINE__, "", "http", "another.com", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
876 a_Cookies_set("name=val; domain=another.com", "badguys.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
877 expect(__LINE__, "", "http", "another.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
878 a_Cookies_set("name=val; domain=another.com", "more.badguys.com", "/",
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
879 NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
880 expect(__LINE__, "", "http", "another.com", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
881 a_Cookies_set("name=val; domain=another.com", "verybadguys.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
882 expect(__LINE__, "", "http", "another.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
883
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
884 a_Cookies_set("name=val; domain=similar.com", "imilar.com", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
885 a_Cookies_set("name2=val2; domain=similar.com", "ssimilar.com", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
886 a_Cookies_set("name3=val3; domain=.similar.com", "imilar.com", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
887 a_Cookies_set("name4=val4; domain=.similar.com", "timilar.com", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
888 a_Cookies_set("name4=val4; domain=.similar.com", "tiimilar.com", "/", NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
889 expect(__LINE__, "", "http", "similar.com", "/");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1605
diff changeset
890
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
891 /* SECURE */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
892 a_Cookies_set("name=val; secure", "secure.com", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
893 expect(__LINE__, "", "http", "secure.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
894 expect(__LINE__, "Cookie: name=val\r\n", "https", "secure.com", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
895
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
896 /* HTTPONLY */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
897 a_Cookies_set("name=val; HttpOnly", "httponly.net", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
898 expect(__LINE__, "Cookie: name=val\r\n", "http", "httponly.net", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
899
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
900 /* GIBBERISH ATTR IGNORED */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
901 a_Cookies_set("name=val; ldkfals", "gibberish.net", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
902 expect(__LINE__, "Cookie: name=val\r\n", "http", "gibberish.net", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
903
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
904 /* WHITESPACE/DELIMITERS */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
905 a_Cookies_set(" name=val ", "whitespace.net", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
906 a_Cookies_set("name2=val2;", "whitespace.net", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
907 expect(__LINE__, "Cookie: name=val; name2=val2\r\n", "http",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
908 "whitespace.net", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
909
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
910 /* NAMELESS/VALUELESS */
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
911 a_Cookies_set("value", "nonameval.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
912 a_Cookies_set("name=", "nonameval.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
913 a_Cookies_set("name2= ", "nonameval.org", "/", NULL);
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
914 expect(__LINE__, "Cookie: name=; name2=\r\n", "http", "nonameval.org", "/");
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
915 a_Cookies_set("=val2", "nonameval.org", "/", NULL);
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
916 expect(__LINE__, "Cookie: name=; name2=\r\n", "http", "nonameval.org", "/");
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
917
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
918
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
919 /* SOME IP ADDRS */
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
920
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
921 a_Cookies_set("name=val", "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]",
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
922 "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
923 expect(__LINE__, "Cookie: name=val\r\n", "http",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
924 "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
925
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
926 a_Cookies_set("name=val", "[::FFFF:129.144.52.38]", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
927 expect(__LINE__, "Cookie: name=val\r\n", "http", "[::FFFF:129.144.52.38]",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
928 "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
929
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
930 a_Cookies_set("name=val", "127.0.0.1", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
931 expect(__LINE__, "Cookie: name=val\r\n", "http", "127.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
932
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
933 a_Cookies_set("name=val; domain=128.0.0.1", "128.0.0.1", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
934 expect(__LINE__, "Cookie: name=val\r\n", "http", "128.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
935
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
936 a_Cookies_set("name=val; domain=130.0.0.1", "129.0.0.1", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
937 expect(__LINE__, "", "http", "129.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
938 expect(__LINE__, "", "http", "130.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
939
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
940 a_Cookies_set("name=val", "2.0.0.1", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
941 a_Cookies_set("name=bad; domain=22.0.0.1", "2.0.0.1", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
942 a_Cookies_set("name=bad; domain=.0.0.1", "2.0.0.1", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
943 a_Cookies_set("name=bad; domain=not-ip.org", "2.0.0.1", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
944 expect(__LINE__, "", "http", "22.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
945 expect(__LINE__, "", "http", "not-ip.org", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
946 expect(__LINE__, "Cookie: name=val\r\n", "http", "2.0.0.1", "/");
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
947
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
948 #if 0
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
949 HAD BEEN PLAYING AROUND WITH REAL PUBLIC SUFFIX
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
950 a_Cookies_set("name=val;domain=sub.sub.yokohama.jp", "sub.sub.yokohama.jp", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
951 MSG("sub sub yokohama should work: %s\n",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
952 a_Cookies_get_query("http", "sub.sub.yokohama.jp", "/"));
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
953 a_Cookies_set("name=val; domain=sub.tokyo.jp", "sub.sub.tokyo.jp", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
954 MSG("sub tokyo jp should fail: %s\n",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
955 a_Cookies_get_query("http", "sub.sub.tokyo.jp", "/"));
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
956 a_Cookies_set("name=val; domain=pref.chiba.jp", "sub.pref.chiba.jp", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
957 MSG("pref chiba jp should succeed: %s\n",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
958 a_Cookies_get_query("http", "sub.pref.chiba.jp", "/"));
1528
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
959 a_Cookies_set("name=val; domain=org", "www.dillo.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
960 a_Cookies_set("name=val; domain=org", "dillo.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
961 a_Cookies_set("name=val; domain=org", ".dillo.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
962 a_Cookies_set("name=val; domain=org.", ".dillo.org", "/", NULL);
d72793539000 more cookies tests
corvid <corvid@lavabit.com>
parents: 1526
diff changeset
963 a_Cookies_set("name=val; domain=org.", ".dillo.org.", "/", NULL);
1526
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
964 MSG("org should fail: %s\n",
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
965 a_Cookies_get_query("http", "www.dillo.org", "/"));
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
966 #endif
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
967
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
968 MSG("TESTS: passed: %u failed: %u\n", passed, failed);
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
969 return 0;
e7571ffc35e1 cookies test program
corvid <corvid@lavabit.com>
parents:
diff changeset
970 }