]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/test_rgw_token.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / test / test_rgw_token.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) 2016 Red Hat, Inc.
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 <errno.h>
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19
20 #include "common/config.h"
21 #include "common/ceph_argparse.h"
22 #include "common/debug.h"
23 #include "include/ceph_assert.h"
24 #include "gtest/gtest.h"
25 #include "rgw/rgw_token.h"
26 #include "rgw/rgw_b64.h"
27
28 #define dout_subsys ceph_subsys_rgw
29
30 namespace {
31
32 using namespace rgw;
33 using std::get;
34 using std::string;
35
36 string access_key{"Smonny"};
37 string secret_key{"Turjan of Miir"};
38
39 std::vector<RGWToken> tokens;
40
41 std::string enc_ad{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAiYWQiLAogICAgICAgICJpZCI6ICJTbW9ubnkiLAogICAgICAgICJrZXkiOiAiVHVyamFuIG9mIE1paXIiCiAgICB9Cn0K"};
42
43 std::string enc_ldap{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAibGRhcCIsCiAgICAgICAgImlkIjogIlNtb25ueSIsCiAgICAgICAgImtleSI6ICJUdXJqYW4gb2YgTWlpciIKICAgIH0KfQo="};
44
45 std::string non_base64{"stuff here"};
46 std::string non_base64_sploded{"90KLscc0Dz4U49HX-7Tx"};
47
48 Formatter* formatter{nullptr};
49 bool verbose {false};
50 }
51
52 TEST(TOKEN, INIT) {
53 formatter = new JSONFormatter(true /* pretty */);
54 ASSERT_NE(formatter, nullptr);
55 }
56
57 TEST(TOKEN, ENCODE) {
58 // encode the two supported types
59 RGWToken token_ad(RGWToken::TOKEN_AD, access_key, secret_key);
60 ASSERT_EQ(token_ad.encode_json_base64(formatter), enc_ad);
61 tokens.push_back(token_ad); // provies copiable
62
63 RGWToken token_ldap(RGWToken::TOKEN_LDAP, access_key, secret_key);
64 ASSERT_EQ(token_ldap.encode_json_base64(formatter), enc_ldap);
65 tokens.push_back(token_ldap);
66 }
67
68 TEST(TOKEN, DECODE) {
69 for (const auto& enc_tok : {enc_ad, enc_ldap}) {
70 RGWToken token{from_base64(enc_tok)}; // decode ctor
71 ASSERT_EQ(token.id, access_key);
72 ASSERT_EQ(token.key, secret_key);
73 }
74 }
75
76 TEST(TOKEN, EMPTY) {
77 std::string empty{""};
78 RGWToken token{from_base64(empty)}; // decode ctor
79 ASSERT_FALSE(token.valid());
80 }
81
82 TEST(TOKEN, BADINPUT) {
83 RGWToken token{from_base64(non_base64)}; // decode ctor
84 ASSERT_FALSE(token.valid());
85 }
86
87 TEST(TOKEN, BADINPUT2) {
88 RGWToken token{from_base64(non_base64_sploded)}; // decode ctor
89 ASSERT_FALSE(token.valid());
90 }
91
92 TEST(TOKEN, BADINPUT3) {
93 try {
94 std::string stuff = from_base64(non_base64_sploded); // decode
95 } catch(...) {
96 // do nothing
97 }
98 ASSERT_EQ(1, 1);
99 }
100
101 TEST(TOKEN, SHUTDOWN) {
102 delete formatter;
103 }
104
105 int main(int argc, char *argv[])
106 {
107 string val;
108 vector<const char*> args;
109
110 argv_to_vec(argc, const_cast<const char**>(argv), args);
111 env_to_vec(args);
112
113 for (auto arg_iter = args.begin(); arg_iter != args.end();) {
114 if (ceph_argparse_flag(args, arg_iter, "--verbose",
115 (char*) nullptr)) {
116 verbose = true;
117 } else {
118 ++arg_iter;
119 }
120 }
121
122 ::testing::InitGoogleTest(&argc, argv);
123 return RUN_ALL_TESTS();
124 }