From: Casey Bodley Date: Tue, 12 Nov 2019 14:43:08 +0000 (-0500) Subject: test: add unit tests for gen_rand_alphanumeric helpers X-Git-Tag: v14.2.17~17^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6cb365ebfd41ba6a842337b9c694f8d285590024;p=ceph.git test: add unit tests for gen_rand_alphanumeric helpers Signed-off-by: Casey Bodley (cherry picked from commit fb8975e0d4ce618c0b61aff3578e71706d23ae22) --- diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 30afc7739fdd2..3605f362b3fd7 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -899,6 +899,10 @@ add_executable(unittest_pageset test_pageset.cc) add_ceph_unittest(unittest_pageset) target_link_libraries(unittest_pageset global) +add_executable(unittest_random_string test_random_string.cc $) +add_ceph_unittest(unittest_random_string) +target_link_libraries(unittest_random_string global ${rgw_libs}) + # unittest_any_ add_executable(unittest_any test_any.cc) add_ceph_unittest(unittest_any) diff --git a/src/test/test_random_string.cc b/src/test/test_random_string.cc new file mode 100644 index 0000000000000..95729e79ec7e5 --- /dev/null +++ b/src/test/test_random_string.cc @@ -0,0 +1,85 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2019 Red Hat + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "rgw/rgw_common.h" +#include "common/ceph_context.h" +#include "global/global_context.h" +#include + +inline bool is_alphanumeric_lower(char c) { + return std::islower(c) || std::isdigit(c); +} +inline bool is_alphanumeric_upper(char c) { + return std::isupper(c) || std::isdigit(c); +} +inline bool is_alphanumeric_plain(char c) { + return std::islower(c) || std::isupper(c) || std::isdigit(c); +} +inline bool is_alphanumeric_no_underscore(char c) { + return is_alphanumeric_plain(c) || c == '-' || c == '.'; +} +inline bool is_alphanumeric(char c) { + return is_alphanumeric_plain(c) || c == '-' || c == '_'; +} +inline bool is_base64(char c) { + return is_alphanumeric_plain(c) || c == '+' || c == '/'; +} + +TEST(RandomString, base64) +{ + char arr[65] = {}; + ASSERT_EQ(0, gen_rand_base64(g_ceph_context, arr, sizeof(arr))); + EXPECT_EQ(0, arr[64]); // must be null terminated + EXPECT_TRUE(std::all_of(arr, arr + 64, is_base64)); +} + +TEST(RandomString, alphanumeric) +{ + char arr[65] = {}; + gen_rand_alphanumeric(g_ceph_context, arr, sizeof(arr)); + EXPECT_EQ(0, arr[64]); + EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric)); +} + +TEST(RandomString, alphanumeric_lower) +{ + char arr[65] = {}; + gen_rand_alphanumeric_lower(g_ceph_context, arr, sizeof(arr)); + EXPECT_EQ(0, arr[64]); + EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_lower)); +} + +TEST(RandomString, alphanumeric_upper) +{ + char arr[65] = {}; + gen_rand_alphanumeric_upper(g_ceph_context, arr, sizeof(arr)); + EXPECT_EQ(0, arr[64]); + EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_upper)); +} + +TEST(RandomString, alphanumeric_no_underscore) +{ + char arr[65] = {}; + gen_rand_alphanumeric_no_underscore(g_ceph_context, arr, sizeof(arr)); + EXPECT_EQ(0, arr[64]); + EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_no_underscore)); +} + +TEST(RandomString, alphanumeric_plain) +{ + char arr[65] = {}; + gen_rand_alphanumeric_plain(g_ceph_context, arr, sizeof(arr)); + EXPECT_EQ(0, arr[64]); + EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_plain)); +}