diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index 327875bc..ee87c78a 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -93,7 +93,9 @@ int main(int argc, char* argv[]) shrpx::test_downstream_get_norm_response_header) || !CU_add_test(pSuite, "util_streq", shrpx::test_util_streq) || !CU_add_test(pSuite, "util_inp_strlower", - shrpx::test_util_inp_strlower)) { + shrpx::test_util_inp_strlower) || + !CU_add_test(pSuite, "util_to_base64", + shrpx::test_util_to_base64)) { CU_cleanup_registry(); return CU_get_error(); } diff --git a/src/util.cc b/src/util.cc index c699a2d5..0cd97af1 100644 --- a/src/util.cc +++ b/src/util.cc @@ -283,7 +283,9 @@ void to_base64(std::string& token68str) break; } } - token68str += std::string(4 - token68str.size() % 4, '='); + if(token68str.size() & 0x3) { + token68str.append(4 - (token68str.size() & 0x3), '='); + } return; } diff --git a/src/util_test.cc b/src/util_test.cc index f3b74812..ba143bc8 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -71,4 +71,15 @@ void test_util_inp_strlower(void) CU_ASSERT("" == a); } +void test_util_to_base64(void) +{ + std::string x = "AAA--B_"; + util::to_base64(x); + CU_ASSERT("AAA++B/=" == x); + + x = "AAA--B_B"; + util::to_base64(x); + CU_ASSERT("AAA++B/B" == x); +} + } // namespace shrpx diff --git a/src/util_test.h b/src/util_test.h index 3472b629..fd4ff693 100644 --- a/src/util_test.h +++ b/src/util_test.h @@ -29,6 +29,7 @@ namespace shrpx { void test_util_streq(void); void test_util_inp_strlower(void); +void test_util_to_base64(void); } // namespace shrpx