1 /* 2 * Copyright 2023, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "DataIOUtilsTest.h" 8 9 #include <String.h> 10 11 #include <cppunit/TestCaller.h> 12 #include <cppunit/TestSuite.h> 13 14 #include <string.h> 15 16 #include "DataIOUtils.h" 17 18 19 DataIOUtilsTest::DataIOUtilsTest() 20 { 21 } 22 23 24 DataIOUtilsTest::~DataIOUtilsTest() 25 { 26 } 27 28 29 void 30 DataIOUtilsTest::TestReadBase64JwtClaims_1() 31 { 32 const char* jwtToken = "eyJpc3MiOiJkZXYuaGRzIiwic3ViIjoiZXJpazY0QGhkcyIs" 33 "ImV4cCI6MTY5MzE5MTMzMiwiaWF0IjoxNjkzMTkxMDMyfQ"; 34 BMemoryIO memoryIo(jwtToken, strlen(jwtToken)); 35 Base64DecodingDataIO base64DecodingIo(&memoryIo, '-', '_'); 36 char actualOutputBuffer[71]; 37 size_t actualReadBytes; 38 39 bzero(actualOutputBuffer, 71); 40 41 // ---------------------- 42 status_t result = base64DecodingIo.ReadExactly(actualOutputBuffer, 70, &actualReadBytes); 43 // ---------------------- 44 45 CPPUNIT_ASSERT_EQUAL(B_OK, result); 46 CPPUNIT_ASSERT_EQUAL(70, actualReadBytes); 47 actualOutputBuffer[actualReadBytes] = 0; 48 49 CPPUNIT_ASSERT_EQUAL(0x7b, (uint8) actualOutputBuffer[0]); 50 51 CPPUNIT_ASSERT_EQUAL( 52 BString("{\"iss\":\"dev.hds\",\"sub\":\"erik64@hds\",\"exp\":1693191332,\"iat\"" 53 ":1693191032}"), 54 BString(actualOutputBuffer)); 55 } 56 57 58 void 59 DataIOUtilsTest::TestReadBase64JwtClaims_2() 60 { 61 const char* jwtToken = "eyJpc3MiOiJkZXYuaGRzIiwic3ViIjoidG93ZWxkb3dudGVhQ" 62 "GhkcyIsImV4cCI6MTY5MzczODgyNiwiaWF0IjoxNjkzNzM4NTI2fQ"; 63 BMemoryIO memoryIo(jwtToken, strlen(jwtToken)); 64 Base64DecodingDataIO base64DecodingIo(&memoryIo, '-', '_'); 65 char actualOutputBuffer[77]; 66 size_t actualReadBytes; 67 68 bzero(actualOutputBuffer, 77); 69 70 // ---------------------- 71 status_t result = base64DecodingIo.ReadExactly(actualOutputBuffer, 76, &actualReadBytes); 72 // ---------------------- 73 74 CPPUNIT_ASSERT_EQUAL(B_OK, result); 75 CPPUNIT_ASSERT_EQUAL(76, actualReadBytes); 76 actualOutputBuffer[actualReadBytes] = 0; 77 78 CPPUNIT_ASSERT_EQUAL(0x7b, (uint8) actualOutputBuffer[0]); 79 80 CPPUNIT_ASSERT_EQUAL( 81 BString("{\"iss\":\"dev.hds\",\"sub\":\"toweldowntea@hds\",\"exp\":1693738826,\"iat\"" 82 ":1693738526}"), 83 BString(actualOutputBuffer)); 84 } 85 86 87 void 88 DataIOUtilsTest::TestCorrupt() 89 { 90 const char* jwtToken = "QW5k$mV3"; 91 // note that '$' is not a valid base64 character 92 BMemoryIO memoryIo(jwtToken, strlen(jwtToken)); 93 Base64DecodingDataIO base64DecodingIo(&memoryIo, '-', '_'); 94 char actualOutputBuffer[7]; 95 size_t actualReadBytes; 96 97 bzero(actualOutputBuffer, 7); 98 99 // ---------------------- 100 status_t result = base64DecodingIo.ReadExactly(actualOutputBuffer, 6, &actualReadBytes); 101 // ---------------------- 102 103 CPPUNIT_ASSERT(B_OK != result); 104 } 105 106 107 /*static*/ void 108 DataIOUtilsTest::AddTests(BTestSuite& parent) 109 { 110 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DataIOUtilsTest"); 111 112 suite.addTest( 113 new CppUnit::TestCaller<DataIOUtilsTest>( 114 "DataIOUtilsTest::TestReadBase64JwtClaims_1", 115 &DataIOUtilsTest::TestReadBase64JwtClaims_1)); 116 117 suite.addTest( 118 new CppUnit::TestCaller<DataIOUtilsTest>( 119 "DataIOUtilsTest::TestReadBase64JwtClaims_2", 120 &DataIOUtilsTest::TestReadBase64JwtClaims_2)); 121 122 suite.addTest( 123 new CppUnit::TestCaller<DataIOUtilsTest>( 124 "DataIOUtilsTest::TestCorrupt", 125 &DataIOUtilsTest::TestCorrupt)); 126 127 parent.addTest("DataIOUtilsTest", &suite); 128 }