| 5 | | #include <opensync/opensync_internals.h> |
|---|
| 6 | | #include <stdlib.h> |
|---|
| 7 | | |
|---|
| 8 | | START_TEST (user_create) |
|---|
| 9 | | { |
|---|
| 10 | | OSyncUserInfo *osuser = _osync_user_new(); |
|---|
| 11 | | fail_unless(osuser != NULL, "osuser == NULL on creation"); |
|---|
| 12 | | char *confdir = _osync_user_get_confdir(osuser); |
|---|
| 13 | | fail_unless(confdir != NULL, "confdir == NULL on creation"); |
|---|
| | 4 | #include <opensync/opensync-helper.h> |
|---|
| | 5 | |
|---|
| | 6 | |
|---|
| | 7 | static int count_entries = 0; |
|---|
| | 8 | |
|---|
| | 9 | static void reset_hashtable_counters() |
|---|
| | 10 | { |
|---|
| | 11 | count_entries = 0; |
|---|
| | 12 | } |
|---|
| | 13 | |
|---|
| | 14 | void foreach_hash(const char *uid, const char *hash, void *data) |
|---|
| | 15 | { |
|---|
| | 16 | count_entries++; |
|---|
| | 17 | } |
|---|
| | 18 | |
|---|
| | 19 | START_TEST (hashtable_new) |
|---|
| | 20 | { |
|---|
| | 21 | OSyncError *error = NULL; |
|---|
| | 22 | char *testbed = setup_testbed(NULL); |
|---|
| | 23 | |
|---|
| | 24 | reset_hashtable_counters(); |
|---|
| | 25 | |
|---|
| | 26 | char *hashpath = g_strdup_printf("%s%chashtable.db", testbed, G_DIR_SEPARATOR); |
|---|
| | 27 | OSyncHashTable *table = osync_hashtable_new(hashpath, "contact", &error); |
|---|
| | 28 | g_free(hashpath); |
|---|
| | 29 | fail_unless(!error, NULL); |
|---|
| | 30 | fail_unless(table != NULL, NULL); |
|---|
| | 31 | |
|---|
| | 32 | /***** load */ |
|---|
| | 33 | fail_unless(osync_hashtable_load(table, &error), NULL); |
|---|
| | 34 | |
|---|
| | 35 | /* check for empty hashtable */ |
|---|
| | 36 | fail_unless(osync_hashtable_num_entries(table) == 0, NULL); |
|---|
| | 37 | |
|---|
| | 38 | /* search non exisiting entry */ |
|---|
| | 39 | fail_unless(osync_hashtable_get_hash(table, "doesntexist") == NULL, NULL); |
|---|
| | 40 | |
|---|
| | 41 | /* No hash, no call of foreach_hash() */ |
|---|
| | 42 | osync_hashtable_foreach(table, foreach_hash, NULL); |
|---|
| | 43 | fail_unless(count_entries == 0, NULL); |
|---|
| | 44 | |
|---|
| | 45 | |
|---|
| | 46 | /* No hashs, no deleted entries */ |
|---|
| | 47 | fail_unless(osync_hashtable_get_deleted(table) == NULL, NULL); |
|---|
| | 48 | |
|---|
| | 49 | /* Request slowsync, even if it's empty.. e.g. first sync. */ |
|---|
| | 50 | fail_unless(osync_hashtable_slowsync(table, &error), NULL); |
|---|
| | 51 | fail_unless(!error, NULL); |
|---|
| | 52 | |
|---|
| | 53 | /* committed all - first sync without any entries ... */ |
|---|
| | 54 | fail_unless(osync_hashtable_save(table, &error), NULL); |
|---|
| | 55 | fail_unless(!error, NULL); |
|---|
| | 56 | |
|---|
| | 57 | /* ref and unref */ |
|---|
| | 58 | fail_unless(osync_hashtable_ref(table) == table, NULL); |
|---|
| | 59 | osync_hashtable_unref(table); |
|---|
| | 60 | |
|---|
| | 61 | osync_hashtable_unref(table); |
|---|
| | 62 | |
|---|
| | 63 | destroy_testbed(testbed); |
|---|
| 17 | | START_TEST (user_confdir) |
|---|
| 18 | | { |
|---|
| 19 | | OSyncUserInfo *osuser = _osync_user_new(); |
|---|
| 20 | | char *configdir = _osync_user_get_confdir(osuser); |
|---|
| 21 | | fail_unless(configdir != NULL, "configdir == NULL on creation"); |
|---|
| 22 | | _osync_user_set_confdir(osuser, "test"); |
|---|
| 23 | | configdir = _osync_user_get_confdir(osuser); |
|---|
| 24 | | if (g_ascii_strcasecmp (configdir, "test") != 0) |
|---|
| 25 | | fail("configpath == \"test\""); |
|---|
| | 67 | START_TEST (hashtable_reload) |
|---|
| | 68 | { |
|---|
| | 69 | OSyncError *error = NULL; |
|---|
| | 70 | char *testbed = setup_testbed(NULL); |
|---|
| | 71 | |
|---|
| | 72 | reset_hashtable_counters(); |
|---|
| | 73 | |
|---|
| | 74 | char *hashpath = g_strdup_printf("%s%chashtable.db", testbed, G_DIR_SEPARATOR); |
|---|
| | 75 | OSyncHashTable *table = osync_hashtable_new(hashpath, "contact", &error); |
|---|
| | 76 | fail_unless(!error, NULL); |
|---|
| | 77 | fail_unless(table != NULL, NULL); |
|---|
| | 78 | |
|---|
| | 79 | /***** load */ |
|---|
| | 80 | fail_unless(osync_hashtable_load(table, &error), NULL); |
|---|
| | 81 | |
|---|
| | 82 | OSyncChange *fakechange = osync_change_new(&error); |
|---|
| | 83 | |
|---|
| | 84 | osync_change_set_uid(fakechange, "test1"); |
|---|
| | 85 | |
|---|
| | 86 | char *rndhash = osync_rand_str(g_random_int_range(100, 200)); |
|---|
| | 87 | |
|---|
| | 88 | osync_change_set_hash(fakechange, rndhash); |
|---|
| | 89 | osync_change_set_changetype(fakechange, OSYNC_CHANGE_TYPE_ADDED); |
|---|
| | 90 | |
|---|
| | 91 | osync_hashtable_update_change(table, fakechange); |
|---|
| | 92 | osync_change_unref(fakechange); |
|---|
| | 93 | |
|---|
| | 94 | /*** store - commit hashtable */ |
|---|
| | 95 | fail_unless(osync_hashtable_save(table, &error), NULL); |
|---|
| | 96 | fail_unless(!error, NULL); |
|---|
| | 97 | |
|---|
| | 98 | osync_hashtable_unref(table); |
|---|
| | 99 | table = NULL; |
|---|
| | 100 | |
|---|
| | 101 | /** reload the hashtable */ |
|---|
| | 102 | OSyncHashTable *newtable = osync_hashtable_new(hashpath, "contact", &error); |
|---|
| | 103 | fail_unless(!error, NULL); |
|---|
| | 104 | fail_unless(newtable != NULL, NULL); |
|---|
| | 105 | |
|---|
| | 106 | /* 0 entries - since not loaded! */ |
|---|
| | 107 | fail_unless(osync_hashtable_num_entries(newtable) == 0, NULL); |
|---|
| | 108 | |
|---|
| | 109 | /* load and count and compare hashs */ |
|---|
| | 110 | fail_unless(osync_hashtable_load(newtable, &error), NULL); |
|---|
| | 111 | |
|---|
| | 112 | fail_unless(osync_hashtable_num_entries(newtable) == 1, NULL); |
|---|
| | 113 | |
|---|
| | 114 | const char *newhash = osync_hashtable_get_hash(newtable, "test1"); |
|---|
| | 115 | fail_unless(newhash != NULL, NULL); |
|---|
| | 116 | fail_unless(!strcmp(newhash, rndhash), NULL); |
|---|
| | 117 | g_free(rndhash); |
|---|
| | 118 | |
|---|
| | 119 | |
|---|
| | 120 | g_free(hashpath); |
|---|
| | 121 | |
|---|
| | 122 | destroy_testbed(testbed); |
|---|
| 29 | | START_TEST (user_null_configdir) |
|---|
| 30 | | { |
|---|
| 31 | | char *configpath = _osync_user_get_confdir(NULL); |
|---|
| 32 | | fail_unless(configpath == NULL, "configpath != NULL on creation"); |
|---|
| | 126 | void compare_uid_hash(const char *uid, const char *hash, void *data) |
|---|
| | 127 | { |
|---|
| | 128 | fail_unless(!strcmp(uid, hash), NULL); |
|---|
| | 129 | } |
|---|
| | 130 | |
|---|
| | 131 | START_TEST (hashtable_stress) |
|---|
| | 132 | { |
|---|
| | 133 | OSyncError *error = NULL; |
|---|
| | 134 | char *testbed = setup_testbed(NULL); |
|---|
| | 135 | |
|---|
| | 136 | reset_hashtable_counters(); |
|---|
| | 137 | |
|---|
| | 138 | char *hashpath = g_strdup_printf("%s%chashtable.db", testbed, G_DIR_SEPARATOR); |
|---|
| | 139 | OSyncHashTable *table = osync_hashtable_new(hashpath, "contact", &error); |
|---|
| | 140 | fail_unless(!error, NULL); |
|---|
| | 141 | fail_unless(table != NULL, NULL); |
|---|
| | 142 | |
|---|
| | 143 | /***** load */ |
|---|
| | 144 | fail_unless(osync_hashtable_load(table, &error), NULL); |
|---|
| | 145 | |
|---|
| | 146 | /* commit 10k changes with uniques values for UID. And this value also |
|---|
| | 147 | also as HASH. So we can validate the result with UID == HASH. */ |
|---|
| | 148 | unsigned int i = 0; |
|---|
| | 149 | unsigned int NUMENTRIES = 10000; |
|---|
| | 150 | for (i=0; i < NUMENTRIES; i++) { |
|---|
| | 151 | char *value = g_strdup_printf("%u", i); |
|---|
| | 152 | OSyncChange *fakechange = osync_change_new(&error); |
|---|
| | 153 | |
|---|
| | 154 | /* UID == HASH */ |
|---|
| | 155 | osync_change_set_uid(fakechange, value); |
|---|
| | 156 | osync_change_set_hash(fakechange, value); |
|---|
| | 157 | |
|---|
| | 158 | osync_change_set_changetype(fakechange, OSYNC_CHANGE_TYPE_ADDED); |
|---|
| | 159 | osync_hashtable_update_change(table, fakechange); |
|---|
| | 160 | |
|---|
| | 161 | osync_change_unref(fakechange); |
|---|
| | 162 | g_free(value); |
|---|
| | 163 | } |
|---|
| | 164 | |
|---|
| | 165 | /*** store - commit 10k hash entries to hashtable */ |
|---|
| | 166 | fail_unless(osync_hashtable_save(table, &error), NULL); |
|---|
| | 167 | fail_unless(!error, NULL); |
|---|
| | 168 | |
|---|
| | 169 | osync_hashtable_unref(table); |
|---|
| | 170 | table = NULL; |
|---|
| | 171 | |
|---|
| | 172 | /** reload the hashtable */ |
|---|
| | 173 | OSyncHashTable *newtable = osync_hashtable_new(hashpath, "contact", &error); |
|---|
| | 174 | fail_unless(!error, NULL); |
|---|
| | 175 | fail_unless(newtable != NULL, NULL); |
|---|
| | 176 | |
|---|
| | 177 | /* 0 entries - since not loaded! */ |
|---|
| | 178 | fail_unless(osync_hashtable_num_entries(newtable) == 0, NULL); |
|---|
| | 179 | |
|---|
| | 180 | /* load and count and compare hashs */ |
|---|
| | 181 | fail_unless(osync_hashtable_load(newtable, &error), NULL); |
|---|
| | 182 | |
|---|
| | 183 | fail_unless(osync_hashtable_num_entries(newtable) == NUMENTRIES, "Only %i of %i\n", osync_hashtable_num_entries(newtable), NUMENTRIES); |
|---|
| | 184 | |
|---|
| | 185 | osync_hashtable_foreach(newtable, compare_uid_hash, NULL); |
|---|
| | 186 | |
|---|
| | 187 | g_free(hashpath); |
|---|
| | 188 | |
|---|
| | 189 | destroy_testbed(testbed); |
|---|
| 52 | | Suite *s = suite_create("User"); |
|---|
| 53 | | TCase *tc_core = tcase_create("Core"); |
|---|
| 54 | | |
|---|
| 55 | | suite_add_tcase (s, tc_core); |
|---|
| 56 | | tcase_add_test(tc_core, user_create); |
|---|
| 57 | | tcase_add_test(tc_core, user_confdir); |
|---|
| 58 | | tcase_add_test(tc_core, user_null_configdir); |
|---|
| 59 | | tcase_add_test(tc_core, user_null_configdir2); |
|---|
| 60 | | tcase_add_test(tc_core, user_null_configdir3); |
|---|
| 61 | | return s; |
|---|
| | 195 | Suite *s = suite_create("Hashtable"); |
|---|
| | 196 | |
|---|
| | 197 | create_case(s, "hashtable_new", hashtable_new); |
|---|
| | 198 | create_case(s, "hashtable_reload", hashtable_reload); |
|---|
| | 199 | create_case(s, "hashtable_stress", hashtable_stress); |
|---|
| | 200 | |
|---|
| | 201 | return s; |
|---|