Add test cases for Base64 Encoder and Base64 Decoder 31/312731/19
authorPhan Xuan Tan <xuan.tan@samsung.com>
Thu, 13 Jun 2024 09:51:31 +0000 (16:51 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 3 Jul 2024 08:38:08 +0000 (08:38 +0000)
Change-Id: I8cd256107c12703650ec07a702f949576c087186

unit-tests/CMakeLists.txt
unit-tests/test_vcore_base64.cpp [new file with mode: 0644]

index bf8a72e..f8b6092 100644 (file)
@@ -61,6 +61,7 @@ SET(UNIT_TESTS_SOURCES
     test_main.cpp
     test_constant.cpp
     test_common.cpp
+    test_vcore_base64.cpp
     test_vcore_cert_store_type.cpp
     test_vcore_api.cpp
     test_vcore_time_conversion.cpp
diff --git a/unit-tests/test_vcore_base64.cpp b/unit-tests/test_vcore_base64.cpp
new file mode 100644 (file)
index 0000000..b128a46
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "test_macros.h"
+#include "vcore/Base64.h"
+#include <cstring>
+
+using namespace ValidationCore;
+using namespace std;
+
+const string base64Test = "TestBase64";
+const string base64DecodeTest = "VGVzdEJhc2U2NA==";
+
+BOOST_AUTO_TEST_SUITE(VCORE_BASE64_TEST)
+
+POSITIVE_TEST_CASE(T_base64_encoder_append)
+{
+       Base64Encoder base64;
+       base64.append(base64Test);
+       base64.finalize();
+       BOOST_CHECK_EQUAL(base64.get(), base64DecodeTest);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_append_after_finalize)
+{
+       Base64Encoder base64;
+       base64.append(base64Test);
+       base64.finalize();
+       string info = base64.get();
+       BOOST_CHECK_EQUAL(info, base64DecodeTest);
+       BOOST_CHECK_THROW(base64.append(base64Test), Base64Encoder::Exception::AlreadyFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_finalize)
+{
+       Base64Encoder base64;
+       BOOST_CHECK_THROW(base64.finalize(), Base64Encoder::Exception::InternalError);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_double_finalize)
+{
+       Base64Encoder base64;
+       base64.reset();
+       base64.finalize();
+       BOOST_CHECK_THROW(base64.finalize(), Base64Encoder::Exception::AlreadyFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_get_without_finalize)
+{
+       Base64Encoder base64;
+       base64.append(base64Test);
+       BOOST_CHECK_THROW(base64.get(), Base64Encoder::Exception::NotFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_append_with_null)
+{
+       Base64Encoder base64;
+       BOOST_CHECK_THROW(base64.append(NULL), std::logic_error);
+}
+
+NEGATIVE_TEST_CASE(T_base64_encoder_get_with_empty)
+{
+       Base64Encoder base64;
+       base64.append(std::string());
+       base64.finalize();
+       BOOST_CHECK_EQUAL(base64.get(), std::string());
+}
+
+POSITIVE_TEST_CASE(T_base64_decoder_get)
+{
+       Base64Decoder base64;
+       base64.append(base64DecodeTest);
+       BOOST_CHECK_EQUAL(base64.finalize(), true);
+       BOOST_CHECK_EQUAL(base64.get(), base64Test);
+}
+
+POSITIVE_TEST_CASE(T_base64_decoder_with_new_line)
+{
+       Base64Decoder base64;
+       base64.append(base64DecodeTest + "\n");
+       BOOST_CHECK_EQUAL(base64.finalize(), true);
+       BOOST_CHECK_EQUAL(base64.get(), base64Test);
+}
+
+NEGATIVE_TEST_CASE(T_base64_decoder_double_finalize)
+{
+       Base64Decoder base64;
+       base64.append(base64DecodeTest);
+       BOOST_CHECK_EQUAL(base64.finalize(), true);
+       BOOST_CHECK_THROW(base64.finalize(), Base64Decoder::Exception::AlreadyFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_decoder_append_after_finalize)
+{
+       Base64Decoder base64;
+       base64.append(base64DecodeTest);
+       BOOST_CHECK_EQUAL(base64.finalize(), true);
+       BOOST_CHECK_THROW(base64.append(std::string()), Base64Decoder::Exception::AlreadyFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_decoder_get_without_finalize)
+{
+       Base64Decoder base64;
+       BOOST_CHECK_THROW(base64.get(), Base64Decoder::Exception::NotFinalized);
+}
+
+NEGATIVE_TEST_CASE(T_base64_decoder_with_empty_argument)
+{
+       Base64Decoder base64;
+       base64.append(std::string());
+       BOOST_CHECK_EQUAL(base64.finalize(), false);
+}
+
+NEGATIVE_TEST_CASE(T_base64_decoder_with_invalid_argument)
+{
+       Base64Decoder base64;
+       base64.append(base64Test + "*");
+       BOOST_CHECK_EQUAL(base64.finalize(), false);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
\ No newline at end of file