File: base64_test.cc

package info (click to toggle)
prometheus-cpp 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: cpp: 3,596; sh: 37; makefile: 12
file content (49 lines) | stat: -rw-r--r-- 1,011 bytes parent folder | download | duplicates (3)
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
#include "detail/base64.h"

#include <gtest/gtest.h>

#include <string>

namespace prometheus {
namespace {

struct TestVector {
  const std::string decoded;
  const std::string encoded;
};

const TestVector testVector[] = {
    {"", ""},
    {"f", "Zg=="},
    {"fo", "Zm8="},
    {"foo", "Zm9v"},
    {"foob", "Zm9vYg=="},
    {"fooba", "Zm9vYmE="},
    {"foobar", "Zm9vYmFy"},
};

const unsigned nVectors = sizeof(testVector) / sizeof(testVector[0]);

using namespace testing;

TEST(Base64Test, decodeTest) {
  for (unsigned i = 0; i < nVectors; ++i) {
    std::string decoded = detail::base64_decode(testVector[i].encoded);
    EXPECT_EQ(testVector[i].decoded, decoded);
  }
}

TEST(Base64Test, rejectInvalidSymbols) {
  EXPECT_ANY_THROW(detail::base64_decode("...."));
}

TEST(Base64Test, rejectInvalidInputSize) {
  EXPECT_ANY_THROW(detail::base64_decode("ABC"));
}

TEST(Base64Test, rejectInvalidPadding) {
  EXPECT_ANY_THROW(detail::base64_decode("A==="));
}

}  // namespace
}  // namespace prometheus