25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include <stdio.h> |
29 #include <stdio.h> |
|
30 #include <inttypes.h> |
30 |
31 |
31 #include "../../util/util.h" |
32 #include "../../util/util.h" |
32 #include "../../test/testutils.h" |
33 #include "../../test/testutils.h" |
|
34 #include "../../test/webdav.h" |
33 #include "../../public/nsapi.h" |
35 #include "../../public/nsapi.h" |
|
36 #include "../../public/webdav.h" |
|
37 #include "../../webdav/webdav.h" |
34 |
38 |
35 #include <ucx/string.h> |
39 #include <ucx/string.h> |
36 #include <ucx/utils.h> |
40 #include <ucx/utils.h> |
|
41 #include <ucx/buffer.h> |
37 |
42 |
38 #include "pgtest.h" |
43 #include "pgtest.h" |
39 #include "vfs.h" |
44 #include "vfs.h" |
|
45 #include "webdav.h" |
40 |
46 |
41 #include <libpq-fe.h> |
47 #include <libpq-fe.h> |
|
48 |
|
49 #include <libxml/tree.h> |
42 |
50 |
43 |
51 |
44 static char *pg_connstr = "postgresql://localhost/test1"; |
52 static char *pg_connstr = "postgresql://localhost/test1"; |
45 static int abort_pg_tests = 0; |
53 static int abort_pg_tests = 0; |
46 static PGconn *test_connection; |
54 static PGconn *test_connection; |
81 ucx_test_register(suite, test_pg_vfs_stat); |
89 ucx_test_register(suite, test_pg_vfs_stat); |
82 ucx_test_register(suite, test_pg_vfs_mkdir); |
90 ucx_test_register(suite, test_pg_vfs_mkdir); |
83 ucx_test_register(suite, test_pg_vfs_unlink); |
91 ucx_test_register(suite, test_pg_vfs_unlink); |
84 ucx_test_register(suite, test_pg_vfs_rmdir); |
92 ucx_test_register(suite, test_pg_vfs_rmdir); |
85 |
93 |
|
94 ucx_test_register(suite, test_pg_webdav_create_from_resdata); |
|
95 ucx_test_register(suite, test_pg_prepare_tests); |
|
96 ucx_test_register(suite, test_pg_webdav_propfind); |
|
97 |
86 PGresult *result = PQexec(test_connection, "BEGIN"); |
98 PGresult *result = PQexec(test_connection, "BEGIN"); |
87 PQclear(result); |
99 PQclear(result); |
88 } |
100 } |
89 } |
101 } |
90 |
102 |
329 |
341 |
330 UCX_TEST_END; |
342 UCX_TEST_END; |
331 |
343 |
332 testutil_destroy_session(sn); |
344 testutil_destroy_session(sn); |
333 } |
345 } |
|
346 |
|
347 /* ----------------------------- WebDAV tests ----------------------------- */ |
|
348 |
|
349 |
|
350 static WebdavBackend* create_test_pgdav(Session *sn, Request *rq) { |
|
351 return pg_webdav_create_from_resdata(sn, rq, &resdata); |
|
352 } |
|
353 |
|
354 UCX_TEST(test_pg_webdav_create_from_resdata) { |
|
355 Session *sn = testutil_session(); |
|
356 Request *rq = testutil_request(sn->pool, "PROPFIND", "/"); |
|
357 |
|
358 UCX_TEST_BEGIN; |
|
359 |
|
360 WebdavBackend *dav = create_test_pgdav(sn, rq); |
|
361 UCX_TEST_ASSERT(dav, "cannot create pg dav backend"); |
|
362 |
|
363 UCX_TEST_END; |
|
364 } |
|
365 |
|
366 UCX_TEST(test_pg_prepare_tests) { |
|
367 Session *sn = testutil_session(); |
|
368 Request *rq = testutil_request(sn->pool, "PUT", "/"); |
|
369 rq->vfs = create_test_pgvfs(sn, rq); |
|
370 VFSContext *vfs = vfs_request_context(sn, rq); |
|
371 |
|
372 UCX_TEST_BEGIN; |
|
373 |
|
374 vfs_mkdir(vfs, "/propfind"); |
|
375 SYS_FILE f1; |
|
376 |
|
377 int64_t res1_id, res2_id; |
|
378 |
|
379 f1 = vfs_open(vfs, "/propfind/res1", O_WRONLY|O_CREAT); |
|
380 UCX_TEST_ASSERT(f1, "res1 create failed"); |
|
381 res1_id = ((PgFile*)f1->data)->resource_id; |
|
382 vfs_close(f1); |
|
383 |
|
384 f1 = vfs_open(vfs, "/propfind/res2", O_WRONLY|O_CREAT); |
|
385 UCX_TEST_ASSERT(f1, "res2 create failed"); |
|
386 res2_id = ((PgFile*)f1->data)->resource_id; |
|
387 vfs_close(f1); |
|
388 |
|
389 f1 = vfs_open(vfs, "/propfind/res3", O_WRONLY|O_CREAT); |
|
390 UCX_TEST_ASSERT(f1, "res3 create failed"); |
|
391 vfs_close(f1); |
|
392 |
|
393 // 2 properties for res1 |
|
394 char idstr[32]; |
|
395 snprintf(idstr, 32, "%" PRId64, res1_id); |
|
396 const char* params[1] = { idstr }; |
|
397 PGresult *result = PQexecParams( |
|
398 test_connection, |
|
399 "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'test', 'testvalue');", |
|
400 1, // number of parameters |
|
401 NULL, |
|
402 params, // parameter value |
|
403 NULL, |
|
404 NULL, |
|
405 0); // 0: result in text format |
|
406 |
|
407 UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); |
|
408 PQclear(result); |
|
409 |
|
410 result = PQexecParams( |
|
411 test_connection, |
|
412 "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'prop2', 'value2');", |
|
413 1, // number of parameters |
|
414 NULL, |
|
415 params, // parameter value |
|
416 NULL, |
|
417 NULL, |
|
418 0); // 0: result in text format |
|
419 |
|
420 UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); |
|
421 PQclear(result); |
|
422 |
|
423 // 1 property for res2 |
|
424 snprintf(idstr, 32, "%" PRId64, res2_id); |
|
425 result = PQexecParams( |
|
426 test_connection, |
|
427 "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'test', 'res2test');", |
|
428 1, // number of parameters |
|
429 NULL, |
|
430 params, // parameter value |
|
431 NULL, |
|
432 NULL, |
|
433 0); // 0: result in text format |
|
434 |
|
435 UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); |
|
436 PQclear(result); |
|
437 |
|
438 UCX_TEST_END; |
|
439 |
|
440 testutil_destroy_session(sn); |
|
441 } |
|
442 |
|
443 UCX_TEST(test_pg_webdav_propfind) { |
|
444 Session *sn; |
|
445 Request *rq; |
|
446 TestIOStream *st; |
|
447 pblock *pb; |
|
448 |
|
449 UCX_TEST_BEGIN; |
|
450 |
|
451 // test data: |
|
452 // |
|
453 // /propfind/ |
|
454 // /propfind/res1 (2 properties) |
|
455 // /propfind/res2 (1 property) |
|
456 // /propfind/res3 |
|
457 |
|
458 int ret; |
|
459 // Test 1 |
|
460 init_test_webdav_method(&sn, &rq, &st, &pb, "PROPFIND", "/propfind", PG_TEST_PROPFIND1); |
|
461 rq->davCollection = create_test_pgdav(sn, rq); |
|
462 |
|
463 ret = webdav_propfind(pb, sn, rq); |
|
464 |
|
465 UCX_TEST_ASSERT(ret == REQ_PROCEED, "webdav_propfind (1) failed"); |
|
466 |
|
467 xmlDoc *doc = xmlReadMemory( |
|
468 st->buf->space, st->buf->size, NULL, NULL, 0); |
|
469 UCX_TEST_ASSERT(doc, "propfind1: response is not valid xml"); |
|
470 |
|
471 printf("\n\n%.*s\n", (int)st->buf->size, st->buf->space); |
|
472 |
|
473 |
|
474 testutil_destroy_session(sn); |
|
475 xmlFreeDoc(doc); |
|
476 testutil_iostream_destroy(st); |
|
477 |
|
478 UCX_TEST_END; |
|
479 } |