validate_recipients() now strips out duplicate recipients ... probably some 15 years...
[citadel] / webcit / tests / test_tools.c
1 #include "webcit_test.h"
2 #include <CUnit/CUnit.h>
3 #include <CUnit/Basic.h>
4 #include <CUnit/TestDB.h>
5
6
7 #include "webcit.h"
8 #include "webserver.h"
9 #include "modules_init.h"
10 #include <stdio.h>
11
12
13 wcsession *TestSessionList = NULL;
14
15
16 ParsedHttpHdrs Hdr;
17 wcsession *TheSession;
18
19 extern StrBuf *Username;
20 extern StrBuf *Passvoid;
21
22
23 extern int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf);
24 extern wcsession *CreateSession(int Lockable, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex);
25 extern void groupdav_main(void);
26
27
28
29 void SetUpContext(void)
30 {
31         memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
32         Hdr.HR.eReqType = eGET;
33         http_new_modules(&Hdr); 
34
35
36         Hdr.http_sock = 1; /* STDOUT */
37 /* Context loop */
38         Hdr.HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
39         TheSession = CreateSession(1, &TestSessionList, &Hdr, NULL);
40         TheSession->lastreq = time(NULL);                       /* log */
41         TheSession->Hdr = &Hdr;
42         Hdr.HTTPHeaders = NewHash(1, NULL);
43         session_attach_modules(TheSession);
44 }
45
46 int SetUpConnection(void)
47 {
48         StrBuf *Response;
49         if (!GetConnected ())
50         {
51                 Response = NewStrBuf();
52                 become_logged_in(Username, Passvoid, Response);
53                 return 1;
54                 
55         }
56         else {
57                 CU_FAIL("Establishing session failed!");
58                 return 0;
59         }
60 }
61
62 void SetHttpURL(ParsedHttpHdrs *Hdr, const char *Title, long tlen, StrBuf *Buf)
63 {
64         StrBuf *Line = NewStrBufPlain (Title, tlen);
65         FreeStrBuf(&Line);
66 }
67
68 /* from context_loop.c: */
69 extern void DestroyHttpHeaderHandler(void *V);
70 extern int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf);
71 void SetUpRequest(const char *UrlPath)
72 {
73         OneHttpHeader *pHdr;
74         wcsession *WCC = WC;
75         StrBuf *Buf;
76         StrBuf *Line, *HeaderName;
77
78         HeaderName = NewStrBuf();
79         Buf = NewStrBuf();
80         Line = NewStrBuf();
81         StrBufPrintf(Line, "GET %s HTTP/1.0\r\n", UrlPath);
82
83         WCC->Hdr->HTTPHeaders = NewHash(1, NULL);
84         pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
85         memset(pHdr, 0, sizeof(OneHttpHeader));
86         pHdr->Val = Line;
87         Put(Hdr.HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
88         syslog(9, "%s\n", ChrPtr(Line));
89
90         if (ReadHttpSubject(&Hdr, Line, HeaderName))
91                 CU_FAIL("Failed to parse Request line / me is bogus!");
92
93         FreeStrBuf(&Buf);
94 }
95
96
97
98 void TearDownRequest(void)
99 {
100 /* End Context loop */
101         http_detach_modules(&Hdr);
102 }
103
104 void TearDownContext(void)
105 {
106         http_destroy_modules(&Hdr);
107 /* End Session Loop */
108         session_detach_modules(TheSession);
109         session_destroy_modules(&TheSession);
110
111 /* End Session loop */
112 /* now shut it down clean. */
113 //      shutdown_sessions();
114         do_housekeeping();
115 }
116
117 void test_worker_entry(StrBuf *UrlPath)
118 {
119
120
121 }
122
123
124 void SetGroupdavHeaders(int DavDepth)
125 {
126         Hdr.HR.dav_depth = DavDepth;
127 }
128
129 void FlushHeaders(void)
130 {
131
132 }
133
134 void test_groupdav_directorycommands(void)
135 {
136         SetUpContext();
137         if (SetUpConnection())
138         {
139                 SetUpRequest("/");
140                 SetGroupdavHeaders(0);
141                 groupdav_main();
142                 FlushHeaders();
143                 TearDownRequest();
144
145                 SetUpRequest("/");
146                 SetGroupdavHeaders(1);
147                 groupdav_main();
148                 FlushHeaders();
149                 TearDownRequest();
150
151
152                 SetUpRequest("/groupdav");
153                 SetGroupdavHeaders(0);
154                 groupdav_main();
155                 FlushHeaders();
156                 TearDownRequest();
157
158                 SetUpRequest("/groupdav");
159                 SetGroupdavHeaders(1);
160                 groupdav_main();
161                 FlushHeaders();
162                 TearDownRequest();
163
164                 SetUpRequest("/groupdav/");
165                 SetGroupdavHeaders(0);
166                 groupdav_main();
167                 FlushHeaders();
168                 TearDownRequest();
169
170                 SetUpRequest("/groupdav/");
171                 SetGroupdavHeaders(1);
172                 groupdav_main();
173                 FlushHeaders();
174                 TearDownRequest();
175
176
177                 SetUpRequest("/groupdav/My%20Folders");
178                 SetGroupdavHeaders(0);
179                 groupdav_main();
180                 FlushHeaders();
181                 TearDownRequest();
182
183                 SetUpRequest("/groupdav/My%20Folders");
184                 SetGroupdavHeaders(1);
185                 groupdav_main();
186                 FlushHeaders();
187                 TearDownRequest();
188
189                 SetUpRequest("/groupdav/My%20Folders/");
190                 SetGroupdavHeaders(0);
191                 groupdav_main();
192                 FlushHeaders();
193                 TearDownRequest();
194
195                 SetUpRequest("/groupdav/My%20Folders/");
196                 SetGroupdavHeaders(1);
197                 groupdav_main();
198                 FlushHeaders();
199                 TearDownRequest();
200
201                 SetUpRequest("/groupdav/My%20Folders/Calendar");
202                 SetGroupdavHeaders(0);
203                 groupdav_main();
204                 FlushHeaders();
205                 TearDownRequest();
206
207                 SetUpRequest("/groupdav/My%20Folders/Calendar");
208                 SetGroupdavHeaders(1);
209                 groupdav_main();
210                 FlushHeaders();
211                 TearDownRequest();
212
213                 SetUpRequest("/groupdav/My%20Folders/Calendar/");
214                 SetGroupdavHeaders(0);
215                 groupdav_main();
216                 FlushHeaders();
217                 TearDownRequest();
218
219                 SetUpRequest("/groupdav/My%20Folders/Calendar/");
220                 SetGroupdavHeaders(1);
221                 groupdav_main();
222                 FlushHeaders();
223                 TearDownRequest();
224
225
226                 SetUpRequest("/");
227                 SetGroupdavHeaders(0);
228                 groupdav_main();
229                 FlushHeaders();
230                 TearDownRequest();
231
232                 SetUpRequest("/");
233                 SetGroupdavHeaders(1);
234                 groupdav_main();
235                 FlushHeaders();
236                 TearDownRequest();
237         }
238
239
240         TearDownContext();
241 }
242
243 extern void httplang_to_locale(StrBuf *LocaleString, wcsession *sess);
244
245 static void test_gettext(const char *str, long len)
246 {
247         StrBuf *Test = NewStrBufPlain(str, len);
248
249         SetUpContext();
250         httplang_to_locale(Test, TheSession);
251         TearDownContext();
252
253         FreeStrBuf(&Test);
254 }
255
256 void test_gettext_headerevaluation_Opera(void)
257 {
258         test_gettext(HKEY("sq;q=1.0,de;q=0.9,as;q=0.8,ar;q=0.7,bn;q=0.6,zh-cn;q=0.5,kn;q=0.4,ch;q=0.3,fo;q=0.2,gn;q=0.1,ce;q=0.1,ie;q=0.1"));
259 }
260
261 void test_gettext_headerevaluation_firefox1(void)
262 {
263         test_gettext(HKEY("de-de,en-us;q=0.7,en;q=0.3"));
264 }
265
266 void test_gettext_headerevaluation_firefox2(void)
267 {
268         test_gettext(HKEY("de,en-ph;q=0.8,en-us;q=0.5,de-at;q=0.3"));
269 }
270
271 void test_gettext_headerevaluation_firefox3(void)
272 {
273         test_gettext(HKEY("de,en-us;q=0.9,it;q=0.9,de-de;q=0.8,en-ph;q=0.7,de-at;q=0.7,zh-cn;q=0.6,cy;q=0.5,ar-om;q=0.5,en-tt;q=0.4,xh;q=0.3,nl-be;q=0.3,cs;q=0.2,sv;q=0.1,tk;q=0.1"));
274 }
275
276 void test_gettext_headerevaluation_ie7(void)
277 {
278 // ie7????
279 // User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; FunWebProducts; FBSMTWB; GTB6.3; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618; Seekmo 10.3.86.0)
280
281         test_gettext(HKEY("en-us,x-ns1MvoLpRxbNhu,x-ns2F0f0NnyPOPN"));
282 }
283
284 static void AddTests(void)
285 {
286         CU_pSuite pGroup = NULL;
287         CU_pTest pTest = NULL;
288 /*
289         pGroup = CU_add_suite("TestLocaleEvaluator", NULL, NULL);
290         pTest = CU_add_test(pGroup, "Test ie7", test_gettext_headerevaluation_ie7);
291         pTest = CU_add_test(pGroup, "Test Opera", test_gettext_headerevaluation_Opera);
292         pTest = CU_add_test(pGroup, "Test firefox1", test_gettext_headerevaluation_firefox1);
293         pTest = CU_add_test(pGroup, "Test firefox2", test_gettext_headerevaluation_firefox2);
294         pTest = CU_add_test(pGroup, "Test firefox3", test_gettext_headerevaluation_firefox3);
295 */
296         pGroup = CU_add_suite("TestUrlPatterns", NULL, NULL);
297         pTest = CU_add_test(pGroup, "Test", test_groupdav_directorycommands);
298
299
300 }
301
302
303
304
305
306
307
308 void run_tests(void)
309 {
310         setvbuf(stdout, NULL, _IONBF, 0);
311
312         CU_set_output_filename("TestAutomated");
313         if (CU_initialize_registry()) {
314                 printf("\nInitialize of test Registry failed.");
315         }
316
317         AddTests();
318
319         printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
320         CU_cleanup_registry();
321
322 }