src/server/plugins/postgresql/pgtest.c

Sun, 17 Apr 2022 11:51:37 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 Apr 2022 11:51:37 +0200
branch
webdav
changeset 292
7c31bcd5b4be
parent 290
efc10acf539f
child 293
d3899857a81d
permissions
-rw-r--r--

update pg resource metadata on close

/*
 * 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 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;
    
}

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);
    SYS_FILE file;
    SYS_FILE file2;
    
    UCX_TEST_BEGIN;
    
    UCX_TEST_END;
}

UCX_TEST(test_pg_vfs_mkdir) {
    
}

mercurial