mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 17:28:37 +00:00
Added more encoding tests
This commit is contained in:
parent
9a7925cca5
commit
98ea0d6e58
2 changed files with 128 additions and 2 deletions
Binary file not shown.
|
@ -1,12 +1,13 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "jwt/jwt.hpp"
|
#include "jwt/jwt.hpp"
|
||||||
|
|
||||||
TEST (EncodeTest, Mytest1)
|
TEST (EncodeTest, StrEncodeHS2561)
|
||||||
{
|
{
|
||||||
using namespace jwt::params;
|
using namespace jwt::params;
|
||||||
|
|
||||||
const std::string expected_sign =
|
const char* expected_sign =
|
||||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
||||||
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
|
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
|
||||||
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
|
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
|
||||||
|
@ -29,6 +30,131 @@ TEST (EncodeTest, Mytest1)
|
||||||
EXPECT_EQ (enc_str, expected_sign);
|
EXPECT_EQ (enc_str, expected_sign);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST (EncodeTest, StrEncodeHS2562)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* expected_sign =
|
||||||
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
||||||
|
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
|
||||||
|
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm("HS256"),
|
||||||
|
secret("secret"),
|
||||||
|
payload(
|
||||||
|
{
|
||||||
|
{"iss", "arun.muralidharan"},
|
||||||
|
{"sub", "admin"},
|
||||||
|
{"id", "a-b-c-d-e-f-1-2-3"}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
obj.add_claim("iat", 1513862371);
|
||||||
|
|
||||||
|
std::string enc_str = obj.signature();
|
||||||
|
EXPECT_EQ (enc_str, expected_sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST (EncodeTest, StrEncodeNONE)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* expected_sign =
|
||||||
|
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm("none")};
|
||||||
|
|
||||||
|
obj.add_claim("aud", "rift.io")
|
||||||
|
.add_claim("exp", 1513863371)
|
||||||
|
.add_claim("sub", "nothing much")
|
||||||
|
;
|
||||||
|
|
||||||
|
std::cout << "Header: " << obj.header() << std::endl;
|
||||||
|
std::cout << "Payload: " << obj.payload() << std::endl;
|
||||||
|
|
||||||
|
std::string enc_str = obj.signature();
|
||||||
|
|
||||||
|
EXPECT_EQ (enc_str, expected_sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (EncodeTest, StrEncodeHS256WithKey)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* expected_sign =
|
||||||
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
||||||
|
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
|
||||||
|
"W6t7mUX6ZJwOVTsVhHSKyBSwi0wnibobdsk456wSmJg";
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm(jwt::algorithm::HS256),
|
||||||
|
secret("0123456789abcdefghijklmnopqrstuvwxyz"),
|
||||||
|
payload(
|
||||||
|
{
|
||||||
|
{"aud", "rift.io"},
|
||||||
|
{"sub", "nothing much"}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
obj.add_claim("exp", 1513863371);
|
||||||
|
|
||||||
|
std::string enc_str = obj.signature();
|
||||||
|
|
||||||
|
EXPECT_EQ (expected_sign, enc_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (EncodeTest, StrEncodeHS384WithKey)
|
||||||
|
{
|
||||||
|
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* expected_sign =
|
||||||
|
"eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9."
|
||||||
|
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
|
||||||
|
"cGN4FZCe9Y2c1dA-jP71IXGnYbJRc4OaUTa5m7N7ybF5h6wBwxWQ-pdcxYchjDBL";
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm(jwt::algorithm::HS384),
|
||||||
|
secret("0123456789abcdefghijklmnopqrstuvwxyz"),
|
||||||
|
payload(
|
||||||
|
{
|
||||||
|
{"aud", "rift.io"},
|
||||||
|
{"sub", "nothing much"}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
obj.add_claim("exp", 1513863371);
|
||||||
|
|
||||||
|
std::string enc_str = obj.signature();
|
||||||
|
|
||||||
|
EXPECT_EQ (expected_sign, enc_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (EncodeTest, StrEncodeHS512WithKey)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* expected_sign =
|
||||||
|
"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9."
|
||||||
|
"eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."
|
||||||
|
"vQ-1JSFN1kPjUI3URP6AFK5z8V7xLhyhw-76QWhQg9Xcy-IgrJ-bCTYLBjgaprrcEWwpSnBQnP3QnIxYK0HEaQ";
|
||||||
|
|
||||||
|
jwt::string_view key = "00112233445566778899";
|
||||||
|
|
||||||
|
//TODO: map of jwt::string_view not working
|
||||||
|
|
||||||
|
std::map<std::string, std::string> p;
|
||||||
|
p["aud"] = "rift.io";
|
||||||
|
p["sub"] = "nothing much";
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm(jwt::algorithm::HS512),
|
||||||
|
secret(key),
|
||||||
|
payload(std::move(p))
|
||||||
|
};
|
||||||
|
obj.add_claim("exp", 1513863371);
|
||||||
|
|
||||||
|
std::string enc_str = obj.signature();
|
||||||
|
|
||||||
|
EXPECT_EQ (enc_str, expected_sign);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue