~ubuntu-branches/ubuntu/trusty/syslog-ng/trusty-proposed

« back to all changes in this revision

Viewing changes to modules/afmongodb/libmongo-client/tests/func/mongo/sync-gridfs-stream/f_sync_gridfs_stream.c

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS), Gergely Nagy
  • Date: 2011-10-11 14:30:48 UTC
  • mfrom: (1.3.7)
  • Revision ID: package-import@ubuntu.com-20111011143048-r1iljux9xbvj3lwh
Tags: 3.3.1.dfsg-1
* New upstream release with important fixes from upstream git tree with
  non-free manpages removed.
* Drop syslog-ng.conf(5) (closes: #496521).
* syslog-ng(8) is generated, and does not mention -Q anymore
  (closes: #616069).
* Supports CAP_SYSLOG on recent kernels (closes: #630172).
* Does not use g_timeout_add_seconds anymore (closes: #609154).

[ Gergely Nagy <algernon@madhouse-project.org> ]
* Update debian/copyright to DEP-5 format.
* Simplified the logrotate file by merging identical entries.
* Include local configuration files from /etc/syslog-ng/conf.d/ (Closes:
  #609050).
* Update syslog-ng.conf to be fully 3.3 compliant.
* Compress both source and binaries (except the syslog-ng meta
  package) with xz, instead of gzip.
* Use dpkg triggers to restart syslog-ng when appropriate.
* Include DFSG-free manual pages for all binaries.
* Build with Hardening enabled.
* Mention syslog(3) in /etc/default/syslog-ng, instead of
  <linux/kernel.h> (Closes: #608605)
* Support 'status' in the init script.
  Patch from Peter Eisentraut <petere@debian.org> (Closes: #644458)
* Build-Depend on libevtlog-dev (>= 0.2.12-5~) for correct shlibs.
* Use [linux-any] in Build-Depends instead of hardcoded links.
  (Closes: #634715)
* Use $SYSLOGNG_OPTS in the init script when reloading syslog-ng.
  (Closes: #589081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "test.h"
 
2
#include "mongo.h"
 
3
#include "compat.h"
 
4
 
 
5
#define FILE_SIZE 1024 * 1024 + 12345
 
6
#define BUFFER_SIZE 64 * 1024
 
7
 
 
8
gchar *write_md5 = NULL;
 
9
 
 
10
void
 
11
test_func_sync_gridfs_stream_write (void)
 
12
{
 
13
  mongo_sync_connection *conn;
 
14
  mongo_sync_gridfs *gfs;
 
15
  mongo_sync_gridfs_stream *stream;
 
16
  bson *meta;
 
17
  guint8 *data, *oid;
 
18
  gint pos = 0;
 
19
  gint filler = 0;
 
20
  gboolean write_ok = TRUE;
 
21
  GChecksum *chk;
 
22
 
 
23
  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
 
24
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
 
25
 
 
26
  stream = mongo_sync_gridfs_stream_new (gfs, NULL);
 
27
  ok (stream == NULL,
 
28
      "mongo_sync_gridfs_stream_new() fails without mongo_util_oid_init()");
 
29
 
 
30
  mongo_util_oid_init (0);
 
31
 
 
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,
 
35
                     BSON_TYPE_NONE);
 
36
  bson_finish (meta);
 
37
  g_free (oid);
 
38
 
 
39
  stream = mongo_sync_gridfs_stream_new (gfs, meta);
 
40
  ok (stream != NULL,
 
41
      "mongo_sync_gridfs_stream_new() works");
 
42
  bson_free (meta);
 
43
 
 
44
  data = g_malloc (BUFFER_SIZE);
 
45
 
 
46
  chk = g_checksum_new (G_CHECKSUM_MD5);
 
47
 
 
48
  while (pos < FILE_SIZE)
 
49
    {
 
50
      gint csize = BUFFER_SIZE;
 
51
 
 
52
      if (csize + pos > FILE_SIZE)
 
53
        csize = FILE_SIZE - pos;
 
54
 
 
55
      memset (data, filler++, BUFFER_SIZE);
 
56
 
 
57
      g_checksum_update (chk, data, csize);
 
58
 
 
59
      write_ok &= mongo_sync_gridfs_stream_write (stream, data, csize);
 
60
      pos += csize;
 
61
    }
 
62
  ok (write_ok == TRUE,
 
63
      "All stream_write()s succeeded");
 
64
 
 
65
  write_md5 = g_strdup (g_checksum_get_string (chk));
 
66
  g_checksum_free (chk);
 
67
 
 
68
  note ("File MD5: %s\n", write_md5);
 
69
 
 
70
  g_free (data);
 
71
  ok (mongo_sync_gridfs_stream_close (stream) == TRUE,
 
72
      "mongo_sync_gridfs_stream_close() works");
 
73
 
 
74
  mongo_sync_gridfs_free (gfs, TRUE);
 
75
}
 
76
 
 
77
void
 
78
test_func_sync_gridfs_stream_write_invalid (void)
 
79
{
 
80
  mongo_sync_connection *conn;
 
81
  mongo_sync_gridfs *gfs;
 
82
  mongo_sync_gridfs_stream *stream;
 
83
  bson *meta;
 
84
  gchar *ns;
 
85
 
 
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);
 
89
 
 
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,
 
93
                     BSON_TYPE_NONE);
 
94
  bson_finish (meta);
 
95
 
 
96
  stream = mongo_sync_gridfs_stream_new (gfs, meta);
 
97
  ok (stream == NULL,
 
98
      "mongo_sync_gridfs_stream_new() should fail if meta has an invalid _id");
 
99
  bson_free (meta);
 
100
 
 
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,
 
104
                     BSON_TYPE_NONE);
 
105
  bson_finish (meta);
 
106
 
 
107
  mongo_sync_cmd_insert (conn, ns, meta, NULL);
 
108
  bson_free (meta);
 
109
 
 
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,
 
113
                     BSON_TYPE_NONE);
 
114
  bson_finish (meta);
 
115
 
 
116
  mongo_sync_cmd_insert (conn, ns, meta, NULL);
 
117
  bson_free (meta);
 
118
 
 
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,
 
123
                     BSON_TYPE_NONE);
 
124
  bson_finish (meta);
 
125
 
 
126
  mongo_sync_cmd_insert (conn, ns, meta, NULL);
 
127
  bson_free (meta);
 
128
 
 
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,
 
135
                     BSON_TYPE_NONE);
 
136
  bson_finish (meta);
 
137
 
 
138
  mongo_sync_cmd_insert (conn, ns, meta, NULL);
 
139
  bson_free (meta);
 
140
 
 
141
  mongo_sync_gridfs_free (gfs, TRUE);
 
142
}
 
143
 
 
144
void
 
145
test_func_sync_gridfs_stream_read (void)
 
146
{
 
147
  mongo_sync_connection *conn;
 
148
  mongo_sync_gridfs *gfs;
 
149
  mongo_sync_gridfs_stream *stream;
 
150
  guint8 data[12345];
 
151
  gint64 pos = 0;
 
152
  bson *meta;
 
153
 
 
154
  GChecksum *chk;
 
155
 
 
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,
 
159
                     BSON_TYPE_NONE);
 
160
  bson_finish (meta);
 
161
 
 
162
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
163
  ok (stream != NULL,
 
164
      "mongo_sync_gridfs_stream_find() works");
 
165
  bson_free (meta);
 
166
 
 
167
  chk = g_checksum_new (G_CHECKSUM_MD5);
 
168
 
 
169
  while (pos < FILE_SIZE)
 
170
    {
 
171
      gint64 r;
 
172
 
 
173
      r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
 
174
      if (r == -1)
 
175
        break;
 
176
 
 
177
      g_checksum_update (chk, data, r);
 
178
      pos += r;
 
179
    }
 
180
 
 
181
  cmp_ok (pos, "==", FILE_SIZE,
 
182
          "mongo_sync_gridfs_stream_read() works");
 
183
  is (g_checksum_get_string (chk), write_md5,
 
184
      "md5sums match");
 
185
 
 
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);
 
190
}
 
191
 
 
192
void
 
193
test_func_sync_gridfs_stream_meta (void)
 
194
{
 
195
  mongo_sync_connection *conn;
 
196
  mongo_sync_gridfs *gfs;
 
197
  mongo_sync_gridfs_stream *stream;
 
198
  bson *meta;
 
199
  const guint8 *id;
 
200
 
 
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,
 
204
                     BSON_TYPE_NONE);
 
205
  bson_finish (meta);
 
206
 
 
207
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
208
  bson_free (meta);
 
209
 
 
210
  id = mongo_sync_gridfs_file_get_id (stream);
 
211
  ok (id != NULL,
 
212
      "mongo_sync_gridfs_file_get_id() works on streams");
 
213
 
 
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");
 
220
 
 
221
  mongo_sync_gridfs_stream_close (stream);
 
222
 
 
223
  mongo_sync_gridfs_free (gfs, TRUE);
 
224
}
 
225
 
 
226
void
 
227
test_func_sync_gridfs_stream_read_invalid (void)
 
228
{
 
229
  mongo_sync_connection *conn;
 
230
  mongo_sync_gridfs *gfs;
 
231
  mongo_sync_gridfs_stream *stream;
 
232
  guint8 data[1245];
 
233
  gint64 r;
 
234
  bson *meta;
 
235
 
 
236
  conn = mongo_sync_connect (config.primary_host, config.primary_port, FALSE);
 
237
  gfs = mongo_sync_gridfs_new (conn, config.gfs_prefix);
 
238
 
 
239
  /* ---- */
 
240
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:string-id", -1,
 
241
                     BSON_TYPE_NONE);
 
242
  bson_finish (meta);
 
243
 
 
244
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
245
  ok (stream == NULL,
 
246
      "mongo_sync_gridfs_stream_find() should fail if _id is non-OID");
 
247
  bson_free (meta);
 
248
 
 
249
  /* ---- */
 
250
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-length", -1,
 
251
                     BSON_TYPE_NONE);
 
252
  bson_finish (meta);
 
253
 
 
254
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
255
  ok (stream == NULL,
 
256
      "mongo_sync_gridfs_stream_find() should fail with invalid metadata");
 
257
  bson_free (meta);
 
258
 
 
259
  /* ---- */
 
260
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:invalid-chunkSize", -1,
 
261
                     BSON_TYPE_NONE);
 
262
  bson_finish (meta);
 
263
 
 
264
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
265
  ok (stream == NULL,
 
266
      "mongo_sync_gridfs_stream_find() should fail with invalid metadata");
 
267
  bson_free (meta);
 
268
 
 
269
  /* no-chunk test */
 
270
  meta = bson_build (BSON_TYPE_STRING, "my-id", "stream:no-chunks", -1,
 
271
                     BSON_TYPE_NONE);
 
272
  bson_finish (meta);
 
273
 
 
274
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
275
  ok (stream != NULL,
 
276
      "mongo_sync_gridfs_stream_find() works [stream:no-chunks]");
 
277
  bson_free (meta);
 
278
 
 
279
  r = mongo_sync_gridfs_stream_read (stream, data, sizeof (data));
 
280
  cmp_ok (r, "==", -1,
 
281
          "Reading from a chunk-less file should fail");
 
282
 
 
283
  mongo_sync_gridfs_stream_close (stream);
 
284
 
 
285
  mongo_sync_gridfs_free (gfs, TRUE);
 
286
}
 
287
 
 
288
void
 
289
test_func_sync_gridfs_stream_seek (void)
 
290
{
 
291
  mongo_sync_connection *conn;
 
292
  mongo_sync_gridfs *gfs;
 
293
  mongo_sync_gridfs_stream *stream;
 
294
  bson *meta;
 
295
  guint8 *chunk1, *chunk2, *chunk3;
 
296
 
 
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,
 
300
                     BSON_TYPE_NONE);
 
301
  bson_finish (meta);
 
302
 
 
303
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
304
  bson_free (meta);
 
305
 
 
306
  chunk1 = g_malloc (300 * 1024);
 
307
  chunk2 = g_malloc (300 * 1024);
 
308
  chunk3 = g_malloc (300 * 1024);
 
309
 
 
310
  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk1, 300 * 1024), "==",
 
311
          300 * 1024,
 
312
          "reading the first chunk works");
 
313
  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk2, 300 * 1024), "==",
 
314
          300 * 1024,
 
315
          "reading the second chunk works");
 
316
  ok (memcmp (chunk1, chunk2, 300 * 1024) != 0,
 
317
      "The two chunks differ, as they should");
 
318
 
 
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");
 
323
 
 
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");
 
330
 
 
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");
 
337
 
 
338
  cmp_ok (mongo_sync_gridfs_stream_read (stream, chunk3, 300 * 1024), "==",
 
339
          300 * 1024,
 
340
          "reading after seeking works");
 
341
 
 
342
  ok (memcmp (chunk1, chunk3, 300 * 1024) == 0,
 
343
      "After seeking, we're at the beginning");
 
344
 
 
345
  mongo_sync_gridfs_stream_close (stream);
 
346
  g_free (chunk3);
 
347
  g_free (chunk2);
 
348
  g_free (chunk1);
 
349
 
 
350
  mongo_sync_gridfs_free (gfs, TRUE);
 
351
}
 
352
 
 
353
void
 
354
test_func_sync_gridfs_stream_seek_invalid (void)
 
355
{
 
356
  mongo_sync_connection *conn;
 
357
  mongo_sync_gridfs *gfs;
 
358
  mongo_sync_gridfs_stream *stream;
 
359
  bson *meta;
 
360
 
 
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,
 
364
                     BSON_TYPE_NONE);
 
365
  bson_finish (meta);
 
366
 
 
367
  stream = mongo_sync_gridfs_stream_find (gfs, meta);
 
368
  bson_free (meta);
 
369
 
 
370
  ok (mongo_sync_gridfs_stream_seek (stream, 1, SEEK_SET) == FALSE,
 
371
      "mongo_sync_gridfs_stream_seek() should fail with no chunks");
 
372
 
 
373
  mongo_sync_gridfs_stream_close (stream);
 
374
 
 
375
  mongo_sync_gridfs_free (gfs, TRUE);
 
376
}
 
377
 
 
378
void
 
379
test_func_sync_gridfs_stream (void)
 
380
{
 
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 ();
 
388
 
 
389
  g_free (write_md5);
 
390
}
 
391
 
 
392
RUN_NET_TEST (32, func_sync_gridfs_stream);