]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test: add unit tests for gen_rand_alphanumeric helpers
authorCasey Bodley <cbodley@redhat.com>
Tue, 12 Nov 2019 14:43:08 +0000 (09:43 -0500)
committerNathan Cutler <ncutler@suse.com>
Wed, 24 Feb 2021 09:47:04 +0000 (10:47 +0100)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
(cherry picked from commit fb8975e0d4ce618c0b61aff3578e71706d23ae22)

src/test/CMakeLists.txt
src/test/test_random_string.cc [new file with mode: 0644]

index 30afc7739fdd2a833353f187d9286f653f109da6..3605f362b3fd7267f10d26195d68bea67a9da40b 100644 (file)
@@ -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 $<TARGET_OBJECTS:unit-main>)
+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 (file)
index 0000000..95729e7
--- /dev/null
@@ -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 <gtest/gtest.h>
+
+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));
+}