Skip to content

Commit

Permalink
Add using namespace jwt to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Aug 18, 2023
1 parent ce1f9df commit 03aa479
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 148 deletions.
140 changes: 71 additions & 69 deletions tests/BaseTest.cpp
Original file line number Diff line number Diff line change
@@ -1,110 +1,112 @@
#include "jwt-cpp/base.h"
#include <gtest/gtest.h>

using namespace jwt;

TEST(BaseTest, Base64Index) {
ASSERT_EQ(0, jwt::alphabet::index(jwt::alphabet::base64::data(), 'A'));
ASSERT_EQ(32, jwt::alphabet::index(jwt::alphabet::base64::data(), 'g'));
ASSERT_EQ(62, jwt::alphabet::index(jwt::alphabet::base64::data(), '+'));
ASSERT_EQ(0, alphabet::index(alphabet::base64::data(), 'A'));
ASSERT_EQ(32, alphabet::index(alphabet::base64::data(), 'g'));
ASSERT_EQ(62, alphabet::index(alphabet::base64::data(), '+'));
}

TEST(BaseTest, Base64URLIndex) {
ASSERT_EQ(0, jwt::alphabet::index(jwt::alphabet::base64url::data(), 'A'));
ASSERT_EQ(32, jwt::alphabet::index(jwt::alphabet::base64url::data(), 'g'));
ASSERT_EQ(62, jwt::alphabet::index(jwt::alphabet::base64url::data(), '-'));
ASSERT_EQ(0, alphabet::index(alphabet::base64url::data(), 'A'));
ASSERT_EQ(32, alphabet::index(alphabet::base64url::data(), 'g'));
ASSERT_EQ(62, alphabet::index(alphabet::base64url::data(), '-'));
}

TEST(BaseTest, BaseDetailsCountPadding) {
using jwt::base::details::padding;
ASSERT_EQ(padding{}, jwt::base::details::count_padding("ABC", {"~"}));
ASSERT_EQ((padding{3, 3}), jwt::base::details::count_padding("ABC~~~", {"~"}));
ASSERT_EQ((padding{5, 5}), jwt::base::details::count_padding("ABC~~~~~", {"~"}));

ASSERT_EQ(padding{}, jwt::base::details::count_padding("ABC", {"~", "!"}));
ASSERT_EQ((padding{1, 1}), jwt::base::details::count_padding("ABC!", {"~", "!"}));
ASSERT_EQ((padding{1, 1}), jwt::base::details::count_padding("ABC~", {"~", "!"}));
ASSERT_EQ((padding{3, 3}), jwt::base::details::count_padding("ABC~~!", {"~", "!"}));
ASSERT_EQ((padding{3, 3}), jwt::base::details::count_padding("ABC!~~", {"~", "!"}));
ASSERT_EQ((padding{5, 5}), jwt::base::details::count_padding("ABC~~!~~", {"~", "!"}));

ASSERT_EQ((padding{2, 6}), jwt::base::details::count_padding("MTIzNA%3d%3d", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), jwt::base::details::count_padding("MTIzNA%3d%3D", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), jwt::base::details::count_padding("MTIzNA%3D%3d", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), jwt::base::details::count_padding("MTIzNA%3D%3D", {"%3d", "%3D"}));
using base::details::padding;
ASSERT_EQ(padding{}, base::details::count_padding("ABC", {"~"}));
ASSERT_EQ((padding{3, 3}), base::details::count_padding("ABC~~~", {"~"}));
ASSERT_EQ((padding{5, 5}), base::details::count_padding("ABC~~~~~", {"~"}));

ASSERT_EQ(padding{}, base::details::count_padding("ABC", {"~", "!"}));
ASSERT_EQ((padding{1, 1}), base::details::count_padding("ABC!", {"~", "!"}));
ASSERT_EQ((padding{1, 1}), base::details::count_padding("ABC~", {"~", "!"}));
ASSERT_EQ((padding{3, 3}), base::details::count_padding("ABC~~!", {"~", "!"}));
ASSERT_EQ((padding{3, 3}), base::details::count_padding("ABC!~~", {"~", "!"}));
ASSERT_EQ((padding{5, 5}), base::details::count_padding("ABC~~!~~", {"~", "!"}));

ASSERT_EQ((padding{2, 6}), base::details::count_padding("MTIzNA%3d%3d", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), base::details::count_padding("MTIzNA%3d%3D", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), base::details::count_padding("MTIzNA%3D%3d", {"%3d", "%3D"}));
ASSERT_EQ((padding{2, 6}), base::details::count_padding("MTIzNA%3D%3D", {"%3d", "%3D"}));

// Some fake scenarios

ASSERT_EQ(padding{}, jwt::base::details::count_padding("", {"~"}));
ASSERT_EQ(padding{}, jwt::base::details::count_padding("ABC", {"~", "~~!"}));
ASSERT_EQ(padding{}, jwt::base::details::count_padding("ABC!", {"~", "~~!"}));
ASSERT_EQ((padding{1, 1}), jwt::base::details::count_padding("ABC~", {"~", "~~!"}));
ASSERT_EQ((padding{1, 3}), jwt::base::details::count_padding("ABC~~!", {"~", "~~!"}));
ASSERT_EQ((padding{2, 2}), jwt::base::details::count_padding("ABC!~~", {"~", "~~!"}));
ASSERT_EQ((padding{3, 5}), jwt::base::details::count_padding("ABC~~!~~", {"~", "~~!"}));
ASSERT_EQ(padding{}, jwt::base::details::count_padding("ABC~~!~~", {}));
ASSERT_EQ(padding{}, base::details::count_padding("", {"~"}));
ASSERT_EQ(padding{}, base::details::count_padding("ABC", {"~", "~~!"}));
ASSERT_EQ(padding{}, base::details::count_padding("ABC!", {"~", "~~!"}));
ASSERT_EQ((padding{1, 1}), base::details::count_padding("ABC~", {"~", "~~!"}));
ASSERT_EQ((padding{1, 3}), base::details::count_padding("ABC~~!", {"~", "~~!"}));
ASSERT_EQ((padding{2, 2}), base::details::count_padding("ABC!~~", {"~", "~~!"}));
ASSERT_EQ((padding{3, 5}), base::details::count_padding("ABC~~!~~", {"~", "~~!"}));
ASSERT_EQ(padding{}, base::details::count_padding("ABC~~!~~", {}));
}

TEST(BaseTest, Base64Decode) {
ASSERT_EQ("1", jwt::base::decode<jwt::alphabet::base64>("MQ=="));
ASSERT_EQ("12", jwt::base::decode<jwt::alphabet::base64>("MTI="));
ASSERT_EQ("123", jwt::base::decode<jwt::alphabet::base64>("MTIz"));
ASSERT_EQ("1234", jwt::base::decode<jwt::alphabet::base64>("MTIzNA=="));
ASSERT_EQ("1", base::decode<alphabet::base64>("MQ=="));
ASSERT_EQ("12", base::decode<alphabet::base64>("MTI="));
ASSERT_EQ("123", base::decode<alphabet::base64>("MTIz"));
ASSERT_EQ("1234", base::decode<alphabet::base64>("MTIzNA=="));
}

TEST(BaseTest, Base64DecodeURL) {
ASSERT_EQ("1", jwt::base::decode<jwt::alphabet::base64url>("MQ%3d%3d"));
ASSERT_EQ("12", jwt::base::decode<jwt::alphabet::base64url>("MTI%3d"));
ASSERT_EQ("123", jwt::base::decode<jwt::alphabet::base64url>("MTIz"));
ASSERT_EQ("1234", jwt::base::decode<jwt::alphabet::base64url>("MTIzNA%3d%3d"));
ASSERT_EQ("1", base::decode<alphabet::base64url>("MQ%3d%3d"));
ASSERT_EQ("12", base::decode<alphabet::base64url>("MTI%3d"));
ASSERT_EQ("123", base::decode<alphabet::base64url>("MTIz"));
ASSERT_EQ("1234", base::decode<alphabet::base64url>("MTIzNA%3d%3d"));
}

TEST(BaseTest, Base64DecodeURLCaseInsensitive) {
ASSERT_EQ("1", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MQ%3d%3d"));
ASSERT_EQ("1", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MQ%3D%3d"));
ASSERT_EQ("1", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MQ%3d%3D"));
ASSERT_EQ("12", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MTI%3d"));
ASSERT_EQ("123", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MTIz"));
ASSERT_EQ("1234", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MTIzNA%3d%3d"));
ASSERT_EQ("1234", jwt::base::decode<jwt::alphabet::helper::base64url_percent_encoding>("MTIzNA%3D%3D"));
ASSERT_EQ("1", base::decode<alphabet::helper::base64url_percent_encoding>("MQ%3d%3d"));
ASSERT_EQ("1", base::decode<alphabet::helper::base64url_percent_encoding>("MQ%3D%3d"));
ASSERT_EQ("1", base::decode<alphabet::helper::base64url_percent_encoding>("MQ%3d%3D"));
ASSERT_EQ("12", base::decode<alphabet::helper::base64url_percent_encoding>("MTI%3d"));
ASSERT_EQ("123", base::decode<alphabet::helper::base64url_percent_encoding>("MTIz"));
ASSERT_EQ("1234", base::decode<alphabet::helper::base64url_percent_encoding>("MTIzNA%3d%3d"));
ASSERT_EQ("1234", base::decode<alphabet::helper::base64url_percent_encoding>("MTIzNA%3D%3D"));
}

TEST(BaseTest, Base64Encode) {
ASSERT_EQ("MQ==", jwt::base::encode<jwt::alphabet::base64>("1"));
ASSERT_EQ("MTI=", jwt::base::encode<jwt::alphabet::base64>("12"));
ASSERT_EQ("MTIz", jwt::base::encode<jwt::alphabet::base64>("123"));
ASSERT_EQ("MTIzNA==", jwt::base::encode<jwt::alphabet::base64>("1234"));
ASSERT_EQ("MQ==", base::encode<alphabet::base64>("1"));
ASSERT_EQ("MTI=", base::encode<alphabet::base64>("12"));
ASSERT_EQ("MTIz", base::encode<alphabet::base64>("123"));
ASSERT_EQ("MTIzNA==", base::encode<alphabet::base64>("1234"));
}

TEST(BaseTest, Base64EncodeURL) {
ASSERT_EQ("MQ%3d%3d", jwt::base::encode<jwt::alphabet::base64url>("1"));
ASSERT_EQ("MTI%3d", jwt::base::encode<jwt::alphabet::base64url>("12"));
ASSERT_EQ("MTIz", jwt::base::encode<jwt::alphabet::base64url>("123"));
ASSERT_EQ("MTIzNA%3d%3d", jwt::base::encode<jwt::alphabet::base64url>("1234"));
ASSERT_EQ("MQ%3d%3d", base::encode<alphabet::base64url>("1"));
ASSERT_EQ("MTI%3d", base::encode<alphabet::base64url>("12"));
ASSERT_EQ("MTIz", base::encode<alphabet::base64url>("123"));
ASSERT_EQ("MTIzNA%3d%3d", base::encode<alphabet::base64url>("1234"));
}

TEST(BaseTest, Base64Pad) {
ASSERT_EQ("MQ==", jwt::base::pad<jwt::alphabet::base64>("MQ"));
ASSERT_EQ("MTI=", jwt::base::pad<jwt::alphabet::base64>("MTI"));
ASSERT_EQ("MTIz", jwt::base::pad<jwt::alphabet::base64>("MTIz"));
ASSERT_EQ("MTIzNA==", jwt::base::pad<jwt::alphabet::base64>("MTIzNA"));
ASSERT_EQ("MQ==", base::pad<alphabet::base64>("MQ"));
ASSERT_EQ("MTI=", base::pad<alphabet::base64>("MTI"));
ASSERT_EQ("MTIz", base::pad<alphabet::base64>("MTIz"));
ASSERT_EQ("MTIzNA==", base::pad<alphabet::base64>("MTIzNA"));
}

TEST(BaseTest, Base64PadURL) {
ASSERT_EQ("MQ%3d%3d", jwt::base::pad<jwt::alphabet::base64url>("MQ"));
ASSERT_EQ("MTI%3d", jwt::base::pad<jwt::alphabet::base64url>("MTI"));
ASSERT_EQ("MTIz", jwt::base::pad<jwt::alphabet::base64url>("MTIz"));
ASSERT_EQ("MTIzNA%3d%3d", jwt::base::pad<jwt::alphabet::base64url>("MTIzNA"));
ASSERT_EQ("MQ%3d%3d", base::pad<alphabet::base64url>("MQ"));
ASSERT_EQ("MTI%3d", base::pad<alphabet::base64url>("MTI"));
ASSERT_EQ("MTIz", base::pad<alphabet::base64url>("MTIz"));
ASSERT_EQ("MTIzNA%3d%3d", base::pad<alphabet::base64url>("MTIzNA"));
}

TEST(BaseTest, Base64Trim) {
ASSERT_EQ("MQ", jwt::base::trim<jwt::alphabet::base64>("MQ=="));
ASSERT_EQ("MTI", jwt::base::trim<jwt::alphabet::base64>("MTI="));
ASSERT_EQ("MTIz", jwt::base::trim<jwt::alphabet::base64>("MTIz"));
ASSERT_EQ("MTIzNA", jwt::base::trim<jwt::alphabet::base64>("MTIzNA=="));
ASSERT_EQ("MQ", base::trim<alphabet::base64>("MQ=="));
ASSERT_EQ("MTI", base::trim<alphabet::base64>("MTI="));
ASSERT_EQ("MTIz", base::trim<alphabet::base64>("MTIz"));
ASSERT_EQ("MTIzNA", base::trim<alphabet::base64>("MTIzNA=="));
}

TEST(BaseTest, Base64TrimURL) {
ASSERT_EQ("MQ", jwt::base::trim<jwt::alphabet::base64url>("MQ%3d%3d"));
ASSERT_EQ("MTI", jwt::base::trim<jwt::alphabet::base64url>("MTI%3d"));
ASSERT_EQ("MTIz", jwt::base::trim<jwt::alphabet::base64url>("MTIz"));
ASSERT_EQ("MTIzNA", jwt::base::trim<jwt::alphabet::base64url>("MTIzNA%3d%3d"));
ASSERT_EQ("MQ", base::trim<alphabet::base64url>("MQ%3d%3d"));
ASSERT_EQ("MTI", base::trim<alphabet::base64url>("MTI%3d"));
ASSERT_EQ("MTIz", base::trim<alphabet::base64url>("MTIz"));
ASSERT_EQ("MTIzNA", base::trim<alphabet::base64url>("MTIzNA%3d%3d"));
}
63 changes: 32 additions & 31 deletions tests/ClaimTest.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "jwt-cpp/jwt.h"
#include <gtest/gtest.h>

using namespace jwt;

TEST(ClaimTest, AudienceAsString) {
std::string token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4";
auto decoded = jwt::decode(token);
auto decoded = decode(token);

ASSERT_TRUE(decoded.has_algorithm());
ASSERT_TRUE(decoded.has_type());
Expand All @@ -26,14 +28,14 @@ TEST(ClaimTest, AudienceAsString) {
}

TEST(ClaimTest, SetAudienceAsString) {
auto token = jwt::create().set_type("JWT").set_audience("test").sign(jwt::algorithm::hs256("test"));
auto token = create().set_type("JWT").set_audience("test").sign(algorithm::hs256("test"));
ASSERT_EQ("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.ny5Fa0vzAg7tNL95KWg_ecBNd3XP3tdAzq0SFA6diY4",
token);
}

TEST(ClaimTest, AudienceAsSet) {
std::string token = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdWQiOlsidGVzdCIsInRlc3QyIl19.";
auto decoded = jwt::decode(token);
auto decoded = decode(token);

ASSERT_TRUE(decoded.has_algorithm());
ASSERT_TRUE(decoded.has_type());
Expand All @@ -56,74 +58,73 @@ TEST(ClaimTest, AudienceAsSet) {
}

TEST(ClaimTest, SetAudienceAsSet) {
auto token = jwt::create()
auto token = create()
.set_type("JWT")
.set_audience({{picojson::value("test"), picojson::value("test2")}})
.sign(jwt::algorithm::none{});
.sign(algorithm::none{});
ASSERT_EQ("eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdWQiOlsidGVzdCIsInRlc3QyIl19.", token);
}

TEST(ClaimTest, SetArray) {
std::vector<int64_t> vect = {100, 20, 10};
auto token =
jwt::create().set_payload_claim("test", jwt::claim(vect.begin(), vect.end())).sign(jwt::algorithm::none{});
auto token = create().set_payload_claim("test", claim(vect.begin(), vect.end())).sign(algorithm::none{});
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
}

TEST(ClaimTest, SetObject) {
std::istringstream iss{"{\"api-x\": [1]}"};
jwt::claim object;
claim object;
iss >> object;
ASSERT_EQ(object.get_type(), jwt::json::type::object);
ASSERT_EQ(object.get_type(), json::type::object);

auto token = jwt::create().set_payload_claim("namespace", object).sign(jwt::algorithm::hs256("test"));
auto token = create().set_payload_claim("namespace", object).sign(algorithm::hs256("test"));
ASSERT_EQ(token,
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA");
}

TEST(ClaimTest, SetAlgorithm) {
auto token = jwt::create().set_algorithm("test").sign(jwt::algorithm::none{});
auto token = create().set_algorithm("test").sign(algorithm::none{});

auto decoded_token = jwt::decode(token);
auto decoded_token = decode(token);
ASSERT_EQ(decoded_token.get_algorithm(), "test");
}

TEST(ClaimTest, AsInt) {
jwt::claim c(picojson::value(static_cast<int64_t>(10)));
claim c(picojson::value(static_cast<int64_t>(10)));
ASSERT_EQ(c.as_integer(), 10);
}

TEST(ClaimTest, AsDate) {
jwt::claim c(picojson::value(static_cast<int64_t>(10)));
claim c(picojson::value(static_cast<int64_t>(10)));
ASSERT_EQ(c.as_date(), std::chrono::system_clock::from_time_t(10));
}

TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) {
jwt::traits::kazuho_picojson::value_type val;
ASSERT_THROW(jwt::traits::kazuho_picojson::as_array(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::as_boolean(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::as_integer(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::as_number(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::as_object(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::as_string(val), std::bad_cast);
ASSERT_THROW(jwt::traits::kazuho_picojson::get_type(val), std::logic_error);
traits::kazuho_picojson::value_type val;
ASSERT_THROW(traits::kazuho_picojson::as_array(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::as_boolean(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::as_integer(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::as_number(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::as_object(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::as_string(val), std::bad_cast);
ASSERT_THROW(traits::kazuho_picojson::get_type(val), std::logic_error);
}

TEST(ClaimTest, PicoJSONTraitsAsBool) {
jwt::traits::kazuho_picojson::value_type val(true);
ASSERT_EQ(jwt::traits::kazuho_picojson::as_boolean(val), true);
ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::boolean);
traits::kazuho_picojson::value_type val(true);
ASSERT_EQ(traits::kazuho_picojson::as_boolean(val), true);
ASSERT_EQ(traits::kazuho_picojson::get_type(val), json::type::boolean);
}

TEST(ClaimTest, PicoJSONTraitsAsDouble) {
jwt::traits::kazuho_picojson::value_type val(10.0);
ASSERT_EQ(jwt::traits::kazuho_picojson::as_number(val), (int)10);
ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::number);
traits::kazuho_picojson::value_type val(10.0);
ASSERT_EQ(traits::kazuho_picojson::as_number(val), (int)10);
ASSERT_EQ(traits::kazuho_picojson::get_type(val), json::type::number);
}

TEST(ClaimTest, MapOfClaim) {
using map = jwt::details::map_of_claims<jwt::traits::kazuho_picojson>;
ASSERT_THROW(map::parse_claims(R"##(__ not json __)##"), jwt::error::invalid_json_exception);
using map = details::map_of_claims<traits::kazuho_picojson>;
ASSERT_THROW(map::parse_claims(R"##(__ not json __)##"), error::invalid_json_exception);
const map claims{
map::parse_claims(R"##({ "array": [1], "string" : "hello world", "number": 9.9, "bool": true})##")};

Expand All @@ -137,5 +138,5 @@ TEST(ClaimTest, MapOfClaim) {
ASSERT_EQ(claims.get_claim("string").as_string(), "hello world");
ASSERT_EQ(claims.get_claim("number").as_number(), 9.9);
ASSERT_EQ(claims.get_claim("bool").as_boolean(), true);
ASSERT_THROW(claims.get_claim("__missing__"), jwt::error::claim_not_present_exception);
ASSERT_THROW(claims.get_claim("__missing__"), error::claim_not_present_exception);
}
Loading

0 comments on commit 03aa479

Please sign in to comment.