1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#include "support.h"
#include <time.h>
static void conv_vnote(const char *filename)
{
char *command = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s .", g_get_current_dir(), filename);
char *testbed = setup_testbed(NULL);
system(command);
g_free(command);
OSyncError *error = NULL;
OSyncEnv *env = init_env();
OSyncFormatEnv *conv_env = osync_conv_env_new(env);
fail_unless(conv_env != NULL, NULL);
char *buffer;
int size;
char *file = g_path_get_basename(filename);
fail_unless(osync_file_read(file, &buffer, &size, &error), NULL);
OSyncChange *change = osync_change_new();
osync_change_set_uid(change, file);
osync_change_set_data(change, buffer, size + 1, TRUE);
osync_change_set_conv_env(change, conv_env);
osync_change_set_objformat_string(change, "plain");
OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, change, &error);
fail_unless(sourceformat != NULL, NULL);
osync_change_set_objformat(change, sourceformat);
osync_change_set_objtype(change, osync_objformat_get_objtype(sourceformat));
OSyncObjFormat *targetformat = osync_conv_find_objformat(conv_env, "xml-note");
fail_unless(targetformat != NULL, NULL);
OSyncChange *newchange = osync_change_copy(change, &error);
fail_unless(newchange != NULL, NULL);
//Convert to
fail_unless(osync_change_convert(conv_env, change, targetformat, &error), NULL);
//Compare old to new
fail_unless(osync_change_compare(newchange, change) == CONV_DATA_SAME, NULL);
//Convert back
fail_unless(osync_change_convert(conv_env, change, sourceformat, &error), NULL);
//Detect the output again
osync_change_set_objformat_string(change, "plain");
fail_unless(osync_change_detect_objformat(conv_env, change, &error) == sourceformat, NULL);
//Compare again
fail_unless(osync_change_compare(newchange, change) == CONV_DATA_SAME, NULL);
osync_conv_env_free(conv_env);
osync_env_finalize(env, NULL);
osync_env_free(env);
destroy_testbed(testbed);
}
static void compare_vnote(const char *lfilename, const char *rfilename, OSyncConvCmpResult result)
{
char *command1 = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s lfile", g_get_current_dir(), lfilename);
char *command2 = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s rfile", g_get_current_dir(), rfilename);
char *testbed = setup_testbed(NULL);
system(command1);
g_free(command1);
system(command2);
g_free(command2);
OSyncError *error = NULL;
OSyncEnv *env = init_env();
OSyncFormatEnv *conv_env = osync_conv_env_new(env);
fail_unless(conv_env != NULL, NULL);
char *buffer;
int size;
fail_unless(osync_file_read("lfile", &buffer, &size, &error), NULL);
OSyncChange *lchange = osync_change_new();
osync_change_set_uid(lchange, "lfile");
osync_change_set_data(lchange, buffer, size + 1, TRUE);
osync_change_set_conv_env(lchange, conv_env);
osync_change_set_objformat_string(lchange, "plain");
OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, lchange, &error);
fail_unless(sourceformat != NULL, NULL);
osync_change_set_objformat(lchange, sourceformat);
osync_change_set_objtype(lchange, osync_objformat_get_objtype(sourceformat));
fail_unless(osync_file_read("rfile", &buffer, &size, &error), NULL);
OSyncChange *rchange = osync_change_new();
osync_change_set_uid(rchange, "rfile");
osync_change_set_data(rchange, buffer, size + 1, TRUE);
osync_change_set_conv_env(rchange, conv_env);
osync_change_set_objformat_string(rchange, "plain");
sourceformat = osync_change_detect_objformat(conv_env, rchange, &error);
fail_unless(sourceformat != NULL, NULL);
osync_change_set_objformat(rchange, sourceformat);
osync_change_set_objtype(rchange, osync_objformat_get_objtype(sourceformat));
fail_unless(osync_change_compare(lchange, rchange) == result, NULL);
osync_conv_env_free(conv_env);
osync_env_finalize(env, NULL);
osync_env_free(env);
destroy_testbed(testbed);
}
static time_t vnote_get_revision(const char *filename)
{
char *command = g_strdup_printf("cp %s/"OPENSYNC_TESTDATA"%s .", g_get_current_dir(), filename);
char *testbed = setup_testbed(NULL);
system(command);
g_free(command);
OSyncError *error = NULL;
OSyncEnv *env = init_env();
OSyncFormatEnv *conv_env = osync_conv_env_new(env);
fail_unless(conv_env != NULL, NULL);
char *buffer;
int size;
char *file = g_path_get_basename(filename);
fail_unless(osync_file_read(file, &buffer, &size, &error), NULL);
OSyncChange *change = osync_change_new();
osync_change_set_uid(change, file);
g_free(file);
osync_change_set_data(change, buffer, size + 1, TRUE);
osync_change_set_conv_env(change, conv_env);
osync_change_set_objformat_string(change, "plain");
OSyncObjFormat *sourceformat = osync_change_detect_objformat(conv_env, change, &error);
fail_unless(sourceformat != NULL, NULL);
osync_change_set_objformat(change, sourceformat);
OSyncObjFormat *targetformat = osync_conv_find_objformat(conv_env, "xml-note");
fail_unless(targetformat != NULL, NULL);
fail_unless(osync_change_convert(conv_env, change, targetformat, &error), NULL);
time_t time = osync_change_get_revision(change, &error);
osync_conv_env_free(conv_env);
osync_env_finalize(env, NULL);
osync_env_free(env);
destroy_testbed(testbed);
return time;
}
START_TEST (conv_vnote1)
{
conv_vnote("data/vnotes/vnote1.vnt");
}
END_TEST
START_TEST (conv_vnote2)
{
conv_vnote("data/vnotes/vnote2.vnt");
}
END_TEST
START_TEST (conv_vnote3)
{
conv_vnote("data/vnotes/vnote3.vnt");
}
END_TEST
START_TEST (conv_vnote_minimal)
{
conv_vnote("data/vnotes/vnote-minimal.vnt");
}
END_TEST
START_TEST (get_revision1)
{
struct tm testtm = {0, 0, 0, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
fail_unless(vnote_get_revision("data/vnotes/vnote1.vnt") == mktime(&testtm), NULL);
}
END_TEST
START_TEST (get_revision2)
{
struct tm testtm = {1, 1, 1, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
fail_unless(vnote_get_revision("data/vnotes/vnote2.vnt") == mktime(&testtm), NULL);
}
END_TEST
START_TEST (get_revision3)
{
struct tm testtm = {0, 0, 0, 6, 4 - 1, 2005 - 1900, 0, 0, 0};
fail_unless(vnote_get_revision("data/vnotes/vnote3.vnt") == mktime(&testtm), NULL);
}
END_TEST
START_TEST (get_revision4)
{
fail_unless(vnote_get_revision("data/vnotes/vnote-minimal.vnt") == -1, NULL);
}
END_TEST
START_TEST (compare_vnote_same1)
{
compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1.vnt", CONV_DATA_SAME);
}
END_TEST
START_TEST (compare_vnote_same2)
{
compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1-same.vnt", CONV_DATA_SAME);
}
END_TEST
START_TEST (compare_vnote_similar1)
{
compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote1-similar.vnt", CONV_DATA_SIMILAR);
}
END_TEST
START_TEST (compare_vnote_mismatch1)
{
compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote2.vnt", CONV_DATA_MISMATCH);
}
END_TEST
START_TEST (compare_vnote_mismatch2)
{
compare_vnote("data/vnotes/vnote1.vnt", "data/vnotes/vnote-minimal.vnt", CONV_DATA_MISMATCH);
}
END_TEST
Suite *vnote_suite(void)
{
Suite *s = suite_create("VNote");
//Suite *s2 = suite_create("VNote");
create_case(s, "conv_vnote1", conv_vnote1);
create_case(s, "conv_vnote2", conv_vnote2);
create_case(s, "conv_vnote3", conv_vnote3);
create_case(s, "conv_vnote_minimal", conv_vnote_minimal);
create_case(s, "get_revision1", get_revision1);
create_case(s, "get_revision2", get_revision2);
create_case(s, "get_revision3", get_revision3);
create_case(s, "get_revision4", get_revision4);
create_case(s, "compare_vnote_same1", compare_vnote_same1);
create_case(s, "compare_vnote_same2", compare_vnote_same2);
create_case(s, "compare_vnote_similar1", compare_vnote_similar1);
create_case(s, "compare_vnote_mismatch1", compare_vnote_mismatch1);
create_case(s, "compare_vnote_mismatch2", compare_vnote_mismatch2);
return s;
}
int main(void)
{
int nf;
Suite *s = vnote_suite();
SRunner *sr;
sr = srunner_create(s);
// srunner_set_fork_status (sr, CK_NOFORK);
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|