5
#define FILE_SIZE 1024 * 1024 + 12345
6
#define BUFFER_SIZE 64 * 1024
8
gchar *write_md5 = NULL;
11
test_func_sync_gridfs_stream_write (void)
13
mongo_sync_connection *conn;
14
mongo_sync_gridfs *gfs;
15
mongo_sync_gridfs_stream *stream;
20
gboolean write_ok = TRUE;
23
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
24
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
26
stream = mongo_sync_gridfs_stream_new (gfs, NULL);
28
"mongo_sync_gridfs_stream_new() fails without mongo_util_oid_init()");
30
mongo_util_oid_init (0);
32
oid = mongo_util_oid_new (1);
33
meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
34
BSON_TYPE_OID, "_id", oid,
39
stream = mongo_sync_gridfs_stream_new (gfs, meta);
41
"mongo_sync_gridfs_stream_new() works");
44
data = g_malloc (BUFFER_SIZE);
46
chk = g_checksum_new (G_CHECKSUM_MD5);
48
while (pos < FILE_SIZE)
50
gint csize = BUFFER_SIZE;
52
if (csize + pos > FILE_SIZE)
53
csize = FILE_SIZE - pos;
55
memset (data, filler++, BUFFER_SIZE);
57
g_checksum_update (chk, data, csize);
59
write_ok &= mongo_sync_gridfs_stream_write (stream, data, csize);
63
"All stream_write()s succeeded");
65
write_md5 = g_strdup (g_checksum_get_string (chk));
66
g_checksum_free (chk);
68
note ("File MD5: %s\n", write_md5);
71
ok (mongo_sync_gridfs_stream_close (stream) == TRUE,
72
"mongo_sync_gridfs_stream_close() works");
74
mongo_sync_gridfs_free (gfs, TRUE);
78
test_func_sync_gridfs_stream_write_invalid (void)
80
mongo_sync_connection *conn;
81
mongo_sync_gridfs *gfs;
82
mongo_sync_gridfs_stream *stream;
86
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
87
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
88
ns = g_strconcat (config.gfs_prefix, ".files", NULL);
90
/* Try to write a file with a custom, non-OID _id */
91
meta = bson_build (BSON_TYPE_STRING, "filename", "lmc-invalid-id", -1,
92
BSON_TYPE_STRING, "_id", "Short and stout", -1,
96
stream = mongo_sync_gridfs_stream_new (gfs, meta);
98
"mongo_sync_gridfs_stream_new() should fail if meta has an invalid _id");
101
/* Write a file with a non-OID _id, bypassing the GridFS API. */
102
meta = bson_build (BSON_TYPE_STRING, "_id", "Short and stout", -1,
103
BSON_TYPE_STRING, "my-id", "stream:string-id", -1,
107
mongo_sync_cmd_insert (conn, ns, meta, NULL);
110
/* Insert metadata with invalid length type. */
111
meta = bson_build (BSON_TYPE_DOUBLE, "length", 1.0,
112
BSON_TYPE_STRING, "my-id", "stream:invalid-length", -1,
116
mongo_sync_cmd_insert (conn, ns, meta, NULL);
119
/* Insert metadata with invalid chunkSize type. */
120
meta = bson_build (BSON_TYPE_INT32, "length", 10,
121
BSON_TYPE_DOUBLE, "chunkSize", 12.5,
122
BSON_TYPE_STRING, "my-id", "stream:invalid-chunkSize", -1,
126
mongo_sync_cmd_insert (conn, ns, meta, NULL);
129
/* Insert a valid metadata, without chunks. */
130
meta = bson_build (BSON_TYPE_INT32, "length", 32,
131
BSON_TYPE_INT32, "chunkSize", 12,
132
BSON_TYPE_UTC_DATETIME, "uploadDate", (gint64)1234,
133
BSON_TYPE_STRING, "md5", "deadbeef", -1,
134
BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
138
mongo_sync_cmd_insert (conn, ns, meta, NULL);
141
mongo_sync_gridfs_free (gfs, TRUE);
145
test_func_sync_gridfs_stream_read (void)
147
mongo_sync_connection *conn;
148
mongo_sync_gridfs *gfs;
149
mongo_sync_gridfs_stream *stream;
156
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
157
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
158
meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
162
stream = mongo_sync_gridfs_stream_find (gfs, meta);
164
"mongo_sync_gridfs_stream_find() works");
167
chk = g_checksum_new (G_CHECKSUM_MD5);
169
while (pos < FILE_SIZE)
173
r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
177
g_checksum_update (chk, data, r);
181
cmp_ok (pos, "==", FILE_SIZE,
182
"mongo_sync_gridfs_stream_read() works");
183
is (g_checksum_get_string (chk), write_md5,
186
g_checksum_free (chk);
187
ok (mongo_sync_gridfs_stream_close (stream) == TRUE,
188
"mongo_sync_gridfs_stream_close() works");
189
mongo_sync_gridfs_free (gfs, TRUE);
193
test_func_sync_gridfs_stream_meta (void)
195
mongo_sync_connection *conn;
196
mongo_sync_gridfs *gfs;
197
mongo_sync_gridfs_stream *stream;
201
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
202
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
203
meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
207
stream = mongo_sync_gridfs_stream_find (gfs, meta);
210
id = mongo_sync_gridfs_file_get_id (stream);
212
"mongo_sync_gridfs_file_get_id() works on streams");
214
ok (mongo_sync_gridfs_file_get_md5 (stream) == NULL,
215
"mongo_sync_gridfs_file_get_md5() fails on streams");
216
ok (mongo_sync_gridfs_file_get_date (stream) == -1,
217
"mongo_sync_gridfs_file_get_date() fails on streams");
218
ok (mongo_sync_gridfs_file_get_metadata (stream) == NULL,
219
"mongo_sync_gridfs_file_get_metadata() fails on streams");
221
mongo_sync_gridfs_stream_close (stream);
223
mongo_sync_gridfs_free (gfs, TRUE);
227
test_func_sync_gridfs_stream_read_invalid (void)
229
mongo_sync_connection *conn;
230
mongo_sync_gridfs *gfs;
231
mongo_sync_gridfs_stream *stream;
236
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
237
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
240
meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:string-id", -1,
244
stream = mongo_sync_gridfs_stream_find (gfs, meta);
246
"mongo_sync_gridfs_stream_find() should fail if _id is non-OID");
250
meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-length", -1,
254
stream = mongo_sync_gridfs_stream_find (gfs, meta);
256
"mongo_sync_gridfs_stream_find() should fail with invalid metadata");
260
meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-chunkSize", -1,
264
stream = mongo_sync_gridfs_stream_find (gfs, meta);
266
"mongo_sync_gridfs_stream_find() should fail with invalid metadata");
270
meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
274
stream = mongo_sync_gridfs_stream_find (gfs, meta);
276
"mongo_sync_gridfs_stream_find() works [stream:no-chunks]");
279
r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
281
"Reading from a chunk-less file should fail");
283
mongo_sync_gridfs_stream_close (stream);
285
mongo_sync_gridfs_free (gfs, TRUE);
289
test_func_sync_gridfs_stream_seek (void)
291
mongo_sync_connection *conn;
292
mongo_sync_gridfs *gfs;
293
mongo_sync_gridfs_stream *stream;
295
guint8 *chunk1, *chunk2, *chunk3;
297
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
298
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
299
meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
303
stream = mongo_sync_gridfs_stream_find (gfs, meta);
306
chunk1 = g_malloc (300 * 1024);
307
chunk2 = g_malloc (300 * 1024);
308
chunk3 = g_malloc (300 * 1024);
310
cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk1, 300 * 1024), "==",
312
"reading the first chunk works");
313
cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk2, 300 * 1024), "==",
315
"reading the second chunk works");
316
ok (memcmp (chunk1, chunk2, 300 * 1024) != 0,
317
"The two chunks differ, as they should");
319
ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_END) == TRUE,
320
"mongo_sync_gridfs_stream_seek() works, with SEEK_END");
321
cmp_ok (stream->file.offset, "==", stream->file.length,
322
"mongo_sync_gridfs_stream_seek() can seek to the end");
324
ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == TRUE,
325
"mongo_sync_gridfs_stream_seek() works, with SEEK_SET");
326
cmp_ok (stream->file.offset, "==", 1,
327
"mongo_sync_gridfs_stream_seek()'s SEEK_SET works");
328
ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == TRUE,
329
"mongo_sync_gridfs_stream_seek() works, with SEEK_SET");
331
ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_CUR) == TRUE,
332
"mongo_sync_gridfs_stream_seek() works, with SEEK_CUR");
333
cmp_ok (stream->file.offset, "==", 0,
334
"mongo_sync_gridfs_stream_seek()'s SEEK_CUR works");
335
ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_CUR) == TRUE,
336
"mongo_sync_gridfs_stream_seek() works, with SEEK_CUR");
338
cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk3, 300 * 1024), "==",
340
"reading after seeking works");
342
ok (memcmp (chunk1, chunk3, 300 * 1024) == 0,
343
"After seeking, we're at the beginning");
345
mongo_sync_gridfs_stream_close (stream);
350
mongo_sync_gridfs_free (gfs, TRUE);
354
test_func_sync_gridfs_stream_seek_invalid (void)
356
mongo_sync_connection *conn;
357
mongo_sync_gridfs *gfs;
358
mongo_sync_gridfs_stream *stream;
361
conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
362
gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
363
meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
367
stream = mongo_sync_gridfs_stream_find (gfs, meta);
370
ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == FALSE,
371
"mongo_sync_gridfs_stream_seek() should fail with no chunks");
373
mongo_sync_gridfs_stream_close (stream);
375
mongo_sync_gridfs_free (gfs, TRUE);
379
test_func_sync_gridfs_stream (void)
381
test_func_sync_gridfs_stream_write ();
382
test_func_sync_gridfs_stream_write_invalid ();
383
test_func_sync_gridfs_stream_read ();
384
test_func_sync_gridfs_stream_read_invalid ();
385
test_func_sync_gridfs_stream_seek ();
386
test_func_sync_gridfs_stream_seek_invalid ();
387
test_func_sync_gridfs_stream_meta ();
392
RUN_NET_TEST (32, func_sync_gridfs_stream);