Exemple #1
0
static void create_file(char **buffer, unsigned int *size)
{
    OSyncFileFormat *outfile = osync_try_malloc0(sizeof(OSyncFileFormat), NULL);

    outfile->path = osync_rand_str(g_random_int_range(1, 100));

    outfile->data = osync_rand_str(g_random_int_range(1, 100));
    outfile->size = strlen(outfile->data);

    *buffer = (char *)outfile;
    *size = sizeof(OSyncFileFormat);
}
void add_data(OSyncMember *member, const char *objtype)
{
	OSyncChange *change = osync_change_new();
	if (!osync_member_make_random_data(member, change, objtype)) {
		printf("Unable to create random data\n");
		abort();
	}

	char *filename = NULL;
	while (1) {
		char *randstr = osync_rand_str(8);
		filename = g_strdup_printf("%s/%s", localdir, randstr);
		g_free(randstr);
		char *command = g_strdup_printf("ls %s &> /dev/null", filename);
		int ret = system(command);
		g_free(command);
		if (ret)
			break;
		g_free(filename);
	}

	OSyncError *error = NULL;
	if (!osync_file_write(filename, osync_change_get_data(change), osync_change_get_datasize(change), 0700, &error)) {
		printf("Unable to write to file %s\n", osync_error_print(&error));
		abort();
	}
	g_free(filename);
}
static osync_bool create_file(char **buffer, unsigned int *size, void *user_data, OSyncError **error)
{
	OSyncFileFormat *outfile = osync_try_malloc0(sizeof(OSyncFileFormat), NULL);
	
	outfile->path = osync_rand_str(g_random_int_range(1, 100), error);
	osync_assert(*error == NULL);
	
	outfile->data = osync_rand_str(g_random_int_range(1, 100), error);
	osync_assert(*error == NULL);
	outfile->size = strlen(outfile->data);
	
	*buffer = (char *)outfile;
	*size = sizeof(OSyncFileFormat);

	return TRUE;
}
Exemple #4
0
void create_random_file(const char *path)
{
	OSyncError *error = NULL;
	char *content = osync_rand_str(g_random_int_range(100, 200), &error);
	fail_unless(error == NULL, NULL);
	osync_assert(osync_file_write(path, content, strlen(content), 0700, NULL) == TRUE);
	g_free(content);
}
Exemple #5
0
static osync_bool conv_plain_to_file(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error)
{
    osync_trace(TRACE_INTERNAL, "Converting plain to file");

    *free_input = FALSE;
    OSyncFileFormat *file = osync_try_malloc0(sizeof(OSyncFileFormat), error);
    osync_assert(file);

    file->path = osync_rand_str(g_random_int_range(1, 100));

    file->data = input;
    file->size = inpsize - 1;

    *output = (char *)file;
    *outpsize = sizeof(OSyncFileFormat);
    return TRUE;
}
Exemple #6
0
static osync_bool conv_plain_to_file(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error)
{
	OSyncFileFormat *file = NULL;
	osync_trace(TRACE_INTERNAL, "Converting plain to file");

	*free_input = FALSE;
	file = osync_try_malloc0(sizeof(OSyncFileFormat), error);
	if (!file)
		goto error;

	file->path = osync_rand_str(100, error);
	if (!file->path)
		goto error;

	file->data = input;
	file->size = inpsize - 1;

	*output = (char *)file;
	*outpsize = sizeof(OSyncFileFormat);
	return TRUE;

error:
	return FALSE;
}
END_TEST

START_TEST (hashtable_reload)
{
	OSyncError *error = NULL;
	char *testbed = setup_testbed(NULL);

	osync_bool new_hashtable = FALSE;

	reset_hashtable_counters();

	char *hashpath = g_strdup_printf("%s%chashtable.db", testbed, G_DIR_SEPARATOR);
	OSyncHashTable *table = osync_hashtable_new(hashpath, "contact", &new_hashtable, &error);
	fail_unless(!error, NULL);
	fail_unless(table != NULL, NULL);
	fail_unless(new_hashtable != FALSE, NULL); /* Expecting a new/fresh hastable */

	/***** load */
	fail_unless(osync_hashtable_load(table, &error), NULL);

	OSyncChange *fakechange = osync_change_new(&error);

	osync_change_set_uid(fakechange, "test1");

	char *rndhash = osync_rand_str(g_random_int_range(100, 200), &error);
	osync_assert(error == NULL);

	osync_change_set_hash(fakechange, rndhash);
	osync_change_set_changetype(fakechange, OSYNC_CHANGE_TYPE_ADDED);

	osync_hashtable_update_change(table, fakechange);
	osync_change_unref(fakechange);

	/*** store - commit hashtable */
	fail_unless(osync_hashtable_save(table, &error), NULL);
	fail_unless(!error, NULL);

	osync_hashtable_unref(table);
	table = NULL;

	/** reload the hashtable */
	OSyncHashTable *newtable = osync_hashtable_new(hashpath, "contact", &new_hashtable, &error);
	fail_unless(!error, NULL);
	fail_unless(newtable != NULL, NULL);
	fail_unless(new_hashtable != TRUE, NULL); /* We expect here no new hashtable */

	/* 0 entries - since not loaded! */
	fail_unless(osync_hashtable_num_entries(newtable) == 0, NULL);

	/* load and count and compare hashs */
	fail_unless(osync_hashtable_load(newtable, &error), NULL);

	fail_unless(osync_hashtable_num_entries(newtable) == 1, NULL);

	const char *newhash = osync_hashtable_get_hash(newtable, "test1");
	fail_unless(newhash != NULL, NULL);
	fail_unless(!strcmp(newhash, rndhash), NULL);
	g_free(rndhash);


	g_free(hashpath);

	destroy_testbed(testbed);
}
int main (int argc, char *argv[])
{
	int i;
	char *pluginname = NULL;
	char *plugindir = NULL;
	char *configfile = NULL;
	char *objtype = NULL;
	OSyncError *error = NULL;

	if (argc < 2)
		usage (argv[0], 1);

	pluginname = argv[1];
	for (i = 2; i < argc; i++) {
		char *arg = argv[i];
		if (!strcmp (arg, "--config")) {
			configfile = argv[i + 1];
			i++;
			if (!configfile)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--type")) {
			objtype = argv[i + 1];
			i++;
			if (!objtype)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--plugindir")) {
			printf("plugindir %s\n", argv[i + 1]);
			plugindir = argv[i + 1];
			i++;
			if (!plugindir)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--random")) {
			only_random = TRUE;
		} else if (!strcmp (arg, "--help")) {
			usage (argv[0], 0);
		} else {
			usage (argv[0], 1);
		}
	}

	OSyncEnv *env = osync_env_new(NULL);
	osync_env_set_option(env, "LOAD_GROUPS", "FALSE");

	if (plugindir)
		osync_env_set_option(env, "PLUGINS_DIRECTORY", plugindir);

	if (!osync_env_initialize(env, &error)) {
		printf("Unable to initialize environment: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		return 1;
	}

	char *testdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
	char *result = mkdtemp(testdir);

	if (result == NULL)
	{
		osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s", g_strerror(errno));
		return 1;
	}

	OSyncGroup *group = osync_group_new(env);
	osync_group_set_name(group, osync_rand_str(8));
	osync_group_set_configdir(group, testdir);
	OSyncMember *member = osync_member_new(group);

	char *config = NULL;
	int size = 0;
	if (configfile) {
		if (!osync_file_read(configfile, &config, &size, &error)) {
			fprintf(stderr, "Unable to read config: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			return 1;
		}
		osync_member_set_config(member, config, size);
	}

	osync_member_set_pluginname(member, pluginname);

	OSyncMember *file = osync_member_new(group);

	localdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
	result = mkdtemp(localdir);

	if (result == NULL)
	{
		osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s", g_strerror(errno));
		return 1;
	}

	config = g_strdup_printf("<config><path>%s</path><recursive>0</recursive></config>", localdir);
	osync_member_set_config(file, config, strlen(config) + 1);
	osync_member_set_pluginname(file, "file-sync");

	if (!osync_group_save(group, &error)) {
		printf("Error while creating syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_env;
	}

	if (!g_thread_supported ()) g_thread_init (NULL);

	OSyncEngine *engine = osengine_new(group, &error);
	if (!engine) {
		printf("Error while creating syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_env;
	}

	if (!osengine_init(engine, &error)) {
		printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_engine;
	}

	int count = 0;
	if (only_random) {
		do {
			count++;
			printf("++++++++++++++++++++++++++++++\n");
			printf("Initializing new round #%i!\n", count);

			if (g_random_int_range(0, 5) == 0) {
				int i;
				OSyncFormatEnv *env = osync_group_get_format_env(group);
				for (i = 0; i < osync_conv_num_objtypes(env); i++) {
					if (g_random_int_range(0, 5) == 0) {
						OSyncObjType *type = osync_conv_nth_objtype(env, i);
						osync_group_set_slow_sync(group, osync_objtype_get_name(type), TRUE);
						printf("Requesting slow-sync for: %s\n", osync_objtype_get_name(type));
					}
				}
				osync_conv_env_free(env);
			}

			change_content();

			check_sync(engine, "Random", 1);
		} while (g_random_int_range(0, 3) != 0);

		printf("Finalizing engine\n");
		osengine_finalize(engine);
		osengine_free(engine);

		engine = osengine_new(group, &error);
		if (!engine) {
			printf("Error while creating syncengine: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			goto error_free_env;
		}

		if (!osengine_init(engine, &error)) {
			printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			goto error_free_engine;
		}
	} else {
		register_tests();
		run_all_tests(engine, file, member, objtype);
	}

	printf("\nCompleted successfully\n");
	return 0;

error_free_engine:
	osengine_free(engine);
error_free_env:
	osync_group_free(group);
	osync_env_free(env);
	return 1;
}