File: f_sync_gridfs_stream.c

package info (click to toggle)
libmongo-client 0.1.5-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,256 kB
  • sloc: ansic: 13,795; makefile: 297; perl: 137; sh: 41
file content (392 lines) | stat: -rw-r--r-- 11,497 bytes parent folder | download | duplicates (2)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "test.h"
#include "mongo.h"
#include "compat.h"

#define FILE_SIZE 1024 * 1024 + 12345
#define BUFFER_SIZE 64 * 1024

gchar *write_md5 = NULL;

void
test_func_sync_gridfs_stream_write (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  guint8 *data, *oid;
  gint pos = 0;
  gint filler = 0;
  gboolean write_ok = TRUE;
  GChecksum *chk;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  stream = mongo_sync_gridfs_stream_new (gfs, NULL);
  ok (stream == NULL,
      "mongo_sync_gridfs_stream_new() fails without mongo_util_oid_init()");

  mongo_util_oid_init (0);

  oid = mongo_util_oid_new (1);
  meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
		     BSON_TYPE_OID, "_id", oid,
		     BSON_TYPE_NONE);
  bson_finish (meta);
  g_free (oid);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_new() works");
  bson_free (meta);

  data = g_malloc (BUFFER_SIZE);

  chk = g_checksum_new (G_CHECKSUM_MD5);

  while (pos < FILE_SIZE)
    {
      gint csize = BUFFER_SIZE;

      if (csize + pos > FILE_SIZE)
	csize = FILE_SIZE - pos;

      memset (data, filler++, BUFFER_SIZE);

      g_checksum_update (chk, data, csize);

      write_ok &= mongo_sync_gridfs_stream_write (stream, data, csize);
      pos += csize;
    }
  ok (write_ok == TRUE,
      "All stream_write()s succeeded");

  write_md5 = g_strdup (g_checksum_get_string (chk));
  g_checksum_free (chk);

  note ("File MD5: %s\n", write_md5);

  g_free (data);
  ok (mongo_sync_gridfs_stream_close (stream) == TRUE,
      "mongo_sync_gridfs_stream_close() works");

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_write_invalid (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  gchar *ns;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  ns = g_strconcat (config.gfs_prefix, ".files", NULL);

  /* Try to write a file with a custom, non-OID _id */
  meta = bson_build (BSON_TYPE_STRING, "filename", "lmc-invalid-id", -1,
		     BSON_TYPE_STRING, "_id", "Short and stout", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_new (gfs, meta);
  ok (stream == NULL,
      "mongo_sync_gridfs_stream_new() should fail if meta has an invalid _id");
  bson_free (meta);

  /* Write a file with a non-OID _id, bypassing the GridFS API. */
  meta = bson_build (BSON_TYPE_STRING, "_id", "Short and stout", -1,
		     BSON_TYPE_STRING, "my-id", "stream:string-id", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  mongo_sync_cmd_insert (conn, ns, meta, NULL);
  bson_free (meta);

  /* Insert metadata with invalid length type. */
  meta = bson_build (BSON_TYPE_DOUBLE, "length", 1.0,
		     BSON_TYPE_STRING, "my-id", "stream:invalid-length", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  mongo_sync_cmd_insert (conn, ns, meta, NULL);
  bson_free (meta);

  /* Insert metadata with invalid chunkSize type. */
  meta = bson_build (BSON_TYPE_INT32, "length", 10,
		     BSON_TYPE_DOUBLE, "chunkSize", 12.5,
		     BSON_TYPE_STRING, "my-id", "stream:invalid-chunkSize", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  mongo_sync_cmd_insert (conn, ns, meta, NULL);
  bson_free (meta);

  /* Insert a valid metadata, without chunks. */
  meta = bson_build (BSON_TYPE_INT32, "length", 32,
		     BSON_TYPE_INT32, "chunkSize", 12,
		     BSON_TYPE_UTC_DATETIME, "uploadDate", (gint64)1234,
		     BSON_TYPE_STRING, "md5", "deadbeef", -1,
		     BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  mongo_sync_cmd_insert (conn, ns, meta, NULL);
  bson_free (meta);

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_read (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  guint8 data[12345];
  gint64 pos = 0;
  bson *meta;

  GChecksum *chk;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_find() works");
  bson_free (meta);

  chk = g_checksum_new (G_CHECKSUM_MD5);

  while (pos < FILE_SIZE)
    {
      gint64 r;

      r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
      if (r == -1)
	break;

      g_checksum_update (chk, data, r);
      pos += r;
    }

  cmp_ok (pos, "==", FILE_SIZE,
	  "mongo_sync_gridfs_stream_read() works");
  is (g_checksum_get_string (chk), write_md5,
      "md5sums match");

  g_checksum_free (chk);
  ok (mongo_sync_gridfs_stream_close (stream) == TRUE,
      "mongo_sync_gridfs_stream_close() works");
  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_meta (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  const guint8 *id;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  bson_free (meta);

  id = mongo_sync_gridfs_file_get_id (stream);
  ok (id != NULL,
      "mongo_sync_gridfs_file_get_id() works on streams");

  ok (mongo_sync_gridfs_file_get_md5 (stream) == NULL,
      "mongo_sync_gridfs_file_get_md5() fails on streams");
  ok (mongo_sync_gridfs_file_get_date (stream) == -1,
      "mongo_sync_gridfs_file_get_date() fails on streams");
  ok (mongo_sync_gridfs_file_get_metadata (stream) == NULL,
      "mongo_sync_gridfs_file_get_metadata() fails on streams");

  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_read_invalid (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  guint8 data[1245];
  gint64 r;
  bson *meta;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);

  /* ---- */
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:string-id", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  ok (stream == NULL,
      "mongo_sync_gridfs_stream_find() should fail if _id is non-OID");
  bson_free (meta);

  /* ---- */
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-length", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  ok (stream == NULL,
      "mongo_sync_gridfs_stream_find() should fail with invalid metadata");
  bson_free (meta);

  /* ---- */
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-chunkSize", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  ok (stream == NULL,
      "mongo_sync_gridfs_stream_find() should fail with invalid metadata");
  bson_free (meta);

  /* no-chunk test */
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  ok (stream != NULL,
      "mongo_sync_gridfs_stream_find() works [stream:no-chunks]");
  bson_free (meta);

  r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
  cmp_ok (r, "==", -1,
	  "Reading from a chunk-less file should fail");

  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_seek (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;
  guint8 *chunk1, *chunk2, *chunk3;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  meta = bson_build (BSON_TYPE_STRING, "filename", "libmongo-test-stream", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  bson_free (meta);

  chunk1 = g_malloc (300 * 1024);
  chunk2 = g_malloc (300 * 1024);
  chunk3 = g_malloc (300 * 1024);

  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk1, 300 * 1024), "==",
	  300 * 1024,
	  "reading the first chunk works");
  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk2, 300 * 1024), "==",
	  300 * 1024,
	  "reading the second chunk works");
  ok (memcmp (chunk1, chunk2, 300 * 1024) != 0,
      "The two chunks differ, as they should");

  ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_END) == TRUE,
      "mongo_sync_gridfs_stream_seek() works, with SEEK_END");
  cmp_ok (stream->file.offset, "==", stream->file.length,
	  "mongo_sync_gridfs_stream_seek() can seek to the end");

  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == TRUE,
      "mongo_sync_gridfs_stream_seek() works, with SEEK_SET");
  cmp_ok (stream->file.offset, "==", 1,
	  "mongo_sync_gridfs_stream_seek()'s SEEK_SET works");
  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == TRUE,
      "mongo_sync_gridfs_stream_seek() works, with SEEK_SET");

  ok (mongo_sync_gridfs_stream_seek (stream, -1, SEEK_CUR) == TRUE,
      "mongo_sync_gridfs_stream_seek() works, with SEEK_CUR");
  cmp_ok (stream->file.offset, "==", 0,
	  "mongo_sync_gridfs_stream_seek()'s SEEK_CUR works");
  ok (mongo_sync_gridfs_stream_seek (stream, 0, SEEK_CUR) == TRUE,
      "mongo_sync_gridfs_stream_seek() works, with SEEK_CUR");

  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk3, 300 * 1024), "==",
	  300 * 1024,
	  "reading after seeking works");

  ok (memcmp (chunk1, chunk3, 300 * 1024) == 0,
      "After seeking, we're at the beginning");

  mongo_sync_gridfs_stream_close (stream);
  g_free (chunk3);
  g_free (chunk2);
  g_free (chunk1);

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream_seek_invalid (void)
{
  mongo_sync_connection *conn;
  mongo_sync_gridfs *gfs;
  mongo_sync_gridfs_stream *stream;
  bson *meta;

  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
		     BSON_TYPE_NONE);
  bson_finish (meta);

  stream = mongo_sync_gridfs_stream_find (gfs, meta);
  bson_free (meta);

  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == FALSE,
      "mongo_sync_gridfs_stream_seek() should fail with no chunks");

  mongo_sync_gridfs_stream_close (stream);

  mongo_sync_gridfs_free (gfs, TRUE);
}

void
test_func_sync_gridfs_stream (void)
{
  test_func_sync_gridfs_stream_write ();
  test_func_sync_gridfs_stream_write_invalid ();
  test_func_sync_gridfs_stream_read ();
  test_func_sync_gridfs_stream_read_invalid ();
  test_func_sync_gridfs_stream_seek ();
  test_func_sync_gridfs_stream_seek_invalid ();
  test_func_sync_gridfs_stream_meta ();

  g_free (write_md5);
}

RUN_NET_TEST (32, func_sync_gridfs_stream);