Example #1
0
// Checks that the server is up and running. This function is synchronous and
// does not use a worker.
//
// server - The server.
// header - The message header.
// table  - The table the message is working against
// input  - The input file stream.
// output - The output file stream.
//
// Returns 0 if successful, otherwise returns -1.
int sky_ping_message_process(sky_server *server,
                                   sky_message_header *header,
                                   sky_table *table, FILE *input, FILE *output)
{
    size_t sz;
    check(server != NULL, "Server required");
    check(header != NULL, "Message header required");
    check(input != NULL, "Input stream required");
    check(output != NULL, "Output stream required");
    UNUSED(table);
    
    struct tagbstring status_str = bsStatic("status");
    struct tagbstring ok_str = bsStatic("ok");

    // Return:
    //   {status:"OK"}
    minipack_fwrite_map(output, 1, &sz);
    check(sz > 0, "Unable to write output");
    check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key");
    check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value");
    
    // Clean up.
    fclose(input);
    fclose(output);

    return 0;

error:
    if(input) fclose(input);
    if(output) fclose(output);
    return -1;
}
Example #2
0
// Deletes a table to the server. This function is synchronous and does not use
// a worker.
//
// server - The server.
// header - The message header.
// table  - The table the message is working against
// input  - The input file stream.
// output - The output file stream.
//
// Returns 0 if successful, otherwise returns -1.
int sky_delete_table_message_process(sky_server *server,
                                     sky_message_header *header,
                                     sky_table *_table, FILE *input, FILE *output)
{
    int rc = 0;
    size_t sz;
    sky_delete_table_message *message = NULL;
    sky_table *table = NULL;
    bstring path = NULL;
    check(server != NULL, "Server required");
    check(header != NULL, "Message header required");
    check(input != NULL, "Input stream required");
    check(output != NULL, "Output stream required");
    (void)_table;
    
    struct tagbstring status_str = bsStatic("status");
    struct tagbstring ok_str = bsStatic("ok");

    // Parse message.
    message = sky_delete_table_message_create(); check_mem(message);
    rc = sky_delete_table_message_unpack(message, input);
    check(rc == 0, "Unable to parse 'delete_table' message");

    // Retrieve table reference from server.
    rc = sky_server_get_table(server, message->name, &table);
    check(rc == 0, "Unable to find table: %s", bdata(message->name));
    check(table != NULL, "Table does not exist: %s", bdata(message->name));

    // Detach table first.
    path = bstrcpy(table->path); check_mem(path);
    rc = sky_server_close_table(server, table);
    check(rc == 0, "Unable to close table before deletion");

    // If the table exists then delete it.
    if(sky_file_exists(path)) {
        rc = sky_file_rm_r(path);
        check(rc == 0, "Unable to delete table: %s", bdata(path));
    }
    
    // Return.
    //   {status:"OK"}
    minipack_fwrite_map(output, 1, &sz);
    check(sz > 0, "Unable to write output");
    check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key");
    check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value");

    fclose(input);
    fclose(output);
    bdestroy(path);
    sky_delete_table_message_free(message);
    
    return 0;

error:
    if(input) fclose(input);
    if(output) fclose(output);
    bdestroy(path);
    sky_delete_table_message_free(message);
    return -1;
}
Example #3
0
int test_sky_table_open() {
    struct tagbstring lock_file_path = bsStatic("tmp/.skylock");
    struct tagbstring data_file_path = bsStatic("tmp/0/data");
    struct tagbstring header_file_path = bsStatic("tmp/0/header");
    cleantmp();
    
    int rc;
    sky_table *table = sky_table_create();
    table->path = bfromcstr("tmp");
    
    // Open table.
    rc = sky_table_open(table);
    mu_assert_int_equals(rc, 0);
    mu_assert(sky_file_exists(&lock_file_path), "");
    mu_assert(sky_file_exists(&data_file_path), "");
    mu_assert(sky_file_exists(&header_file_path), "");
    
    // Close table.
    rc = sky_table_close(table);
    mu_assert_int_equals(rc, 0);
    mu_assert(!sky_file_exists(&lock_file_path), "");
    mu_assert(sky_file_exists(&data_file_path), "");
    mu_assert(sky_file_exists(&header_file_path), "");

    sky_table_free(table);
    return 0;
}
Example #4
0
File: util.c Project: dasfaha/sky
// Retrieves a flag stating if the type name is a function type.
//
// name - The name of the type.
//
// Returns true if the the type is a function type, otherwise returns false.
bool qip_is_function_type_name(bstring name)
{
    struct tagbstring function_str1 = bsStatic("Function");
    struct tagbstring function_str2 = bsStatic("Function<");
    
    return binstr(name, 0, &function_str1) != BSTR_ERR
        || binstr(name, 0, &function_str2) != BSTR_ERR;
}
Example #5
0
int test1 (void) {
int ret = 0;

	printf ("TEST: CBString = operator\n");

	try {
		CBString c0;
		struct tagbstring t = bsStatic ("test");

		ret += c0.iswriteprotected();
		c0.writeprotect ();
		ret += 1 != c0.iswriteprotected();
		EXCEPTION_EXPECTED (c0 = 'x');
		EXCEPTION_EXPECTED (c0 = (unsigned char) 'x');
		EXCEPTION_EXPECTED (c0 = "test");
		EXCEPTION_EXPECTED (c0 = CBString ("test"));
		EXCEPTION_EXPECTED (c0 = t);
	}
	catch (struct CBStringException err) {
		printf ("Exception thrown [%d]: %s\n", __LINE__, err.what());
		ret ++;
	}

	try {
		CBString c0, c1;
		struct tagbstring t = bsStatic ("test");

		printf ("\tc = 'x';\n");
		c0 = 'x';
		ret += (c0 != "x");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc = (unsigned char)'x';\n");
		c0 = (unsigned char) 'x';
		ret += (c0 != "x");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc = \"test\";\n");
		c0 = "test";
		ret += (c0 != "test");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc = CBStr[\"test\"];\n");
		c1 = c0;
		ret += (c0 != c1);
		ret += '\0' != ((const char *)c1)[c1.length()];
		printf ("\tc = tbstr[\"test\"];\n");
		c0 = t;
		ret += (c0 != "test");
		ret += '\0' != ((const char *)c0)[c0.length()];
	}

	catch (struct CBStringException err) {
		printf ("Exception thrown [%d]: %s\n", __LINE__, err.what());
		ret ++;
	}

	printf ("\t# failures: %d\n", ret);
	return ret;
}
Example #6
0
int test2 (void) {
int ret = 0;

	printf ("TEST: CBString += operator\n");

	try {
		CBString c0;
		struct tagbstring t = bsStatic ("test");

		c0.writeprotect ();
		EXCEPTION_EXPECTED (c0 += 'x');
		EXCEPTION_EXPECTED (c0 += (unsigned char) 'x');
		EXCEPTION_EXPECTED (c0 += "test");
		EXCEPTION_EXPECTED (c0 += CBString ("test"));
		EXCEPTION_EXPECTED (c0 += t);
	}
	catch (struct CBStringException err) {
		printf ("Exception thrown [%d]: %s\n", __LINE__, err.what());
		ret ++;
	}

	try {
		CBString c0;
		struct tagbstring t = bsStatic ("extra");

		c0 = "test";
		printf ("\tc += 'x';\n");
		c0 += 'x';
		ret += (c0 != "testx");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc += (unsigned char)'x';\n");
		c0 += (unsigned char) 'y';
		ret += (c0 != "testxy");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc += \"test\";\n");
		c0 += "test";
		ret += (c0 != "testxytest");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc += CBStr[\"test\"];\n");
		c0 += CBString (c0);
		ret += (c0 != "testxytesttestxytest");
		ret += '\0' != ((const char *)c0)[c0.length()];
		printf ("\tc += tbstr[\"test\"];\n");
		c0 += t;
		ret += (c0 != "testxytesttestxytestextra");
		ret += '\0' != ((const char *)c0)[c0.length()];
	}

	catch (struct CBStringException err) {
		printf ("Exception thrown [%d]: %s\n", __LINE__, err.what());
		ret ++;
	}

	printf ("\t# failures: %d\n", ret);
	return ret;
}
Example #7
0
// Retrieves a list of properties from the server for a table. This function is
// synchronous and does not use a worker.
//
// server - The server.
// header - The message header.
// table  - The table the message is working against
// input  - The input file stream.
// output - The output file stream.
//
// Returns 0 if successful, otherwise returns -1.
int sky_get_properties_message_process(sky_server *server,
                                       sky_message_header *header,
                                       sky_table *table, FILE *input, FILE *output)
{
    int rc = 0;
    size_t sz;
    sky_get_properties_message *message = NULL;
    check(server != NULL, "Server required");
    check(header != NULL, "Message header required");
    check(table != NULL, "Table required");
    check(input != NULL, "Input stream required");
    check(output != NULL, "Output stream required");
    
    struct tagbstring status_str = bsStatic("status");
    struct tagbstring ok_str = bsStatic("ok");
    struct tagbstring properties_str = bsStatic("properties");

    // Parse message.
    message = sky_get_properties_message_create(); check_mem(message);
    rc = sky_get_properties_message_unpack(message, input);
    check(rc == 0, "Unable to parse 'get_properties' message");

    // Return.
    //   {status:"OK", properties:[{...}]}
    minipack_fwrite_map(output, 2, &sz);
    check(sz > 0, "Unable to write output");
    check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key");
    check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value");
    check(sky_minipack_fwrite_bstring(output, &properties_str) == 0, "Unable to write properties key");

    // Loop over properties and serialize them.
    minipack_fwrite_array(output, table->property_file->property_count, &sz);
    check(sz > 0, "Unable to write properties array");
    
    uint32_t i;
    for(i=0; i<table->property_file->property_count; i++) {
        sky_property *property = table->property_file->properties[i];
        check(sky_property_pack(property, output) == 0, "Unable to write property");
    }

    // Clean up.
    sky_get_properties_message_free(message);
    fclose(input);
    fclose(output);

    return 0;

error:
    sky_get_properties_message_free(message);
    if(input) fclose(input);
    if(output) fclose(output);
    return -1;
}
Example #8
0
int test_sky_data_file_deduplicate_insertion_event_data() {
    struct tagbstring STR1 = bsStatic("foo");
    struct tagbstring STR2 = bsStatic("bar");
    struct tagbstring STR3 = bsStatic("zzz");
    int rc;
    sky_event *event;
    sky_data_file *data_file;
    INIT_DATA_FILE("tests/fixtures/data_files/dedup/0/a", 0);

    // Add initial event.
    event = sky_event_create(1, 10LL, 1);
    event->data_count = 8;
    event->data = calloc(event->data_count, sizeof(*event->data));
    event->data[0] = sky_event_data_create_string(-1, &STR2);
    event->data[1] = sky_event_data_create_int(-2, 200);
    event->data[2] = sky_event_data_create_double(-3, 1.0);
    event->data[3] = sky_event_data_create_boolean(-4, false);
    event->data[4] = sky_event_data_create_string(1, &STR1);
    event->data[5] = sky_event_data_create_int(2, 100);
    event->data[6] = sky_event_data_create_double(3, 100.2);
    event->data[7] = sky_event_data_create_boolean(4, true);
    rc = sky_data_file_add_event(data_file, event);
    mu_assert_int_equals(rc, 0);
    
    ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/b");

    // Add the same event data, different timestamp.
    event->timestamp = 11LL;
    rc = sky_data_file_add_event(data_file, event);
    mu_assert_int_equals(rc, 0);
    sky_event_free(event);
    
    ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/c");

    // Add somewhat different object state event.
    event = sky_event_create(1, 12LL, 0);
    event->data_count = 4;
    event->data = calloc(event->data_count, sizeof(*event->data));
    event->data[0] = sky_event_data_create_string(1, &STR3);
    event->data[1] = sky_event_data_create_int(2, 20);
    event->data[2] = sky_event_data_create_double(3, 100.2);
    event->data[3] = sky_event_data_create_boolean(4, false);
    rc = sky_data_file_add_event(data_file, event);
    mu_assert_int_equals(rc, 0);
    sky_event_free(event);

    ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/d");

    sky_data_file_free(data_file);
    return 0;
}
Example #9
0
int
tor_pt_check_config(const tor_pt_config_t *cfg, const bstring methodname)
{
	struct tagbstring supported_ver = bsStatic(TOR_PT_MANAGED_TRANSPORT_V1);
	struct tagbstring all_methods = bsStatic("*");
	int i;

	if (cfg->transport_ver == NULL) {
		fprintf(stdout, "ENV-ERROR No Managed Transport Version specified\n");
		return -1;
	}

	for (i = 0; i < cfg->transport_ver->qty; i++) {
		if (0 == bstrcmp(&supported_ver, cfg->transport_ver->entry[i]))
			goto found_compatible_version;
	}

	fprintf(stdout, "VERSION-ERROR no-version\n");

	return -1;

found_compatible_version:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
	fprintf(stdout, "VERSION %s\n", bdata(&supported_ver));
#pragma GCC diagnostic pop

	if (cfg->is_client) {
		for (i = 0; i < cfg->client.methods->qty; i++) {
			if (0 == bstrcmp(&all_methods,
						cfg->client.methods->entry[i]))
				goto found_compatible_method;

			if (0 == bstrcmp(methodname,
						cfg->client.methods->entry[i]))
				goto found_compatible_method;
		}

		tor_pt_on_client_done();

		return -1;
	} else {
		/* Validate the server options */
	}

found_compatible_method:
	return 0;
}
Example #10
0
int test_sky_action_file_save() {
    int rc;
    struct tagbstring path = bsStatic("tmp/actions");
    
    // Initialize action file.
    sky_action_file *action_file = sky_action_file_create();
    sky_action_file_set_path(action_file, &path);
    
    // Action 1
    sky_action *action1 = sky_action_create();
    action1->name = bfromcstr("foo");
    rc = sky_action_file_add_action(action_file, action1);
    mu_assert_int_equals(rc, 0);
    mu_assert_int_equals(action_file->action_count, 1);

    // Action 2
    sky_action *action2 = sky_action_create();
    action2->name = bfromcstr("this_is_a_really_long_action_name_woohoo");
    rc = sky_action_file_add_action(action_file, action2);
    mu_assert_int_equals(rc, 0);
    mu_assert_int_equals(action_file->action_count, 2);

    // Save
    rc = sky_action_file_save(action_file);
    mu_assert_int_equals(rc, 0);
    mu_assert_file("tmp/actions", "tests/fixtures/action_files/0");

    sky_action_file_free(action_file);
    return 0;
}
Example #11
0
END_TEST

START_TEST(core_012)
{
	struct tagbstring t = bsStatic("Hello world");
	struct bStream * s;
	int ret = 0;
	bstring b, c;
	s = bsFromBstr(&t);
	ck_assert(s != NULL);
	b = bfromcstr("");
	ck_assert(b != NULL);
	ret = bsread(b, s, 6);
	ck_assert_int_eq(ret, 0);
	ret = biseqcstr(b, "Hello ");
	ck_assert_int_eq(ret, 1);
	if (b) {
		b->slen = 0;
	}
	ret = bsread(b, s, 6);
	ck_assert_int_eq(ret, 0);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	c = bsclose(s);
	ck_assert(c == NULL);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
}
Example #12
0
int test1 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b, c, d;
int ret = 0;

	printf ("TEST: bTail and bHead functions.\n");
	b = bTail (&t, 5);
	c = bHead (&t, 5);
	ret += 0 >= biseqcstr (b, "world");
	ret += 0 >= biseqcstr (c, "Hello");
	bdestroy (b);
	bdestroy (c);

	b = bTail (&t, 0);
	c = bHead (&t, 0);
	ret += 0 >= biseqcstr (b, "");
	ret += 0 >= biseqcstr (c, "");
	bdestroy (b);
	bdestroy (c);

	d = bstrcpy (&t);
	b = bTail (d, 5);
	c = bHead (d, 5);
	ret += 0 >= biseqcstr (b, "world");
	ret += 0 >= biseqcstr (c, "Hello");
	bdestroy (b);
	bdestroy (c);
	bdestroy (d);

	printf ("\t# failures: %d\n", ret);

	return ret;
}
Example #13
0
int Host_load(tst_t *settings, Value *val)
{
    CONFIRM_TYPE("Host");
    Class *cls = val->as.cls;
    char *sql = NULL;
    struct tagbstring ROUTES_VAR = bsStatic("routes");

    const char *name = AST_str(settings, cls->params, "name", VAL_QSTRING);
    check(name, "No name set for Host.");

    sql = sqlite3_mprintf(bdata(&HOST_SQL), SERVER_ID, name, name);
    
    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to store Host: %s", name);

    cls->id = HOST_ID = DB_lastid();

    Value *routes = AST_get(settings, cls->params, &ROUTES_VAR, VAL_HASH);
    check(routes, "Didn't find any routes for %s", name);

    AST_walk_hash(settings, routes, Route_load);

    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Example #14
0
int test13 (void) {
struct tagbstring t0 = bsStatic ("Random String");
struct vfgetc vctx;
bstring b;
int ret = 0;
int i;

	printf ("TEST: bSecureInput, bSecureDestroy.\n");

	for (i=0; i < 1000; i++) {
		unsigned char * h;

		vctx.ofs = 0;
		vctx.base = &t0;

		b = bSecureInput (INT_MAX, '\n', (bNgetc) test13_fgetc, &vctx);
		ret += 1 != biseq (b, &t0);
		h = b->data;
		bSecureDestroy (b);

		/* WARNING! Technically unsound code follows: */
		ret += (0 == memcmp (h, t0.data, t0.slen));

		if (ret) break;
	}

	printf ("\t# failures: %d\n", ret);

	return ret;
}
Example #15
0
END_TEST

START_TEST(core_011)
{
	struct tagbstring t = bsStatic("Hello world");
	unsigned char Ytstr[] = {
		0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a,
		0xa1, 0x99, 0x9c, 0x96, 0x8e
	};
	bstring b, c;
	int ret = 0;
	b = bYEncode(&t);
	ck_assert(b != NULL);
	ck_assert_int_eq(ret, 0);
	ret = bisstemeqblk(b, Ytstr, 11);
	ck_assert_int_eq(ret, 1);
	c = bYDecode(b);
	ck_assert(c != NULL);
	ret = biseq(c, &t);
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
}
Example #16
0
// Serializes a property to a file stream.
//
// property - The property.
// file   - The file stream to write to.
//
// Returns 0 if successful, otherwise returns -1.
int sky_property_pack(sky_property *property, FILE *file)
{
    int rc;
    size_t sz;
    check(property != NULL, "Property required");
    check(file != NULL, "File stream required");

    struct tagbstring id_str = bsStatic("id");
    struct tagbstring type_str = bsStatic("type");
    struct tagbstring data_type_str = bsStatic("dataType");
    struct tagbstring name_str = bsStatic("name");

    // Update the type just in case.
    rc = sky_property_update_type(property);
    check(rc == 0, "Unable to update property type");

    // Map
    minipack_fwrite_map(file, 4, &sz);
    check(sz > 0, "Unable to write map");
    
    // ID
    check(sky_minipack_fwrite_bstring(file, &id_str) == 0, "Unable to write id key");
    minipack_fwrite_int(file, property->id, &sz);
    check(sz > 0, "Unable to write id value");

    // Type
    check(sky_minipack_fwrite_bstring(file, &type_str) == 0, "Unable to write type key");
    minipack_fwrite_uint(file, property->type, &sz);
    check(sz > 0, "Unable to write type value");

    // Data Type
    check(sky_minipack_fwrite_bstring(file, &data_type_str) == 0, "Unable to write data type key");
    bstring data_type_name = sky_data_type_to_str(property->data_type);
    check(sky_minipack_fwrite_bstring(file, data_type_name) == 0, "Unable to write data type value");
    bdestroy(data_type_name);

    // Name
    check(sky_minipack_fwrite_bstring(file, &name_str) == 0, "Unable to write name key");
    check(sky_minipack_fwrite_bstring(file, property->name) == 0, "Unable to write name value");

    return 0;

error:
    return -1;
}
Example #17
0
int test_sky_lua_generate_header() {
    importtmp("tests/fixtures/sky_lua/0/data.json");
    sky_table *table = sky_table_create();
    table->path = bfromcstr("tmp");
    sky_table_open(table);

    struct tagbstring source = bsStatic(
        "function aggregate(event)\n"
        "  label = event:first_name() .. ' ' .. event:last_name()\n"
        "  return event.x + event.y\n"
        "end\n"
    );
    bstring event_decl = NULL;
    bstring event_metatype = NULL;
    bstring init_descriptor_func = NULL;
    int rc = sky_lua_generate_event_info(&source, table->property_file, &event_decl, &event_metatype, &init_descriptor_func);
    mu_assert_int_equals(rc, 0);
    mu_assert_bstring(event_decl,
        "typedef struct {\n"
        "  int64_t ts;\n"
        "  uint32_t timestamp;\n"
        "  uint16_t action_id;\n"
        "  sky_string_t _first_name;\n"
        "  sky_string_t _last_name;\n"
        "  int32_t x;\n"
        "  int32_t y;\n"
        "} sky_lua_event_t;"
    );
    mu_assert_bstring(event_metatype,
        "ffi.metatype('sky_lua_event_t', {\n"
        "  __index = {\n"
        "    first_name = function(event) return ffi.string(event._first_name.data, event._first_name.length) end,\n"
        "    last_name = function(event) return ffi.string(event._last_name.data, event._last_name.length) end,\n"
        "    x = function(event) return event.x end,\n"
        "    y = function(event) return event.y end,\n"
        "  }\n"
        "})\n"
    );
    mu_assert_bstring(init_descriptor_func,
        "function sky_init_descriptor(_descriptor)\n"
        "  descriptor = ffi.cast('sky_data_descriptor_t*', _descriptor)\n"
        "  descriptor:set_data_sz(ffi.sizeof('sky_lua_event_t'));\n"
        "  descriptor:set_ts_offset(ffi.offsetof('sky_lua_event_t', 'ts'));\n"
        "  descriptor:set_timestamp_offset(ffi.offsetof('sky_lua_event_t', 'timestamp'));\n"
        "  descriptor:set_action_id_offset(ffi.offsetof('sky_lua_event_t', 'action_id'));\n"
        "  descriptor:set_property(4, ffi.offsetof('sky_lua_event_t', '_first_name'), 1);\n"
        "  descriptor:set_property(5, ffi.offsetof('sky_lua_event_t', '_last_name'), 1);\n"
        "  descriptor:set_property(2, ffi.offsetof('sky_lua_event_t', 'x'), 2);\n"
        "  descriptor:set_property(3, ffi.offsetof('sky_lua_event_t', 'y'), 2);\n"
        "end\n"
    );
    bdestroy(event_decl);
    bdestroy(event_metatype);
    bdestroy(init_descriptor_func);
    sky_table_free(table);
    return 0;
}
Example #18
0
int Config_load(const char *config_file, const char *db_file)
{
    int rc = 0;
    tst_t *settings = NULL;
    struct tagbstring SETTINGS_VAR = bsStatic("settings");
    struct tagbstring MIMETYPES_VAR = bsStatic("mimetypes");

    settings = Parse_config_file(config_file);
    check(settings != NULL, "Error parsing config file: %s.", config_file);

    rc = Config_setup(db_file);
    check(rc == 0, "Failed to configure config db: %s", db_file);

    rc = AST_walk(settings, Server_load);
    check(rc == 0, "Failed to process the config file: %s", config_file);

    Value *set = AST_get(settings, settings, &SETTINGS_VAR, VAL_HASH);

    if(set) {
        rc = AST_walk_hash(settings, set, Settings_load);
        check(rc == 0, "Failed to load the settings. Aborting.");
    }

    rc = Mimetypes_import();
    check(rc == 0, "Failed to import default mimetypes.");

    Value *mime = AST_get(settings, settings, &MIMETYPES_VAR, VAL_HASH);
    if(mime) {
        AST_walk_hash(settings, mime, Mimetypes_load);
        check(rc == 0, "Failed to load the mimetypes. Aborting.");
    }

    rc = Config_commit();
    check(rc == 0, "Failed to commit config db: %s", db_file);

    AST_destroy(settings);
    DB_close();
    return 0;
error:
    AST_destroy(settings);
    DB_close();
    return -1;
}
Example #19
0
int test_sky_data_file_set_header_path() {
    int rc;
    struct tagbstring path = bsStatic("/dev/null");
    sky_data_file *data_file = sky_data_file_create();
    rc = sky_data_file_set_path(data_file, &path);
    mu_assert_int_equals(rc, 0);
    mu_assert_bstring(data_file->path, "/dev/null");
    sky_data_file_free(data_file);
    return 0;
}
Example #20
0
int test_sky_aggregate() {
    importtmp("tests/fixtures/sky_lua/1/data.json");
    sky_table *table = sky_table_create();
    table->path = bfromcstr("tmp");
    sky_table_open(table);
    sky_data_descriptor *descriptor = sky_data_descriptor_create();

    // Initialize the path iterator.
    sky_path_iterator iterator;
    sky_path_iterator_init(&iterator);
    iterator.cursor.data_descriptor = descriptor;
    sky_path_iterator_set_tablet(&iterator, table->tablets[0]);

    struct tagbstring source = bsStatic(
        "function aggregate(cursor, data)\n"
        "  data.path_count = (data.path_count or 0) + 1\n"
        "  while cursor:next() do\n"
        "    data.event_count = (data.event_count or 0) + 1\n"
        "    data.z = (data.z or 0) + cursor.event.x + cursor.event.y\n"
        "  end\n"
        "end\n"
    );
    lua_State *L = NULL;
    int rc = sky_lua_initscript_with_table(&source, table, descriptor, &L);
    mu_assert_int_equals(rc, 0);

    // Allocate data.
    mu_assert_int_equals(descriptor->data_sz, 24);
    iterator.cursor.data = calloc(1, descriptor->data_sz);

    // Start benchmark.
    struct timeval tv;
    gettimeofday(&tv, NULL);
    int64_t t0 = (tv.tv_sec*1000) + (tv.tv_usec/1000);

    // Call sky_aggregate() function.
    uint32_t i;
    for(i=0; i<1; i++) {
        //sky_path_iterator_set_tablet(&iterator, table->tablets[0]);
        lua_getglobal(L, "sky_aggregate");
        lua_pushlightuserdata(L, &iterator);
        lua_call(L, 1, 0);
    }

    // End benchmark.
    gettimeofday(&tv, NULL);
    int64_t t1 = (tv.tv_sec*1000) + (tv.tv_usec/1000);
    printf("[lua] t=%.3fs\n", ((float)(t1-t0))/1000);

    sky_path_iterator_uninit(&iterator);
    sky_table_free(table);
    free(iterator.cursor.data);
    return 0;
}
Example #21
0
char *test_Decode_RError()
{
    char *data[] = {
	"d1:eli201e7:Generice1:t2:aa1:y1:ee",
	"d1:eli202e6:Servere1:t2:aa1:y1:ee",
	"d1:eli203e8:Protocole1:t2:aa1:y1:ee",
	"d1:eli204e6:Methode1:t2:aa1:y1:ee",
	NULL
    };

    int expected_code[] = { 201, 202, 203, 204 };
    struct tagbstring expected_msg[] = {
	bsStatic("Generic"),
	bsStatic("Server"),
	bsStatic("Protocol"),
	bsStatic("Method")
    };

    int i = 0;
    while (data[i])
    {
	Message *message = Message_Decode(data[i], strlen(data[i]), NULL);

	mu_assert(message->type == RError, "Wrong message type");
	mu_assert(same_bytes_len("aa", message->t, message->t_len), "Wrong transaction id");

	RErrorData *data = &message->data.rerror;

	mu_assert(data->code == expected_code[i], "Wrong error code");
	mu_assert(bstrcmp(&expected_msg[i], data->message) == 0, "Wrong message");

	Message_Destroy(message);

	i++;
    }

    return NULL;
}
Example #22
0
// Creates a wrapped module.
//
// Returns a new module.
sky_qip_module *sky_qip_module_create()
{
    struct tagbstring core_class_path = bsStatic(SKY_LIB_CORE_PATH);
    struct tagbstring sky_class_path = bsStatic(SKY_LIB_SKY_PATH);

    sky_qip_module *module = NULL;
    module = calloc(1, sizeof(sky_qip_module)); check_mem(module);
    
    // Setup compiler.
    module->compiler = qip_compiler_create(); check_mem(module->compiler);
    module->compiler->process_dynamic_class = sky_qip_module_process_dynamic_class_callback;
    module->compiler->dependency_count = 1;
    module->compiler->dependencies = calloc(module->compiler->dependency_count, sizeof(*module->compiler->dependencies));
    module->compiler->dependencies[0] = bfromcstr("String");
    qip_compiler_add_class_path(module->compiler, &core_class_path);
    qip_compiler_add_class_path(module->compiler, &sky_class_path);

    return module;

error:
    sky_qip_module_free(module);
    return NULL;
}
Example #23
0
int test_sky_create_table_message_process() {
    cleantmp();
    sky_server *server = sky_server_create(NULL);
    server->path = bfromcstr("tmp");
    sky_message_header *header = sky_message_header_create();

    FILE *input = fopen("tests/fixtures/create_table_message/1/input", "r");
    FILE *output = fopen("tmp/output", "w");
    int rc = sky_create_table_message_process(server, header, NULL, input, output);
    mu_assert_int_equals(rc, 0);
    
    struct tagbstring foo_db_path = bsStatic("tmp/foo");
    struct tagbstring foo_db_0_path = bsStatic("tmp/foo/0");
    struct tagbstring foo_db_9_path = bsStatic("tmp/foo/9");
    mu_assert_bool(sky_file_exists(&foo_db_path));
    mu_assert_bool(sky_file_exists(&foo_db_0_path));
    mu_assert_bool(sky_file_exists(&foo_db_9_path));
    mu_assert_file("tmp/output", "tests/fixtures/create_table_message/1/output");

    sky_message_header_free(header);
    sky_server_free(server);
    return 0;
}
Example #24
0
int test6 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b;
int ret = 0;

	printf ("TEST: bInsertChrs function.\n");
	ret += 0 <= bInsertChrs (&t, 6, 4, 'x', '?');
	ret += 0 >  bInsertChrs (b = bstrcpy (&t), 6, 4, 'x', '?');
	ret += 0 >= biseqcstr (b, "Hello xxxxworld");
	bdestroy (b);

	printf ("\t# failures: %d\n", ret);

	return ret;
}
Example #25
0
END_TEST

START_TEST(core_001)
{
	struct tagbstring t = bsStatic("Hello world");
	bstring b, c, d;
	int ret = 0;
	b = bTail(&t, 5);
	ck_assert(b != NULL);
	c = bHead(&t, 5);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "Hello");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	b = bTail(&t, 0);
	ck_assert(b != NULL);
	c = bHead(&t, 0);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	d = bstrcpy(&t);
	ck_assert(d != NULL);
	b = bTail(d, 5);
	ck_assert(b != NULL);
	c = bHead(d, 5);
	ck_assert(c != NULL);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	ret = biseqcstr(c, "Hello");
	ck_assert_int_eq(ret, 1);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(c);
	ck_assert_int_eq(ret, BSTR_OK);
	ret = bdestroy(d);
	ck_assert_int_eq(ret, BSTR_OK);
}
Example #26
0
int test_sky_action_file_load() {
    int rc;
    struct tagbstring path = bsStatic("tests/fixtures/action_files/0");
    
    // Initialize and load action file.
    sky_action_file *action_file = sky_action_file_create();
    sky_action_file_set_path(action_file, &path);
    rc = sky_action_file_load(action_file);
    mu_assert_int_equals(rc, 0);

    // Assert actions.
    mu_assert_int_equals(action_file->action_count, 2);

    sky_action_file_free(action_file);
    return 0;
}
Example #27
0
int test2 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b;
int ret = 0, reto;

	printf ("TEST: bSetChar function.\n");
	ret += 0 <= bSetChar (&t, 4, ',');
	ret += 0 >  bSetChar (b = bstrcpy (&t), 4, ',');
	ret += 0 >= biseqcstr (b, "Hell, world");
	ret += 0 <= bSetChar (b, -1, 'x');
	b->slen = 2;
	ret += 0 >  bSetChar (b, 1, 'i');
	ret += 0 >= biseqcstr (b, "Hi");
	ret += 0 >  bSetChar (b, 2, 's');
	ret += 0 >= biseqcstr (b, "His");
	ret += 0 >  bSetChar (b, 1, '\0');
	ret += blength (b) != 3;
	ret += bchare (b, 0, '?') != 'H';
	ret += bchare (b, 1, '?') != '\0';
	ret += bchare (b, 2, '?') != 's';
	bdestroy (b);

	printf ("\t# failures: %d\n", ret);

	reto = ret;
	ret = 0;

	printf ("TEST: bSetCstrChar function.\n");
	ret += 0 <= bSetCstrChar (&t, 4, ',');
	ret += 0 >  bSetCstrChar (b = bstrcpy (&t), 4, ',');
	ret += 0 >= biseqcstr (b, "Hell, world");
	ret += 0 <= bSetCstrChar (b, -1, 'x');
	b->slen = 2;
	ret += 0 >  bSetCstrChar (b, 1, 'i');
	ret += 0 >= biseqcstr (b, "Hi");
	ret += 0 >  bSetCstrChar (b, 2, 's');
	ret += 0 >= biseqcstr (b, "His");
	ret += 0 >  bSetCstrChar (b, 1, '\0');
	ret += blength (b) != 1;
	ret += bchare (b, 0, '?') != 'H';
	bdestroy (b);

	printf ("\t# failures: %d\n", ret);

	return reto + ret;
}
Example #28
0
int Server_load(tst_t *settings, Value *val)
{
    CONFIRM_TYPE("Server");
    Class *cls = val->as.cls;
    int rc = 0;
    char *sql = NULL;
    struct tagbstring HOSTS_VAR = bsStatic("hosts");
    const char *bind_addr = NULL;

    if(tst_search(cls->params, bdata(&BIND_ADDR), blength(&BIND_ADDR))) {
        bind_addr = AST_str(settings, cls->params, "bind_addr", VAL_QSTRING);
    } else {
        bind_addr = "0.0.0.0";
    }

    sql = sqlite3_mprintf(bdata(&SERVER_SQL),
            AST_str(settings, cls->params, "uuid", VAL_QSTRING),
            AST_str(settings, cls->params, "access_log", VAL_QSTRING),
            AST_str(settings, cls->params, "error_log", VAL_QSTRING),
            AST_str(settings, cls->params, "pid_file", VAL_QSTRING),
            AST_str(settings, cls->params, "chroot", VAL_QSTRING),
            AST_str(settings, cls->params, "default_host", VAL_QSTRING),
            AST_str(settings, cls->params, "name", VAL_QSTRING),
            bind_addr,
            AST_str(settings, cls->params, "port", VAL_NUMBER));

    rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to exec SQL: %s", sql);

    cls->id = SERVER_ID = DB_lastid();

    Value *hosts = AST_get(settings, cls->params, &HOSTS_VAR, VAL_LIST);
    check(hosts != NULL, "Could not find Server.hosts setting in host %s:%s", 
            AST_str(settings, cls->params, "uuid", VAL_QSTRING),
            AST_str(settings, cls->params, "name", VAL_QSTRING));

    AST_walk_list(settings, hosts->as.list, Host_load);


    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Example #29
0
int test8 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b;
char * c;
int ret = 0;

	printf ("TEST: NetStr functions.\n");
	c = bStr2NetStr (&t);
	ret += 0 != strcmp (c, "11:Hello world,");
	b = bNetStr2Bstr (c);
	ret += 0 >= biseq (b, &t);
	bdestroy (b);
	bcstrfree (c);

	printf ("\t# failures: %d\n", ret);

	return ret;
}
Example #30
0
int test5 (void) {
struct tagbstring t = bsStatic ("Hello world");
bstring b;
int ret = 0;

	printf ("TEST: bReverse function.\n");
	ret += 0 <= bReverse (&t);
	ret += 0 >  bReverse (b = bstrcpy (&t));
	ret += 0 >= biseqcstr (b, "dlrow olleH");
	b->slen = 0;
	ret += 0 >  bReverse (b);
	ret += 0 >= biseqcstr (b, "");
	bdestroy (b);

	printf ("\t# failures: %d\n", ret);

	return ret;
}