# HG changeset patch # User Olaf Wintermann # Date 1650135929 -7200 # Node ID 285d483db2fbb657e867cfd871126177d76c14e3 # Parent 7dd45173f68a832bbfaa5b3e771b693031d2fd98 add first pg vfs tests diff -r 7dd45173f68a -r 285d483db2fb src/server/daemon/vfs.c --- a/src/server/daemon/vfs.c Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/daemon/vfs.c Sat Apr 16 21:05:29 2022 +0200 @@ -78,7 +78,7 @@ sys_dir_close }; -int vfs_init() { +int vfs_init(void) { vfs_type_map = ucx_map_new(16); if(!vfs_type_map) { return -1; diff -r 7dd45173f68a -r 285d483db2fb src/server/daemon/vfs.h --- a/src/server/daemon/vfs.h Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/daemon/vfs.h Sat Apr 16 21:05:29 2022 +0200 @@ -47,7 +47,7 @@ }; typedef enum VFSAioOp VFSAioOp; -int vfs_init(); +int vfs_init(void); typedef int(*vfs_op_f)(VFSContext *, const char *); typedef int(*sys_op_f)(VFSContext *, const char *, SysACL *); diff -r 7dd45173f68a -r 285d483db2fb src/server/plugins/postgresql/pgtest.c --- a/src/server/plugins/postgresql/pgtest.c Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/plugins/postgresql/pgtest.c Sat Apr 16 21:05:29 2022 +0200 @@ -28,8 +28,136 @@ #include -#include +#include "../../util/util.h" +#include "../../test/testutils.h" +#include "../../public/nsapi.h" + +#include +#include + +#include "pgtest.h" +#include "vfs.h" + +#include + + +static char *pg_connstr = "postgresql://localhost/test1"; +static int abort_pg_tests = 0; +static PGconn *test_connection; +static ResourceData resdata; void register_pg_tests(int argc, char **argv, UcxTestSuite *suite) { + test_connection = PQconnectdb(pg_connstr); + if(!test_connection) { + abort_pg_tests = 1; + } + + if(PQstatus(test_connection) != CONNECTION_OK) { + abort_pg_tests = 1; + } + + ucx_test_register(suite, test_pg_conn); + if(!abort_pg_tests) { + resdata.data = test_connection; + + ucx_test_register(suite, test_pg_vfs_open); + ucx_test_register(suite, test_pg_vfs_io); + + PGresult *result = PQexec(test_connection, "BEGIN"); + } } + +UCX_TEST(test_pg_conn) { + char *msg = test_connection ? PQerrorMessage(test_connection) : "no connection"; + + UCX_TEST_BEGIN; + + if(abort_pg_tests) { + int msglen = strlen(msg); + if(msglen > 0 && msg[msglen-1] == '\n') { + msglen--; + } + fprintf(stdout, "%.*s: ", msglen, msg); + UCX_TEST_ASSERT(1 == 0, "skip pg tests"); + } else { + UCX_TEST_ASSERT(1 == 1, "ok"); + } + + UCX_TEST_END; +} + + +static VFS* create_test_pgvfs(Session *sn, Request *rq) { + return pg_vfs_create_from_resourcedata(sn, rq, &resdata); +} + + +UCX_TEST(test_pg_vfs_open) { + 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); + SYS_FILE file; + + UCX_TEST_BEGIN; + + file = vfs_open(vfs, "/test_notfound1", O_RDONLY); + UCX_TEST_ASSERT(!file, "/test_notfound should not exist"); + + file = vfs_open(vfs, "/test_file1", O_RDWR | O_CREAT); + UCX_TEST_ASSERT(file, "cannot create file 1"); + + vfs_close(file); + + UCX_TEST_END; +} + +UCX_TEST(test_pg_vfs_io) { + 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); + SYS_FILE file; + SYS_FILE file2; + + UCX_TEST_BEGIN; + + file = vfs_open(vfs, "/test_f1", O_WRONLY | O_CREAT); + UCX_TEST_ASSERT(file, "cannot open file1"); + + int w = system_fwrite(file, "test1\n", 6); + UCX_TEST_ASSERT(w == 6, "fwrite ret (1)"); + w = system_fwrite(file, "2", 1); + UCX_TEST_ASSERT(w == 1, "fwrite ret (2)"); + + vfs_close(file); + + file = vfs_open(vfs, "/test_f1", O_RDONLY); + file2 = vfs_open(vfs, "/test_f2", O_WRONLY | O_CREAT); + UCX_TEST_ASSERT(file, "cannot open file1"); + UCX_TEST_ASSERT(file2, "cannot open file2"); + + char buf[128]; + int r = system_fread(file, buf, 128); + UCX_TEST_ASSERT(r == 7, "cannot read from file1"); + + w = system_fwrite(file2, buf, r); + UCX_TEST_ASSERT(w == 7, "cannot write to file2"); + + vfs_close(file); + vfs_close(file2); + + file2 = vfs_open(vfs, "/test_f2", O_RDONLY); + + r = system_fread(file, buf, 128); + UCX_TEST_ASSERT(r == 7, "fread ret"); + UCX_TEST_ASSERT(!memcmp(buf, "test1\n2", 7), "wrong buffer content after read"); + + vfs_close(file2); + + + UCX_TEST_END; + +} + diff -r 7dd45173f68a -r 285d483db2fb src/server/plugins/postgresql/pgtest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/postgresql/pgtest.h Sat Apr 16 21:05:29 2022 +0200 @@ -0,0 +1,34 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: pgtest.h + * Author: olaf + * + * Created on 16. April 2022, 14:37 + */ + +#ifndef PGTEST_H +#define PGTEST_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +UCX_TEST(test_pg_conn); + +UCX_TEST(test_pg_vfs_open); +UCX_TEST(test_pg_vfs_io); + + +#ifdef __cplusplus +} +#endif + +#endif /* PGTEST_H */ + diff -r 7dd45173f68a -r 285d483db2fb src/server/plugins/postgresql/vfs.c --- a/src/server/plugins/postgresql/vfs.c Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.c Sat Apr 16 21:05:29 2022 +0200 @@ -136,6 +136,10 @@ } // resdata will be freed automatically when the request is finished + return pg_vfs_create_from_resourcedata(sn, rq, resdata); +} + +VFS* pg_vfs_create_from_resourcedata(Session *sn, Request *rq, ResourceData *resdata) { // Create a new VFS object and a separate instance object // VFS contains fptrs that can be copied from pg_vfs_class // instance contains request specific data (PGconn) @@ -412,7 +416,12 @@ fd = -2; } - int lo_mode = INV_READ; // TODO: evaluate oflags + int lo_mode = INV_READ; + if((oflags & O_RDWR) == O_RDWR) { + lo_mode = INV_READ|INV_WRITE; + } else if((oflags & O_WRONLY) == O_WRONLY) { + lo_mode = INV_WRITE; + } fd = lo_open(pg->connection, oid, lo_mode); } diff -r 7dd45173f68a -r 285d483db2fb src/server/plugins/postgresql/vfs.h --- a/src/server/plugins/postgresql/vfs.h Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.h Sat Apr 16 21:05:29 2022 +0200 @@ -63,6 +63,8 @@ VFS* pg_vfs_create(Session *sn, Request *rq, pblock *pb); +VFS* pg_vfs_create_from_resourcedata(Session *sn, Request *rq, ResourceData *resdata); + /* * Resolve a path into a parent_id and resource name diff -r 7dd45173f68a -r 285d483db2fb src/server/test/main.c --- a/src/server/test/main.c Sat Apr 16 14:36:08 2022 +0200 +++ b/src/server/test/main.c Sat Apr 16 21:05:29 2022 +0200 @@ -38,6 +38,7 @@ #include "../public/nsapi.h" #include "../util/plist.h" #include "../util/date.h" +#include "../daemon/vfs.h" #include @@ -60,6 +61,7 @@ int main(int argc, char **argv) { pool_init(NULL, NULL, NULL); + vfs_init(); //test();