1*4b347fccSAndrew Lindesay /*
2*4b347fccSAndrew Lindesay * Copyright 2023, Andrew Lindesay <apl@lindesay.co.nz>.
3*4b347fccSAndrew Lindesay * All rights reserved. Distributed under the terms of the MIT License.
4*4b347fccSAndrew Lindesay */
5*4b347fccSAndrew Lindesay
6*4b347fccSAndrew Lindesay
7*4b347fccSAndrew Lindesay #include "JwtTokenHelperTest.h"
8*4b347fccSAndrew Lindesay
9*4b347fccSAndrew Lindesay #include <String.h>
10*4b347fccSAndrew Lindesay
11*4b347fccSAndrew Lindesay #include <cppunit/TestCaller.h>
12*4b347fccSAndrew Lindesay #include <cppunit/TestSuite.h>
13*4b347fccSAndrew Lindesay
14*4b347fccSAndrew Lindesay #include <string.h>
15*4b347fccSAndrew Lindesay
16*4b347fccSAndrew Lindesay #include "JwtTokenHelper.h"
17*4b347fccSAndrew Lindesay
18*4b347fccSAndrew Lindesay
JwtTokenHelperTest()19*4b347fccSAndrew Lindesay JwtTokenHelperTest::JwtTokenHelperTest()
20*4b347fccSAndrew Lindesay {
21*4b347fccSAndrew Lindesay }
22*4b347fccSAndrew Lindesay
23*4b347fccSAndrew Lindesay
~JwtTokenHelperTest()24*4b347fccSAndrew Lindesay JwtTokenHelperTest::~JwtTokenHelperTest()
25*4b347fccSAndrew Lindesay {
26*4b347fccSAndrew Lindesay }
27*4b347fccSAndrew Lindesay
28*4b347fccSAndrew Lindesay
29*4b347fccSAndrew Lindesay void
TestParseTokenClaims()30*4b347fccSAndrew Lindesay JwtTokenHelperTest::TestParseTokenClaims()
31*4b347fccSAndrew Lindesay {
32*4b347fccSAndrew Lindesay const char* jwtToken = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkZXYuaGRzIiwic3ViIj"
33*4b347fccSAndrew Lindesay "oiZXJpazY0QGhkcyIsImV4cCI6MTY5MzkwNzM1NywiaWF0IjoxNjkzOTA3MDU3fQ.DJOz0"
34*4b347fccSAndrew Lindesay "TmgN0Ya8De-oV0mBwWb-8FYavLbaFUFhCLqr-s";
35*4b347fccSAndrew Lindesay BMessage actualMessage;
36*4b347fccSAndrew Lindesay
37*4b347fccSAndrew Lindesay // ----------------------
38*4b347fccSAndrew Lindesay status_t result = JwtTokenHelper::ParseClaims(BString(jwtToken), actualMessage);
39*4b347fccSAndrew Lindesay // ----------------------
40*4b347fccSAndrew Lindesay
41*4b347fccSAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, result);
42*4b347fccSAndrew Lindesay
43*4b347fccSAndrew Lindesay _AssertStringValue(actualMessage, "iss", "dev.hds");
44*4b347fccSAndrew Lindesay _AssertStringValue(actualMessage, "sub", "erik64@hds");
45*4b347fccSAndrew Lindesay _AssertDoubleValue(actualMessage, "exp", 1693907357);
46*4b347fccSAndrew Lindesay _AssertDoubleValue(actualMessage, "iat", 1693907057);
47*4b347fccSAndrew Lindesay }
48*4b347fccSAndrew Lindesay
49*4b347fccSAndrew Lindesay
50*4b347fccSAndrew Lindesay void
TestCorrupt()51*4b347fccSAndrew Lindesay JwtTokenHelperTest::TestCorrupt()
52*4b347fccSAndrew Lindesay {
53*4b347fccSAndrew Lindesay const char* jwtToken = "application/json";
54*4b347fccSAndrew Lindesay BMessage actualMessage;
55*4b347fccSAndrew Lindesay
56*4b347fccSAndrew Lindesay // ----------------------
57*4b347fccSAndrew Lindesay status_t result = JwtTokenHelper::ParseClaims(BString(jwtToken), actualMessage);
58*4b347fccSAndrew Lindesay // ----------------------
59*4b347fccSAndrew Lindesay
60*4b347fccSAndrew Lindesay CPPUNIT_ASSERT(B_OK != result);
61*4b347fccSAndrew Lindesay }
62*4b347fccSAndrew Lindesay
63*4b347fccSAndrew Lindesay
64*4b347fccSAndrew Lindesay /*static*/ void
AddTests(BTestSuite & parent)65*4b347fccSAndrew Lindesay JwtTokenHelperTest::AddTests(BTestSuite& parent)
66*4b347fccSAndrew Lindesay {
67*4b347fccSAndrew Lindesay CppUnit::TestSuite& suite = *new CppUnit::TestSuite("JwtTokenHelperTest");
68*4b347fccSAndrew Lindesay
69*4b347fccSAndrew Lindesay suite.addTest(
70*4b347fccSAndrew Lindesay new CppUnit::TestCaller<JwtTokenHelperTest>(
71*4b347fccSAndrew Lindesay "JwtTokenHelperTest::TestParseTokenClaims",
72*4b347fccSAndrew Lindesay &JwtTokenHelperTest::TestParseTokenClaims));
73*4b347fccSAndrew Lindesay
74*4b347fccSAndrew Lindesay suite.addTest(
75*4b347fccSAndrew Lindesay new CppUnit::TestCaller<JwtTokenHelperTest>(
76*4b347fccSAndrew Lindesay "JwtTokenHelperTest::TestCorrupt",
77*4b347fccSAndrew Lindesay &JwtTokenHelperTest::TestCorrupt));
78*4b347fccSAndrew Lindesay
79*4b347fccSAndrew Lindesay parent.addTest("JwtTokenHelperTest", &suite);
80*4b347fccSAndrew Lindesay }
81*4b347fccSAndrew Lindesay
82*4b347fccSAndrew Lindesay
83*4b347fccSAndrew Lindesay void
_AssertStringValue(const BMessage & message,const char * key,const char * expectedValue) const84*4b347fccSAndrew Lindesay JwtTokenHelperTest::_AssertStringValue(const BMessage& message, const char* key,
85*4b347fccSAndrew Lindesay const char* expectedValue) const
86*4b347fccSAndrew Lindesay {
87*4b347fccSAndrew Lindesay BString value;
88*4b347fccSAndrew Lindesay status_t result = message.FindString(key, &value);
89*4b347fccSAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, result);
90*4b347fccSAndrew Lindesay CPPUNIT_ASSERT_EQUAL(BString(expectedValue), value);
91*4b347fccSAndrew Lindesay }
92*4b347fccSAndrew Lindesay
93*4b347fccSAndrew Lindesay
94*4b347fccSAndrew Lindesay void
_AssertDoubleValue(const BMessage & message,const char * key,int64 expectedValue) const95*4b347fccSAndrew Lindesay JwtTokenHelperTest::_AssertDoubleValue(const BMessage& message, const char* key,
96*4b347fccSAndrew Lindesay int64 expectedValue) const
97*4b347fccSAndrew Lindesay {
98*4b347fccSAndrew Lindesay double value;
99*4b347fccSAndrew Lindesay status_t result = message.FindDouble(key, &value);
100*4b347fccSAndrew Lindesay CPPUNIT_ASSERT_EQUAL(B_OK, result);
101*4b347fccSAndrew Lindesay CPPUNIT_ASSERT_EQUAL((double) expectedValue, value);
102*4b347fccSAndrew Lindesay }