YACA: Add common helper functions and structs 42/71242/8
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 24 May 2016 15:03:12 +0000 (17:03 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 13 Jun 2016 12:49:05 +0000 (14:49 +0200)
Change-Id: I9133eca6ad3529db9e8c7e74fffd3f767b2da690

src/yaca/debug.h [new file with mode: 0644]
src/yaca/yaca-test-common.cpp [new file with mode: 0644]
src/yaca/yaca-test-common.h [new file with mode: 0644]

diff --git a/src/yaca/debug.h b/src/yaca/debug.h
new file mode 100644 (file)
index 0000000..149e2ba
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
+ *
+ *  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
+ */
+
+/**
+ * @file debug.h
+ * @brief
+ */
+
+#pragma once
+
+extern "C" {
+
+typedef void (*yaca_error_cb)(const char*);
+
+void yaca_debug_set_error_cb(yaca_error_cb cb);
+
+char *yaca_debug_translate_error(yaca_error_e err);
+
+} /* extern */
diff --git a/src/yaca/yaca-test-common.cpp b/src/yaca/yaca-test-common.cpp
new file mode 100644 (file)
index 0000000..eee4c27
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2000 - 2016 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
+ */
+/*
+ * @file       yaca-test-common.cpp
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#include <vector>
+#include <string>
+
+#include "yaca-test-common.h"
+
+namespace {
+
+std::vector<std::string> opensslErrors;
+
+} // anonymous
+
+void error_cb(const char *msg)
+{
+    opensslErrors.push_back(msg ? msg : "NULL");
+}
+
+const std::vector<std::string>& get_openssl_errors()
+{
+    return opensslErrors;
+}
+
+void clear_openssl_errors()
+{
+    opensslErrors.clear();
+}
+
+const char *yaca_error(int error)
+{
+    return yaca_debug_translate_error(static_cast<yaca_error_e>(error));
+}
diff --git a/src/yaca/yaca-test-common.h b/src/yaca/yaca-test-common.h
new file mode 100644 (file)
index 0000000..9b6663d
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ *  Copyright (c) 2000 - 2016 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
+ */
+/*
+ * @file       yaca-test-common.h
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#pragma once
+
+#include <utility>
+#include <vector>
+#include <string>
+#include <iostream>
+#include <sstream>
+
+#include <yaca_crypto.h>
+#include <yaca_error.h>
+
+#include <dpl/gdbbacktrace.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_runner.h>
+
+#include "debug.h"
+
+#define YACA_ASSERT_MSG(test, message)                                            \
+do {                                                                              \
+    if (!(test)) {                                                                \
+        std::ostringstream assertMsg;                                             \
+        assertMsg << message << DPL::gdbbacktrace();                              \
+        if (!get_openssl_errors().empty()) {                                      \
+            assertMsg << std::endl << "OPENSSL ERRORS:" << std::endl;             \
+            for (auto &err : get_openssl_errors())                                \
+                assertMsg << err;                                                 \
+            clear_openssl_errors();                                               \
+        }                                                                         \
+        DPL::Test::TestFailed e(#test,                                            \
+                                __FILE__,                                         \
+                                __LINE__,                                         \
+                                assertMsg.str());                                 \
+        if (!std::uncaught_exception())                                           \
+            throw e;                                                              \
+        DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
+    }                                                                             \
+} while (0)
+
+const char *yaca_error(int error);
+
+void error_cb(const char *msg);
+
+const std::vector<std::string>& get_openssl_errors();
+void clear_openssl_errors();
+
+struct YacaTest {
+    YacaTest() {}
+    ~YacaTest() {}
+
+    void init(const std::string&)
+    {
+        int ret = yaca_initialize();
+        YACA_ASSERT_MSG(ret == 0, "yaca_initialize() failed with " << yaca_error(ret));
+
+        yaca_debug_set_error_cb(&error_cb);
+    }
+
+    void finish()
+    {
+        yaca_cleanup();
+    }
+};
+
+// YACA_ASSERT wrappers
+#define YACA_RESULT(expected, func)                                            \
+do {                                                                           \
+    int ret = func;                                                            \
+    YACA_ASSERT_MSG(ret == (expected),                                         \
+                    "Expected: " << yaca_error((expected)) <<                  \
+                    " got: " << yaca_error(ret) );                             \
+} while(0);
+
+#define YACA_SUCCESS(func) YACA_RESULT(YACA_ERROR_NONE, func);
+
+#define YACA_INVALID_PARAM(func) YACA_RESULT(YACA_ERROR_INVALID_PARAMETER, func);