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