]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/test_random_string.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / test_random_string.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2019 Red Hat
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "common/random_string.h"
16 #include "common/ceph_context.h"
17 #include "global/global_context.h"
18 #include <gtest/gtest.h>
19
20 inline bool is_alphanumeric_lower(char c) {
21 return std::islower(c) || std::isdigit(c);
22 }
23 inline bool is_alphanumeric_upper(char c) {
24 return std::isupper(c) || std::isdigit(c);
25 }
26 inline bool is_alphanumeric_plain(char c) {
27 return std::islower(c) || std::isupper(c) || std::isdigit(c);
28 }
29 inline bool is_alphanumeric_no_underscore(char c) {
30 return is_alphanumeric_plain(c) || c == '-' || c == '.';
31 }
32 inline bool is_alphanumeric(char c) {
33 return is_alphanumeric_plain(c) || c == '-' || c == '_';
34 }
35 inline bool is_base64(char c) {
36 return is_alphanumeric_plain(c) || c == '+' || c == '/';
37 }
38
39 TEST(RandomString, base64)
40 {
41 char arr[65] = {};
42 ASSERT_EQ(0, gen_rand_base64(g_ceph_context, arr, sizeof(arr)));
43 EXPECT_EQ(0, arr[64]); // must be null terminated
44 EXPECT_TRUE(std::all_of(arr, arr + 64, is_base64));
45 }
46
47 TEST(RandomString, alphanumeric)
48 {
49 char arr[65] = {};
50 gen_rand_alphanumeric(g_ceph_context, arr, sizeof(arr));
51 EXPECT_EQ(0, arr[64]);
52 EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric));
53 }
54
55 TEST(RandomString, alphanumeric_string)
56 {
57 std::string str = gen_rand_alphanumeric(g_ceph_context, 64);
58 EXPECT_EQ(64, str.size());
59 EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric));
60 }
61
62 TEST(RandomString, alphanumeric_lower)
63 {
64 char arr[65] = {};
65 gen_rand_alphanumeric_lower(g_ceph_context, arr, sizeof(arr));
66 EXPECT_EQ(0, arr[64]);
67 EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_lower));
68 }
69
70 TEST(RandomString, alphanumeric_lower_string)
71 {
72 std::string str = gen_rand_alphanumeric_lower(g_ceph_context, 64);
73 EXPECT_EQ(64, str.size());
74 EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric_lower));
75 }
76
77 TEST(RandomString, alphanumeric_upper)
78 {
79 char arr[65] = {};
80 gen_rand_alphanumeric_upper(g_ceph_context, arr, sizeof(arr));
81 EXPECT_EQ(0, arr[64]);
82 EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_upper));
83 }
84
85 TEST(RandomString, alphanumeric_upper_string)
86 {
87 std::string str = gen_rand_alphanumeric_upper(g_ceph_context, 64);
88 EXPECT_EQ(64, str.size());
89 EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric_upper));
90 }
91
92 TEST(RandomString, alphanumeric_no_underscore)
93 {
94 char arr[65] = {};
95 gen_rand_alphanumeric_no_underscore(g_ceph_context, arr, sizeof(arr));
96 EXPECT_EQ(0, arr[64]);
97 EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_no_underscore));
98 }
99
100 TEST(RandomString, alphanumeric_no_underscore_string)
101 {
102 std::string str = gen_rand_alphanumeric_no_underscore(g_ceph_context, 64);
103 EXPECT_EQ(64, str.size());
104 EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric_no_underscore));
105 }
106
107 TEST(RandomString, alphanumeric_plain)
108 {
109 char arr[65] = {};
110 gen_rand_alphanumeric_plain(g_ceph_context, arr, sizeof(arr));
111 EXPECT_EQ(0, arr[64]);
112 EXPECT_TRUE(std::all_of(arr, arr + 64, is_alphanumeric_plain));
113 }
114
115 TEST(RandomString, alphanumeric_plain_string)
116 {
117 std::string str = gen_rand_alphanumeric_plain(g_ceph_context, 64);
118 EXPECT_EQ(64, str.size());
119 EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric_plain));
120 }