Example #1
0
static int
test1()
{
DCB     *dcb;
int     result;
	int eno = 0;

        /* Poll tests */  
        ss_dfprintf(stderr,
                    "testpoll : Initialise the polling system."); 
        init_test_env(NULL);
        poll_init();
        ss_dfprintf(stderr, "\t..done\nAdd a DCB");
        dcb = dcb_alloc(DCB_ROLE_REQUEST_HANDLER);

		if(dcb == NULL){
			ss_dfprintf(stderr, "\nError on function call: dcb_alloc() returned NULL.\n");
			return 1;
		}

        dcb->fd = socket(AF_UNIX, SOCK_STREAM, 0);

        if(dcb->fd < 0){
                        char errbuf[STRERROR_BUFLEN];
			ss_dfprintf(stderr, "\nError on function call: socket() returned %d: %s\n",errno,strerror_r(errno,errbuf,sizeof(errbuf)));
				    return 1;
		}


        if((eno = poll_add_dcb(dcb)) != 0){
			ss_dfprintf(stderr, "\nError on function call: poll_add_dcb() returned %d.\n",eno);
				    return 1;
		}

        if((eno = poll_remove_dcb(dcb)) != 0){
			ss_dfprintf(stderr, "\nError on function call: poll_remove_dcb() returned %d.\n",eno);
				    return 1;
		}

        if((eno = poll_add_dcb(dcb)) != 0){
			ss_dfprintf(stderr, "\nError on function call: poll_add_dcb() returned %d.\n",eno);
				    return 1;
		}

        ss_dfprintf(stderr, "\t..done\nStart wait for events.");
        sleep(10);
        poll_shutdown();
        ss_dfprintf(stderr, "\t..done\nTidy up.");
        dcb_close(dcb);
        ss_dfprintf(stderr, "\t..done\n");
		
	return 0;
        
}
Example #2
0
int
test2()
{
GWBUF   *buffer;
char len = 128;
char query[129];

        buffer = gwbuf_alloc(132);
	ss_info_dassert((buffer != NULL),"Buffer should not be null");

	memset(query,';',128);
    memset(query+128,'\0',1);
	*((unsigned char*)buffer->start) = len;
	*((unsigned char*)buffer->start+1) = 0;
	*((unsigned char*)buffer->start+2) = 0;
	*((unsigned char*)buffer->start+3) = 1;
	*((unsigned char*)buffer->start+4) = 0x03;
	memcpy(buffer->start + 5,query,strlen(query));
	char* result = modutil_get_SQL(buffer);
	ss_dassert(strcmp(result,query) == 0);
	gwbuf_free(buffer);
	free(result);
        ss_dfprintf(stderr, "\t..done\n");
	return 0;

}
Example #3
0
static int
test1()
{
GWBUF   *buffer;
char    *(sql[100]);
int     result, length, residual;

        /* Poll tests */  
        ss_dfprintf(stderr,
                    "testmodutil : Rudimentary tests."); 
        buffer = gwbuf_alloc(100);
        ss_info_dassert(0 == modutil_is_SQL(buffer), "Default buffer should be diagnosed as not SQL");
        /* There would ideally be some straightforward way to create a SQL buffer? */
        ss_dfprintf(stderr, "\t..done\nExtract SQL from buffer");
        ss_info_dassert(0 == modutil_extract_SQL(buffer, sql, &length), "Default buffer should fail");
        ss_dfprintf(stderr, "\t..done\nExtract SQL from buffer different way?");
        ss_info_dassert(0 == modutil_MySQL_Query(buffer, sql, &length, &residual), "Default buffer should fail");
        ss_dfprintf(stderr, "\t..done\nReplace SQL in buffer");
        ss_info_dassert(0 == modutil_replace_SQL(buffer, "select * from some_table;"), "Default buffer should fail");
        ss_dfprintf(stderr, "\t..done\nTidy up.");
        gwbuf_free(buffer);
        ss_dfprintf(stderr, "\t..done\n");
		
	return 0;
        
}
Example #4
0
/**
 * test1	Allocate a server and do lots of other things
 *
  */
static int
test1()
{
SERVER   *server;
int     result;
char    *status;

        /* Server tests */
        ss_dfprintf(stderr,
                    "testserver : creating server called MyServer"); 
        server = server_alloc("MyServer", "HTTPD", 9876);
        skygw_log_sync_all();

        //ss_info_dassert(NULL != service, "New server with valid protocol and port must not be null");
        //ss_info_dassert(0 != service_isvalid(service), "Service must be valid after creation");

        ss_dfprintf(stderr, "\t..done\nTest Parameter for Server.");
        ss_info_dassert(NULL == serverGetParameter(server, "name"), "Parameter should be null when not set");
        serverAddParameter(server, "name", "value");
        skygw_log_sync_all();
        ss_info_dassert(0 == strcmp("value", serverGetParameter(server, "name")), "Parameter should be returned correctly");
        ss_dfprintf(stderr, "\t..done\nTesting Unique Name for Server.");
        ss_info_dassert(NULL == server_find_by_unique_name("uniquename"), "Should not find non-existent unique name.");
        server_set_unique_name(server, "uniquename");
        skygw_log_sync_all();
        ss_info_dassert(server == server_find_by_unique_name("uniquename"), "Should find by unique name.");
        ss_dfprintf(stderr, "\t..done\nTesting Status Setting for Server.");
        status = server_status(server);
        skygw_log_sync_all();
        ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running by default.");
        if (NULL != status) free(status);
        server_set_status(server, SERVER_MASTER);
        status = server_status(server);
        skygw_log_sync_all();
        ss_info_dassert(0 == strcmp("Master, Running", status), "Should find correct status.");
        server_clear_status(server, SERVER_MASTER);		
		free(status);
        status = server_status(server);
        skygw_log_sync_all();
        ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running after master status cleared.");
        if (NULL != status) free(status);
        ss_dfprintf(stderr, "\t..done\nRun Prints for Server and all Servers.");
        printServer(server);
        printAllServers();
        skygw_log_sync_all();
        ss_dfprintf(stderr, "\t..done\nFreeing Server.");
        ss_info_dassert(0 != server_free(server), "Free should succeed");
        ss_dfprintf(stderr, "\t..done\n");
	return 0;
        
}
Example #5
0
/**
 * test1	Allocate a dcb and do lots of other things
 *
  */
static int
test1()
{
DCB   *dcb, *extra, *clone;
int     size = 100;
int     bite1 = 35;
int     bite2 = 60;
int     bite3 = 10;
int     buflen;

        /* Single buffer tests */
        ss_dfprintf(stderr,
                    "testdcb : creating buffer with type DCB_ROLE_SERVICE_LISTENER"); 
        dcb = dcb_alloc(DCB_ROLE_SERVICE_LISTENER);
        printDCB(dcb);
        ss_info_dassert(dcb_isvalid(dcb), "New DCB must be valid");
        ss_dfprintf(stderr, "\t..done\nAllocated dcb.");
        clone = dcb_clone(dcb);
        ss_dfprintf(stderr, "\t..done\nCloned dcb");
        printAllDCBs();
        ss_info_dassert(true, "Something is true");
        ss_dfprintf(stderr, "\t..done\n");
        dcb_close(dcb);
        ss_dfprintf(stderr, "Freed original dcb");
        ss_info_dassert(!dcb_isvalid(dcb), "Freed DCB must not be valid");
        ss_dfprintf(stderr, "\t..done\nMake clone DCB a zombie");
        clone->state = DCB_STATE_NOPOLLING;
        dcb_close(clone);
        ss_info_dassert(dcb_get_zombies() == clone, "Clone DCB must be start of zombie list now");
        ss_dfprintf(stderr, "\t..done\nProcess the zombies list");
        dcb_process_zombies(0);
        ss_dfprintf(stderr, "\t..done\nCheck clone no longer valid");
        ss_info_dassert(!dcb_isvalid(clone), "After zombie processing, clone DCB must not be valid");
        ss_dfprintf(stderr, "\t..done\n");
		
	return 0;
}
Example #6
0
/**
 * test1	spinlock_acquire_nowait tests
 *
 * Test that spinlock_acquire_nowait returns false if the spinlock
 * is already taken.
 *
 * Test that spinlock_acquire_nowait returns true if the spinlock
 * is not taken.
 *
 * Test that spinlock_acquire_nowait does hold the spinlock.
 */
static bool do_hashtest(
        int argelems,
        int argsize)
{
        bool       succp = true;
        HASHTABLE* h;
        int        nelems;
        int        i;
        int*       val_arr;
        int        hsize;
        int        longest;
        int*       iter;
        
        ss_dfprintf(stderr,
                    "testhash : creating hash table of size %d, including %d "
                    "elements in total, at time %g.",
                    argsize,
                    argelems,
                    (double)clock()-start); 
        
        val_arr = (int *)malloc(sizeof(void *)*argelems);
        
        h = hashtable_alloc(argsize, hfun, cmpfun);

        ss_dfprintf(stderr, "\t..done\nAdd %d elements to hash table.", argelems);
        
        for (i=0; i<argelems; i++) {
            val_arr[i] = i;
            hashtable_add(h, (void *)&val_arr[i], (void *)&val_arr[i]);
        }
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);
        
        ss_dfprintf(stderr, "\t..done\nRead hash table statistics.");
        
        hashtable_get_stats((void *)h, &hsize, &nelems, &longest);

        ss_dfprintf(stderr, "\t..done\nValidate read values.");
        
        ss_info_dassert(hsize == (argsize > 0 ? argsize: 1), "Invalid hash size");
        ss_info_dassert((nelems == argelems) || (nelems == 0 && argsize == 0),
                        "Invalid element count");
        ss_info_dassert(longest <= nelems, "Too large longest list value");
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);

        ss_dfprintf(stderr, "\t..done\nValidate iterator.");
        
        HASHITERATOR *iterator = hashtable_iterator(h);
        read_lock(h);
        for (i=0; i < (argelems+1); i++) {
            iter = (int *)hashtable_next(iterator);
            if (iter == NULL) break;
            if (argelems < 100) ss_dfprintf(stderr, "\nNext item, iter = %d, i = %d", *iter, i);
        }
        read_unlock(h);
        ss_info_dassert((i == argelems) || (i == 0 && argsize == 0), "\nIncorrect number of elements from iterator");
        hashtable_iterator_free(iterator);
        if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start);

        ss_dfprintf(stderr, "\t\t..done\n\nTest completed successfully.\n\n");
        
        CHK_HASHTABLE(h);
        hashtable_free(h);
        

		free(val_arr);
        return succp;
}
Example #7
0
int main(int argc, char** argv)
{
        slist_cursor_t*    c;
        const char*        q;
        query_test_t*      qtest;
        bool               succp;
        bool               failp = true;
        unsigned int       f = 0;
        int                nsucc = 0;
        int                nfail = 0;
        int                rc = 0;
        MYSQL*             mysql;
        char*              workingdir;
        char               ddoption[1024];

        ss_dfprintf(stderr, ">> testmain\n");
        c = slist_init();

        /** Test some functions */
        q = "SELECT MY_UDF('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT repeat('a', 1024)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT soundex('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "SELECT ssoundexx('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT now()";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT rand()";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "SELECT rand(234), MY_UDF('Hello'), soundex('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        
        /** Read-only SELECTs */
        q = "SELECT user from mysql.user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "select tt1.id, tt2.id from t1 tt1, t2 tt2 where tt1.name is "
                "not null and tt2.name is not null";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, false));

        /** SELECT ..INTO clauses > session updates */
        q = "SELECT user from mysql.user INTO DUMPFILE '/tmp/dump1'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user INTO DUMPFILE '/tmp/dump2 ' from mysql.user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user from mysql.user INTO OUTFILE '/tmp/out1'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        /** Database and table name must be separated by a dot */
        q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql-user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, false));

        /** Database and table name must be separated by a dot */
        q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql_foo_user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
        
        q = "SELECT user FROM mysql.user limit 1 INTO @local_variable";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user INTO @local_variable FROM mysql.user limit 1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
        
        q = "SELECT non_existent_attr INTO @d FROM non_existent_table";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "select * from table1 "
                "where table1.field IN "
                "(select * from table1a union select * from table1b) union "
                "select * from table2 where table2.field = "
                "(select (select f1 from table2a where table2a.f2 = table2b.f3) "
                "from table2b where table2b.f1 = table2.f2) union "
                "select * from table3";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));
                
        /** RENAME TABLEs */
        q = "RENAME TABLE T1 to T2";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        
        /** INSERTs */
        q = "INSERT INTO T1 (SELECT * FROM T2)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "INSERT INTO T1 VALUES(2, 'foo', 'toomanyattributes')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "INSERT INTO T2 VALUES(1, 'sthrgey')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "INSERT INTO T2 VALUES(8, 'ergstrhe')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "INSERT INTO T2 VALUES(9, NULL)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));


        /** Ok, delimeter is client-side parameter which shouldn't be handled
         * on server side.
         */
        q = "delimiter //";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, true));

        /** SETs, USEs > Session updates */
        q = "SET @a=1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
        
        q = "USE TEST";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        
        /** Object creation statements */
        q = "create procedure si (out param1 int) \nbegin select count(*) "
                "into param1 from t1; \nend";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));
        
        q = "CREATE TABLE T1 (id integer primary key, name varchar(10))";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "DROP TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "ALTER TABLE T1 ADD COLUMN WHYME INTEGER NOT NULL";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "TRUNCATE TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "DROP SERVER IF EXISTS VICTIMSRV";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));

        q = "CREATE USER FOO IDENTIFIED BY 'BAR'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "OPTIMIZE NO_WRITE_TO_BINLOG TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "SELECT NOW();CREATE TABLE T1 (ID INTEGER);"
                "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));
        
        
        /** Setting database makes this SESSION_WRITE */
        q = "USE TEST;CREATE TABLE T1 (ID INTEGER);"
                "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
        
        /**
         * Init libmysqld.
         */
        workingdir = getenv("PWD");
        
        if (workingdir == NULL) {
                fprintf(stderr,
                        "Failed to resolve the working directory, $PWD is not "
                        "set.\n");
                ss_dassert(workingdir != NULL);
        } else if (access(workingdir, R_OK) != 0) {
                char errbuf[STRERROR_BUFLEN];
                fprintf(stderr,
                        "Failed to access the working directory due %d, %s\n",
                        errno,
                        strerror_r(errno, errbuf, sizeof(errbuf)));
                ss_dassert(false);
        } else {
                char** so = server_options;
                snprintf(datadir, 1023, "%s/data", workingdir);
                mkdir(datadir, 0777);
                snprintf(ddoption, 1023, "--datadir=%s", datadir);

                while (strncmp(*so++, "--datadir=", 10) != 0) ;

                if (*so == NULL) {
                        fprintf(stderr, "Failed to find datadir option.\n");
                        ss_dassert(*so != NULL);
                }
                *so = ddoption;
                
                snprintf(mysqldir, 1023, "%s/mysql", workingdir);
                setenv("MYSQL_HOME", mysqldir, 1);
        }
        failp = mysql_library_init(num_elements, server_options, server_groups);

        if (failp) {
                MYSQL* mysql = mysql_init(NULL);
                ss_dassert(mysql != NULL);
                fprintf(stderr,
                        "mysql_init failed, %d : %s\n",
                        mysql_errno(mysql),
                        mysql_error(mysql));
                ss_dassert(!failp);
        }

        fprintf(stderr,
                "\nExecuting selected cases in "
                "skygw_query_classifier_get_type :\n\n");
        /**
         * Set cursor to the beginning, scan through the list and execute
         * test cases.
         */
        succp = slcursor_move_to_begin(c);
        
        while(succp) {
                qtest = slcursor_get_case(c);
                qtest->qt_result_type =
                        skygw_query_classifier_get_type(qtest->qt_query_str, f,
						&mysql);
                succp = slcursor_step_ahead(c);
        }
        /**
         * Scan through test results and compare them against expected
         * results.
         */
        succp = slcursor_move_to_begin(c);
        fprintf(stderr, "\nScanning through the results :\n\n");
        
        while(succp) {
                qtest = slcursor_get_case(c);
            
                if (!query_test_types_match(qtest)) {
                        nfail += 1;
                        ss_dfprintf(stderr,
                                    "* Failed: \"%s\" -> %s (Expected %s)\n",
                                    query_test_get_querystr(qtest),
                                    STRQTYPE(query_test_get_result_type(qtest)),
                                    STRQTYPE(query_test_get_query_type(qtest)));
                } else {
                        nsucc += 1;
                        ss_dfprintf(stderr,
                                    "Succeed\t: \"%s\" -> %s\n",
                                    query_test_get_querystr(qtest),
                                    STRQTYPE(query_test_get_query_type(qtest)));
                }
                succp = slcursor_step_ahead(c);
        }
        fprintf(stderr,
                "------------------------------------------\n"
                "Tests in total %d, SUCCEED %d, FAILED %d\n",
                nsucc+nfail,
                nsucc,
                nfail);

        /**
         * Scan test results and re-execute those which are marked to be
         * executed also in the server. This serves mostly debugging purposes.
         */
        succp = slcursor_move_to_begin(c);
        mysql = mysql_init(NULL);

        if (mysql == NULL) {
                fprintf(stderr, "mysql_init failed\n");
                ss_dassert(mysql != NULL);
        }

        mysql_options(mysql,
                      MYSQL_READ_DEFAULT_GROUP,
                      "libmysqld_client");
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

        mysql = mysql_real_connect(mysql,
                                   NULL,
                                   "skygw",
                                   "skygw",
                                   NULL,
                                   0,
                                   NULL,
                                   CLIENT_MULTI_STATEMENTS);
        
        if (mysql == NULL) {
                fprintf(stderr, "mysql_real_connect failed\n");
                ss_dassert(mysql != NULL);
        }

        fprintf(stderr,
                "\nRe-execution of selected cases in Embedded server :\n\n");
        
        while(succp) {
                qtest = slcursor_get_case(c);

                if (query_test_exec_also_in_server(qtest)) {
                        MYSQL_RES*  results;
                        MYSQL_ROW   record;
                        const char* query_str;
                
                        query_str = query_test_get_querystr(qtest);
                        failp = mysql_query(mysql, query_str);

                        if (failp) {
                                ss_dfprintf(stderr,
                                            "* Failed: \"%s\" -> %d : %s\n",
                                            query_str,
                                            mysql_errno(mysql),
                                            mysql_error(mysql));
                        } else {
                                ss_dfprintf(stderr,
                                            "Succeed\t: \"%s\"\n",
                                            query_str);
                                results = mysql_store_result(mysql);
                    
                                if (results != NULL) {
                        
                                        while((record = mysql_fetch_row(results))) {
                                                while(record != NULL && *record != NULL) {
                                                        ss_dfprintf(stderr, "%s ", *record);
                                                        record++;
                                                }
                                                ss_dfprintf(stderr, "\n");
                                        }
                                        mysql_free_result(results);
                                }
                        }
                }
                succp = slcursor_step_ahead(c);
            
        }
        slist_done(c);
        fprintf(stderr, "------------------------------------------\n");
        
return_with_handle:
        mysql_close(mysql);
        mysql_thread_end();
        mysql_library_end();
        
return_without_server:
        ss_dfprintf(stderr, "\n<< testmain\n");
        fflush(stderr);
        return rc;
}