diff --git a/tests/test_jwt_decode_verifiy_with_exception b/tests/test_jwt_decode_verifiy_with_exception new file mode 100755 index 0000000..f5aadf1 Binary files /dev/null and b/tests/test_jwt_decode_verifiy_with_exception differ diff --git a/tests/test_jwt_decode_verifiy_with_exception.cc b/tests/test_jwt_decode_verifiy_with_exception.cc new file mode 100644 index 0000000..db93b40 --- /dev/null +++ b/tests/test_jwt_decode_verifiy_with_exception.cc @@ -0,0 +1,170 @@ +#include +#include +#include + +#include "jwt/jwt.hpp" +#include "gtest/gtest.h" + +TEST (DecodeVerifyExp, BeforeExpiryTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret")}; + obj.add_claim("iss", "arun.muralidharan") + .add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10}) + ; + + auto enc_str = obj.signature(); + + auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true)); +} + +TEST (DecodeVerifyExp, AfterExpiryTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret")}; + obj.add_claim("iss", "arun.muralidharan") + .add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1}) + ; + + auto enc_str = obj.signature(); + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true)), + jwt::TokenExpiredError); + +} + +TEST (DecodeVerifyExp, AfterExpiryWithLeeway) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret")}; + obj.add_claim("iss", "arun.muralidharan") + .add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1}) + ; + + auto enc_str = obj.signature(); + auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), verify(true), leeway(2)); + (void)dec_obj; +} + +TEST (DecodeVerifyExp, ValidIssuerTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret")}; + obj.add_claim("iss", "arun.muralidharan") + .add_claim("sub", "test") + ; + + auto enc_str = obj.signature(); + + auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.muralidharan")); + (void)dec_obj; +} + +TEST (DecodeVerifyExp, InvalidIssuerTest_1) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})}; + auto enc_str = obj.signature(); + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.muralidharan")), + jwt::InvalidIssuerError); + +} + +TEST (DecodeVerifyExp, InvalidIssuerTest_2) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})}; + obj.add_claim("iss", "arun.muralidharan"); + + auto enc_str = obj.signature(); + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), issuer("arun.murali")), + jwt::InvalidIssuerError); +} + +TEST (DecodeVerifyExp, NotImmatureSignatureTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})}; + obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() - std::chrono::seconds{10}); + + auto enc_str = obj.signature(); + + auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret")); + (void)dec_obj; +} + +TEST (DecodeVerifyExp, ImmatureSignatureTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})}; + obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10}); + + auto enc_str = obj.signature(); + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret")), + jwt::ImmatureSignatureError); +} + +TEST (DecodeVerifyExp, ImmatureSignatureTestWithLeeway) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}})}; + obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10}); + + auto enc_str = obj.signature(); + + auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), leeway(10)); + (void)dec_obj; +} + +TEST (DecodeVerifyExp, InvalidAudienceTest) +{ + using namespace jwt::params; + + jwt::jwt_object obj{algorithm("hs256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})}; + + auto enc_str = obj.signature(); + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"hs256"}), secret("secret"), aud("ww")), + jwt::InvalidAudienceError); +} + +TEST (DecodeVerifyExp, InvalidSignatureTest) +{ + using namespace jwt::params; + + const char* inv_enc_str = + "eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ"; + + EXPECT_THROW (jwt::decode(inv_enc_str, algorithms({"none", "hs256"})), + jwt::SignatureFormatError); +} + +TEST (DecodeVerifyExp, KeyNotPresentTest) +{ + using namespace jwt::params; + + const char* enc_str = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." + "eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0." + "jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk"; + + EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "hs256"}), verify(false)), + jwt::KeyNotPresentError); +} + +int main(int argc, char* argv[]) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}