summaryrefslogtreecommitdiff
path: root/tests/func/mongo/sync/f_sync_safe_mode_cache.c
blob: 082617f4bba33bc2c835aa034d39611ff1a80352 (plain)
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
#include "test.h"
#include <mongo.h>

#include <errno.h>
#include <string.h>

#include "libmongo-private.h"

void
test_func_mongo_sync_safe_mode_basics_cache (void)
{
  mongo_sync_connection *conn;
  mongo_sync_conn_recovery_cache *cache;

  const bson *docs[10];
  bson *b1, *b2, *b3, *b4, *cmd;
  mongo_packet *p;
  gchar *error;

  mongo_util_oid_init (0);

  b1 = bson_new ();
  bson_append_string (b1, "func_mongo_sync_safe_mode", "works", -1);
  bson_finish (b1);

  b2 = bson_new ();
  bson_append_int32 (b2, "int32", 1984);
  bson_finish (b2);

  b3 = test_bson_generate_full ();
  b4 = test_bson_generate_full ();

  docs[0] = b1;
  docs[1] = b2;
  docs[2] = b3;
  docs[3] = b4;

  cache = mongo_sync_conn_recovery_cache_new ();

  mongo_sync_conn_recovery_cache_seed_add (cache,
                                           config.primary_host,
                                           config.primary_port);

  conn = mongo_sync_connect_recovery_cache (cache, FALSE);

  /* Test inserts */
  mongo_sync_conn_set_safe_mode (conn, FALSE);
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 4, docs) == TRUE,
      "mongo_sync_cmd_insert_n() should not fail with safe mode off");

  mongo_sync_conn_set_safe_mode (conn, TRUE);
  ok (mongo_sync_cmd_insert_n (conn, config.ns, 4, docs) == FALSE,
      "mongo_sync_cmd_insert_n() should fail with safe mode on");

  /* Test a custom command */
  cmd = bson_new ();
  bson_append_int32 (cmd, "bogusCommand", 1);
  bson_finish (cmd);

  mongo_sync_cmd_reset_error (conn, config.db);
  mongo_sync_conn_set_safe_mode (conn, FALSE);
  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  mongo_sync_cmd_get_last_error (conn, config.db, &error);
  ok (p == NULL && strcmp (error, "no such cmd: bogusCommand") == 0,
      "mongo_sync_cmd_custom() with a bogus command fails with safe-mode off");
  bson_free (cmd);
  g_free (error);

  cmd = bson_new ();
  bson_append_int32 (cmd, "bogusCommand2", 1);
  bson_finish (cmd);
  mongo_sync_cmd_reset_error (conn, config.db);
  mongo_sync_conn_set_safe_mode (conn, TRUE);
  p = mongo_sync_cmd_custom (conn, config.db, cmd);
  mongo_sync_cmd_get_last_error (conn, config.db, &error);
  ok (p == NULL && strcmp (error, "no such cmd: bogusCommand2") == 0,
      "mongo_sync_cmd_custom() with a bogus command fails with safe-mode on");
  bson_free (cmd);
  g_free (error);

  mongo_sync_disconnect (conn);
  mongo_sync_conn_recovery_cache_free (cache);

  bson_free (b1);
  bson_free (b2);
  bson_free (b3);
  bson_free (b4);
}

#define INVALID_NS "1234567890123456789012345678901234567890123456789012345678901234567890.test"

void
test_func_mongo_sync_safe_mode_invalid_db_cache (void)
{
  mongo_sync_connection *conn;
  mongo_sync_conn_recovery_cache *cache;
  bson *b;
  const bson *docs[1];

  b = bson_new ();
  bson_append_int32 (b, "int32", 1984);
  bson_finish (b);

  docs[0] = b;

  cache = mongo_sync_conn_recovery_cache_new ();

  mongo_sync_conn_recovery_cache_seed_add (cache,
                                           config.primary_host,
                                           config.primary_port);

  conn = mongo_sync_connect_recovery_cache (cache, TRUE);

  mongo_sync_conn_set_safe_mode (conn, TRUE);

  ok (mongo_sync_cmd_insert_n (conn, INVALID_NS, 1, docs) == FALSE,
      "mongo_sync_cmd_insert_n() should fail with safe mode on and an invalid NS");

  mongo_sync_disconnect (conn);
  mongo_sync_conn_recovery_cache_free (cache);
  bson_free (b);
}

void
test_func_mongo_sync_safe_mode_cache (void)
{
  test_func_mongo_sync_safe_mode_basics_cache ();
  test_func_mongo_sync_safe_mode_invalid_db_cache ();
}

RUN_NET_TEST (5, func_mongo_sync_safe_mode_cache);