summaryrefslogtreecommitdiffstats
path: root/src/test/test_random_string.cc
blob: fa198a3466aab39ed1e3ea8acfb036d4ae9b0130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// -*- 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 "common/random_string.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_string)
{
  std::string str = gen_rand_alphanumeric(g_ceph_context, 64);
  EXPECT_EQ(64, str.size());
  EXPECT_TRUE(std::all_of(str.begin(), str.end(), 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_lower_string)
{
  std::string str = gen_rand_alphanumeric_lower(g_ceph_context, 64);
  EXPECT_EQ(64, str.size());
  EXPECT_TRUE(std::all_of(str.begin(), str.end(), 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_upper_string)
{
  std::string str = gen_rand_alphanumeric_upper(g_ceph_context, 64);
  EXPECT_EQ(64, str.size());
  EXPECT_TRUE(std::all_of(str.begin(), str.end(), 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_no_underscore_string)
{
  std::string str = gen_rand_alphanumeric_no_underscore(g_ceph_context, 64);
  EXPECT_EQ(64, str.size());
  EXPECT_TRUE(std::all_of(str.begin(), str.end(), 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));
}

TEST(RandomString, alphanumeric_plain_string)
{
  std::string str = gen_rand_alphanumeric_plain(g_ceph_context, 64);
  EXPECT_EQ(64, str.size());
  EXPECT_TRUE(std::all_of(str.begin(), str.end(), is_alphanumeric_plain));
}