Change binary protocol behavior to disallow the use of key verification. (We disable...
[m6w6/libmemcached] / tests / function.c
index b6850288b5782571b964e1b47a8b466a7ac25212..5164b828270295c7a723f108697e264eb3354fac 100644 (file)
@@ -395,10 +395,13 @@ static test_return  cas2_test(memcached_st *memc)
 static test_return  cas_test(memcached_st *memc)
 {
   memcached_return rc;
-  char *key= "fun";
-  size_t key_length= strlen("fun");
-  char *value= "we the people";
-  size_t value_length= strlen("we the people");
+  const char *key= "fun";
+  size_t key_length= strlen(key);
+  const char *value= "we the people";
+  size_t value_length= strlen(value);
+  const char *value2= "change the value";
+  size_t value2_length= strlen(value2);
+
   memcached_result_st results_obj;
   memcached_result_st *results;
   unsigned int set= 1;
@@ -421,24 +424,27 @@ static test_return  cas_test(memcached_st *memc)
   assert(results);
   assert(rc == MEMCACHED_SUCCESS);
   WATCHPOINT_ASSERT(memcached_result_cas(results));
-
-  assert(!memcmp(value, "we the people", strlen("we the people")));
-  assert(strlen("we the people") == value_length);
+  assert(!memcmp(value, memcached_result_value(results), value_length));
+  assert(strlen(memcached_result_value(results)) == value_length);
   assert(rc == MEMCACHED_SUCCESS);
+  uint64_t cas = memcached_result_cas(results);
 
-  rc= memcached_cas(memc, key, key_length,
-                    "change the value", strlen("change the value"), 
-                    0, 0, memcached_result_cas(results));
-
+  #if 0
+  results= memcached_fetch_result(memc, &results_obj, &rc);
+  assert(rc == MEMCACHED_END);
+  assert(results == NULL);
+#endif
+  
+  rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
   assert(rc == MEMCACHED_SUCCESS);
 
-  rc= memcached_cas(memc, key, key_length,
-                    "change the value", strlen("change the value"), 
-                    0, 0, 23);
-
+  /*
+   * The item will have a new cas value, so try to set it again with the old
+   * value. This should fail!
+   */
+  rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas);
   assert(rc == MEMCACHED_DATA_EXISTS);
 
-
   memcached_result_free(&results_obj);
 
   return 0;
@@ -1397,6 +1403,7 @@ static test_return  user_supplied_bug1(memcached_st *memc)
     sprintf(key, "%d", x);
     rc = memcached_set(memc, key, strlen(key), 
                        randomstuff, strlen(randomstuff), 10, 0);
+    assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
     /* If we fail, lets try again */
     if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
       rc = memcached_set(memc, key, strlen(key), 
@@ -1928,7 +1935,11 @@ static test_return  user_supplied_bug12(memcached_st *memc)
                           1, &number_value);
 
   assert(value == NULL);
-  assert(rc == MEMCACHED_NOTFOUND);
+  /* The binary protocol will set the key if it doesn't exist */
+  if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1) 
+    assert(rc == MEMCACHED_SUCCESS);
+  else
+    assert(rc == MEMCACHED_NOTFOUND);
 
   rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0);
 
@@ -2123,13 +2134,75 @@ static test_return  user_supplied_bug17(memcached_st *memc)
     return 0;
 }
 
+/*
+  From Andrei on IRC
+*/
+
+test_return user_supplied_bug19(memcached_st *memc)
+{
+  memcached_st *m;
+  memcached_server_st *s;
+  memcached_return res;
+
+  (void)memc;
+
+  m= memcached_create(NULL);
+  memcached_server_add_with_weight(m, "localhost", 11311, 100);
+  memcached_server_add_with_weight(m, "localhost", 11312, 100);
+
+  s= memcached_server_by_key(m, "a", 1, &res);
+  memcached_server_free(s);
+
+  memcached_free(m);
+
+  return 0;
+}
+
+/* CAS test from Andei */
+test_return user_supplied_bug20(memcached_st *memc)
+{
+  memcached_return status;
+  memcached_result_st *result, result_obj;
+  char *key = "abc";
+  size_t key_len = strlen("abc");
+  char *value = "foobar";
+  size_t value_len = strlen(value);
+
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
+
+  status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0);
+  assert(status == MEMCACHED_SUCCESS);
+
+  status = memcached_mget(memc, &key, &key_len, 1);
+  assert(status == MEMCACHED_SUCCESS);
+
+  result= memcached_result_create(memc, &result_obj);
+  assert(result);
+
+  memcached_result_create(memc, &result_obj);
+  result= memcached_fetch_result(memc, &result_obj, &status);
+
+  assert(result);
+  assert(status == MEMCACHED_SUCCESS);
+
+  memcached_result_free(result);
+
+  return 0;
+}
+
 #include "ketama_test_cases.h"
-test_return user_supplied_bug18(memcached_st *memc)
+test_return user_supplied_bug18(memcached_st *trash)
 {
   memcached_return rc;
   int value;
   int i;
   memcached_server_st *server_pool;
+  memcached_st *memc;
+
+  (void)trash;
+
+  memc= memcached_create(NULL);
+  assert(memc);
     
   rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
   assert(rc == MEMCACHED_SUCCESS);
@@ -2143,10 +2216,6 @@ test_return user_supplied_bug18(memcached_st *memc)
   value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
   assert(value == MEMCACHED_HASH_MD5);
 
-  while (memc->number_of_hosts > 0)
-  {
-    memcached_server_remove(memc->hosts);
-  }
   server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100");
   memcached_server_push(memc, server_pool);
   
@@ -2165,7 +2234,7 @@ test_return user_supplied_bug18(memcached_st *memc)
   /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets
    * us test the boundary wraparound.
    */
-  assert(memcached_generate_hash(memc, (unsigned char *)"VDEAAAAA", 8) == memc->continuum[0].index);
+  assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index);
 
   /* verify the standard ketama set. */
   for (i= 0; i < 99; i++)
@@ -2174,6 +2243,10 @@ test_return user_supplied_bug18(memcached_st *memc)
     char *hostname = memc->hosts[server_idx].hostname;
     assert(strcmp(hostname, test_cases[i].server) == 0);
   }
+
+  memcached_server_list_free(server_pool);
+  memcached_free(memc);
+
   return 0;
 }
 
@@ -2183,7 +2256,7 @@ static test_return  result_static(memcached_st *memc)
   memcached_result_st *result_ptr;
 
   result_ptr= memcached_result_create(memc, &result);
-  assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED);
+  assert(result.is_allocated == false);
   assert(result_ptr);
   memcached_result_free(&result);
 
@@ -2207,7 +2280,7 @@ static test_return  string_static_null(memcached_st *memc)
   memcached_string_st *string_ptr;
 
   string_ptr= memcached_string_create(memc, &string, 0);
-  assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED);
+  assert(string.is_allocated == false);
   assert(string_ptr);
   memcached_string_free(&string);
 
@@ -2572,6 +2645,29 @@ static memcached_return  pre_nonblock(memcached_st *memc)
   return MEMCACHED_SUCCESS;
 }
 
+static memcached_return  pre_nonblock_binary(memcached_st *memc)
+{
+  memcached_return rc= MEMCACHED_FAILURE;
+  memcached_st *clone;
+
+  clone= memcached_clone(NULL, memc);
+  assert(clone);
+  // The memcached_version needs to be done on a clone, because the server
+  // will not toggle protocol on an connection.
+  memcached_version(clone);
+
+  if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2) 
+  {
+    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+    rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+    assert(rc == MEMCACHED_SUCCESS);
+    assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
+  }
+
+  memcached_free(clone);
+  return rc;
+}
+
 static memcached_return  pre_murmur(memcached_st *memc)
 {
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
@@ -2995,8 +3091,19 @@ test_st user_tests[] ={
   {"user_supplied_bug14", 1, user_supplied_bug14 },
   {"user_supplied_bug15", 1, user_supplied_bug15 },
   {"user_supplied_bug16", 1, user_supplied_bug16 },
+#ifndef __sun
+  /* 
+  ** It seems to be something weird with the character sets.. 
+  ** value_fetch is unable to parse the value line (iscntrl "fails"), so I
+  ** guess I need to find out how this is supposed to work.. Perhaps I need
+  ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success,
+  ** so just disable the code for now...).
+  */
   {"user_supplied_bug17", 1, user_supplied_bug17 },
+#endif
   {"user_supplied_bug18", 1, user_supplied_bug18 },
+  {"user_supplied_bug19", 1, user_supplied_bug19 },
+  {"user_supplied_bug20", 1, user_supplied_bug20 },
   {0, 0, 0}
 };
 
@@ -3061,6 +3168,7 @@ collection_st collection[] ={
   {"string", 0, 0, string_tests},
   {"result", 0, 0, result_tests},
   {"async", pre_nonblock, 0, async_tests},
+  {"async_binary", pre_nonblock_binary, 0, async_tests},
   {"user", 0, 0, user_tests},
   {"generate", 0, 0, generate_tests},
   {"generate_hsieh", pre_hsieh, 0, generate_tests},