4fc57b0b1ddad52f29a96a773456be86c6c0a810
[awesomized/libmemcached] / test / tests / memcached / encoding_key.cpp
1 #include "test/lib/common.hpp"
2 #include "test/lib/MemcachedCluster.hpp"
3
4 #define TEST_KEY S("test")
5 #define INITIAL_VAL S("initial value, which is longer than AES_BLOCK_SIZE")
6 #define REPLACED_VAL S("replaced value, thich is longer than AES_BLOCK_SIZE")
7
8 static inline void check(memcached_st *enc, memcached_st *raw, const char *val, size_t len) {
9 memcached_return_t enc_rc, raw_rc;
10 size_t enc_length, raw_length;
11 Malloced enc_value(memcached_get(enc, TEST_KEY, &enc_length, nullptr, &enc_rc));
12 Malloced raw_value(memcached_get(raw, TEST_KEY, &raw_length, nullptr, &raw_rc));
13
14 REQUIRE(enc_rc == MEMCACHED_SUCCESS);
15 REQUIRE(raw_rc == MEMCACHED_SUCCESS);
16 REQUIRE_FALSE(enc_length == raw_length);
17 REQUIRE(memcmp(*raw_value, *enc_value, raw_length));
18 REQUIRE(enc_length == len);
19 REQUIRE_FALSE(memcmp(val, *enc_value, enc_length));
20 }
21
22 TEST_CASE("memcached_encoding_key") {
23 pair<string, MemcachedCluster> tests[]{
24 {"network", MemcachedCluster::network()},
25 {"socket", MemcachedCluster::socket()}
26 };
27
28 LOOPED_SECTION(tests) {
29 auto memc = &test.memc;
30
31 SECTION("accepts encoding key") {
32 MemcachedPtr copy(memcached_clone(nullptr, memc));
33
34 REQUIRE_SUCCESS(memcached_set_encoding_key(memc, S(__func__)));
35
36 SECTION("sets encoded value") {
37 REQUIRE_SUCCESS(memcached_set(memc, TEST_KEY, INITIAL_VAL, 0, 0));
38
39 SECTION("gets encoded value") {
40 check(memc, copy.memc, INITIAL_VAL);
41 }
42
43 SECTION("cloned gets encoded value") {
44 MemcachedPtr dupe(memcached_clone(nullptr, memc));
45
46 check(dupe.memc, copy.memc, INITIAL_VAL);
47 }
48 }
49
50 SECTION("adds encoded value") {
51
52 REQUIRE_SUCCESS(memcached_set(memc, TEST_KEY, INITIAL_VAL, 0, 0));
53 REQUIRE_RC(MEMCACHED_NOTSTORED, memcached_add(memc, TEST_KEY, REPLACED_VAL, 0, 0));
54
55 check(memc, copy.memc, INITIAL_VAL);
56
57 test.flush();
58
59 REQUIRE_SUCCESS(memcached_add(memc, TEST_KEY, REPLACED_VAL, 0, 0));
60
61 SECTION("gets encoded value") {
62 check(memc, copy.memc, REPLACED_VAL);
63 }
64 }
65
66 SECTION("replaces encoded value") {
67 REQUIRE_SUCCESS(memcached_set(memc, TEST_KEY, INITIAL_VAL, 0, 0));
68
69 check(memc, copy.memc, INITIAL_VAL);
70
71 REQUIRE_SUCCESS(memcached_replace(memc, TEST_KEY, REPLACED_VAL, 0, 0));
72
73 SECTION("gets encoded value") {
74 check(memc, copy.memc, REPLACED_VAL);
75 }
76 }
77
78 SECTION("unsupported") {
79 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_increment(memc, TEST_KEY, 0, nullptr));
80 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_decrement(memc, TEST_KEY, 0, nullptr));
81 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_increment_with_initial(memc, TEST_KEY, 0, 0, 0, nullptr));
82 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_decrement_with_initial(memc, TEST_KEY, 0, 0, 0, nullptr));
83 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_append(memc, TEST_KEY, REPLACED_VAL, 0, 0));
84 REQUIRE_RC(MEMCACHED_NOT_SUPPORTED, memcached_prepend(memc, TEST_KEY, REPLACED_VAL, 0, 0));
85 }
86 }
87 }
88 }