コード例 #1
0
END_TEST

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

	OSyncGroup *group = osync_group_new(&error);
	fail_unless(group != NULL, NULL);
	fail_unless(error == NULL, NULL);
	
	osync_group_set_schemadir(group, testbed);
	osync_group_load(group, "configs/group", &error);
	fail_unless(error == NULL, osync_error_print(&error));
	fail_unless(group != NULL, NULL);
	mark_point();
	
	/* TODO filter member handling 
	OSyncMember *leftmember = osync_group_nth_member(group, 0);
	OSyncMember *rightmember = osync_group_nth_member(group, 1);
	*/
	OSyncCustomFilter *custom_filter = osync_custom_filter_new("mockobjtype3", "mockformat3", "mockformat1_custom_filter", dummy_filter_hook, &error);
	fail_unless(custom_filter != NULL, NULL);

	OSyncFilter *filter1 = osync_filter_new("mockobjtype1", OSYNC_FILTER_DENY, &error);
	OSyncFilter *filter2 = osync_filter_new("mockobjtype2", OSYNC_FILTER_ALLOW, &error);

	OSyncFilter *filter3 = osync_filter_new_custom(custom_filter, "test", OSYNC_FILTER_IGNORE, &error);
	
	fail_unless(filter1 != NULL, NULL);
	fail_unless(filter2 != NULL, NULL);
	fail_unless(filter3 != NULL, NULL);

	osync_group_add_filter(group, filter1);
	osync_group_add_filter(group, filter2);
	osync_group_add_filter(group, filter3);

	fail_unless(osync_group_num_filters(group) == 3, NULL);
	fail_unless(osync_group_nth_filter(group, 0) == filter1, NULL);
	fail_unless(osync_group_nth_filter(group, 1) == filter2, NULL);
	fail_unless(osync_group_nth_filter(group, 2) == filter3, NULL);
	
	mark_point();
	osync_group_save(group, NULL);
	mark_point();

	osync_group_load(group, "configs/group", NULL);
	fail_unless(group != NULL, NULL);
	mark_point();

	fail_unless(osync_group_num_filters(group) == 3, NULL);
	filter1 = osync_group_nth_filter(group, 0);
	fail_unless(filter1 != NULL, NULL);
	/* filter aren't member specific anymore. */
	//fail_unless(filter1->sourcememberid == 1, NULL);
	//fail_unless(filter1->destmemberid == 2, NULL);
	fail_unless(!strcmp(osync_filter_get_objtype(filter1), "mockobjtype1"), NULL);
	/* cross objtype syncing isn't supported anymore with 0.30 API */
	//fail_unless(!strcmp(filter1->destobjtype, "2"), NULL);
	//fail_unless(!strcmp(filter1->detectobjtype, "3"), NULL);
	fail_unless(filter1->action == OSYNC_FILTER_DENY, NULL);
	
	filter1 = osync_group_nth_filter(group, 1);
	fail_unless(filter1 != NULL, NULL);
	/* filter aren't member specific anymore. */
	//fail_unless(filter1->sourcememberid == 2, NULL);
	//fail_unless(filter1->destmemberid == 1, NULL);
	/* cross objtype syncing isn't supported anymore with 0.30 API */
	fail_unless(!strcmp(osync_filter_get_objtype(filter1), "mockobjtype2"), NULL);
	/* cross objtype syncing isn't supported anymore with 0.30 API */
	//fail_unless(!strcmp(filter1->destobjtype, "5"), NULL);
	//fail_unless(!strcmp(filter1->detectobjtype, "6"), NULL);
	fail_unless(filter1->action == OSYNC_FILTER_ALLOW, NULL);

	filter1 = osync_group_nth_filter(group, 2);
	fail_unless(filter1 != NULL, NULL);

	/* filter aren't member specific anymore. */
	//fail_unless(filter1->sourcememberid == 1, NULL);
	//fail_unless(filter1->destmemberid == 2, NULL);
	/* cross objtype syncing isn't supported anymore with 0.30 API */

	// FIXME - custom filter handling for objtype
	///fail_unless(!strcmp(osync_filter_get_objtype(filter1), "mockobjtype3"), NULL);

	/* cross objtype syncing isn't supported anymore with 0.30 API */
	//fail_unless(!strcmp(filter1->destobjtype, "8"), NULL);
	//fail_unless(!strcmp(filter1->detectobjtype, "9"), NULL);
	fail_unless(filter1->custom_filter != NULL, NULL);
	fail_unless(filter1->custom_filter->hook != NULL, NULL);
	fail_unless(!strcmp(osync_filter_get_config(filter1), "test"), NULL);

	osync_group_unref(group);

	destroy_testbed(testbed);
}
コード例 #2
0
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;
}