YACA: Digest ASCII test vectors
[platform/core/test/security-tests.git] / src / yaca / yaca-test-digest.cpp
index 18afe4d..3856977 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "dpl/test/test_runner.h"
 #include "yaca-test-common.h"
+#include "yaca-test-vector.h"
 
 #include <yaca_digest.h>
 
@@ -120,3 +121,50 @@ RUNNER_TEST(T2050_yaca_digest_get_output_length, YacaTest)
     digest_length_test(YACA_DIGEST_SHA384, 384);
     digest_length_test(YACA_DIGEST_SHA512, 512);
 }
+
+RUNNER_TEST(T2060_yaca_digest_test_vectors, YacaTest)
+{
+    auto tvv = loadTestVector("digest_ascii.txt");
+
+    for (const auto& tv : tvv) {
+        yaca_digest_algorithm_e algo;
+        size_t repeats;
+        std::string input;
+        Buffer expected;
+
+        tv.get("algo", algo);
+        tv.get("repeats", repeats);
+        tv.get("input", input);
+        tv.get("output", expected);
+
+        auto ctx_ptr = digest_init(algo);
+        size_t out_len;
+
+        out_len = get_output_length(ctx_ptr);
+
+        if (input.size() > 0) {
+            for (size_t i = 0; i < repeats; i++)
+                YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(),
+                                                input.c_str(),
+                                                input.size()));
+        }
+
+        Buffer output(out_len);
+        YACA_SUCCESS(yaca_digest_finalize(ctx_ptr.get(), output.data(), &out_len));
+        RUNNER_ASSERT_MSG(output.size() >= out_len,
+                          "Length returned from yaca_digest_finalize() (" << out_len <<
+                          ") is greater than the one returned from yaca_context_get_output_length() ("
+                          << output.size() << ")");
+        output.resize(out_len);
+
+        YACA_ASSERT_MSG(output.size() == expected.size(),
+                        " Digest calculated for \""  << truncate_str(input, 16) << "\" with "
+                        << digest2str(algo) << " is " << output.size() * 8
+                        <<" bits long. Expected " << expected.size() * 8 << " bits");
+
+        int ret = yaca_memcmp(output.data(), expected.data(), output.size());
+        YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
+                        "Digest calculated for \"" << truncate_str(input, 16) << "\" with "
+                        << digest2str(algo) << " is different than expected");
+    }
+}