Reorganize arguments to BOOST_REQUIRE_MESSAGE
[platform/core/security/libcryptsvc.git] / test / cs_test.cc
1 /*
2  *  Copyright (c) 2016-2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Kyungwook Tak <k.tak@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        cs_test.cc
19  * @author      Kyungwook Tak (k.tak@samsung.com)
20  * @version
21  * @brief
22  */
23
24 #include <stdlib.h>
25 #include <memory>
26 #include <iostream>
27
28 #include <boost/test/unit_test.hpp>
29
30 #include <SecCryptoSvc.h>
31
32 BOOST_AUTO_TEST_SUITE(CRYPTO_SERVICE_TEST)
33
34 BOOST_AUTO_TEST_CASE(PLATFORM_UNIQUE_KEY)
35 {
36     constexpr unsigned int KeyLen = 16;
37     unsigned char cek[KeyLen] = {0};
38
39     BOOST_REQUIRE_MESSAGE(SecFrameGeneratePlatformUniqueKey(KeyLen, cek),
40         "Failed to SecFrameGeneratePlatformUniqueKey.");
41
42     std::unique_ptr<char, void(*)(void *)> encoded_cek(
43         Base64Encoding(reinterpret_cast<char *>(cek), static_cast<int>(KeyLen)),
44         free);
45     BOOST_REQUIRE_MESSAGE(!!encoded_cek, "Failed to base64 encoding.");
46
47     std::cout << "base64 encoded platform unique key(with len 16): "
48         << encoded_cek.get() << std::endl;
49 }
50
51 BOOST_AUTO_TEST_CASE(GETDUID_16)
52 {
53     std::unique_ptr<char, void(*)(void *)> duid(GetDuid(16), free);
54     BOOST_REQUIRE_MESSAGE(!!duid, "returned duid shouldn't be null");
55     std::cout << "duid: " << duid.get() << std::endl;
56 }
57
58 BOOST_AUTO_TEST_CASE(GETDUID_20)
59 {
60     std::unique_ptr<char, void(*)(void *)> duid(GetDuid(20), free);
61     BOOST_REQUIRE_MESSAGE(!!duid, "returned duid shouldn't be null");
62     std::cout << "duid: " << duid.get() << std::endl;
63 }
64
65 static void derive_key_with_pass(const char *pass, int passlen)
66 {
67     int retval = CS_ERROR_NONE;
68
69     constexpr unsigned int KeyLen = 20;
70     unsigned char *key = nullptr;
71     retval = cs_derive_key_with_pass(pass, passlen, KeyLen, &key);
72     BOOST_REQUIRE_MESSAGE(retval == CS_ERROR_NONE,
73         "Failed to cs_derive_key_with_pass with retval: " << retval);
74
75     std::unique_ptr<unsigned char, void(*)(void *)> pKey(key, free);
76
77     std::unique_ptr<char, void(*)(void *)> encoded_key(
78         Base64Encoding(reinterpret_cast<char *>(key), static_cast<int>(KeyLen)),
79         free);
80     BOOST_REQUIRE_MESSAGE(!!encoded_key, "Failed to base64 encoding.");
81
82     std::cout << "base64 encoded key derived from pass(len " << KeyLen
83         << "): " << encoded_key.get() << std::endl;
84 }
85
86 BOOST_AUTO_TEST_CASE(DERIVE_KEY_WITH_PASS)
87 {
88     const char *test_pass = "test-password";
89     derive_key_with_pass(test_pass, 5);
90     derive_key_with_pass(test_pass, 10);
91     derive_key_with_pass(test_pass, strlen(test_pass));
92
93     const char empty_pass[30] = {0, };
94     derive_key_with_pass(empty_pass, strlen(empty_pass));
95 }
96
97 BOOST_AUTO_TEST_SUITE_END()