summaryrefslogtreecommitdiffstats
path: root/src/test/test_rgw_token.cc
blob: 6136b6043c55f2dc66d662fdff29cfe369c10cfa (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
121
122
123
124
// -*- 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) 2016 Red Hat, Inc.
 *
 * 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 <errno.h>
#include <iostream>
#include <sstream>
#include <string>

#include "common/config.h"
#include "common/ceph_argparse.h"
#include "common/debug.h"
#include "include/ceph_assert.h"
#include "gtest/gtest.h"
#include "rgw_token.h"
#include "rgw_b64.h"

#define dout_subsys ceph_subsys_rgw

namespace {

  using namespace rgw;
  using std::get;
  using std::string;

  string access_key{"Smonny"};
  string secret_key{"Turjan of Miir"};

  std::vector<RGWToken> tokens;

  std::string enc_ad{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAiYWQiLAogICAgICAgICJpZCI6ICJTbW9ubnkiLAogICAgICAgICJrZXkiOiAiVHVyamFuIG9mIE1paXIiCiAgICB9Cn0K"};

  std::string enc_ldap{"ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAibGRhcCIsCiAgICAgICAgImlkIjogIlNtb25ueSIsCiAgICAgICAgImtleSI6ICJUdXJqYW4gb2YgTWlpciIKICAgIH0KfQo="};

  std::string non_base64{"stuff here"};
  std::string non_base64_sploded{"90KLscc0Dz4U49HX-7Tx"};

  Formatter* token_formatter{nullptr};
  bool verbose {false};
}

using namespace std;

TEST(TOKEN, INIT) {
  token_formatter = new JSONFormatter(true /* pretty */);
  ASSERT_NE(token_formatter, nullptr);
}

TEST(TOKEN, ENCODE) {
  // encode the two supported types
  RGWToken token_ad(RGWToken::TOKEN_AD, access_key, secret_key);
  ASSERT_EQ(token_ad.encode_json_base64(token_formatter), enc_ad);
  tokens.push_back(token_ad); // provies copiable

  RGWToken token_ldap(RGWToken::TOKEN_LDAP, access_key, secret_key);
  ASSERT_EQ(token_ldap.encode_json_base64(token_formatter), enc_ldap);
  tokens.push_back(token_ldap);
}

TEST(TOKEN, DECODE) {
  for (const auto& enc_tok : {enc_ad, enc_ldap}) {
    RGWToken token{from_base64(enc_tok)}; // decode ctor
    ASSERT_EQ(token.id, access_key);
    ASSERT_EQ(token.key, secret_key);
  }
}

TEST(TOKEN, EMPTY) {
    std::string empty{""};
    RGWToken token{from_base64(empty)}; // decode ctor
    ASSERT_FALSE(token.valid());
}

TEST(TOKEN, BADINPUT) {
    RGWToken token{from_base64(non_base64)}; // decode ctor
    ASSERT_FALSE(token.valid());
}

TEST(TOKEN, BADINPUT2) {
    RGWToken token{from_base64(non_base64_sploded)}; // decode ctor
    ASSERT_FALSE(token.valid());
}

TEST(TOKEN, BADINPUT3) {
  try {
    std::string stuff = from_base64(non_base64_sploded); // decode
  } catch(...) {
    // do nothing
  }
  ASSERT_EQ(1, 1);
}

TEST(TOKEN, SHUTDOWN) {
  delete token_formatter;
}

int main(int argc, char *argv[])
{
  auto args = argv_to_vec(argc, argv);
  env_to_vec(args);

  string val;
  for (auto arg_iter = args.begin(); arg_iter != args.end();) {
    if (ceph_argparse_flag(args, arg_iter, "--verbose",
			      (char*) nullptr)) {
      verbose = true;
    } else {
      ++arg_iter;
    }
  }

  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}