From 8a271ceb322c26e0d8e4484fea3c82c1e6f46457 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 9 Mar 2016 09:23:45 -0500 Subject: [PATCH] Verify keys by default --- ext/memcached_rb/memcached_rb.c | 11 ++++++-- test/unit/client_test.rb | 50 ++++++++++++++------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/ext/memcached_rb/memcached_rb.c b/ext/memcached_rb/memcached_rb.c index 5cd1dc7..039074b 100644 --- a/ext/memcached_rb/memcached_rb.c +++ b/ext/memcached_rb/memcached_rb.c @@ -120,6 +120,7 @@ rb_connection_initialize(VALUE self, VALUE rb_servers) { memcached_ctx *ctx = get_ctx(self); int i; + memcached_return_t rc; for (i = 0; i < RARRAY_LEN(rb_servers); ++i) { VALUE rb_server = rb_ary_entry(rb_servers, i); @@ -133,13 +134,19 @@ rb_connection_initialize(VALUE self, VALUE rb_servers) VALUE rb_port = rb_ary_entry(rb_server, 2); // TODO: add weight //VALUE rb_weight = rb_ary_entry(rb_server, 3); - memcached_server_add (ctx->memc, StringValueCStr(rb_hostname), NUM2INT(rb_port)); + rc = memcached_server_add (ctx->memc, StringValueCStr(rb_hostname), NUM2INT(rb_port)); + handle_memcached_return(rc); } else if (id_socket == SYM2ID(rb_backend)) { VALUE rb_fd = rb_ary_entry(rb_server, 1); - memcached_server_add_unix_socket (ctx->memc, StringValueCStr(rb_fd)); + rc = memcached_server_add_unix_socket (ctx->memc, StringValueCStr(rb_fd)); + handle_memcached_return(rc); } } + // Verify keys by default + rc = memcached_behavior_set(ctx->memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, true); + handle_memcached_return(rc); + return Qnil; } diff --git a/test/unit/client_test.rb b/test/unit/client_test.rb index ad56467..b8280ea 100644 --- a/test/unit/client_test.rb +++ b/test/unit/client_test.rb @@ -48,15 +48,15 @@ class ClientTest < BaseTest # Error states -# def test_key_with_spaces -# key = "i have a space" -# #assert_raises(Memcached::ABadKeyWasProvidedOrCharactersOutOfRange) do -# cache.set key, @value -# #end -# #assert_raises(Memcached::ABadKeyWasProvidedOrCharactersOutOfRange) do -# #cache.get(key) -# #end -# end + def test_key_with_spaces + key = "i have a space" + assert_raises(Memcached::BadKeyProvided) do + cache.set key, @value + end + assert_raises(Memcached::BadKeyProvided) do + cache.get(key) + end + end # def test_key_with_null # key = "with\000null" @@ -70,19 +70,18 @@ class ClientTest < BaseTest # cache.get_multi([key]) # end -# def test_key_with_invalid_control_characters -# key = "ch\303\242teau" -## assert_raises(Memcached::ABadKeyWasProvidedOrCharactersOutOfRange) do -# cache.set key, @value -## end -## assert_raises(Memcached::ABadKeyWasProvidedOrCharactersOutOfRange) do -# cache.get(key) -## end -# -## assert_raises(Memcached::ABadKeyWasProvidedOrCharactersOutOfRange) do -# cache.get_multi([key]) -## end -# end + def test_key_with_invalid_control_characters + key = "ch\303\242teau" + assert_raises(Memcached::BadKeyProvided) do + cache.set key, @value + end + assert_raises(Memcached::BadKeyProvided) do + cache.get(key) + end + assert_raises(Memcached::BadKeyProvided) do + cache.get_multi([key]) + end + end def test_key_too_long key = "x"*251 @@ -108,13 +107,6 @@ class ClientTest < BaseTest # end # end # -# def test_key_error_message -# key = "i have a space" -# cache.get key -## rescue Memcached::ABadKeyWasProvidedOrCharactersOutOfRange => e -## assert_match /#{key}/, e.message -# end - def test_server_error_message err = assert_raises(Memcached::E2big) do cache.set key, "I'm big" * 1000000