@@ -617,7 +617,7 @@ TEST(EncodingTest, Base64Vectors) {
617617 std::vector<uint8_t > out;
618618 EXPECT_TRUE (hmac_cpp::base64_decode (" Zg" , out, hmac_cpp::Base64Alphabet::Standard, false , false ));
619619 EXPECT_EQ (out, std::vector<uint8_t >({' f' }));
620- EXPECT_TRUE (hmac_cpp::base64_decode (" +__/" , out, hmac_cpp::Base64Alphabet::Url));
620+ EXPECT_TRUE (hmac_cpp::base64_decode (" +__/" , out, hmac_cpp::Base64Alphabet::Url, false , false ));
621621 EXPECT_EQ (out, std::vector<uint8_t >({0xfb ,0xff ,0xff }));
622622}
623623
@@ -682,6 +682,73 @@ TEST(EncodingTest, Base36Vectors) {
682682 }
683683}
684684
685+ TEST (EncodingPropertyTest, Base64RoundTrip) {
686+ std::mt19937 rng (123 );
687+ std::uniform_int_distribution<int > len_dist (0 ,64 );
688+ for (int i = 0 ; i < 50 ; ++i) {
689+ int len = len_dist (rng);
690+ std::vector<uint8_t > buf (len);
691+ for (int j = 0 ; j < len; ++j) buf[j] = static_cast <uint8_t >(rng ());
692+ for (auto alpha : {hmac_cpp::Base64Alphabet::Standard, hmac_cpp::Base64Alphabet::Url}) {
693+ for (bool pad : {true , false }) {
694+ std::string enc = hmac_cpp::base64_encode (buf.data (), buf.size (), alpha, pad);
695+ std::vector<uint8_t > dec;
696+ EXPECT_TRUE (hmac_cpp::base64_decode (enc, dec, alpha, pad, true ));
697+ EXPECT_EQ (dec, buf);
698+ std::string re = hmac_cpp::base64_encode (dec.data (), dec.size (), alpha, pad);
699+ EXPECT_EQ (re, enc);
700+ }
701+ }
702+ }
703+ }
704+
705+ TEST (EncodingPropertyTest, Base64StrictRejects) {
706+ std::vector<uint8_t > out;
707+ EXPECT_FALSE (hmac_cpp::base64_decode (" +__/" , out, hmac_cpp::Base64Alphabet::Url, false , true ));
708+ EXPECT_FALSE (hmac_cpp::base64_decode (" AAAAA" , out, hmac_cpp::Base64Alphabet::Standard, false , true ));
709+ }
710+
711+ TEST (EncodingPropertyTest, Base32RoundTrip) {
712+ std::mt19937 rng (321 );
713+ std::uniform_int_distribution<int > len_dist (0 ,40 );
714+ for (int i = 0 ; i < 50 ; ++i) {
715+ int len = len_dist (rng);
716+ std::vector<uint8_t > buf (len);
717+ for (int j = 0 ; j < len; ++j) buf[j] = static_cast <uint8_t >(rng ());
718+ for (bool pad : {true , false }) {
719+ std::string enc = hmac_cpp::base32_encode (buf.data (), buf.size (), pad);
720+ std::vector<uint8_t > dec;
721+ EXPECT_TRUE (hmac_cpp::base32_decode (enc, dec, pad, true ));
722+ EXPECT_EQ (dec, buf);
723+ std::string re = hmac_cpp::base32_encode (dec.data (), dec.size (), pad);
724+ EXPECT_EQ (re, enc);
725+ }
726+ }
727+ }
728+
729+ TEST (EncodingPropertyTest, Base32RejectsInvalid) {
730+ std::vector<uint8_t > out;
731+ EXPECT_FALSE (hmac_cpp::base32_decode (" A" , out));
732+ EXPECT_FALSE (hmac_cpp::base32_decode (" AAA" , out));
733+ EXPECT_FALSE (hmac_cpp::base32_decode (" AAAAAA" , out));
734+ EXPECT_FALSE (hmac_cpp::base32_decode (" MZX=====MZXW6===" , out));
735+ }
736+
737+ TEST (EncodingPropertyTest, Base36LeadingZeros) {
738+ std::mt19937 rng (777 );
739+ std::uniform_int_distribution<int > len_dist (0 ,10 );
740+ for (int i = 0 ; i < 20 ; ++i) {
741+ int zeros = len_dist (rng) % 5 ;
742+ int data_len = len_dist (rng);
743+ std::vector<uint8_t > buf (zeros + data_len);
744+ for (int j = 0 ; j < data_len; ++j) buf[zeros + j] = static_cast <uint8_t >(rng ());
745+ std::string enc = hmac_cpp::base36_encode (buf.data (), buf.size ());
746+ std::vector<uint8_t > dec;
747+ EXPECT_TRUE (hmac_cpp::base36_decode (enc, dec));
748+ EXPECT_EQ (dec, buf);
749+ }
750+ }
751+
685752int main (int argc, char **argv) {
686753 ::testing::InitGoogleTest (&argc, argv);
687754 return RUN_ALL_TESTS ();
0 commit comments