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) 2016 Red Hat, Inc.
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.
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_token.h"
28 #define dout_subsys ceph_subsys_rgw
36 string access_key
{"Smonny"};
37 string secret_key
{"Turjan of Miir"};
39 std::vector
<RGWToken
> tokens
;
41 std::string enc_ad
{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAiYWQiLAogICAgICAgICJpZCI6ICJTbW9ubnkiLAogICAgICAgICJrZXkiOiAiVHVyamFuIG9mIE1paXIiCiAgICB9Cn0K"};
43 std::string enc_ldap
{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAibGRhcCIsCiAgICAgICAgImlkIjogIlNtb25ueSIsCiAgICAgICAgImtleSI6ICJUdXJqYW4gb2YgTWlpciIKICAgIH0KfQo="};
45 std::string non_base64
{"stuff here"};
46 std::string non_base64_sploded
{"90KLscc0Dz4U49HX-7Tx"};
48 Formatter
* token_formatter
{nullptr};
55 token_formatter
= new JSONFormatter(true /* pretty */);
56 ASSERT_NE(token_formatter
, nullptr);
60 // encode the two supported types
61 RGWToken
token_ad(RGWToken::TOKEN_AD
, access_key
, secret_key
);
62 ASSERT_EQ(token_ad
.encode_json_base64(token_formatter
), enc_ad
);
63 tokens
.push_back(token_ad
); // provies copiable
65 RGWToken
token_ldap(RGWToken::TOKEN_LDAP
, access_key
, secret_key
);
66 ASSERT_EQ(token_ldap
.encode_json_base64(token_formatter
), enc_ldap
);
67 tokens
.push_back(token_ldap
);
71 for (const auto& enc_tok
: {enc_ad
, enc_ldap
}) {
72 RGWToken token
{from_base64(enc_tok
)}; // decode ctor
73 ASSERT_EQ(token
.id
, access_key
);
74 ASSERT_EQ(token
.key
, secret_key
);
79 std::string empty
{""};
80 RGWToken token
{from_base64(empty
)}; // decode ctor
81 ASSERT_FALSE(token
.valid());
84 TEST(TOKEN
, BADINPUT
) {
85 RGWToken token
{from_base64(non_base64
)}; // decode ctor
86 ASSERT_FALSE(token
.valid());
89 TEST(TOKEN
, BADINPUT2
) {
90 RGWToken token
{from_base64(non_base64_sploded
)}; // decode ctor
91 ASSERT_FALSE(token
.valid());
94 TEST(TOKEN
, BADINPUT3
) {
96 std::string stuff
= from_base64(non_base64_sploded
); // decode
103 TEST(TOKEN
, SHUTDOWN
) {
104 delete token_formatter
;
107 int main(int argc
, char *argv
[])
109 auto args
= argv_to_vec(argc
, argv
);
113 for (auto arg_iter
= args
.begin(); arg_iter
!= args
.end();) {
114 if (ceph_argparse_flag(args
, arg_iter
, "--verbose",
122 ::testing::InitGoogleTest(&argc
, argv
);
123 return RUN_ALL_TESTS();