Пример #1
0
END_TEST

START_TEST (test_setget_setmultipletimesandget) // Added 10 
{
	struct storage_record record;
	int fields = 0;
	int intval = 0;

	// Do a set
	strncpy(record.value, "col 5000", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5001", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5002", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5003", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5004", sizeof record.value);
	int status = storage_set(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	// Do a get
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col %d", &intval);
	fail_unless(fields == 1 && intval == 5004, "Got wrong value.");
}
Пример #2
0
END_TEST


START_TEST (DeletionTest) // For testing deleting records
{
	struct storage_record record;
	int fields = 0;
	int intval = 0;

	// Do several sets
	strncpy(record.value, "col 5000", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5001", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5002", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col NULL", sizeof record.value); //We deleted it here
	storage_set(INTTABLE, KEY, &record, test_conn);
	
	
	// Do several gets
	strncpy(record.value, "", sizeof record.value);
	storage_get(INTTABLE, KEY, &record, test_conn);
	storage_get(INTTABLE, KEY, &record, test_conn);
	storage_get(INTTABLE, KEY, &record, test_conn);

	strncpy(record.value, "col 5004", sizeof record.value);
	int status = storage_set(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	status = storage_get(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col %d", &intval);
	fail_unless(fields == 1 && intval == 5004, "Got wrong value.");
}
Пример #3
0
END_TEST

START_TEST (GetSetGetSetManyTimes) // Added 12
{
	struct storage_record record;
	int fields = 0;
	int intval = 0;

	// Do several sets
	strncpy(record.value, "col 5000", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5001", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5002", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	strncpy(record.value, "col 5003", sizeof record.value);
	storage_set(INTTABLE, KEY, &record, test_conn);
	
	
	// Do several gets
	strncpy(record.value, "", sizeof record.value);
	storage_get(INTTABLE, KEY, &record, test_conn);
	storage_get(INTTABLE, KEY, &record, test_conn);
	storage_get(INTTABLE, KEY, &record, test_conn);

	strncpy(record.value, "col 5004", sizeof record.value);
	int status = storage_set(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	status = storage_get(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col %d", &intval);
	fail_unless(fields == 1 && intval == 5004, "Got wrong value.");
}
Пример #4
0
END_TEST




/*
 * One server instance tests:
 * 	set/get from simple tables.
 */

START_TEST (test_setget_posint)
{
	struct storage_record record;
	int fields = 0;
	int intval = 0;

	// Do a set
	strncpy(record.value, "col 2", sizeof record.value);
	int status = storage_set(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	// Do a get
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col %d", &intval);
	fail_unless(fields == 1 && intval == 2, "Got wrong value.");
}
Пример #5
0
END_TEST


/*
 * One server instance get/set pass tests:
 * 	set/get basic table/key/value names from one table.
 * 	set/get extended table/key/value names from one table.
 * 	set/get extended table/key/value names from two tables.
 */

START_TEST (test_oneserver_onetable)
{
	struct storage_record r;

	// Start and connect to the server.
	void *conn = start_connect(ONETABLE_CONF, "test_oneserver_onetable.serverout", NULL);
	fail_unless(conn != NULL, "Couldn't start or connect to server.");

	// Set a key/value.
	strncpy(r.value, VALUE, sizeof r.value);
	int status = storage_set(TABLE, KEY, &r, conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	// Get a value.
	strncpy(r.value, "", sizeof r.value);
	status = storage_get(TABLE, KEY, &r, conn);
	fail_unless(status == 0, "Error getting a value.");
	fail_unless(strcmp(r.value, VALUE) == 0, "Got wrong value.");

	// Disconnect from the server.
	status = storage_disconnect(conn);
	//fail_unless(status == 0, "Error disconnecting from the server.");
}
Пример #6
0
END_TEST


START_TEST (test_setcomplex_updatethreecols) {
	struct storage_record record;
	int fields = 0;
	int intval = 0;
	float floatval = 0;
	char strval[MAX_VALUE_LEN];
	strncpy(strval, "", sizeof strval);

	// Update the value
	strncpy(record.value, "col1 -8,col2 -8,col3 ABC", sizeof record.value);
	int status = storage_set(THREECOLSTABLE, KEY1, &record, test_conn);
	fail_unless(status == 0, "Error updating a value.");

	// Get the new value.
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(THREECOLSTABLE, KEY1, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col1 %d , col2 %f , col3 %[a-zA-Z0-9 ]", &intval, &floatval, strval);
	fail_unless(fields == 3, "Got wrong number of fields.");
	fail_unless(intval == -8, "Got wrong value.");
	fail_unless(floatval == -8, "Got wrong value.");
	fail_unless(strcmp(trimtrailingspc(strval), "ABC") == 0, "Got wrong value.");
}
Пример #7
0
END_TEST

START_TEST (correct_table_input_for_third_table)
{
	struct storage_record record;
	strncpy(record.value, TABLE3_COLVAL, sizeof record.value);
	int status = storage_set(TABLE3, KEY, &record, test_conn);
	fail_unless(status == 0, "storage_set with correct input type should work");
}
Пример #8
0
/**
 * @brief Attempts to set a value given table and key names and the new value in the server.
 *
 * Reports the error number set by the storage server.
 * Does not exit if an error occurs.
 * @param r A pointer to a record structure.
 * @return Returns 0 on success, -1 otherwise.
 */
int client_set(struct storage_record *r)
{
    
    char table[MAX_TABLE_LEN] = {0}, key[MAX_KEY_LEN] = {0}, value[MAX_VALUE_LEN] = {0};
    
    bool read_success = false;
    while(read_success == false)
    {
        printf("Please enter the table name: ");
        char *l = fgets(input_buffer, sizeof input_buffer, stdin);
        if(l != input_buffer || (sscanf(input_buffer, "%[a-zA-Z0-9] %s", table, trash) != 1))
            printf("Please enter a valid table name (only alphanumeric characters).\n");
        else
            read_success = true;
    }
    
    read_success = false;
    while(read_success == false)
    {
        printf("Please enter the key: ");
        char *l = fgets(input_buffer, sizeof input_buffer, stdin);
        if(l != input_buffer || (sscanf(input_buffer, "%[a-zA-Z0-9] %s", key, trash) != 1))
            printf("Please enter a valid key (only alphanumeric characters).\n");
        else
            read_success = true;
    }
    
    read_success = false;
    while(read_success == false)
    {
        printf("Please enter the value: ");
        char *l = fgets(input_buffer, sizeof input_buffer, stdin);
        if(l != input_buffer || (sscanf(input_buffer, "%[^\n]", value) == 0))
            printf("Please enter a valid value (comma separated column-value pairs).\n");
        else
            read_success = true;
    }
    
    if(strcmp(value, "NULL") != 0) // Trying to modify a record
        strncpy(r->value, value, sizeof r->value);
    else // Trying to delete a record
        r = NULL;
    
    gettimeofday(&start_time, NULL);
    int status = storage_set(table, key, r, conn);
    gettimeofday(&end_time, NULL);
    set_processing_time.tv_usec += (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec);
    sprintf(trash, "storage_set performed in %ld microseconds\n", (end_time.tv_sec - start_time.tv_sec)*1000000L + (end_time.tv_usec - start_time.tv_usec));
    logger(client_time_log, trash);
    
    if(status != 0)
        printf("storage_set failed. Error code: %d.\n", errno);
    else
        printf("storage_set: value changed for key '%s' in table '%s' to '%s'.\n", key, table, r->value);
    
    return status;
}
Пример #9
0
END_TEST

START_TEST (test_setmissing_missingtable)
{
	struct storage_record record;
	strncpy(record.value, VALUE, sizeof record.value);
	int status = storage_set(MISSINGTABLE, KEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with missing table should fail.");
	fail_unless(errno == ERR_TABLE_NOT_FOUND, "storage_set with missing table not setting errno properly.");
}
Пример #10
0
END_TEST

START_TEST (test_setinvalid_badkey)
{
	struct storage_record record;
	strncpy(record.value, VALUE, sizeof record.value);
	int status = storage_set(TABLE, BADKEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with bad key name should fail.");
	fail_unless(errno == ERR_INVALID_PARAM, "storage_set with bad key name not setting errno properly.");
}
Пример #11
0
END_TEST

START_TEST (non_exisiting_table)
{
	struct storage_record record;
	strncpy(record.value, TABLE1_COLVAL, sizeof record.value);
	int status = storage_set("nonexistingtable", KEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with non existing table should fail");
	fail_unless(errno == ERR_TABLE_NOT_FOUND, "not setting errno properly. errno should equals to ERR_TABLE_NOT_FOUND");
}
Пример #12
0
END_TEST

START_TEST (test_setinvalidcomplex_missingcolumn)
{
        struct storage_record record;
        strncpy(record.value, "col1 22,col2 2.2", sizeof record.value);
        int status = storage_set(THREECOLSTABLE, KEY, &record, test_conn);
        fail_unless(status == -1, "storage_set with missing column should fail.");
        fail_unless(errno == ERR_INVALID_PARAM, "storage_set with missing column not setting errno properly.");
}
Пример #13
0
END_TEST


START_TEST (test_setinvalid_invalidtable)
{
	struct storage_record record;
	strncpy(record.value, INTCOLVAL, sizeof record.value);
	int status = storage_set(NULL, KEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with invalid param should fail.");
	fail_unless(errno == ERR_INVALID_PARAM, "storage_set with invalid param not setting errno properly.");
}
Пример #14
0
END_TEST

/*
START_TEST (test_query_float0)
{
	// Do a query.  Expect no matches.
	int foundkeys = storage_query(FLOATTABLE, "col > 10.0", test_keys, MAX_RECORDS_PER_TABLE, test_conn);
	fail_unless(foundkeys == 0, "Query didn't find the correct number of keys.");

	// Make sure next key is not set to anything.
	fail_unless(strcmp(test_keys[0], "") == 0, "No extra keys should be modified.\n");
}
END_TEST


START_TEST (test_query_float1)
{
	// Do a query.  Expect one match.
	int foundkeys = storage_query(FLOATTABLE, "col < -1.0", test_keys, MAX_RECORDS_PER_TABLE, test_conn);
	fail_unless(foundkeys == 1, "Query didn't find the correct number of keys.");

	// Check the matching keys.
	fail_unless(
		( strcmp(test_keys[0], KEY1) == 0 ),
		"The returned keys don't match the query.\n");

	// Make sure next key is not set to anything.
	fail_unless(strcmp(test_keys[1], "") == 0, "No extra keys should be modified.\n");
}
END_TEST
*/



/*
 * Set operations with simple tables.
 * 	update an existing record (pass).
 * 	delete an existing record (pass).
 */

START_TEST (test_set_deleteint)
{
	// Delete a key/value pair.
	int status = storage_set(INTTABLE, KEY1, NULL, test_conn);
	fail_unless(status == 0, "Error deleting the key/value pair.");

	// Try to get the deleted value.
	struct storage_record record;
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(INTTABLE, KEY1, &record, test_conn);
	fail_unless(status == -1, "storage_get for deleted key should fail.");
	fail_unless(errno == ERR_KEY_NOT_FOUND, "storage_get for deleted key not setting errno properly.");
}
Пример #15
0
END_TEST

START_TEST (test_set_deletestr)
{
	// Delete a key/value pair.
	int status = storage_set(STRTABLE, KEY1, NULL, test_conn);
	fail_unless(status == 0, "Error deleting the key/value pair.");

	// Try to get the deleted value.
	struct storage_record record;
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(STRTABLE, KEY1, &record, test_conn);
	fail_unless(status == -1, "storage_get for deleted key should fail.");
	fail_unless(errno == ERR_KEY_NOT_FOUND, "storage_get for deleted key not setting errno properly.");
}
Пример #16
0
/**
 * @brief Text fixture setup.  Start the server with complex tables and populate the tables.
 */
void test_setup_complex_populate()
{
	test_conn = init_start_connect(COMPLEXTABLES_CONF, "complexdata.serverout", NULL);
	fail_unless(test_conn != NULL, "Couldn't start or connect to server.");

	struct storage_record record;
	int status = 0;
	int i = 0;

	// Create an empty keys array.
	// No need to free this memory since Check will clean it up anyway.
	for (i = 0; i < MAX_RECORDS_PER_TABLE; i++) {
		test_keys[i] = (char*)malloc(MAX_KEY_LEN); 
		strncpy(test_keys[i], "", sizeof(test_keys[i]));
	}

	// Do a bunch of sets (don't bother checking for error).

	strncpy(record.value, "col1 -2,col2 -2,col3 abc", sizeof record.value);
	status = storage_set(THREECOLSTABLE, KEY1, &record, test_conn);
	strncpy(record.value, "col1 2,col2 2,col3 def", sizeof record.value);
	status = storage_set(THREECOLSTABLE, KEY2, &record, test_conn);
	strncpy(record.value, "col1 4,col2 4,col3 abc def", sizeof record.value);
	status = storage_set(THREECOLSTABLE, KEY3, &record, test_conn);

	strncpy(record.value, "col1 abc,col2 -2,col3 -2,col4 ABC", sizeof record.value);
	status = storage_set(FOURCOLSTABLE, KEY1, &record, test_conn);
	strncpy(record.value, "col1 def,col2 2,col3 2,col4 DEF", sizeof record.value);
	status = storage_set(FOURCOLSTABLE, KEY2, &record, test_conn);
	strncpy(record.value, "col1 abc def,col2 4,col3 4,col4 ABC DEF", sizeof record.value);
	status = storage_set(FOURCOLSTABLE, KEY3, &record, test_conn);

	strncpy(record.value, "col1 abc,col2 ABC,col3 -2,col4 2,col5 -2,col6 2", sizeof record.value);
	status = storage_set(SIXCOLSTABLE, KEY1, &record, test_conn);
	strncpy(record.value, "col1 abc,col2 ABC,col3 2,col4 -2,col5 2,col6 -2", sizeof record.value);
	status = storage_set(SIXCOLSTABLE, KEY2, &record, test_conn);
	strncpy(record.value, "col1 def,col2 DEF,col3 4,col4 -4,col5 4,col6 -4", sizeof record.value);
	status = storage_set(SIXCOLSTABLE, KEY3, &record, test_conn);

}
Пример #17
0
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST have value.
   Ignore CAS.
*/
ST_RES *cmd_qlist_add(ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || !req->value_sz) {
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS));
	}
	MC_METADATA md;
	memset(&md, 0, sizeof(md));

	uint8_t *qla = buf_a;
	int qla_sz = sizeof(buf_a);
	uint8_t *qlb = (uint8_t *)req->value;
	uint8_t *qlc = buf_b;
	int qlc_sz = sizeof(buf_b);
	
	int r = storage_get(&md, (char*)qla, qla_sz, req->key, req->key_sz);
	if(r < 0){
		r = qlist_pack(qla, qla_sz, NULL, 0);
		if(r < 0) {
			set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
			goto exit;
		}
		md.flags = FLAG_QLIST;
		md.cas = ++unique_number || ++unique_number;
	}

	if( (md.flags & FLAG_QLIST) == 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	r = qlist_or(qlc, qlc_sz, qla, qlb);
	if(r < 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	md.cas = (md.cas+1) || (md.cas+2);
	r = storage_set(&md, (char*)qlc, r, req->key, req->key_sz);
	if(r < 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}

	res->status = MEMCACHE_STATUS_OK;
	res->cas = md.cas;
exit:
	return(res);
}
Пример #18
0
END_TEST



/* Added
 * Two client instance get/set pass tests:
 *      set/get basic table/key/value names from one table.
 *      set/get extended table/key/value names from one table.
 *      set/get extended table/key/value names from two tables.
 */

START_TEST (test_restartclient_onetable)
{
        struct storage_record r;

        // Start and connect to the server.
        int serverpid = 0;
        void *conn = start_connect(ONETABLE_CONF, "test_restartclient_onetable.serverout", &serverpid);
        fail_unless(conn != NULL, "Couldn't start or connect to server.");

        // Set a key/value.
        strncpy(r.value, VALUE, sizeof r.value);
        int status = storage_set(TABLE, KEY, &r, conn);
        fail_unless(status == 0, "Error setting a key/value pair.");

        // Disconnect from the server.
        status = storage_disconnect(conn);
        //fail_unless(status == 0, "Error disconnecting from the server.");

        // Reconnect to the server
	conn = storage_connect(SERVERHOST, server_port);
        fail_unless(conn != NULL, "Couldn't connect to server.");
	
	// Authenticate with the server.
	status = storage_auth(SERVERUSERNAME, SERVERPASSWORD, conn);
	fail_unless(status == 0, "Authentication failed.");

        // Get a value.
        strncpy(r.value, "", sizeof r.value);
        status = storage_get(TABLE, KEY, &r, conn);
        fail_unless(status == 0, "Error getting a value.");
        fail_unless(strcmp(r.value, VALUE) == 0, "Got wrong value.");

        // Disconnect from the server.
        status = storage_disconnect(conn);
        //fail_unless(status == 0, "Error disconnecting from the server.");
}
Пример #19
0
END_TEST


/* Added/Modified
 * Two server instance get/set pass tests:
 * 	set/get basic table/key/value names from one table.
 * 	set/get extended table/key/value names from one table.
 * 	set/get extended table/key/value names from two tables.
 */

START_TEST (test_restartserver_onetable)
{
	struct storage_record r;

	// Start and connect to the server.
	int serverpid = 0;
	void *conn = start_connect(ONETABLE_CONF, "test_restartserver_onetable.serverout", &serverpid);
	fail_unless(conn != NULL, "Couldn't start or connect to server.");

	// Set a key/value.
	strncpy(r.value, VALUE, sizeof r.value);
	int status = storage_set(TABLE, KEY, &r, conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	// Disconnect from the server.
	status = storage_disconnect(conn);
	//fail_unless(status == 0, "Error disconnecting from the server.");
	
	// Kill the server.
	status = kill_server(serverpid);
	fail_unless(status == 0, "Couldn't kill server.");

	// Start the server.
	conn = start_connect(ONETABLE_CONF, "test_restartserver_onetable-2.serverout", &serverpid);
	fail_unless(conn != NULL, "Couldn't start or connect to server.");

	// Get a value.
	strncpy(r.value, "", sizeof r.value);
	status = storage_get(TABLE, KEY, &r, conn);
	fail_unless(status == -1, "storage_get with missing key should fail.");
        fail_unless(errno == ERR_KEY_NOT_FOUND, "storage_get with missing key not setting errno properly.");

	// Disconnect from the server.
	status = storage_disconnect(conn);
	//fail_unless(status == 0, "Error disconnecting from the server.");
}
Пример #20
0
END_TEST


/*
 * Set failure tests:
 * 	set with invalid table/key/conn (fail)
 * 	set with bad table/key/values (fail)
 * 	set with non-existent table/key (fail)
 */

START_TEST (test_setinvalid_invalidtable)
{
	struct storage_record record;
	strncpy(record.value, INTCOLVAL, sizeof record.value);
	int status = storage_set(NULL, KEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with invalid param should fail.");
	fail_unless(errno == ERR_INVALID_PARAM, "storage_set with invalid param not setting errno properly.");
}
Пример #21
0
static enum CMD_TYPE parse_cmd(char* str, size_t len, char** output_val) {
    if (len > 0) {
        if (strcmp(str,"q")==0)return CMD_QUIT;
        char* dup_str = strdup(str);
        char cmd[16]="",key[32]="",val[2048]="";
        char *saveptr=NULL;
        char *token = strtok_r(dup_str," ",&saveptr);
        int token_len = token ? strlen(token) : 0;
        // required
        if (token_len == 0 || token_len > 16) { free(dup_str);return CMD_ERR; }
        memcpy(cmd,token,token_len);
        // optional
        token = strtok_r(NULL," ",&saveptr);
        token_len = token ? strlen(token) : 0;
        if (token_len && token_len <= 32) memcpy(key,token,token_len);
        // optional
        token = strtok_r(NULL," ",&saveptr);
        token_len = token ? strlen(token) : 0;
        if (token_len) {
            if (token_len <= 2048) {
                memcpy(val,token,token_len);
            } else {
                free(dup_str);return CMD_ERR;
            }
        }
        free(dup_str);

        if (strncmp(cmd,"set",3)==0) {
            if (strlen(key)==0)return CMD_ERR;
            storage_set(key,val);
            // todo check mmap limit
            return CMD_OK;
        } else if (strncmp(cmd,"get",3)==0) {
            if (strlen(key)==0)return CMD_ERR;
            char* val = storage_get(key);
            *output_val = val;
            return val == NULL ? CMD_NULL : CMD_GET;
        } else if (strncmp(cmd,"ping",4)==0) {
            *output_val = "pong";
            return CMD_ECHO;
        }
    }
    return CMD_UNKNOWN;
}
Пример #22
0
END_TEST


/*
 * Set failure tests:
 * 	set with invalid table/key/conn (fail)
 * 	set with bad table/key (fail)
 * 	set with non-existent table/key (fail)
 */


START_TEST (test_setinvalid_badkey)
{
	struct storage_record record;
	strncpy(record.value, VALUE, sizeof record.value);
	int status = storage_set(TABLE, BADKEY, &record, test_conn);
	fail_unless(status == -1, "storage_set with bad key name should fail.");
	fail_unless(errno == ERR_INVALID_PARAM, "storage_set with bad key name not setting errno properly.");
}
Пример #23
0
END_TEST

START_TEST (test_setget_largefloat) // Added 18
{
	struct storage_record record;
	int fields = 0;
	int intval = 0;

	// Do a set
	strncpy(record.value, "col 500.2", sizeof record.value);
	int status = storage_set(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error setting a key/value pair.");

	// Do a get
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(INTTABLE, KEY, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	fields = sscanf(record.value, "col %f", &intval);
	fail_unless(fields == 1 && intval == 500.2, "Got wrong value.");
}
Пример #24
0
END_TEST
/*
START_TEST (test_set_updatefloat)
{
	struct storage_record record;
	float floatval = 0;

	// Update the value
	strncpy(record.value, "col 8.8", sizeof record.value);
	int status = storage_set(FLOATTABLE, KEY1, &record, test_conn);
	fail_unless(status == 0, "Error updating a value.");

	// Get the new value.
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(FLOATTABLE, KEY1, &record, test_conn);
	fail_unless(status == 0, "Error getting a value.");
	int fields = sscanf(record.value, "col %f", &floatval);
	fail_unless(fields == 1 && floatcmp(floatval, 8.8) == 0, "Got wrong value.");
}
END_TEST
*/

/*
 * Set operations with complex tables.
 * 	update an existing record (pass).
 * 	delete an existing record (pass).
 */

START_TEST (test_setcomplex_deletethreecols)
{
	// Delete a key/value pair.
	int status = storage_set(THREECOLSTABLE, KEY1, NULL, test_conn);
	fail_unless(status == 0, "Error deleting the key/value pair.");

	// Try to get the deleted value.
	struct storage_record record;
	strncpy(record.value, "", sizeof record.value);
	status = storage_get(THREECOLSTABLE, KEY1, &record, test_conn);
	fail_unless(status == -1, "storage_get for deleted key should fail.");
	fail_unless(errno == ERR_KEY_NOT_FOUND, "storage_get for deleted key not setting errno properly.");
}
Пример #25
0
/*
   Request:
      MUST NOT have extras.
      MUST have key.
      MUST have value.
   Ignore CAS.
*/
ST_RES *cmd_qlist_del(ST_REQ *req, ST_RES *res) {
	if(req->extras_sz || !req->key_sz || !req->value_sz)
		return(set_error_code(res, MEMCACHE_STATUS_INVALID_ARGUMENTS));
	
	MC_METADATA md;
	memset(&md, 0, sizeof(md));

	uint8_t *qla = buf_a;
	int qla_sz = sizeof(buf_a);
	uint8_t *qlb = (uint8_t *)req->value;
	uint8_t *qlc = buf_b;
	int qlc_sz = sizeof(buf_b);

	int r = storage_get(&md, (char*)qla, qla_sz, req->key, req->key_sz);
	if(r < 0)
		goto exit_ok;
		
	if( (md.flags & FLAG_QLIST) == 0) {
		set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
		goto exit;
	}
	
	r = qlist_andnot(qlc, qlc_sz, qla, qlb);
	
	if(r == EMPTY_QLIST_SIZE) {
		storage_delete(req->key, req->key_sz);
	}else{
		md.cas = (md.cas+1) || (md.cas+2);
		r = storage_set(&md, (char*)qlc, r, req->key, req->key_sz);
		if(r < 0) {
			set_error_code(res, MEMCACHE_STATUS_ITEM_NOT_STORED);
			goto exit;
		}
	}

exit_ok:
	res->status = MEMCACHE_STATUS_OK;
	res->cas = md.cas;
exit:
	return(res);
}
Пример #26
0
/**
 * @brief Text fixture setup.  Start the server and populate the tables.
 */
void test_setup_simple_populate()
{
	test_conn = init_start_connect(SIMPLETABLES_CONF, "simpledata.serverout", NULL);
	fail_unless(test_conn != NULL, "Couldn't start or connect to server.");

	struct storage_record record;
	int status = 0;
	int i = 0;

	// Create an empty keys array.
	// No need to free this memory since Check will clean it up anyway.
	for (i = 0; i < MAX_RECORDS_PER_TABLE; i++) {
		test_keys[i] = (char*)malloc(MAX_KEY_LEN); 
		strncpy(test_keys[i], "", sizeof(test_keys[i]));
	}

	// Do a bunch of sets (don't bother checking for error).

	strncpy(record.value, "col -2", sizeof record.value);
	status = storage_set(INTTABLE, KEY1, &record, test_conn);
	strncpy(record.value, "col 2", sizeof record.value);
	status = storage_set(INTTABLE, KEY2, &record, test_conn);
	strncpy(record.value, "col 4", sizeof record.value);
	status = storage_set(INTTABLE, KEY3, &record, test_conn);

//	strncpy(record.value, "col -2.2", sizeof record.value);
//	status = storage_set(FLOATTABLE, KEY1, &record, test_conn);
//	strncpy(record.value, "col 2.2", sizeof record.value);
//	status = storage_set(FLOATTABLE, KEY2, &record, test_conn);
//	strncpy(record.value, "col 4.0", sizeof record.value);
//	status = storage_set(FLOATTABLE, KEY3, &record, test_conn);

	strncpy(record.value, "col abc", sizeof record.value);
	status = storage_set(STRTABLE, KEY1, &record, test_conn);
	strncpy(record.value, "col def", sizeof record.value);
	status = storage_set(STRTABLE, KEY2, &record, test_conn);
	strncpy(record.value, "col abc def", sizeof record.value);
	status = storage_set(STRTABLE, KEY3, &record, test_conn);
}
Пример #27
0
int main(){
    void *conn = storage_connect(HOSTNAME , PORT);
    if(conn == NULL) {
        printf("Error starting the server, please make sure the socket is not being used.\n");
        printf("Notice that the parameters inside the *.conf file should be as follows:\n");
        printf("Hostname: %s\n",HOSTNAME);
        printf("Port: %d\n",PORT);
        return -1;
    }
    int status = storage_auth(ADMIN , DEC_PASSWORD , conn);
    if (status != 0) {
        printf("Error authenticating the server, make sure that the parameters inside the *.conf file are as follows:\n");
        printf("Username: %s\n",ADMIN);
        printf("Decrypted Password: %s\n",DEC_PASSWORD);
        return -1;
    }

    char *keys[100];
    struct storage_record record;                                     //test case 1 for set
    strcpy(record.value,VALUE1);
    status = storage_set(TABLE1, KEY1, &record, conn);
    if (status != 0) {
        printf("The test failed to add a key.");
        return -1;
    }
    else {
        strcpy(record.value,VALUE2);
        status = storage_set(TABLE2, KEY2, &record, conn);
        if (status != 0) {
            printf("The test failed to add a key.");
            return -1;
        }
    }


    printf("\n\nTest 1: ");             ///Test 1
    status = storage_query(TABLE1 , "invalid query" , keys , 1 , conn );
    if(status != -1) {
        printf("test failed, query should return an error.");
    }
    else {
        if (errno != 1){
            printf("test failed, errno not being set correctly.");
        }
        else
            printf("test is ok.");
    }




    printf("\n\nTest 2: ");         ///Test 2
    status = storage_query(INV_TABLE , "col1 = string" , keys , 1 , conn );
    if(status != -1) {
        printf("test failed, query should return an error.");
    }
    else {
        if (errno != 1){
            printf("test failed, errno not being set correctly.");
        }
        else
            printf("test is ok.");
    }


    printf("\n\nTest 3: ");                 ///Test 3
    status = storage_query(TABLE1 , "col1 > string" , keys , 1 , conn );
    if(status != -1) {
        printf("test failed, query should return an error.");
    }
    else {
        if (errno != 1){
            printf("test failed, errno not being set correctly.");
        }
        else
            printf("test is ok.");
    }



    printf("\n\nTest 4: ");         ///Test 4
    status = storage_query(TABLE2 , "col2 = string" , keys , 1 , conn );
    if(status != -1) {
        printf("test failed, query should return an error.");
    }
    else {
        if (errno != 1){
            printf("test failed, errno not being set correctly.");
        }
        else
            printf("test is ok.");
    }



    printf("\n\nTest 5: ");     ///Test 5
    status = storage_query(TABLE2 , "col2 > 0" , keys , 1 , conn );
    if(status == -1) {
        printf("test failed, query failed.");
    }
    else {
        if (status != 1){
            printf("test failed, number of keys returned not correct.");
        }
        else {
            if(strcmp(keys[1],"") == 0) {
                if(strcmp(keys[0],KEY2) == 0)
                    printf("test is ok.");
                else
                    printf("test failed, key retrieved isn't as expected.");
            }
            else
                printf("test failed, keys returned are different than expected.");
        }
    }

    printf("\n\n");

    return 0;
}
Пример #28
0
int main(int argc, char *argv[])
{
	// initial variables used throughout all of the commands
	int i;
	int SERVERPORT;
	char SERVERHOST[MAX_HOST_LEN], SERVERUSERNAME[MAX_USERNAME_LEN], SERVERPASSWORD[MAX_ENC_PASSWORD_LEN], SERVERPORTstr[MAX_PORT_LEN];
	char TABLE[MAX_TABLE_LEN], KEY[MAX_KEY_LEN];
	char **key = malloc(sizeof(char**)*800);
	char PREDICATES_1[500];
	char PREDICATES[500];
	void *conn = NULL;
	int status;
	struct storage_record r;
	int authenticated = 0;

	do{
		printmenu(&i); // function to prompt the user for the menu and read in and update the variable i

		if(i == 0){// Connect to server
			if(conn != NULL)
				printf("You are already connected. Please choose another option.\n\n");
			else
			{
				conn = storage_connect("localhost",2222);
				if(!conn)
				{
					printf("Cannot connect to server @ %s:%d. Error code: %d.\n", SERVERHOST, SERVERPORT, errno);
					//return -1;
				}
				else printf("\nUser conncected with the server\n");
				status = storage_auth("admin", "dog4sale", conn);
				if(status != 0)
				{
					printf("storage_auth failed with username '%s' and password '%s'. " \
					"Error code: %d, status: %d\n", SERVERUSERNAME, SERVERPASSWORD, errno, status);
					//storage_disconnect(conn);
					//return status;
				}
				else{
					authenticated = 1;
					printf("\nUser authenticated with the server\n");
				}
			}
		}
		if(i == 1)
		{// Connect to server
			if(conn != NULL)
				printf("You are already connected. Please choose another option.\n\n");
			else
			{
				printf("Enter the Server Host\n");
				fgets(SERVERHOST, MAX_HOST_LEN, stdin);
				sscanf(SERVERHOST, "%s", SERVERHOST);
				printf("Enter the Server Port\n");
				fgets(SERVERPORTstr, MAX_PORT_LEN, stdin);
				sscanf(SERVERPORTstr, "%d", &SERVERPORT);
				conn = storage_connect(SERVERHOST, SERVERPORT);
				if(!conn)
				{
					printf("Cannot connect to server @ %s:%d. Error code: %d.\n", SERVERHOST, SERVERPORT, errno);
					//return -1;
				}
				else printf("\nUser connected with the server\n");
			}
		}

		else if(i == 2)
		{// Authenticate the client.
			if(conn == NULL)
				printf("You are not connected yet. Please connect to the server first.\n\n");
			//else if(authenticated == 1)
				//printf("You are already authenticated. Please choose another option.\n\n");
			else
			{
				printf("Enter the UserName Host\n");
				fgets(SERVERUSERNAME, MAX_USERNAME_LEN, stdin);
				sscanf(SERVERUSERNAME, "%s", SERVERUSERNAME);
				printf("Enter the Password Host\n");
				fgets(SERVERPASSWORD, MAX_ENC_PASSWORD_LEN, stdin);
				sscanf(SERVERPASSWORD, "%s", SERVERPASSWORD);
				status = storage_auth(SERVERUSERNAME, SERVERPASSWORD, conn);
				if(status != 0)
				{
					printf("storage_auth failed with username '%s' and password '%s'. " \
							"Error code: %d, status: %d\n", SERVERUSERNAME, SERVERPASSWORD, errno, status);
				}
				else{
					authenticated = 1;
					printf("\nUser authenticated with the server\n");
				}
			}
		}

		else if(i == 3)
		{// SET command
			if(conn == NULL)
				printf("You are not connected yet. Please connect to the server first.\n\n");
			else
			{
				printf("Please enter the Table\n");
				fgets(TABLE, MAX_TABLE_LEN, stdin);
				sscanf(TABLE, "%s", TABLE);
				printf("Please enter the Key\n");
				fgets(KEY, MAX_KEY_LEN, stdin);
				sscanf(KEY, "%s", KEY);
				printf("Please Enter the record that you wish to store\n");
				fgets(r.value, MAX_VALUE_LEN, stdin);
				r.value[strlen(r.value)-1] = '\0';
				//strncpy(r.value, value, sizeof value);

				//This is for measuring the server processing time
				//remember when the process started
				clock_t t;
				t=clock();
				//issue set call to DATABASE with TABLE, KEY and r.value
				if(!strcmp(r.value,"null")){
					status = storage_set(TABLE,KEY,NULL,conn);
				}
				if(strcmp(r.value,"null"))
				status = storage_set(TABLE, KEY, &r, conn);
				//find how long it took to perform the GET operation
				//current time minus the time when the operation started
				t=clock()-t;

				if(status != 0)
				{
					printf("storage_set failed. Error code: %d.\n", errno);
				}
				else
					printf("storage_set: successful.\n");
			}
		}

		else if(i == 4)
		{// GET command
			if(conn == NULL)
				printf("You are not connected yet. Please connect to the server first.\n\n");
			//else if(authenticated == 0)
				//printf("You are not authenticated yet. Please authenticate first.\n\n");
			else
			{
				printf("Please enter the Table\n");
				fgets(TABLE, MAX_TABLE_LEN, stdin);
				sscanf(TABLE, "%s", TABLE);
				printf("Please enter the Key\n");
				fgets(KEY, MAX_KEY_LEN, stdin);
				sscanf(KEY, "%s", KEY);
				status = storage_get(TABLE, KEY, &r, conn);
				if(status != 0)
				{
					printf("storage_get failed. Error code: %d.\n", errno);
					storage_disconnect(conn);
					return status;
				}
				else
					printf("storage_get: the value returned for key '%s' is '%s'.\n", KEY, r.value);
			}
		}

		else if(i == 5)
		{// QUERY command
			if(conn == NULL)
				printf("You are not connected yet. Please connect to the server first.\n\n");
			else
			{
				printf("Please enter the Table\n");
				fgets(TABLE, MAX_TABLE_LEN, stdin);
				sscanf(TABLE, "%s", TABLE);

				printf("Please enter the Predicates\n");
				fgets(PREDICATES_1, 500, stdin);
				sscanf(PREDICATES_1, "%[^'\n]",PREDICATES);
				// (const char *table, const char *predicates, char **keys, const int max_keys, void *conn)
				status = storage_query(TABLE, PREDICATES, key, &r, conn);
				//int foundkeys = storage_query(INTTABLE, "col<-1", test_keys, MAX_RECORDS_PER_TABLE, test_conn);
				printf("status/number of keys: %d\n",status);
				if(status == -1)
				{
					printf("storage_query failed. Error code: %d.\n", errno);
					storage_disconnect(conn);
					return status;
				}
				else {
					int p = 0;

					int i = 0;

					while (key[i] != NULL){
						printf("key: '%s'\n", key[i]);
						i++;

					}
				}
			}
		}


		else if(i==6)
		{// Disconnect from server
			status = storage_disconnect(conn);
			if(status != 0)
			{
				printf("storage_disconnect failed. Error code: %d.\n", errno);
				return status;
			}
			else
			{
				conn = NULL;
				authenticated = 0;
				printf("You are now disconnected\n");
			}
		}
		else if (i==8){
			// import the census into the data base
			if(conn == NULL)
				printf("You are not connected yet. Please connect to the server first.\n\n");
			 FILE* fp;
			fp=fopen("census", "r");               //open the file for reading only
   			if(fp == NULL)
   			 printf("Error opening file");
  			 else
   			{
				int loop;
       				//populate the table with all the data in the data file (city + its population)
				do {
					char name[100];
					char value[MAX_VALUE_LEN];
					char line[100];
       					char *l = fgets(line, sizeof line, fp);
					if (!feof(fp)){
						sscanf(l, "%s %s", name, value);
						printf("name: %s , value: %s \n", name, value);
						strcpy(r.value,value);
						status = storage_set("census", name, &r, conn);
						if(status != 0)
						{
							printf("storage_set failed. Error code: %d.\n", errno);
						}			
					}
					
				   }
       				 while(!feof(fp));     //read the first line (city)	
   			}
		}

	} while (i!=7); // if i is 6, we want to exit from client

	// Exit
	return 0;
}
Пример #29
0
const char* testClient(void* parameters)
{
    struct workerTask* worker = (struct workerTask*)parameters;
    
    uint16_t randContext[3];
    for(int i = 0; i < 3; i++)
    {
        randContext[i] = time(NULL) ^ worker->workerID;
    }
    
    if(!worker->conn)
    {
        //spread the load from new connections so the server won't be overloaded
        usleep(erand48(randContext) * 1000 + 1000*worker->connOpenDelay);
        
        worker->conn = storage_connect(worker->hostname, worker->port);
        if(!worker->conn)
        {
            printf("storage_connect failed\n");
            return ece297strerror(errno);
        }
        
        int result = storage_auth(worker->username, worker->password, worker->conn);
        if(result == -1)
        {
            printf("storage_auth failed\n");
            storage_disconnect(worker->conn);
            worker->conn = NULL;
            return ece297strerror(errno);
        }
    }
    
    uint64_t loopCount = worker->numKeys;
    if(worker->type == kClientRunWorkload)
    {
        loopCount = worker->count;
    }
    
    uint64_t period = 0;
    
    //0 throughput = no limit
    if(worker->throughput)
    {
        period = (1/worker->throughput) * 1000000;
        
        //start at a random time
        usleep(erand48(randContext) * period);
    }
    struct timeval next;
    gettimeofday(&next, NULL);
    
    for(uint64_t i = 0; i < loopCount; i++)
    {
        if(worker->throughput)
        {
            int64_t timeRemaining = -uSElapsed(&next);
            if(timeRemaining > 0)
            {
                usleep((uint32_t)timeRemaining);
            }
            uint64_t newTime = next.tv_usec + period;
            next.tv_sec += newTime / 1000000;
            next.tv_usec = newTime % 1000000;
        }
        switch (worker->type)
        {
            case kClientAddKeys:
            {
                char keyBuf[20]; //as per ECE297 spec
				stringGen(worker->startingKey + i, worker->keySecret, keyBuf, sizeof(keyBuf));
                
                struct storage_record record;
                memset(&record.metadata, 0, sizeof(record.metadata));
                
				stringGen(worker->startingKey + i, worker->valueSecret, record.value, sizeof(record.value));
                                				
                struct timeval start;
                gettimeofday(&start, NULL);
                if(storage_set(worker->table, keyBuf, &record, worker->conn) == -1)
                {
                    printf("storage_set failed\n");
                    return ece297strerror(errno);
                }
                recordLatency(timeElapsed(&start), worker->latencyResults);
                break;
            }
                                
            case kClientRunWorkload:
            {
				//WATCH the floating point promotion - it must be cast to a uint64_t BEFORE adding worker->startingKey
                uint64_t keyIndex = ((uint64_t)(erand48(randContext) * worker->numKeys)) + worker->startingKey;
                
                char keyBuf[20]; //as per ECE297 spec
				stringGen(keyIndex, worker->keySecret, keyBuf, sizeof(keyBuf));
                char expectedValue[1024];
				stringGen(keyIndex, worker->valueSecret, expectedValue, sizeof(expectedValue));
				
                struct timeval start;
                gettimeofday(&start, NULL);
                struct storage_record rec;
                if(storage_get(worker->table, keyBuf, &rec, worker->conn) == -1)
                {
                    printf("storage_get failed (key index = %u)\n", keyIndex);
                    return ece297strerror(errno);
                }
                if(strcmp(rec.value, expectedValue)) {
                	return "Server returned incorrect key";
                }
				
                recordLatency(timeElapsed(&start), worker->latencyResults);
            }
        }
    }
    return NULL;
}
Пример #30
0
int main(){
    void *conn = storage_connect(HOSTNAME , PORT);
    if(conn == NULL) {
        printf("Error starting the server, please make sure the socket is not being used.\n");
        printf("Notice that the parameters inside the *.conf file should be as follows:\n");
        printf("Hostname: %s\n",HOSTNAME);
        printf("Port: %d\n",PORT);
        return -1;
    }
    int status = storage_auth(ADMIN , DEC_PASSWORD , conn);
    if (status != 0) {
        printf("Error authenticating the server, make sure that the parameters inside the *.conf file are as follows:\n");
        printf("Username: %s\n",ADMIN);
        printf("Decrypted Password: %s\n",DEC_PASSWORD);
        return -1;
    }



    printf("\n\nTes 1: ");
    struct storage_record record;
    strcpy(record.value,VALUE1);                                     //test case 1 for get
    status = storage_set(TABLE1, KEY1, &record, conn);
    if (status != 0) {
        printf("The test failed, not set value.");
    }
    else {
        status = storage_get(TABLE1, KEY1, &record, conn);
        if (status != 0) {
            printf("The test failed.");
        }
        else if ((strcmp(record.value, VALUE1) == 0)) {
            printf("Test is ok.");
        }
        else {
            printf("The test failed, value retrieved not the same that should be.");
        }
    }


    printf("\n\nTest 2: ");
    strcpy(record.value,VALUE2);                                      //test case 2 for get
    status = storage_set(TABLE2, KEY2, &record, conn);
    if (status != 0) {
        printf("The test failed, not set value.");
    }
    else {
        status = storage_get(TABLE2, KEY2, &record, conn);
        if (status != 0) {
            printf("The test failed.");
        }
        else if ((strcmp(record.value, VALUE2) == 0)) {
            printf("Test is ok.");
        }
        else {
            printf("The test failed, value retrieved not the same that should be.");
        }
    }


    printf("\n\nTest 3: ");              ///Test case 3
	status = storage_get(INV_TABLE, KEY1 , &record , conn);
	if(status != 0) {
        if(errno == 1)
    		printf("The test is ok.");
        else
            printf("The test failed, errno not set correctly.");
	}
	else {
		printf("The test failed, should have failed to get the value.");
    }





    printf("\n\nTest 4: ");                      ///Test case 4
 	status = storage_get(TABLE2, INV_KEY , &record , conn);
	if(status != 0) {
        if(errno == 1)
    		printf("The test is ok.");
        else
            printf("The test failed, errno not set correctly.");
	}
	else {
		printf("The test failed, should have failed to get the value.");
       }



    printf("\n\nTest 5: ");
    status = storage_get(TABLE2, KEY3 , &record , conn);    ///TEST 5
    if (status != 0){
        if (errno == 6)
            printf("The test is ok.\n");
        else
            printf("The test failed, errno not set correctly.");
    }
    else {
        printf("The test failed, should have failed to get.\n");
    }



    return 0;
}