src/server/plugins/postgresql/pgtest.c

Tue, 19 Apr 2022 18:23:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 19 Apr 2022 18:23:54 +0200
branch
webdav
changeset 299
f927ef81a12b
parent 298
8f5c556120a5
child 306
e03737cea6e2
permissions
-rw-r--r--

add empty pg webdav funcs

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2022 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>

#include "../../util/util.h"
#include "../../test/testutils.h"
#include "../../public/nsapi.h"

#include <ucx/string.h>
#include <ucx/utils.h>

#include "pgtest.h"
#include "vfs.h"

#include <libpq-fe.h>


static char *pg_connstr = "postgresql://localhost/test1";
static int abort_pg_tests = 0;
static PGconn *test_connection;
static ResourceData resdata;

void debug_print_resources(void) {
    PGresult *result = PQexec(test_connection, "select * from Resource;");
    int n = PQntuples(result);
    printf("\nntuples: %d\n-----------------------------------------------\n", n);
    printf("%10s  %10s  %s\n", "resource_id", "parent_id", "nodename");
    for(int i=0;i<n;i++) {
        char *res_id = PQgetvalue(result, i, 0);
        char *parent_id = PQgetvalue(result, i, 1);
        char *nodename = PQgetvalue(result, i, 2);
        printf("%10s  %10s  %s\n", res_id, parent_id, nodename);
    }
    printf("\n");
}


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);
        ucx_test_register(suite, test_pg_vfs_stat);
        ucx_test_register(suite, test_pg_vfs_mkdir);
        ucx_test_register(suite, test_pg_vfs_unlink);
        ucx_test_register(suite, test_pg_vfs_rmdir);
        
        PGresult *result = PQexec(test_connection, "BEGIN");
        PQclear(result);
    }
}

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;
    
    testutil_destroy_session(sn);
}

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;
    
    testutil_destroy_session(sn);
}

UCX_TEST(test_pg_vfs_stat) {
    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;
    
    // testdata, content doesn't matter
    char test1[512];
    memset(test1, 'x', 512);
    const int test_len1 = 200;
    const int test_len2 = 432;
    
    SYS_FILE f1 = vfs_open(vfs, "/test_s1", O_WRONLY|O_CREAT);
    UCX_TEST_ASSERT(f1, "cannot open test_s1");
    system_fwrite(f1, test1, test_len1);
    vfs_close(f1);
    
    SYS_FILE f2 = vfs_open(vfs, "/test_s2", O_RDWR|O_CREAT);
    UCX_TEST_ASSERT(f2, "cannot open test_s2");
    system_fwrite(f2, test1, test_len2);
    vfs_close(f2);
    
    struct stat st1, st2;
    int r1 = vfs_stat(vfs, "/test_s1", &st1);
    int r2 = vfs_stat(vfs, "/test_s2", &st2);
    
    UCX_TEST_ASSERT(r1 == 0, "stat1 failed");
    UCX_TEST_ASSERT(r2 == 0, "stat2 failed");
    
    UCX_TEST_ASSERT(st1.st_size == test_len1, "s1 wrong length");
    UCX_TEST_ASSERT(st2.st_size == test_len2, "s2 wrong length");
    
    int testfail = vfs_stat(vfs, "/test_stat_fail", &st1);
    UCX_TEST_ASSERT(testfail != 0, "stat 3 should fail");
    
    UCX_TEST_END;
    
    testutil_destroy_session(sn);
}

UCX_TEST(test_pg_vfs_mkdir) {
    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;
    
    SYS_FILE f1 = vfs_open(vfs, "/test_mkdir/file", O_WRONLY|O_CREAT);
    UCX_TEST_ASSERT(f1 == NULL, "open should fail");
    
    int r = vfs_mkdir(vfs, "/test_mkdir");
    UCX_TEST_ASSERT(r == 0, "mkdir failed");
    
    f1 = vfs_open(vfs, "/test_mkdir/file", O_WRONLY|O_CREAT);
    UCX_TEST_ASSERT(f1, "open failed");
    
    r = vfs_mkdir(vfs, "/test_mkdir/test_sub");
    UCX_TEST_ASSERT(r == 0, "mkdir failed (2)");
    
    UCX_TEST_END;
    
    testutil_destroy_session(sn);
}

UCX_TEST(test_pg_vfs_unlink) {
    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;
    
    SYS_FILE f1 = vfs_open(vfs, "/test_unlink1", O_WRONLY|O_CREAT);
    UCX_TEST_ASSERT(f1, "cannot create test file");
    system_fwrite(f1, "test", 4);
    
    PgFile *pgfile = f1->data;
    Oid oid = pgfile->oid;
    
    vfs_close(f1);
    
    int r = vfs_unlink(vfs, "/test_unlink1");
    UCX_TEST_ASSERT(r == 0, "unlink failed");
    
    f1 = vfs_open(vfs, "/test_unlink1", O_RDONLY);
    UCX_TEST_ASSERT(f1 == NULL, "test file not deleted");
    
    PGresult *result = PQexec(test_connection, "savepoint sp;");
    PQclear(result);
    int pgfd = lo_open(test_connection, oid, INV_READ);
    UCX_TEST_ASSERT(pgfd < 0, "large object not deleted");
    result = PQexec(test_connection, "rollback to savepoint sp;");
    PQclear(result);
    
    r = vfs_unlink(vfs, "/test_unlink1");
    UCX_TEST_ASSERT(r, "unlink should fail");
    
    UCX_TEST_END;
    
    testutil_destroy_session(sn);
}

UCX_TEST(test_pg_vfs_rmdir) {
    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);
    
    PQexec(test_connection, "delete from Resource where parent_id is not null;");
    
    UCX_TEST_BEGIN;
    
    int r;
    SYS_FILE f1;
    
    // prepare some dirs/files
    r = vfs_mkdir(vfs, "/rmdir_test");
    UCX_TEST_ASSERT(r == 0, "mkdir failed (1)");
    r = vfs_mkdir(vfs, "/rmdir_test/subdir1");
    UCX_TEST_ASSERT(r == 0, "mkdir failed (2)");
    r = vfs_mkdir(vfs, "/rmdir_test/subdir2");
    UCX_TEST_ASSERT(r == 0, "mkdir failed (3)");
    
    f1 = vfs_open(vfs, "/rmdir_test/subdir2/file", O_CREAT|O_WRONLY);
    UCX_TEST_ASSERT(f1, "open failed");
    vfs_close(f1);
    
    // test rmdir
    r = vfs_rmdir(vfs, "/rmdir_test/subdir1");
    UCX_TEST_ASSERT(r == 0, "rmdir failed");;
    
    r = vfs_rmdir(vfs, "/rmdir_test/subdir2");
    UCX_TEST_ASSERT(r != 0, "rmdir should fail if the dir is not empty");
    
    r = vfs_unlink(vfs, "/rmdir_test/subdir2/file");
    UCX_TEST_ASSERT(r == 0, "unlink failed");
    
    r = vfs_rmdir(vfs, "/rmdir_test/subdir2");
    UCX_TEST_ASSERT(r == 0, "rmdir failed 2");
    
    UCX_TEST_END;
    
    testutil_destroy_session(sn);
}

mercurial