--- a/src/server/plugins/postgresql/pgtest.c Thu Apr 21 17:16:49 2022 +0200 +++ b/src/server/plugins/postgresql/pgtest.c Sun Apr 24 18:35:44 2022 +0200 @@ -27,19 +27,27 @@ */ #include <stdio.h> +#include <inttypes.h> #include "../../util/util.h" #include "../../test/testutils.h" +#include "../../test/webdav.h" #include "../../public/nsapi.h" +#include "../../public/webdav.h" +#include "../../webdav/webdav.h" #include <ucx/string.h> #include <ucx/utils.h> +#include <ucx/buffer.h> #include "pgtest.h" #include "vfs.h" +#include "webdav.h" #include <libpq-fe.h> +#include <libxml/tree.h> + static char *pg_connstr = "postgresql://localhost/test1"; static int abort_pg_tests = 0; @@ -83,6 +91,10 @@ ucx_test_register(suite, test_pg_vfs_unlink); ucx_test_register(suite, test_pg_vfs_rmdir); + ucx_test_register(suite, test_pg_webdav_create_from_resdata); + ucx_test_register(suite, test_pg_prepare_tests); + ucx_test_register(suite, test_pg_webdav_propfind); + PGresult *result = PQexec(test_connection, "BEGIN"); PQclear(result); } @@ -331,3 +343,137 @@ testutil_destroy_session(sn); } + +/* ----------------------------- WebDAV tests ----------------------------- */ + + +static WebdavBackend* create_test_pgdav(Session *sn, Request *rq) { + return pg_webdav_create_from_resdata(sn, rq, &resdata); +} + +UCX_TEST(test_pg_webdav_create_from_resdata) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PROPFIND", "/"); + + UCX_TEST_BEGIN; + + WebdavBackend *dav = create_test_pgdav(sn, rq); + UCX_TEST_ASSERT(dav, "cannot create pg dav backend"); + + UCX_TEST_END; +} + +UCX_TEST(test_pg_prepare_tests) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PUT", "/"); + rq->vfs = create_test_pgvfs(sn, rq); + VFSContext *vfs = vfs_request_context(sn, rq); + + UCX_TEST_BEGIN; + + vfs_mkdir(vfs, "/propfind"); + SYS_FILE f1; + + int64_t res1_id, res2_id; + + f1 = vfs_open(vfs, "/propfind/res1", O_WRONLY|O_CREAT); + UCX_TEST_ASSERT(f1, "res1 create failed"); + res1_id = ((PgFile*)f1->data)->resource_id; + vfs_close(f1); + + f1 = vfs_open(vfs, "/propfind/res2", O_WRONLY|O_CREAT); + UCX_TEST_ASSERT(f1, "res2 create failed"); + res2_id = ((PgFile*)f1->data)->resource_id; + vfs_close(f1); + + f1 = vfs_open(vfs, "/propfind/res3", O_WRONLY|O_CREAT); + UCX_TEST_ASSERT(f1, "res3 create failed"); + vfs_close(f1); + + // 2 properties for res1 + char idstr[32]; + snprintf(idstr, 32, "%" PRId64, res1_id); + const char* params[1] = { idstr }; + PGresult *result = PQexecParams( + test_connection, + "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'test', 'testvalue');", + 1, // number of parameters + NULL, + params, // parameter value + NULL, + NULL, + 0); // 0: result in text format + + UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); + PQclear(result); + + result = PQexecParams( + test_connection, + "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'prop2', 'value2');", + 1, // number of parameters + NULL, + params, // parameter value + NULL, + NULL, + 0); // 0: result in text format + + UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); + PQclear(result); + + // 1 property for res2 + snprintf(idstr, 32, "%" PRId64, res2_id); + result = PQexecParams( + test_connection, + "insert into Property(resource_id, xmlns, pname, pvalue) values ($1, 'http://example.com/', 'test', 'res2test');", + 1, // number of parameters + NULL, + params, // parameter value + NULL, + NULL, + 0); // 0: result in text format + + UCX_TEST_ASSERT(PQresultStatus(result) == PGRES_COMMAND_OK, "cannot create property 1"); + PQclear(result); + + UCX_TEST_END; + + testutil_destroy_session(sn); +} + +UCX_TEST(test_pg_webdav_propfind) { + Session *sn; + Request *rq; + TestIOStream *st; + pblock *pb; + + UCX_TEST_BEGIN; + + // test data: + // + // /propfind/ + // /propfind/res1 (2 properties) + // /propfind/res2 (1 property) + // /propfind/res3 + + int ret; + // Test 1 + init_test_webdav_method(&sn, &rq, &st, &pb, "PROPFIND", "/propfind", PG_TEST_PROPFIND1); + rq->davCollection = create_test_pgdav(sn, rq); + + ret = webdav_propfind(pb, sn, rq); + + UCX_TEST_ASSERT(ret == REQ_PROCEED, "webdav_propfind (1) failed"); + + xmlDoc *doc = xmlReadMemory( + st->buf->space, st->buf->size, NULL, NULL, 0); + UCX_TEST_ASSERT(doc, "propfind1: response is not valid xml"); + + printf("\n\n%.*s\n", (int)st->buf->size, st->buf->space); + + + testutil_destroy_session(sn); + xmlFreeDoc(doc); + testutil_iostream_destroy(st); + + UCX_TEST_END; +}