1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2019 Red Hat
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.
15 #include "common/random_string.h"
16 #include "common/ceph_context.h"
17 #include "global/global_context.h"
18 #include <gtest/gtest.h>
20 inline bool is_alphanumeric_lower(char c
) {
21 return std::islower(c
) || std::isdigit(c
);
23 inline bool is_alphanumeric_upper(char c
) {
24 return std::isupper(c
) || std::isdigit(c
);
26 inline bool is_alphanumeric_plain(char c
) {
27 return std::islower(c
) || std::isupper(c
) || std::isdigit(c
);
29 inline bool is_alphanumeric_no_underscore(char c
) {
30 return is_alphanumeric_plain(c
) || c
== '-' || c
== '.';
32 inline bool is_alphanumeric(char c
) {
33 return is_alphanumeric_plain(c
) || c
== '-' || c
== '_';
35 inline bool is_base64(char c
) {
36 return is_alphanumeric_plain(c
) || c
== '+' || c
== '/';
39 TEST(RandomString
, base64
)
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
));
47 TEST(RandomString
, alphanumeric
)
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
));
55 TEST(RandomString
, alphanumeric_string
)
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
));
62 TEST(RandomString
, alphanumeric_lower
)
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
));
70 TEST(RandomString
, alphanumeric_lower_string
)
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
));
77 TEST(RandomString
, alphanumeric_upper
)
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
));
85 TEST(RandomString
, alphanumeric_upper_string
)
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
));
92 TEST(RandomString
, alphanumeric_no_underscore
)
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
));
100 TEST(RandomString
, alphanumeric_no_underscore_string
)
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
));
107 TEST(RandomString
, alphanumeric_plain
)
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
));
115 TEST(RandomString
, alphanumeric_plain_string
)
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
));