From 15e895bf652b61c812e44ee9481adc1892eaba21 Mon Sep 17 00:00:00 2001 From: Konrad Lipinski Date: Tue, 22 Oct 2019 18:52:52 +0200 Subject: [PATCH 01/16] Implement asymmetric key initial value import Change-Id: I0f5e4ab9b156abc3ab97a59f32b4adef9779eb98 --- src/manager/crypto/tz-backend/internals.cpp | 34 ++++++++++++++--------------- tools/ckm_initial_values/main.cpp | 4 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/manager/crypto/tz-backend/internals.cpp b/src/manager/crypto/tz-backend/internals.cpp index cc8efca..9962cf5 100644 --- a/src/manager/crypto/tz-backend/internals.cpp +++ b/src/manager/crypto/tz-backend/internals.cpp @@ -84,6 +84,20 @@ void generateDSAParams(const int sizeBits, CKM::RawBuffer &prime, #endif } +tz_data_type toTzDataType(const CKM::DataType dataType) { + switch (int(dataType)) { + case CKM::DataType::Type::BINARY_DATA: return TYPE_GENERIC_SECRET; + case CKM::DataType::Type::KEY_AES: return TYPE_SKEY; + case CKM::DataType::Type::KEY_DSA_PRIVATE: return TYPE_AKEY_PRIVATE_DSA; + case CKM::DataType::Type::KEY_RSA_PRIVATE: return TYPE_AKEY_PRIVATE_RSA; + case CKM::DataType::Type::KEY_DSA_PUBLIC: return TYPE_AKEY_PUBLIC_DSA; + case CKM::DataType::Type::KEY_RSA_PUBLIC: return TYPE_AKEY_PUBLIC_RSA; + default: + ThrowErr(CKM::Exc::Crypto::DataTypeNotSupported, + "Data type could not be imported by tz-backend"); + } +} + } // namespace namespace CKM { @@ -265,21 +279,7 @@ RawBuffer importData(const Data &data, RawBuffer &tag) { - uint32_t dataType; - - if (data.type.isSKey()) { - dataType = TYPE_SKEY; - } else if (data.type.isBinaryData()) { - dataType = TYPE_GENERIC_SECRET; - } else if (data.type.isKeyPrivate()) { - dataType = TYPE_AKEY_PRIVATE; - } else if (data.type.isKeyPublic()) { - dataType = TYPE_AKEY_PUBLIC; - } else { - ThrowErr(Exc::Crypto::DataTypeNotSupported, - "Data type could not be imported by tz-backend"); - } - + const auto dataType = toTzDataType(data.type); RawBuffer result; RawBuffer pwdBuf(pwd.begin(), pwd.end()); @@ -493,7 +493,7 @@ RawBuffer asymmetricEncrypt(const RawBuffer &key, getAlgType(algo), key, pwd, - unpack(alg, ParamName::ED_IV), + result, // unused dummy data, result); return result; @@ -521,7 +521,7 @@ RawBuffer asymmetricDecrypt(const RawBuffer &key, getAlgType(algo), key, pwd, - unpack(alg, ParamName::ED_IV), + result, // unused dummy cipher, result); return result; diff --git a/tools/ckm_initial_values/main.cpp b/tools/ckm_initial_values/main.cpp index f717e5e..c5d9e7d 100644 --- a/tools/ckm_initial_values/main.cpp +++ b/tools/ckm_initial_values/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2018-2019 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. @@ -137,7 +137,7 @@ void usage() " -a|--accessors [,[,...]]" << std::endl << " A list of key-manager clients allowed to access given initial" << std::endl << " value separated by commas." << std::endl << - " -b|--backend A key-manager's backed to use when saving the initial values." << std::endl << + " -b|--backend A key-manager's backend to use when saving the initial values." << std::endl << " Allowed values: 'software' and 'hardware'." << std::endl; } -- 2.7.4 From b48ce806a3be66c66ab18da5496a868cf0cafe11 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Thu, 21 Nov 2019 09:56:55 +0100 Subject: [PATCH 02/16] Release 0.1.33 * Implement asymmetric key initial value import * Add key-manager script for platform upgrade * Make some single arg constructors explicit * Refactor Decider, route all encrypted storage to tz backend * Devirtualize DescriptorSet * Refactor RawBuffer hex dumps * ckmc_alias_new: replace str* calls with memcpy * [ocsp] Fix static string length calculation * Improve ckm deserialization errors detection * Treat pwd data deserialization as an error Change-Id: I60f2fe6d0a3d539e2a63743f9b3a61ae31287bd9 --- packaging/key-manager.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/key-manager.spec b/packaging/key-manager.spec index 3d4cc91..883f60d 100644 --- a/packaging/key-manager.spec +++ b/packaging/key-manager.spec @@ -5,7 +5,7 @@ Name: key-manager Summary: Central Key Manager and utilities -Version: 0.1.32 +Version: 0.1.33 Release: 1 Group: Security/Secure Storage License: Apache-2.0 and BSD-3-Clause -- 2.7.4 From d852ca671c590f178ed0bc3dbd308dc4770069f1 Mon Sep 17 00:00:00 2001 From: Dongsun Lee Date: Fri, 29 Nov 2019 11:42:59 +0900 Subject: [PATCH 03/16] Fix documentation error(This function points to itself in the @see tag) Change-Id: I464677cf7e23d41a133e3ea83a71754a17ca8541 Signed-off-by: Dongsun Lee --- src/include/ckmc/ckmc-manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/ckmc/ckmc-manager.h b/src/include/ckmc/ckmc-manager.h index 251052c..4d90b66 100644 --- a/src/include/ckmc/ckmc-manager.h +++ b/src/include/ckmc/ckmc-manager.h @@ -661,7 +661,7 @@ int ckmc_create_signature(const char *private_key_alias, const char *password, c * @pre User is already logged in and the user key is already loaded into memory in plain text form. * @see ckmc_create_key_pair_rsa() * @see ckmc_create_key_pair_ecdsa() - * @see ckmc_verify_signature() + * @see ckmc_create_signature() * @see #ckmc_hash_algo_e * @see #ckmc_rsa_padding_algo_e */ -- 2.7.4 From 85ccbe5c56a9cd16a764d94e5a456c1262a54d8b Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Fri, 10 Jan 2020 10:11:23 +0100 Subject: [PATCH 04/16] Fix build for gcc 9 Change-Id: I2d5eb654f7e7ab6fa9145d902542b5fe1984da64 --- packaging/key-manager.spec | 4 ++++ src/manager/common/openssl-error-handler.cpp | 3 ++- src/manager/common/pkcs12-impl.cpp | 4 +++- src/manager/crypto/sw-backend/obj.h | 3 ++- src/manager/dpl/core/src/binary_queue.cpp | 3 ++- src/manager/main/message-service.h | 5 +++-- src/manager/service/file-system.cpp | 6 +++--- src/manager/service/ocsp.cpp | 3 ++- 8 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packaging/key-manager.spec b/packaging/key-manager.spec index 883f60d..2dabc94 100644 --- a/packaging/key-manager.spec +++ b/packaging/key-manager.spec @@ -149,6 +149,10 @@ Includes ckm_initial_values tool for initial values XML generation export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" %endif +# needed to surpress sqlcipher errors while its still embedded +export CFLAGS="$CFLAGS -Wno-cast-function-type -Wno-implicit-fallthrough" +export CXXFLAGS="$CXXFLAGS -Wno-cast-function-type -Wno-implicit-fallthrough" + export LDFLAGS+="-Wl,--rpath=%{_libdir},-Bsymbolic-functions " %cmake . -DVERSION=%{version} \ diff --git a/src/manager/common/openssl-error-handler.cpp b/src/manager/common/openssl-error-handler.cpp index a43e094..c7c1263 100644 --- a/src/manager/common/openssl-error-handler.cpp +++ b/src/manager/common/openssl-error-handler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017-2020 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. @@ -30,6 +30,7 @@ #include "openssl-error-handler.h" #include +#include #include #include #include diff --git a/src/manager/common/pkcs12-impl.cpp b/src/manager/common/pkcs12-impl.cpp index 100e9b5..17f60a1 100644 --- a/src/manager/common/pkcs12-impl.cpp +++ b/src/manager/common/pkcs12-impl.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2014 - 2019 Samsung Electronics Co. +/* Copyright (c) 2014-2020 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. @@ -31,6 +31,8 @@ #include #include +#include + namespace CKM { namespace { diff --git a/src/manager/crypto/sw-backend/obj.h b/src/manager/crypto/sw-backend/obj.h index 0a00734..aeed086 100644 --- a/src/manager/crypto/sw-backend/obj.h +++ b/src/manager/crypto/sw-backend/obj.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000-2020 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. @@ -19,6 +19,7 @@ * @version 1.0 */ #pragma once +#include #include #include diff --git a/src/manager/dpl/core/src/binary_queue.cpp b/src/manager/dpl/core/src/binary_queue.cpp index 36b174e..d30e18b 100644 --- a/src/manager/dpl/core/src/binary_queue.cpp +++ b/src/manager/dpl/core/src/binary_queue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2011-2020 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. @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/src/manager/main/message-service.h b/src/manager/main/message-service.h index fc36cc6..19fe074 100644 --- a/src/manager/main/message-service.h +++ b/src/manager/main/message-service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000-2020 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. @@ -98,7 +98,8 @@ template template void MessageService::Register(Mgr &mgr) { - mgr.Register([this](const Msg & msg) { + mgr.template Register([this](const Msg & msg) { + //intentional fall to Unknown option this->AddMessage(msg); }); } diff --git a/src/manager/service/file-system.cpp b/src/manager/service/file-system.cpp index 079a4cd..5ab97b2 100644 --- a/src/manager/service/file-system.cpp +++ b/src/manager/service/file-system.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000-2020 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. @@ -215,11 +215,11 @@ UidVector FileSystem::getUIDsFromDBFile() try { uids.emplace_back(static_cast(std::stoi((filename.c_str()) + CKM_KEY_PREFIX.size()))); - } catch (const std::invalid_argument) { + } catch (const std::invalid_argument &) { LogDebug("Error in extracting uid from db file. " "Error=std::invalid_argument. " "This will be ignored.File=" << filename); - } catch (const std::out_of_range) { + } catch (const std::out_of_range &) { LogDebug("Error in extracting uid from db file. " "Error=std::out_of_range. " "This will be ignored. File=" << filename); diff --git a/src/manager/service/ocsp.cpp b/src/manager/service/ocsp.cpp index c6cb2fd..0bf71a9 100644 --- a/src/manager/service/ocsp.cpp +++ b/src/manager/service/ocsp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2014-2020 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. @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include -- 2.7.4 From 77639c672c8533959348b2d0a351a5d54136d504 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Wed, 15 Jan 2020 09:33:24 +0100 Subject: [PATCH 05/16] Release 0.1.34 * Fix build for gcc 9 * Fix documentation error(This function points to itself in the @see tag) Change-Id: Ica82caa211fa8005183a1834f860aff6b42ad3c0 --- packaging/key-manager.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/key-manager.spec b/packaging/key-manager.spec index 2dabc94..03668c4 100644 --- a/packaging/key-manager.spec +++ b/packaging/key-manager.spec @@ -5,7 +5,7 @@ Name: key-manager Summary: Central Key Manager and utilities -Version: 0.1.33 +Version: 0.1.34 Release: 1 Group: Security/Secure Storage License: Apache-2.0 and BSD-3-Clause -- 2.7.4 From 1b490565354785bce2b0ba6ecd255fcc01a29310 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Thu, 30 Jan 2020 10:05:39 +0100 Subject: [PATCH 06/16] Fix build break with boost 1.71.0 Change-Id: I539f28540d327b4cd87a63f39b84a2a36a35e34a --- tests/colour_log_formatter.cpp | 14 ++++++++++++++ tests/colour_log_formatter.h | 1 + 2 files changed, 15 insertions(+) diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp index b2d2b9b..fb51aaa 100644 --- a/tests/colour_log_formatter.cpp +++ b/tests/colour_log_formatter.cpp @@ -124,6 +124,20 @@ colour_log_formatter::log_build_info(std::ostream &output) //____________________________________________________________________________// void +colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info) +{ + if (log_build_info) + output << "Platform: " << BOOST_PLATFORM << '\n' + << "Compiler: " << BOOST_COMPILER << '\n' + << "STL : " << BOOST_STDLIB << '\n'; + output << "Boost : " << BOOST_VERSION / 100000 << '.' + << BOOST_VERSION / 100 % 1000 << '.' + << BOOST_VERSION % 100 << std::endl; +} + +//____________________________________________________________________________// + +void colour_log_formatter::test_unit_start( std::ostream &output, test_unit const &tu) diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h index 064cfe2..937ef9b 100644 --- a/tests/colour_log_formatter.h +++ b/tests/colour_log_formatter.h @@ -25,6 +25,7 @@ public: boost::unit_test::counter_t test_cases_amount); void log_finish(std::ostream &); void log_build_info(std::ostream &); + void log_build_info(std::ostream &output, bool log_build_info = true); void test_unit_start( std::ostream &, -- 2.7.4 From 8819f732fddba68b58e6841a7c8ed123af0298b3 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Thu, 30 Jan 2020 12:31:20 +0100 Subject: [PATCH 07/16] Release 0.1.35 * Fix build break with boost 1.71.0 Change-Id: Ib4ea4024a5751d78bed1effd6c52753a333cd985 --- packaging/key-manager.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/key-manager.spec b/packaging/key-manager.spec index 03668c4..e849246 100644 --- a/packaging/key-manager.spec +++ b/packaging/key-manager.spec @@ -5,7 +5,7 @@ Name: key-manager Summary: Central Key Manager and utilities -Version: 0.1.34 +Version: 0.1.35 Release: 1 Group: Security/Secure Storage License: Apache-2.0 and BSD-3-Clause -- 2.7.4 From 684af762f87eff9526ac82683e71ce9db3bc1369 Mon Sep 17 00:00:00 2001 From: Konrad Lipinski Date: Tue, 17 Mar 2020 12:09:20 +0100 Subject: [PATCH 08/16] Move to -std=c++14 Change-Id: Id2f9eaa0ab2237aa8a8da379949cd239ec69d565 --- CMakeLists.txt | 8 ++++---- src/manager/crypto/sw-backend/store.cpp | 14 ++++---------- src/manager/crypto/tz-backend/store.cpp | 14 ++++---------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0d9bc5..cfa5cc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,13 +29,13 @@ INCLUDE(FindPkgConfig) ############################# compiler flags ################################## SET(CMAKE_C_FLAGS_PROFILING "-g -O0 -pg -Wp,-U_FORTIFY_SOURCE") -SET(CMAKE_CXX_FLAGS_PROFILING "-g -std=c++0x -O0 -pg -Wp,-U_FORTIFY_SOURCE") +SET(CMAKE_CXX_FLAGS_PROFILING "-g -std=c++14 -O0 -pg -Wp,-U_FORTIFY_SOURCE") SET(CMAKE_C_FLAGS_DEBUG "-g -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") -SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++0x -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") +SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++14 -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") SET(CMAKE_C_FLAGS_RELEASE "-g -O2") -SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++0x -O2") +SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++14 -O2") SET(CMAKE_C_FLAGS_CCOV "-g -O2 --coverage") -SET(CMAKE_CXX_FLAGS_CCOV "-g -std=c++0x -O2 --coverage") +SET(CMAKE_CXX_FLAGS_CCOV "-g -std=c++14 -O2 --coverage") # Force PIE SET(CMAKE_POSITION_INDEPENDENT_CODE "True") diff --git a/src/manager/crypto/sw-backend/store.cpp b/src/manager/crypto/sw-backend/store.cpp index 0f27005..62dd580 100644 --- a/src/manager/crypto/sw-backend/store.cpp +++ b/src/manager/crypto/sw-backend/store.cpp @@ -44,12 +44,6 @@ enum EncryptionScheme { PASSWORD = 1 << 0 }; -template -std::unique_ptr make_unique(Args &&...args) -{ - return std::unique_ptr(new T(std::forward(args)...)); -} - RawBuffer generateRandIV() { RawBuffer civ(EVP_MAX_IV_LENGTH); @@ -164,16 +158,16 @@ GObjUPtr Store::getObject(const Token &token, const Password &pass) RawBuffer data = unpack(token.data, pass); if (token.dataType.isKeyPrivate() || token.dataType.isKeyPublic()) - return make_unique(data, token.dataType); + return std::make_unique(data, token.dataType); if (token.dataType == DataType(DataType::KEY_AES)) - return make_unique(data, token.dataType); + return std::make_unique(data, token.dataType); if (token.dataType.isCertificate() || token.dataType.isChainCert()) - return make_unique(data, token.dataType); + return std::make_unique(data, token.dataType); if (token.dataType.isBinaryData()) - return make_unique(data, token.dataType); + return std::make_unique(data, token.dataType); ThrowErr(Exc::Crypto::DataTypeNotSupported, "This type of data is not supported by openssl backend: ", (int)token.dataType); diff --git a/src/manager/crypto/tz-backend/store.cpp b/src/manager/crypto/tz-backend/store.cpp index 978a155..e5402ea 100644 --- a/src/manager/crypto/tz-backend/store.cpp +++ b/src/manager/crypto/tz-backend/store.cpp @@ -34,12 +34,6 @@ namespace TZ { namespace { -template -std::unique_ptr make_unique(Args &&...args) -{ - return std::unique_ptr(new T(std::forward(args)...)); -} - // internal SW encryption scheme flags enum EncryptionScheme { NONE = 0, @@ -115,17 +109,17 @@ GObjUPtr Store::getObject(const Token &token, const Password &pass) } if (token.dataType.isKeyPrivate() || token.dataType.isKeyPublic()) - return make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); + return std::make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); if (token.dataType.isSKey()) - return make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); + return std::make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); if (token.dataType.isCertificate() || token.dataType.isChainCert()) - return make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); + return std::make_unique(scheme, id, Pwd(pass, iv, tag), token.dataType); if (token.dataType.isBinaryData()) { RawBuffer exported_data = Internals::getData(id, Pwd(pass, iv, tag)); - return make_unique(std::move(exported_data)); + return std::make_unique(std::move(exported_data)); } ThrowErr(Exc::Crypto::DataTypeNotSupported, -- 2.7.4 From efb2e30c43358527ec47579b6306ca1becd46e04 Mon Sep 17 00:00:00 2001 From: Konrad Lipinski Date: Tue, 17 Mar 2020 17:31:45 +0100 Subject: [PATCH 09/16] Store DB::Crypto::m_connection as unique_ptr Change-Id: I289c8c7c62af72ae34ac1692f89af1d2bfd813f6 --- src/manager/service/db-crypto.cpp | 49 ++++++++++++++++----------------------- src/manager/service/db-crypto.h | 8 +++---- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/manager/service/db-crypto.cpp b/src/manager/service/db-crypto.cpp index d7acb0f..1121d66 100644 --- a/src/manager/service/db-crypto.cpp +++ b/src/manager/service/db-crypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2020 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. @@ -150,10 +150,10 @@ const char *DB_CMD_NAME_SELECT_BY_TYPE_AND_PERMISSION = namespace CKM { namespace DB { Crypto::Crypto(const std::string &path, const RawBuffer &rawPass) : - m_connection(nullptr), m_inUserTransaction(false) + m_inUserTransaction(false) { try { - m_connection = new SqlConnection(path, SqlConnection::Flag::Option::CRW); + m_connection.reset(new SqlConnection(path, SqlConnection::Flag::Option::CRW)); m_connection->SetKey(rawPass); initDatabase(); m_connection->ExecCommand("VACUUM;"); @@ -173,27 +173,18 @@ Crypto::Crypto(const std::string &path, const RawBuffer &rawPass) : } Crypto::Crypto(Crypto &&other) : - m_connection(other.m_connection), + m_connection(std::move(other.m_connection)), m_inUserTransaction(other.m_inUserTransaction) { - other.m_connection = NULL; other.m_inUserTransaction = false; } -Crypto::~Crypto() -{ - delete m_connection; -} - Crypto &Crypto::operator=(Crypto &&other) { if (this == &other) return *this; - delete m_connection; - - m_connection = other.m_connection; - other.m_connection = NULL; + m_connection = std::move(other.m_connection); m_inUserTransaction = other.m_inUserTransaction; other.m_inUserTransaction = false; @@ -232,7 +223,7 @@ bool Crypto::getDBVersion(int &schemaVersion) Transaction transaction(this); if (m_connection->CheckTableExist("SCHEMA_INFO")) { - SchemaInfo SchemaInfo(m_connection); + SchemaInfo SchemaInfo(m_connection.get()); if (SchemaInfo.getVersionInfo(schemaVersion)) { LogDebug("Current DB version: " << schemaVersion); return true; @@ -285,7 +276,7 @@ void Crypto::initDatabase() } // update DB version info - SchemaInfo SchemaInfo(m_connection); + SchemaInfo SchemaInfo(m_connection.get()); SchemaInfo.setVersionInfo(); transaction.commit(); } @@ -323,7 +314,7 @@ void Crypto::createDBSchema() "Can not create the database schema: no initialization script"); m_connection->ExecCommand((*script).c_str()); - SchemaInfo SchemaInfo(m_connection); + SchemaInfo SchemaInfo(m_connection.get()); SchemaInfo.setVersionInfo(); transaction.commit(); } @@ -344,7 +335,7 @@ void Crypto::resetDB() bool Crypto::isNameOwnerPresent(const Name &name, const ClientId &owner) const { try { - NameTable nameTable(this->m_connection); + NameTable nameTable(m_connection.get()); return nameTable.isPresent(name, owner); } catch (const SqlConnection::Exception::SyntaxError &) { LogError("Couldn't prepare insert statement"); @@ -361,9 +352,9 @@ void Crypto::saveRows(const Name &name, const ClientId &owner, { try { // transaction is present in the layer above - NameTable nameTable(this->m_connection); - ObjectTable objectTable(this->m_connection); - PermissionTable permissionTable(this->m_connection); + NameTable nameTable(m_connection.get()); + ObjectTable objectTable(m_connection.get()); + PermissionTable permissionTable(m_connection.get()); nameTable.addRow(name, owner); for (const auto &i : rows) @@ -389,9 +380,9 @@ void Crypto::saveRow(const Row &row) { try { // transaction is present in the layer above - NameTable nameTable(this->m_connection); - ObjectTable objectTable(this->m_connection); - PermissionTable permissionTable(this->m_connection); + NameTable nameTable(m_connection.get()); + ObjectTable objectTable(m_connection.get()); + PermissionTable permissionTable(m_connection.get()); nameTable.addRow(row.name, row.owner); objectTable.addRow(row); permissionTable.setPermission(row.name, @@ -412,7 +403,7 @@ void Crypto::updateRow(const Row &row) { try { // transaction is present in the layer above - ObjectTable objectTable(this->m_connection); + ObjectTable objectTable(m_connection.get()); objectTable.updateRow(row); return; } catch (const SqlConnection::Exception::SyntaxError &) { @@ -430,7 +421,7 @@ bool Crypto::deleteRow( { try { // transaction is present in the layer above - NameTable nameTable(this->m_connection); + NameTable nameTable(m_connection.get()); if (nameTable.isPresent(name, owner)) { nameTable.deleteRow(name, owner); @@ -473,7 +464,7 @@ PermissionMaskOptional Crypto::getPermissionRow( const ClientId &accessor) const { try { - PermissionTable permissionTable(this->m_connection); + PermissionTable permissionTable(m_connection.get()); return permissionTable.getPermissionRow(name, owner, accessor); } catch (const SqlConnection::Exception::InvalidColumn &) { LogError("Select statement invalid column error"); @@ -679,7 +670,7 @@ void Crypto::deleteKey(const ClientId &owner) deleteCommand->BindString(1, owner.c_str()); deleteCommand->Step(); - NameTable nameTable(this->m_connection); + NameTable nameTable(m_connection.get()); nameTable.deleteAllRows(owner); transaction.commit(); @@ -700,7 +691,7 @@ void Crypto::setPermission( const PermissionMask permissionMask) { try { - PermissionTable permissionTable(this->m_connection); + PermissionTable permissionTable(m_connection.get()); permissionTable.setPermission(name, owner, accessor, permissionMask); return; } catch (const SqlConnection::Exception::SyntaxError &) { diff --git a/src/manager/service/db-crypto.h b/src/manager/service/db-crypto.h index 4642b74..a915307 100644 --- a/src/manager/service/db-crypto.h +++ b/src/manager/service/db-crypto.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2014-2020 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. @@ -43,7 +43,7 @@ public: using RowOptional = boost::optional; using RawBufferOptional = boost::optional; - Crypto() : m_connection(nullptr), m_inUserTransaction(false) {} + Crypto() : m_inUserTransaction(false) {} // user name instead of path? Crypto(const std::string &path, const RawBuffer &rawPass); @@ -53,7 +53,7 @@ public: Crypto &operator=(const Crypto &) = delete; Crypto &operator=(Crypto &&other); - virtual ~Crypto(); + virtual ~Crypto() = default; void saveRow( const Row &row); @@ -198,7 +198,7 @@ public: }; protected: - SqlConnection *m_connection; + std::unique_ptr m_connection; Row getRow( const SqlConnection::DataCommandUniquePtr &selectCommand) const; -- 2.7.4 From 14c65b45b49ee451ef748508d4d95a1e252d93ba Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Wed, 18 Mar 2020 07:52:14 +0100 Subject: [PATCH 10/16] Fix build break Previously, log_build_info was not having additional bool parameter. When new parameter was added, new function was added that overridden previous one but had no chance of being properly used (ambiguity introduced). This failed at compile time in some envs, depending on options used. Change-Id: Icb8ffeae5c0c51bca2e9a6f2a0956fc6fe1590ec --- tests/colour_log_formatter.cpp | 13 ------------- tests/colour_log_formatter.h | 1 - 2 files changed, 14 deletions(-) diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp index fb51aaa..38d7e7c 100644 --- a/tests/colour_log_formatter.cpp +++ b/tests/colour_log_formatter.cpp @@ -111,19 +111,6 @@ colour_log_formatter::log_finish(std::ostream &ostr) //____________________________________________________________________________// void -colour_log_formatter::log_build_info(std::ostream &output) -{ - output << "Platform: " << BOOST_PLATFORM << '\n' - << "Compiler: " << BOOST_COMPILER << '\n' - << "STL : " << BOOST_STDLIB << '\n' - << "Boost : " << BOOST_VERSION / 100000 << "." - << BOOST_VERSION / 100 % 1000 << "." - << BOOST_VERSION % 100 << std::endl; -} - -//____________________________________________________________________________// - -void colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info) { if (log_build_info) diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h index 937ef9b..7fbc934 100644 --- a/tests/colour_log_formatter.h +++ b/tests/colour_log_formatter.h @@ -24,7 +24,6 @@ public: std::ostream &, boost::unit_test::counter_t test_cases_amount); void log_finish(std::ostream &); - void log_build_info(std::ostream &); void log_build_info(std::ostream &output, bool log_build_info = true); void test_unit_start( -- 2.7.4 From f3eaa030bad3fe97b204f4c3e63c0711cf7b6287 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Wed, 18 Mar 2020 08:03:36 +0100 Subject: [PATCH 11/16] Release 0.1.36 * Fixed build break in some environments * Store DB::Crypto::m_connection as unique_ptr * Move to -std=c++14 Change-Id: I8a1982b8f4f22f22ce5a460f4a85f2e7197a3637 --- packaging/key-manager.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/key-manager.spec b/packaging/key-manager.spec index e849246..8010dd7 100644 --- a/packaging/key-manager.spec +++ b/packaging/key-manager.spec @@ -5,7 +5,7 @@ Name: key-manager Summary: Central Key Manager and utilities -Version: 0.1.35 +Version: 0.1.36 Release: 1 Group: Security/Secure Storage License: Apache-2.0 and BSD-3-Clause -- 2.7.4 From f813624c0f590599b106e24316edf3291c6090e9 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Mon, 16 Mar 2020 15:54:45 +0100 Subject: [PATCH 12/16] Categorize tests into positive and negative Wise men said: "Thou shalt not covet positive tests more than the negative ones" To easily distinguish between positive and negative tests their names will be prefixed with "POSITIVE_" and "NEGATIVE_" string respectively. Boost test macros wrappers included. Existing tests have been categorized. Change-Id: Ifb21077437ebf82d2a2f4b4c70c53ab61b320c49 --- tests/boost_macros_wrapper.h | 23 +++++++++++++++++++++ tests/test_async-observer.cpp | 12 ++++++++--- tests/test_base64.cpp | 10 +++++----- tests/test_binary-queue.cpp | 16 +++++++-------- tests/test_certificate.cpp | 12 +++++------ tests/test_comm-manager.cpp | 16 +++++++-------- tests/test_crypto-logic.cpp | 30 ++++++++++++++++++++-------- tests/test_data-type.cpp | 17 ++++++++++------ tests/test_db_crypto.cpp | 34 +++++++++++++++---------------- tests/test_descriptor-set.cpp | 22 ++++++++++---------- tests/test_dpl-db.cpp | 8 ++++---- tests/test_dpl-exception.cpp | 6 +++--- tests/test_encryption-scheme.cpp | 26 ++++++++++++------------ tests/test_exception.cpp | 18 ++++++++--------- tests/test_for-each-file.cpp | 6 +++--- tests/test_generic-backend.cpp | 6 +++--- tests/test_key-provider.cpp | 43 +++++++++++++++++++++++----------------- tests/test_key.cpp | 21 ++++++++++++++------ tests/test_log-provider.cpp | 12 +++++------ tests/test_safe-buffer.cpp | 12 +++++------ tests/test_serialization.cpp | 26 ++++++++++++++++++++---- tests/test_sql.cpp | 18 ++++++++--------- tests/test_ss-crypto.cpp | 6 +++--- tests/test_stringify.cpp | 10 +++++----- tests/test_sw-backend.cpp | 13 +++++++----- tests/test_xml-parser.cpp | 26 ++++++++++++------------ 26 files changed, 267 insertions(+), 182 deletions(-) create mode 100644 tests/boost_macros_wrapper.h diff --git a/tests/boost_macros_wrapper.h b/tests/boost_macros_wrapper.h new file mode 100644 index 0000000..0a8c32a --- /dev/null +++ b/tests/boost_macros_wrapper.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020 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 + */ + +#pragma once + +#include + +#define POSITIVE_TEST_CASE(name) BOOST_AUTO_TEST_CASE(POSITIVE_ ## name) + +#define NEGATIVE_TEST_CASE(name) BOOST_AUTO_TEST_CASE(NEGATIVE_ ## name) diff --git a/tests/test_async-observer.cpp b/tests/test_async-observer.cpp index 7479605..37d4344 100644 --- a/tests/test_async-observer.cpp +++ b/tests/test_async-observer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000 - 2020 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. @@ -14,8 +14,8 @@ * limitations under the License */ #include -#include #include +#include using namespace CKM; @@ -54,11 +54,17 @@ const std::string CERT_PEM = BOOST_AUTO_TEST_SUITE(ASYNC_OBSERVER_TEST) -BOOST_AUTO_TEST_CASE(base) + +NEGATIVE_TEST_CASE(base) { TestObserver o; BOOST_REQUIRE_THROW(o.ReceivedError(0), std::invalid_argument); +} + +POSITIVE_TEST_CASE(base) +{ + TestObserver o; BOOST_REQUIRE_NO_THROW(o.ReceivedSaveKey()); BOOST_REQUIRE_NO_THROW(o.ReceivedSaveCertificate()); diff --git a/tests/test_base64.cpp b/tests/test_base64.cpp index cc6ce45..6a75d19 100644 --- a/tests/test_base64.cpp +++ b/tests/test_base64.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016-2020 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. @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include @@ -47,7 +47,7 @@ const RawBuffer rawbuf(RAW_DATA_VEC.begin(), RAW_DATA_VEC.end()); BOOST_AUTO_TEST_SUITE(BASE64_TEST) -BOOST_AUTO_TEST_CASE(ENCODE_DECODE_POSITIVE) +POSITIVE_TEST_CASE(ENCODE_DECODE) { /* try encode */ Base64Encoder encoder; @@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE(ENCODE_DECODE_POSITIVE) "Original data and encoded-decoded data is different!"); } -BOOST_AUTO_TEST_CASE(THROW_SOMETHING) +NEGATIVE_TEST_CASE(THROW_SOMETHING) { /* encode data */ Base64Encoder encoder; @@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(THROW_SOMETHING) BOOST_REQUIRE_NO_THROW(decdata = decoder.get()); } -BOOST_AUTO_TEST_CASE(ILLEGAL_DATA) +NEGATIVE_TEST_CASE(ILLEGAL_DATA) { Base64Decoder decoder; BOOST_REQUIRE_NO_THROW(decoder.append(rawbuf)); diff --git a/tests/test_binary-queue.cpp b/tests/test_binary-queue.cpp index 5180ded..28b1833 100644 --- a/tests/test_binary-queue.cpp +++ b/tests/test_binary-queue.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000 - 2020 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License */ -#include +#include #include #include @@ -28,7 +28,7 @@ RawBuffer buf({0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}); BOOST_AUTO_TEST_SUITE(BINARY_QUEUE_TEST) -BOOST_AUTO_TEST_CASE(copy_assignment) +POSITIVE_TEST_CASE(copy_assignment) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(copy_assignment) BOOST_REQUIRE(buf1 == buf2); } -BOOST_AUTO_TEST_CASE(append_copy_to) +POSITIVE_TEST_CASE(append_copy_to) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(append_copy_to) BOOST_REQUIRE(buf1 == buf2); } -BOOST_AUTO_TEST_CASE(append_move_to) +POSITIVE_TEST_CASE(append_move_to) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); @@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(append_move_to) BOOST_REQUIRE(buf == buf2); } -BOOST_AUTO_TEST_CASE(read) +POSITIVE_TEST_CASE(read) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); @@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE(read) BOOST_REQUIRE(buf == buf2); } -BOOST_AUTO_TEST_CASE(write) +POSITIVE_TEST_CASE(write) { BinaryQueue bq1; bq1.AppendCopy(buf.data(), buf.size()); @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(write) BOOST_REQUIRE(buf1 == buf2); } -BOOST_AUTO_TEST_CASE(bucket_visitor) +POSITIVE_TEST_CASE(bucket_visitor) { static std::vector globalBuf; diff --git a/tests/test_certificate.cpp b/tests/test_certificate.cpp index 8c7695a..3e5f03e 100644 --- a/tests/test_certificate.cpp +++ b/tests/test_certificate.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2000 - 2020 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. @@ -15,7 +15,7 @@ */ #include -#include +#include namespace { const std::string CERT_PEM = @@ -80,7 +80,7 @@ using namespace CKM; BOOST_AUTO_TEST_SUITE(CERTIFICATE_TEST) -BOOST_AUTO_TEST_CASE(constructors) +POSITIVE_TEST_CASE(constructors) { RawBuffer certpem(CERT_PEM.begin(), CERT_PEM.end()); BOOST_REQUIRE_NO_THROW(CertificateImpl(certpem, DataFormat::FORM_PEM)); @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(constructors) BOOST_REQUIRE(!Certificate::create(dummy, DataFormat::FORM_PEM)); } -BOOST_AUTO_TEST_CASE(move_semantics) +POSITIVE_TEST_CASE(move_semantics) { RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); @@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(move_semantics) BOOST_REQUIRE(cert1.getDER() == moveConstructed.getDER()); } -BOOST_AUTO_TEST_CASE(get_evp_sh_ptr) +POSITIVE_TEST_CASE(get_evp_sh_ptr) { RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); RawBuffer pubkeybuf(CERT_PUBKEY_PEM.begin(), CERT_PUBKEY_PEM.end()); @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(get_evp_sh_ptr) BOOST_REQUIRE(pubkeyFromCert.getDER() == pubkey.getDER()); } -BOOST_AUTO_TEST_CASE(get_ocsp_url) +POSITIVE_TEST_CASE(get_ocsp_url) { RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); CertificateImpl cert(certbuf, DataFormat::FORM_PEM); diff --git a/tests/test_comm-manager.cpp b/tests/test_comm-manager.cpp index 6817632..a6216a9 100644 --- a/tests/test_comm-manager.cpp +++ b/tests/test_comm-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2000-2020 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. @@ -19,7 +19,7 @@ * @version 1.0 */ -#include +#include #include #include #include @@ -63,14 +63,14 @@ struct Listener { BOOST_AUTO_TEST_SUITE(MESSAGE_MANAGER_TEST) -BOOST_AUTO_TEST_CASE(TMM_0010_NoListener) +POSITIVE_TEST_CASE(TMM_0010_NoListener) { CKM::CommunicationManager mgr; BOOST_REQUIRE_MESSAGE(0 == mgr.SendMessage(MessageA(22)), "There should be no listener."); } -BOOST_AUTO_TEST_CASE(TMM_0020_Basic) +POSITIVE_TEST_CASE(TMM_0020_Basic) { CKM::CommunicationManager mgr; int received = 0; @@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(TMM_0020_Basic) BOOST_REQUIRE_MESSAGE(received == 4, "Wrong message received i=" << received); } -BOOST_AUTO_TEST_CASE(TMM_0030_MultipleMessages) +POSITIVE_TEST_CASE(TMM_0030_MultipleMessages) { CKM::CommunicationManager mgr; int reci = 0; @@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE(TMM_0030_MultipleMessages) BOOST_REQUIRE_MESSAGE(recc == 'c', "Previous message overwritten c=" << recc); } -BOOST_AUTO_TEST_CASE(TMM_0040_Listener) +POSITIVE_TEST_CASE(TMM_0040_Listener) { CKM::CommunicationManager mgr; Listener l; @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(TMM_0040_Listener) "Previous message overwritten str=" << l.str); } -BOOST_AUTO_TEST_CASE(TMM_0050_2Listeners) +POSITIVE_TEST_CASE(TMM_0050_2Listeners) { CKM::CommunicationManager mgr; bool called[2]; @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(TMM_0050_2Listeners) BOOST_REQUIRE_MESSAGE(called[1], "Second listener not called"); } -BOOST_AUTO_TEST_CASE(TMM_0060_Stress) +POSITIVE_TEST_CASE(TMM_0060_Stress) { CKM::CommunicationManager mgr; diff --git a/tests/test_crypto-logic.cpp b/tests/test_crypto-logic.cpp index d13f6af..142489a 100644 --- a/tests/test_crypto-logic.cpp +++ b/tests/test_crypto-logic.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include "test_common.h" @@ -44,7 +44,7 @@ Password createRandomPass(size_t size) BOOST_AUTO_TEST_SUITE(CRYPTO_LOGIC_TEST) -BOOST_AUTO_TEST_CASE(move_semantics) +POSITIVE_TEST_CASE(move_semantics) { CryptoLogic logic; @@ -63,7 +63,21 @@ BOOST_AUTO_TEST_CASE(move_semantics) BOOST_REQUIRE(moveAssigned.haveKey(client)); } -BOOST_AUTO_TEST_CASE(push_key) +POSITIVE_TEST_CASE(push_key) +{ + CryptoLogic logic; + + const ClientId client = "test_client"; + + BOOST_REQUIRE_NO_THROW(logic.pushKey(client, createRandom(10))); + + ClientId increasingOwner = "a"; + for (size_t i = 0; i < 20; ++i, increasingOwner.push_back('a')) { + BOOST_REQUIRE_NO_THROW(logic.pushKey(increasingOwner, createRandom(10))); + } +} + +NEGATIVE_TEST_CASE(push_key) { CryptoLogic logic; @@ -85,7 +99,7 @@ BOOST_AUTO_TEST_CASE(push_key) } } -BOOST_AUTO_TEST_CASE(row_encryption) +POSITIVE_TEST_CASE(row_encryption) { Policy policy(Password(), true); Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10)); @@ -99,15 +113,13 @@ BOOST_AUTO_TEST_CASE(row_encryption) CryptoLogic logic; - BOOST_REQUIRE_THROW(logic.encryptRow(row), Exc::InternalError); - auto key = createRandom(32); BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key)); BOOST_REQUIRE_NO_THROW(logic.encryptRow(row)); BOOST_REQUIRE_NO_THROW(logic.decryptRow(policy.password, row)); } -BOOST_AUTO_TEST_CASE(row_encryption_negatives) +NEGATIVE_TEST_CASE(row_encryption) { Policy policy(Password(), true); Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10)); @@ -121,6 +133,8 @@ BOOST_AUTO_TEST_CASE(row_encryption_negatives) CryptoLogic logic; + BOOST_REQUIRE_THROW(logic.encryptRow(row), Exc::InternalError); + auto key = createRandom(32); BOOST_REQUIRE_NO_THROW(logic.pushKey(owner, key)); BOOST_REQUIRE_NO_THROW(logic.encryptRow(row)); diff --git a/tests/test_data-type.cpp b/tests/test_data-type.cpp index c147541..410489b 100644 --- a/tests/test_data-type.cpp +++ b/tests/test_data-type.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 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. @@ -20,7 +20,7 @@ */ #include -#include +#include #include #include @@ -31,7 +31,7 @@ using CKM::AlgoType; BOOST_AUTO_TEST_SUITE(DATA_TYPE_TEST) -BOOST_AUTO_TEST_CASE(CONSTRUCTOR) +NEGATIVE_TEST_CASE(CONSTRUCTOR) { BOOST_REQUIRE_THROW(DataType(static_cast(999)), CKM::Exc::InputParam); @@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(CONSTRUCTOR) CKM::Exc::InputParam); } -BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING) +POSITIVE_TEST_CASE(KEY_TYPE_CASTING) { std::vector> pairs; @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(KEY_TYPE_CASTING) BOOST_REQUIRE(p.second == DataType(static_cast(p.first))); } -BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS) +POSITIVE_TEST_CASE(UNARY_OPERATIONS) { BOOST_REQUIRE(DataType(DataType::KEY_AES).isSKey()); BOOST_REQUIRE(!DataType(DataType::KEY_RSA_PUBLIC).isSKey()); @@ -110,7 +110,7 @@ BOOST_AUTO_TEST_CASE(UNARY_OPERATIONS) BOOST_REQUIRE(!DataType().isKey()); } -BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE) +POSITIVE_TEST_CASE(GET_CHAIN_TYPE) { DataType type; @@ -119,6 +119,11 @@ BOOST_AUTO_TEST_CASE(GET_CHAIN_TYPE) BOOST_REQUIRE(type.getChainDatatype(8) == DataType(DataType::CHAIN_CERT_8)); BOOST_REQUIRE(type.getChainDatatype(13) == DataType(DataType::CHAIN_CERT_13)); BOOST_REQUIRE(type.getChainDatatype(15) == DataType(DataType::DB_CHAIN_LAST)); +} + +NEGATIVE_TEST_CASE(GET_CHAIN_TYPE) +{ + DataType type; BOOST_REQUIRE_THROW(type.getChainDatatype(16), CKM::Exc::InputParam); } diff --git a/tests/test_db_crypto.cpp b/tests/test_db_crypto.cpp index 7d6c698..68b01b4 100644 --- a/tests/test_db_crypto.cpp +++ b/tests/test_db_crypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved * * Contact: Kyungwook Tak * @@ -20,7 +20,7 @@ * @version * @brief */ -#include +#include #include #include #include @@ -44,7 +44,7 @@ const unsigned int c_names_per_owner = 15; } // namespace anonymous BOOST_FIXTURE_TEST_SUITE(DBCRYPTO_TEST, DBFixture) -BOOST_AUTO_TEST_CASE(DBtestSimple) +POSITIVE_TEST_CASE(DBtestSimple) { DB::Row rowPattern = create_default_row(); rowPattern.data = RawBuffer(32, 1); @@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(DBtestSimple) check_DB_integrity(rowPattern); } -BOOST_AUTO_TEST_CASE(DBtestBIG) +POSITIVE_TEST_CASE(DBtestBIG) { DB::Row rowPattern = create_default_row(); rowPattern.data = createBigBlob(4096); @@ -62,7 +62,7 @@ BOOST_AUTO_TEST_CASE(DBtestBIG) check_DB_integrity(rowPattern); } -BOOST_AUTO_TEST_CASE(DBtestGlobal) +POSITIVE_TEST_CASE(DBtestGlobal) { DB::Row rowPattern = create_default_row(); rowPattern.data = RawBuffer(1024, 2); @@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE(DBtestGlobal) DB::Row name_duplicate = rowPattern; rowPattern.owner = rowPattern.owner + "1"; } -BOOST_AUTO_TEST_CASE(DBtestTransaction) +POSITIVE_TEST_CASE(DBtestTransaction) { DB::Row rowPattern = create_default_row(); rowPattern.data = RawBuffer(100, 20); @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(DBtestTransaction) BOOST_CHECK_MESSAGE(!row_optional, "Row still present after rollback"); } -BOOST_AUTO_TEST_CASE(DBtestBackend) +POSITIVE_TEST_CASE(DBtestBackend) { DB::Row rowPattern = create_default_row(); rowPattern.data = RawBuffer(32, 1); @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_FIXTURE_TEST_SUITE(DBCRYPTO_PERF_TEST, DBFixture) -BOOST_AUTO_TEST_CASE(DBperfAddNames) +POSITIVE_TEST_CASE(DBperfAddNames) { // actual test performance_start("saveRow"); @@ -127,7 +127,7 @@ BOOST_AUTO_TEST_CASE(DBperfAddNames) performance_stop(c_num_names_add_test); } -BOOST_AUTO_TEST_CASE(DBperfLookupAliasByOwner) +POSITIVE_TEST_CASE(DBperfLookupAliasByOwner) { // prepare data generate_perf_DB(c_num_names, c_names_per_owner); @@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(DBperfLookupAliasByOwner) } // TODO this test makes no sense. Rewrite it. -BOOST_AUTO_TEST_CASE(DBperfLookupAliasRandomOwnershipNoPermissions) +POSITIVE_TEST_CASE(DBperfLookupAliasRandomOwnershipNoPermissions) { // prepare data generate_perf_DB(c_num_names, c_names_per_owner); @@ -182,7 +182,7 @@ BOOST_AUTO_TEST_CASE(DBperfLookupAliasRandomOwnershipNoPermissions) performance_stop(c_test_retries * c_num_names); } -BOOST_AUTO_TEST_CASE(DBperfAddPermissions) +POSITIVE_TEST_CASE(DBperfAddPermissions) { // prepare data generate_perf_DB(c_num_names, c_names_per_owner); @@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(DBperfAddPermissions) performance_stop(iterations); } -BOOST_AUTO_TEST_CASE(DBperfAliasRemoval) +POSITIVE_TEST_CASE(DBperfAliasRemoval) { // prepare data generate_perf_DB(c_num_names, c_names_per_owner); @@ -225,7 +225,7 @@ BOOST_AUTO_TEST_CASE(DBperfAliasRemoval) } } -BOOST_AUTO_TEST_CASE(DBperfGetAliasList) +POSITIVE_TEST_CASE(DBperfGetAliasList) { // prepare data generate_perf_DB(c_num_names, c_names_per_owner); @@ -318,25 +318,25 @@ struct DBVer3Migration : public DBFixture { }; } -BOOST_AUTO_TEST_CASE(DBMigrationDBVer1) +POSITIVE_TEST_CASE(DBMigrationDBVer1) { DBVer1Migration DBver1; verifyDBisValid(DBver1); } -BOOST_AUTO_TEST_CASE(DBMigrationDBVer2) +POSITIVE_TEST_CASE(DBMigrationDBVer2) { DBVer2Migration DBver2; verifyDBisValid(DBver2); } -BOOST_AUTO_TEST_CASE(DBMigrationDBVer3) +POSITIVE_TEST_CASE(DBMigrationDBVer3) { DBVer3Migration DBver3; verifyDBisValid(DBver3); } -BOOST_AUTO_TEST_CASE(DBMigrationDBCurrent) +POSITIVE_TEST_CASE(DBMigrationDBCurrent) { DBFixture currentDB; diff --git a/tests/test_descriptor-set.cpp b/tests/test_descriptor-set.cpp index 4d345a8..1215639 100644 --- a/tests/test_descriptor-set.cpp +++ b/tests/test_descriptor-set.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014 - 2020 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. @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -91,7 +91,7 @@ BOOST_AUTO_TEST_SUITE(DESCRIPTOR_SET_TEST) /* * Wait on empty descriptor set. Function should return immediately. */ -BOOST_AUTO_TEST_CASE(T010_Empty) +POSITIVE_TEST_CASE(T010_Empty) { DescriptorSet descriptors; @@ -102,7 +102,7 @@ BOOST_AUTO_TEST_CASE(T010_Empty) * Add and remove (twice) descriptor. Wait on empty set. No callback should be called. wait() should * return immediately. */ -BOOST_AUTO_TEST_CASE(T020_AddRemove) +POSITIVE_TEST_CASE(T020_AddRemove) { DescriptorSet descriptors; descriptors.add(10, POLLALL, unexpectedCallback); @@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(T020_AddRemove) * Add 2 descriptors and purge all. Wait on empty set. No callback should be called. wait() should * return immediately. */ -BOOST_AUTO_TEST_CASE(T030_AddPurge) +POSITIVE_TEST_CASE(T030_AddPurge) { DescriptorSet descriptors; descriptors.add(10, POLLALL, unexpectedCallback); @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(T030_AddPurge) * Add pipe[1] descriptor and wait for write possibility. Provided callback should be called * immediately. */ -BOOST_AUTO_TEST_CASE(T040_Callback) +POSITIVE_TEST_CASE(T040_Callback) { DescriptorSet descriptors; bool callback = false; @@ -150,7 +150,7 @@ BOOST_AUTO_TEST_CASE(T040_Callback) * Add pipe[1] descriptor twice with different callbacks. The first one should be overwritten and * shouldn't be called. The second one should be called instead. */ -BOOST_AUTO_TEST_CASE(T050_DoubleAdd) +POSITIVE_TEST_CASE(T050_DoubleAdd) { DescriptorSet descriptors; bool callback = false; @@ -171,7 +171,7 @@ BOOST_AUTO_TEST_CASE(T050_DoubleAdd) * Add pipe[0] descriptor and wait. Callback should not be called. Instead the 8s timeout should * occur and a proper exception should be thrown. */ -BOOST_AUTO_TEST_CASE(T060_Timeout) +NEGATIVE_TEST_CASE(T060_Timeout) { DescriptorSet descriptors; @@ -186,7 +186,7 @@ BOOST_AUTO_TEST_CASE(T060_Timeout) /* * Create pipe and try to write it. Start thread that will read it. */ -BOOST_AUTO_TEST_CASE(T070_Write) +POSITIVE_TEST_CASE(T070_Write) { DescriptorSet descriptors; bool callback = false; @@ -214,7 +214,7 @@ BOOST_AUTO_TEST_CASE(T070_Write) /* * Create pipe and try to read it. Start thread that will write it. */ -BOOST_AUTO_TEST_CASE(T080_Read) +POSITIVE_TEST_CASE(T080_Read) { DescriptorSet descriptors; bool callback = false; @@ -243,7 +243,7 @@ BOOST_AUTO_TEST_CASE(T080_Read) * the pipe, remove it from the descriptor set and try to write the second pipe. The thread will * read it. In second pipe callback remove the second pipe descriptor from the set. */ -BOOST_AUTO_TEST_CASE(T090_WriteAfterRead) +POSITIVE_TEST_CASE(T090_WriteAfterRead) { DescriptorSet descriptors; bool callback1 = false; diff --git a/tests/test_dpl-db.cpp b/tests/test_dpl-db.cpp index 610c9ad..e439487 100644 --- a/tests/test_dpl-db.cpp +++ b/tests/test_dpl-db.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License */ -#include +#include #include @@ -24,7 +24,7 @@ BOOST_AUTO_TEST_SUITE(DPL_DB_TEST) BOOST_AUTO_TEST_SUITE(NAIVE_SYNCHRONIZATION_OBJECT) -BOOST_AUTO_TEST_CASE(base) +POSITIVE_TEST_CASE(base) { NaiveSynchronizationObject obj; @@ -36,7 +36,7 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(SQL_CONNECTION) -BOOST_AUTO_TEST_CASE(connection_broken) +NEGATIVE_TEST_CASE(connection_broken) { BOOST_REQUIRE_THROW( SqlConnection("test-db", static_cast(999999)), diff --git a/tests/test_dpl-exception.cpp b/tests/test_dpl-exception.cpp index 35214e5..f4d2aa8 100644 --- a/tests/test_dpl-exception.cpp +++ b/tests/test_dpl-exception.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License */ -#include +#include #include @@ -21,7 +21,7 @@ using namespace CKM; BOOST_AUTO_TEST_SUITE(DPL_EXCEPTION_TEST) -BOOST_AUTO_TEST_CASE(dpl_exception) +POSITIVE_TEST_CASE(dpl_exception) { try { throw Exception(__FILE__, __func__, __LINE__, "message"); diff --git a/tests/test_encryption-scheme.cpp b/tests/test_encryption-scheme.cpp index e77c38b..b16bc5b 100644 --- a/tests/test_encryption-scheme.cpp +++ b/tests/test_encryption-scheme.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 -2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015 -2020 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. @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include @@ -64,7 +64,7 @@ private: BOOST_FIXTURE_TEST_SUITE(ENCRYPTION_SCHEME_TEST, OnlycapFixture) // Test database should have the old scheme -BOOST_AUTO_TEST_CASE(T010_Check_old_scheme) +POSITIVE_TEST_CASE(T010_Check_old_scheme) { SchemeTest test; test.RestoreDb(); @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(T010_Check_old_scheme) test.CheckSchemeVersion(filter, OLD_ENC_SCHEME); } -BOOST_AUTO_TEST_CASE(T1111_Alias_Info_old_scheme) +POSITIVE_TEST_CASE(T1111_Alias_Info_old_scheme) { SchemeTest test; test.RestoreDb(); @@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(T1111_Alias_Info_old_scheme) } // Newly written data should use the new scheme -BOOST_AUTO_TEST_CASE(T020_Check_new_scheme) +POSITIVE_TEST_CASE(T020_Check_new_scheme) { SchemeTest test; test.RemoveUserData(); @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(T020_Check_new_scheme) test.CheckSchemeVersion(filter, NEW_ENC_SCHEME); } -BOOST_AUTO_TEST_CASE(T030_Remove_old_scheme) +POSITIVE_TEST_CASE(T030_Remove_old_scheme) { SchemeTest test; test.RestoreDb(); @@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(T030_Remove_old_scheme) BOOST_REQUIRE_MESSAGE(aliases == 0, "All aliases should be removed"); } -BOOST_AUTO_TEST_CASE(T040_Remove_new_scheme) +POSITIVE_TEST_CASE(T040_Remove_new_scheme) { SchemeTest test; test.RemoveUserData(); @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(T040_Remove_new_scheme) } // Reading old db should reencrypt objects with new scheme -BOOST_AUTO_TEST_CASE(T100_Read) +POSITIVE_TEST_CASE(T100_Read) { SchemeTest test; test.RestoreDb(); @@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(T100_Read) test.CheckSchemeVersion(filter, NEW_ENC_SCHEME); } -BOOST_AUTO_TEST_CASE(T110_Count_objects_after_read) +POSITIVE_TEST_CASE(T110_Count_objects_after_read) { SchemeTest test; test.RestoreDb(); @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(T110_Count_objects_after_read) } // Reading old db with incorrect passwords should leave the scheme unchanged -BOOST_AUTO_TEST_CASE(T120_Read_wrong_pass) +NEGATIVE_TEST_CASE(T120_Read_wrong_pass) { SchemeTest test; test.RestoreDb(); @@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(T120_Read_wrong_pass) } // Signing/verification should reencrypt objects with new scheme -BOOST_AUTO_TEST_CASE(T200_SignVerify) +POSITIVE_TEST_CASE(T200_SignVerify) { SchemeTest test; test.RestoreDb(); @@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(T200_SignVerify) } // Encryption/decryption should reencrypt objects with new scheme -BOOST_AUTO_TEST_CASE(T210_EncryptDecrypt) +POSITIVE_TEST_CASE(T210_EncryptDecrypt) { SchemeTest test; test.RestoreDb(); @@ -180,7 +180,7 @@ BOOST_AUTO_TEST_CASE(T210_EncryptDecrypt) } // Chain creation should reencrypt objects with new scheme -BOOST_AUTO_TEST_CASE(T220_CreateChain) +POSITIVE_TEST_CASE(T220_CreateChain) { SchemeTest test; test.RestoreDb(); diff --git a/tests/test_exception.cpp b/tests/test_exception.cpp index 009b2d2..cac3ec2 100644 --- a/tests/test_exception.cpp +++ b/tests/test_exception.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -15,7 +15,7 @@ */ #include -#include +#include #include using namespace CKM; @@ -50,37 +50,37 @@ void checkExceptionInternal(const Exc::Exception &e, int ec, const std::string & BOOST_AUTO_TEST_SUITE(EXCEPTION_TEST) -BOOST_AUTO_TEST_CASE(internal_error) +POSITIVE_TEST_CASE(internal_error) { CHECK_EXCEPTION(Exc::InternalError, CKM_API_ERROR_SERVER_ERROR, false); } -BOOST_AUTO_TEST_CASE(database_locked) +POSITIVE_TEST_CASE(database_locked) { CHECK_EXCEPTION(Exc::DatabaseLocked, CKM_API_ERROR_DB_LOCKED, false); } -BOOST_AUTO_TEST_CASE(database_failed) +POSITIVE_TEST_CASE(database_failed) { CHECK_EXCEPTION(Exc::DatabaseFailed, CKM_API_ERROR_DB_ERROR, false); } -BOOST_AUTO_TEST_CASE(filesystem_failed) +POSITIVE_TEST_CASE(filesystem_failed) { CHECK_EXCEPTION(Exc::FileSystemFailed, CKM_API_ERROR_FILE_SYSTEM, false); } -BOOST_AUTO_TEST_CASE(inputparam) +POSITIVE_TEST_CASE(inputparam) { CHECK_EXCEPTION(Exc::InputParam, CKM_API_ERROR_INPUT_PARAM, true); } -BOOST_AUTO_TEST_CASE(authentication_failed) +POSITIVE_TEST_CASE(authentication_failed) { CHECK_EXCEPTION(Exc::AuthenticationFailed, CKM_API_ERROR_AUTHENTICATION_FAILED, true); } -BOOST_AUTO_TEST_CASE(transaction_failed) +POSITIVE_TEST_CASE(transaction_failed) { CHECK_EXCEPTION(Exc::TransactionFailed, CKM_API_ERROR_DB_ERROR, false); } diff --git a/tests/test_for-each-file.cpp b/tests/test_for-each-file.cpp index 543910b..6a7954f 100644 --- a/tests/test_for-each-file.cpp +++ b/tests/test_for-each-file.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 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. @@ -20,7 +20,7 @@ */ #include -#include +#include #include #include @@ -30,7 +30,7 @@ using namespace CKM; BOOST_AUTO_TEST_SUITE(TRAVERSE_DIR_TEST) -BOOST_AUTO_TEST_CASE(T010_check_prefix) +POSITIVE_TEST_CASE(T010_check_prefix) { std::vector files; diff --git a/tests/test_generic-backend.cpp b/tests/test_generic-backend.cpp index c71e5dd..6bac2c1 100644 --- a/tests/test_generic-backend.cpp +++ b/tests/test_generic-backend.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2017 - 2020 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. @@ -18,7 +18,7 @@ #include #include -#include +#include using namespace CKM; @@ -43,7 +43,7 @@ public: BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST) -BOOST_AUTO_TEST_CASE(gobj) +NEGATIVE_TEST_CASE(gobj) { GObjTest obj; diff --git a/tests/test_key-provider.cpp b/tests/test_key-provider.cpp index 81d381d..ea369d1 100644 --- a/tests/test_key-provider.cpp +++ b/tests/test_key-provider.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved * * Contact: Kyungwook Tak * @@ -21,7 +21,7 @@ * @brief */ #define BOOST_TEST_MODULE KEY_MANAGER_TEST -#include +#include #include #include #include @@ -39,7 +39,7 @@ const std::string CLIENT_ID_2 = "SAMPLE_CLIENT_ID_2"; extern bool isLibInitialized; BOOST_AUTO_TEST_SUITE(KEY_PROVIDER_TEST) -BOOST_AUTO_TEST_CASE(KeyDomainKEK) +POSITIVE_TEST_CASE(KeyDomainKEK) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -52,7 +52,7 @@ BOOST_AUTO_TEST_CASE(KeyDomainKEK) "KeyProvider created, but uninitialized"); } -BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword) +NEGATIVE_TEST_CASE(KeyDomainKekInvalidPassword) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(KeyDomainKekInvalidPassword) "KeyProvider not created, but initialized"); } -BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK) +POSITIVE_TEST_CASE(KeygetPureDomainKEK) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(KeygetPureDomainKEK) BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getPureDomainKEK()); } -BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK) +POSITIVE_TEST_CASE(KeyGetWrappedDomainKEK) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -94,7 +94,7 @@ BOOST_AUTO_TEST_CASE(KeyGetWrappedDomainKEK) BOOST_REQUIRE_NO_THROW(rb_test = keyProvider.getWrappedDomainKEK(PASSWORD)); } -BOOST_AUTO_TEST_CASE(KeyGenerateDEK) +POSITIVE_TEST_CASE(KeyGenerateDEK) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE(KeyGenerateDEK) BOOST_REQUIRE_NO_THROW(rb_DEK1 = keyProvider.generateDEK(CLIENT_ID_1)); } -BOOST_AUTO_TEST_CASE(KeyGetPureDEK) +POSITIVE_TEST_CASE(KeyGetPureDEK) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -126,7 +126,7 @@ BOOST_AUTO_TEST_CASE(KeyGetPureDEK) BOOST_REQUIRE_NO_THROW(rb_pureDEK1 = keyProvider.getPureDEK(rb_DEK1)); } -BOOST_AUTO_TEST_CASE(KeyReencrypt) +POSITIVE_TEST_CASE(KeyReencrypt) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -137,7 +137,7 @@ BOOST_AUTO_TEST_CASE(KeyReencrypt) NEW_PASSWORD)); } -BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password) +NEGATIVE_TEST_CASE(KeyReencrypt_incorrect_password) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(KeyReencrypt_incorrect_password) NEW_PASSWORD)), CKM::Exc::AuthenticationFailed); } -BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt) +POSITIVE_TEST_CASE(KeyGetPureDEK_after_reencrypt) { BOOST_REQUIRE_MESSAGE(isLibInitialized, "Library is not initialized!"); @@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(KeyGetPureDEK_after_reencrypt) BOOST_REQUIRE_NO_THROW(keyProvider.getPureDEK(rb_DEK1)); } -BOOST_AUTO_TEST_CASE(wrapped_container) +POSITIVE_TEST_CASE(wrapped_container) { CKM::WrappedKeyAndInfoContainer wrappedContainer; @@ -171,9 +171,6 @@ BOOST_AUTO_TEST_CASE(wrapped_container) BOOST_REQUIRE_NO_THROW(wrappedContainer.setKeyInfoSalt(salt.data(), salt.size())); BOOST_REQUIRE_NO_THROW(wrappedContainer.setKeyInfoClient("key_info_client")); - BOOST_REQUIRE_THROW(wrappedContainer.setKeyInfoClient("key_info_client_waaaaay_too_long"), - CKM::Exc::InternalError); - CKM::WrappedKeyAndInfoContainer wrappedContainer2; BOOST_REQUIRE_NO_THROW( wrappedContainer2.setKeyInfo(&wrappedContainer.getWrappedKeyAndInfo().keyInfo)); @@ -194,8 +191,18 @@ BOOST_AUTO_TEST_CASE(wrapped_container) wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE; BOOST_REQUIRE_NO_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3( reinterpret_cast(&wrapped3))); +} + +NEGATIVE_TEST_CASE(wrapped_container) +{ + CKM::WrappedKeyAndInfoContainer wrappedContainer; + + BOOST_REQUIRE_THROW(wrappedContainer.setKeyInfoClient("key_info_client_waaaaay_too_long"), + CKM::Exc::InternalError); + + CKM::WrappedKeyAndInfo wrapped3; - wrapped3.keyInfo.keyLength++; + wrapped3.keyInfo.keyLength = MAX_WRAPPED_KEY_SIZE + 1; BOOST_REQUIRE_THROW(CKM::WrappedKeyAndInfoContainer wrappedContainer3( reinterpret_cast(&wrapped3)), CKM::Exc::InternalError); @@ -208,7 +215,7 @@ BOOST_AUTO_TEST_CASE(wrapped_container) CKM::Exc::InternalError); } -BOOST_AUTO_TEST_CASE(container) +POSITIVE_TEST_CASE(container) { CKM::KeyAndInfoContainer container; BOOST_REQUIRE_NO_THROW(container.setKeyInfoKeyLength(10)); @@ -221,7 +228,7 @@ BOOST_AUTO_TEST_CASE(container) container2.getKeyAndInfo().keyInfo.keyLength); } -BOOST_AUTO_TEST_CASE(moves) +POSITIVE_TEST_CASE(moves) { CKM::KeyProvider provider; diff --git a/tests/test_key.cpp b/tests/test_key.cpp index fa80504..20c3d7b 100644 --- a/tests/test_key.cpp +++ b/tests/test_key.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -16,7 +16,7 @@ #include #include -#include +#include #include namespace { @@ -36,7 +36,7 @@ using namespace CKM; BOOST_AUTO_TEST_SUITE(KEY_TEST) -BOOST_AUTO_TEST_CASE(constructors) +POSITIVE_TEST_CASE(constructors) { RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end()); @@ -45,6 +45,14 @@ BOOST_AUTO_TEST_CASE(constructors) // valid key type case BOOST_REQUIRE(!KeyImpl(key.getEvpShPtr(), KeyType::KEY_RSA_PUBLIC).empty()); +} + +NEGATIVE_TEST_CASE(constructors) +{ + RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end()); + + KeyImpl key(keybuf); + BOOST_REQUIRE(!key.empty()); // invalid key type cases BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), KeyType::KEY_DSA_PUBLIC).empty()); @@ -55,14 +63,15 @@ BOOST_AUTO_TEST_CASE(constructors) BOOST_REQUIRE(KeyImpl(key.getEvpShPtr(), static_cast(999999)).empty()); } -BOOST_AUTO_TEST_CASE(get_size) + +POSITIVE_TEST_CASE(get_size) { RawBuffer keybuf(PUBKEY_PEM.begin(), PUBKEY_PEM.end()); KeyImpl key(keybuf); BOOST_REQUIRE(!key.empty()); - // not ipmlemented yet but test for coverage. It'll just return 0 + // not implemented yet but test for coverage. It'll just return 0 BOOST_REQUIRE_NO_THROW(key.getSize()); } @@ -70,7 +79,7 @@ BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(AES_KEY_TEST) -BOOST_AUTO_TEST_CASE(constructors) +POSITIVE_TEST_CASE(constructors) { // invalid key size RawBuffer keybuf({0x01, 0x02, 0x03, 0x04}); diff --git a/tests/test_log-provider.cpp b/tests/test_log-provider.cpp index 5d13104..5354d81 100644 --- a/tests/test_log-provider.cpp +++ b/tests/test_log-provider.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License */ -#include +#include #include #include @@ -47,28 +47,28 @@ void testProvider(AbstractLogProvider &provider) BOOST_AUTO_TEST_SUITE(LOG_PROVIDER_TEST) -BOOST_AUTO_TEST_CASE(oldstyle_backend) +POSITIVE_TEST_CASE(oldstyle_backend) { OldStyleLogProvider provider; testProvider(provider); } -BOOST_AUTO_TEST_CASE(journal_backend) +POSITIVE_TEST_CASE(journal_backend) { JournalLogProvider provider; testProvider(provider); } -BOOST_AUTO_TEST_CASE(dlog_backend) +POSITIVE_TEST_CASE(dlog_backend) { DLOGLogProvider provider; testProvider(provider); } -BOOST_AUTO_TEST_CASE(log_system) +POSITIVE_TEST_CASE(log_system) { LogSystem system; diff --git a/tests/test_safe-buffer.cpp b/tests/test_safe-buffer.cpp index 47ecbef..ff9dccd 100644 --- a/tests/test_safe-buffer.cpp +++ b/tests/test_safe-buffer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd. All rights reserved * * Contact: Kyungwook Tak * @@ -22,7 +22,7 @@ */ #include -#include +#include #include #include @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_SUITE(SAFE_BUFFER_TEST) // Tests for SafeBuffer. Checks if memory occupied by the buffer is wiped after it's deleted. -BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group) +POSITIVE_TEST_CASE(SafeBufferTest_uc_control_group) { size_t cnt = buffer_erase_test>(); @@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE(SafeBufferTest_uc_control_group) "Less than 1/2 of data matches the original."); } -BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group) +POSITIVE_TEST_CASE(SafeBufferTest_item_control_group) { size_t cnt = buffer_erase_test>(); @@ -99,7 +99,7 @@ BOOST_AUTO_TEST_CASE(SafeBufferTest_item_control_group) "Less than 1/2 of data matches the original."); } -BOOST_AUTO_TEST_CASE(SafeBufferTest_uc) +POSITIVE_TEST_CASE(SafeBufferTest_uc) { size_t cnt = buffer_erase_test(); @@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(SafeBufferTest_uc) "More than 1/10 of data matches the original."); } -BOOST_AUTO_TEST_CASE(SafeBufferTest_item) +POSITIVE_TEST_CASE(SafeBufferTest_item) { size_t cnt = buffer_erase_test::Type>(); diff --git a/tests/test_serialization.cpp b/tests/test_serialization.cpp index 5fff3af..8b7152e 100644 --- a/tests/test_serialization.cpp +++ b/tests/test_serialization.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015 - 2020 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. @@ -21,7 +21,7 @@ #include -#include +#include #include #include @@ -86,7 +86,7 @@ void setParam(CryptoAlgorithm &algo, ParamName name, const T &value, BOOST_AUTO_TEST_SUITE(SERIALIZATION_TEST) -BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) +POSITIVE_TEST_CASE(Serialization_CryptoAlgorithm) { CryptoAlgorithm ca; setParam(ca, ParamName::ALGO_TYPE, static_cast(AlgoType::AES_GCM), @@ -109,6 +109,24 @@ BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) checkBufferParam(output, ParamName::ED_IV, IV); checkIntParam(output, ParamName::ED_TAG_LEN, 128); checkBufferParam(output, ParamName::ED_AAD, AAD); +} + +NEGATIVE_TEST_CASE(Serialization_CryptoAlgorithm) +{ + CryptoAlgorithm ca; + setParam(ca, ParamName::ALGO_TYPE, static_cast(AlgoType::AES_GCM), + true); + setParam(ca, ParamName::ED_IV, IV, true); + setParam(ca, ParamName::ED_TAG_LEN, 128, true); + setParam(ca, ParamName::ED_AAD, AAD, true); + + CryptoAlgorithmSerializable input(ca); + CryptoAlgorithmSerializable output; + auto msg = MessageBuffer::Serialize(input); + RawBuffer buffer = msg.Pop(); + MessageBuffer resp; + resp.Push(buffer); + resp.Deserialize(output); // wrong type checkBufferParamNegative(output, ParamName::ALGO_TYPE); @@ -125,7 +143,7 @@ BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm) checkIntParamNegative(output, static_cast(666)); } -BOOST_AUTO_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) +NEGATIVE_TEST_CASE(Serialization_CryptoAlgorithm_wrong_name) { CryptoAlgorithm ca; // param name out of range diff --git a/tests/test_sql.cpp b/tests/test_sql.cpp index 8fac9c1..a63455a 100644 --- a/tests/test_sql.cpp +++ b/tests/test_sql.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved * * Contact: Kyungwook Tak * @@ -21,7 +21,7 @@ * @brief */ #include -#include +#include #include #include #include @@ -49,7 +49,7 @@ const char *select_table = "SELECT * FROM t1"; CKM::RawBuffer raw_password = createDefaultPass(); BOOST_AUTO_TEST_SUITE(SQL_TEST) -BOOST_AUTO_TEST_CASE(sqlTestConversion) +POSITIVE_TEST_CASE(sqlTestConversion) { BOOST_REQUIRE_MESSAGE(raw_password.size() == RAW_PASS_SIZE, "Password should have 32 characters, got: " << raw_password.size()); @@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE(sqlTestConversion) BOOST_CHECK(pass_check == pattern); } -BOOST_AUTO_TEST_CASE(sqlTestConversionBig) +POSITIVE_TEST_CASE(sqlTestConversionBig) { /* 192 ~ 208 in hex */ const std::string tmppattern = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0"; @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(sqlTestConversionBig) BOOST_CHECK(pass_hex == tmppattern); } -BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooShort) +NEGATIVE_TEST_CASE(sqlTestSetKeyTooShort) { using namespace CKM::DB; BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT); @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooShort) SqlConnection::Exception::InvalidArguments); } -BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooLong) +NEGATIVE_TEST_CASE(sqlTestSetKeyTooLong) { using namespace CKM::DB; BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT); @@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(sqlTestSetKeyTooLong) SqlConnection::Exception::InvalidArguments); } -BOOST_AUTO_TEST_CASE(sqlTestConnectionUnencrypted) +POSITIVE_TEST_CASE(sqlTestConnectionUnencrypted) { using namespace CKM::DB; BOOST_CHECK(unlink(encrypt_me_not) == 0 || errno == ENOENT); @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(sqlTestConnectionUnencrypted) } } -BOOST_AUTO_TEST_CASE(sqlTestConnectionEncrypted) +POSITIVE_TEST_CASE(sqlTestConnectionEncrypted) { using namespace CKM::DB; BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT); @@ -147,7 +147,7 @@ BOOST_AUTO_TEST_CASE(sqlTestConnectionEncrypted) } } -BOOST_AUTO_TEST_CASE(sqlTestConnectionEncryptedNegative) +NEGATIVE_TEST_CASE(sqlTestConnectionEncryptedNegative) { using namespace CKM::DB; BOOST_CHECK(unlink(encrypt_me) == 0 || errno == ENOENT); diff --git a/tests/test_ss-crypto.cpp b/tests/test_ss-crypto.cpp index c009207..fa2b848 100644 --- a/tests/test_ss-crypto.cpp +++ b/tests/test_ss-crypto.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -15,7 +15,7 @@ */ #include -#include +#include #include #include @@ -46,7 +46,7 @@ RawBuffer readFile(const std::string &path) BOOST_AUTO_TEST_SUITE(SS_CRYPTO_TEST) -BOOST_AUTO_TEST_CASE(decrypt) +POSITIVE_TEST_CASE(decrypt) { const std::string seed = "secure-storage::test1"; const std::string path = std::string(SS_TEST_DIR) + "/" + seed + "/test-data-1"; diff --git a/tests/test_stringify.cpp b/tests/test_stringify.cpp index f4c5c7a..0fd3727 100644 --- a/tests/test_stringify.cpp +++ b/tests/test_stringify.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -15,26 +15,26 @@ */ #include -#include +#include #include BOOST_AUTO_TEST_SUITE(STRINGIFY_TEST) -BOOST_AUTO_TEST_CASE(stringify_default) +POSITIVE_TEST_CASE(stringify_default) { BOOST_REQUIRE(Stringify("a", "b", "c") == "abc"); BOOST_REQUIRE(Stringify(std::string("a"), "b", "c") == "abc"); BOOST_REQUIRE(Stringify().empty()); } -BOOST_AUTO_TEST_CASE(stringify_avoid) +POSITIVE_TEST_CASE(stringify_avoid) { BOOST_REQUIRE(StringifyAvoid("a", "b", "c").empty()); BOOST_REQUIRE(StringifyAvoid(std::string("a"), "b", "c").empty()); BOOST_REQUIRE(StringifyAvoid().empty()); } -BOOST_AUTO_TEST_CASE(stringify_error) +POSITIVE_TEST_CASE(stringify_error) { BOOST_REQUIRE(StringifyError("a", "b", "c") == "abc"); BOOST_REQUIRE(StringifyError(std::string("a"), "b", "c") == "abc"); diff --git a/tests/test_sw-backend.cpp b/tests/test_sw-backend.cpp index 603a1bf..2de262c 100644 --- a/tests/test_sw-backend.cpp +++ b/tests/test_sw-backend.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2017 - 2020 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. @@ -15,7 +15,7 @@ */ #include -#include +#include #include "test_common.h" @@ -26,17 +26,20 @@ BOOST_AUTO_TEST_SUITE(SW_BACKEND_TEST) BOOST_AUTO_TEST_SUITE(CRYPTO) -BOOST_AUTO_TEST_CASE(constructs) +POSITIVE_TEST_CASE(constructs) { BOOST_REQUIRE_NO_THROW(AesCbcEncryption128(createRandom(16), createRandom(16))); +} +NEGATIVE_TEST_CASE(constructs) +{ BOOST_REQUIRE_THROW(AesCbcEncryption128(createRandom(16), createRandom(5)), Exc::Crypto::InternalError); BOOST_REQUIRE_THROW(AesCbcEncryption128(createRandom(17), createRandom(16)), Exc::Crypto::InternalError); } -BOOST_AUTO_TEST_CASE(encryption_cbc) +POSITIVE_TEST_CASE(encryption_cbc) { AesCbcEncryption128 cipher(createRandom(16), createRandom(16)); @@ -44,7 +47,7 @@ BOOST_AUTO_TEST_CASE(encryption_cbc) BOOST_REQUIRE_NO_THROW(cipher.Finalize()); } -BOOST_AUTO_TEST_CASE(encryption_gcm) +POSITIVE_TEST_CASE(encryption_gcm) { AesGcmEncryption128 cipher(createRandom(16), createRandom(16)); diff --git a/tests/test_xml-parser.cpp b/tests/test_xml-parser.cpp index 5029306..e601550 100644 --- a/tests/test_xml-parser.cpp +++ b/tests/test_xml-parser.cpp @@ -1,5 +1,5 @@ -/* - * Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved + /* + * Copyright (c) 2015-2020 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. @@ -22,7 +22,7 @@ #include #include -#include +#include #include using namespace CKM; @@ -60,7 +60,7 @@ void dummyEndCallback(const XML::Parser::ElementHandlerPtr &) BOOST_AUTO_TEST_SUITE(XML_PARSER_TEST) -BOOST_AUTO_TEST_CASE(XmlParserTest_wrong_argument) +NEGATIVE_TEST_CASE(XmlParserTest_wrong_argument) { std::string emptyPath; XML::Parser parser(emptyPath); @@ -75,34 +75,34 @@ BOOST_AUTO_TEST_CASE(XmlParserTest_wrong_argument) BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_PARSE_FAILED == parser.Parse()); } -BOOST_AUTO_TEST_CASE(XmlParserTest_no_XML_file) +NEGATIVE_TEST_CASE(XmlParserTest_no_XML_file) { XML::Parser parser(format_test_path("i-am-not-here").c_str()); BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_VALIDATION_FAILED == parser.Validate( format_test_path(XSD_1_okay).c_str())); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_verify) +POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_verify) { XML::Parser parser(format_test_path(XML_1_okay).c_str()); BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str())); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_verify) +NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_verify) { XML::Parser parser(format_test_path(XML_1_wrong).c_str()); BOOST_REQUIRE(Parser::ErrorCode::ERROR_XML_VALIDATION_FAILED == parser.Validate( format_test_path(XSD_1_okay).c_str())); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_wrong_schema) +NEGATIVE_TEST_CASE(XmlParserTest_XML1_wrong_schema) { XML::Parser parser(format_test_path(XML_1_okay).c_str()); BOOST_REQUIRE(Parser::ErrorCode::ERROR_XSD_PARSE_FAILED == parser.Validate( format_test_path(XSD_1_wrong).c_str())); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks) +NEGATIVE_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks) { XML::Parser parser(format_test_path(XML_1_okay).c_str()); BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str())); @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse_incorrect_callbacks) BOOST_REQUIRE(Parser::ErrorCode::PARSE_SUCCESS == parser.Parse()); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML1_correct_parse) +POSITIVE_TEST_CASE(XmlParserTest_XML1_correct_parse) { XML::Parser parser(format_test_path(XML_1_okay).c_str()); BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_1_okay).c_str())); @@ -291,7 +291,7 @@ private: int m_expectedSum; }; -BOOST_AUTO_TEST_CASE(XmlParserTest_XML2_structure) +POSITIVE_TEST_CASE(XmlParserTest_XML2_structure) { StructureTest parser(format_test_path(XML_2_structure).c_str()); BOOST_REQUIRE(0 == parser.Parse()); @@ -300,7 +300,7 @@ BOOST_AUTO_TEST_CASE(XmlParserTest_XML2_structure) parser.getExpectedSum()); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse) +POSITIVE_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse) { XML::Parser parser(format_test_path(XML_3_encrypted).c_str()); BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_3_encrypted).c_str())); @@ -316,7 +316,7 @@ BOOST_AUTO_TEST_CASE(XmlParserTest_XML3_encrypted_correct_parse) BOOST_REQUIRE(endCallbackFlag == true); } -BOOST_AUTO_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse) +POSITIVE_TEST_CASE(XmlParserTest_XML4_device_key_correct_parse) { XML::Parser parser(format_test_path(XML_4_device_key).c_str()); BOOST_REQUIRE(0 == parser.Validate(format_test_path(XSD_4_device_key).c_str())); -- 2.7.4 From 18351c6c9322d9b36e9a9aecfa587c5ec5cc2794 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Mon, 23 Mar 2020 20:44:41 +0100 Subject: [PATCH 13/16] Implement negative cert tests - 50% negative tests for CertificateImpl - Positive OCSP test updated - Minor changes in CertificateImpl Change-Id: I6bdb9e6140694357cba93b8efe26f622744ce927 --- src/manager/common/certificate-impl.cpp | 5 +- src/manager/common/certificate-impl.h | 4 +- tests/test_certificate.cpp | 132 +++++++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/src/manager/common/certificate-impl.cpp b/src/manager/common/certificate-impl.cpp index ea652ba..6a5f5e8 100644 --- a/src/manager/common/certificate-impl.cpp +++ b/src/manager/common/certificate-impl.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved +/* Copyright (c) 2000 - 2020 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. @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -68,6 +69,8 @@ CertificateImpl::CertificateImpl(const RawBuffer &der, DataFormat format) CertificateImpl::CertificateImpl(X509 *x509, bool duplicate) { + Assert(x509); + if (duplicate) m_x509 = X509_dup(x509); else diff --git a/src/manager/common/certificate-impl.h b/src/manager/common/certificate-impl.h index f9d544a..9c0faf0 100644 --- a/src/manager/common/certificate-impl.h +++ b/src/manager/common/certificate-impl.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved +/* Copyright (c) 2000 - 2020 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. @@ -33,7 +33,7 @@ namespace CKM { class COMMON_API CertificateImpl : public Certificate { public: - CertificateImpl() : m_x509(nullptr) {} + CertificateImpl() = delete; explicit CertificateImpl(X509 *x509, bool duplicate = true); CertificateImpl(const RawBuffer &data, DataFormat format); diff --git a/tests/test_certificate.cpp b/tests/test_certificate.cpp index 3e5f03e..0fec3ce 100644 --- a/tests/test_certificate.cpp +++ b/tests/test_certificate.cpp @@ -76,6 +76,85 @@ const std::string CERT_PUBKEY_PEM = "-----END PUBLIC KEY-----\n"; } +const std::string CERT_OCSP_PEM = + "-----BEGIN CERTIFICATE-----\n" + "MIIN+jCCC+KgAwIBAgITIAAGDxuOsc5CY0aDQgAAAAYPGzANBgkqhkiG9w0BAQsF\n" + "ADCBizELMAkGA1UEBhMCVVMxEzARBgNVBAgTCldhc2hpbmd0b24xEDAOBgNVBAcT\n" + "B1JlZG1vbmQxHjAcBgNVBAoTFU1pY3Jvc29mdCBDb3Jwb3JhdGlvbjEVMBMGA1UE\n" + "CxMMTWljcm9zb2Z0IElUMR4wHAYDVQQDExVNaWNyb3NvZnQgSVQgVExTIENBIDIw\n" + "HhcNMTkwNDMwMjA0ODAwWhcNMjEwNDMwMjA0ODAwWjAXMRUwEwYDVQQDEwx3d3cu\n" + "YmluZy5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDhIlPb0iP7\n" + "xRmUScK43QI7Ci/lvfMumWhRFAHcFzjIDHs74sq0B+ze8HW5PR6LWRe/d3yR5dC8\n" + "7gQs0qXGitzsP9vWJcpwKV273tlnWiEfgZx5tvNCFdHOqoYoHL3a8zed/JkGTEeX\n" + "ukGEX0TeBgCjcVTj5qRxJhjlWxs3AcB/q4f4vi3QG80TbSU2UO0lkvhvfs73C1jq\n" + "i7Zspia/YsMqcQ6X+APAZ+4guKjQr5q32tzj2FGtJO6ZmZuNV9Wwb32891UhwZ3D\n" + "2PrIcnCNlIQ1/Fah6im7Vc67qO2x/++r7gO7PtR8byCFnFuNUVQxhSIkCkj6FvPx\n" + "cYefok0wJ0VRAgMBAAGjggnIMIIJxDCCAfQGCisGAQQB1nkCBAIEggHkBIIB4AHe\n" + "AHUA7ku9t3XOYLrhQmkfq+GeZqMPfl+wctiDAMR7iXqo/csAAAFqcApGzAAABAMA\n" + "RjBEAiAiASIs5j19VcTLbxcOGHQlIl62d3iy1FY8dnNq+6lebQIgchbSq2Qh78zs\n" + "mmucyslucBycij/FYUe3F1lNpJiB9KsAdgBVgdTCFpA2AUrqC5tXPFPwwOQ4eHAl\n" + "CBcvo6odBxPTDAAAAWpwCkgDAAAEAwBHMEUCIQDx6RqcvDdfIY9qdAuaRFBVvSHN\n" + "ttpAzie3KP9AAiGvBwIgSMvjse/hJusDoRFnSTtX96ierTaqzQH4oDLLnW/Gwc0A\n" + "dQBc3EOS/uarRUSxXprUVuYQN/vV+kfcoXOUsl7m9scOygAAAWpwCkbmAAAEAwBG\n" + "MEQCICbYF6Lv93BFrwLguzmas/5gQ87fzRHkTaMxDSD7PlhRAiA/DXOeTcHaiUPQ\n" + "WsKbJ/7x9EWKvVisqtQMnYk6cBxbBwB2AESUZS6w7s6vxEAH2Kj+KMDa5oK+2Msx\n" + "tT/TM5a1toGoAAABanAKRtoAAAQDAEcwRQIgardRfR7bxwSGF212a603dXYz6O5z\n" + "YHpPks8/RR/AMzQCIQD4VYDD+2zVDHEjz8elkKEzhgzTdOMtc1yYhCU+eHAGkjAn\n" + "BgkrBgEEAYI3FQoEGjAYMAoGCCsGAQUFBwMCMAoGCCsGAQUFBwMBMD4GCSsGAQQB\n" + "gjcVBwQxMC8GJysGAQQBgjcVCIfahnWD7tkBgsmFG4G1nmGF9OtggV2E0t9CgueT\n" + "egIBZAIBHTCBhQYIKwYBBQUHAQEEeTB3MFEGCCsGAQUFBzAChkVodHRwOi8vd3d3\n" + "Lm1pY3Jvc29mdC5jb20vcGtpL21zY29ycC9NaWNyb3NvZnQlMjBJVCUyMFRMUyUy\n" + "MENBJTIwMi5jcnQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLm1zb2NzcC5jb20w\n" + "HQYDVR0OBBYEFDcHtZt8HkKSRO8ETiTkCLixB9PaMAsGA1UdDwQEAwIEsDCCBW0G\n" + "A1UdEQSCBWQwggVgggx3d3cuYmluZy5jb22CEGRpY3QuYmluZy5jb20uY26CEyou\n" + "cGxhdGZvcm0uYmluZy5jb22CCiouYmluZy5jb22CCGJpbmcuY29tghZpZW9ubGlu\n" + "ZS5taWNyb3NvZnQuY29tghMqLndpbmRvd3NzZWFyY2guY29tghljbi5pZW9ubGlu\n" + "ZS5taWNyb3NvZnQuY29tghEqLm9yaWdpbi5iaW5nLmNvbYINKi5tbS5iaW5nLm5l\n" + "dIIOKi5hcGkuYmluZy5jb22CGGVjbi5kZXYudmlydHVhbGVhcnRoLm5ldIINKi5j\n" + "bi5iaW5nLm5ldIINKi5jbi5iaW5nLmNvbYIQc3NsLWFwaS5iaW5nLmNvbYIQc3Ns\n" + "LWFwaS5iaW5nLm5ldIIOKi5hcGkuYmluZy5uZXSCDiouYmluZ2FwaXMuY29tgg9i\n" + "aW5nc2FuZGJveC5jb22CFmZlZWRiYWNrLm1pY3Jvc29mdC5jb22CG2luc2VydG1l\n" + "ZGlhLmJpbmcub2ZmaWNlLm5ldIIOci5iYXQuYmluZy5jb22CECouci5iYXQuYmlu\n" + "Zy5jb22CEiouZGljdC5iaW5nLmNvbS5jboIPKi5kaWN0LmJpbmcuY29tgg4qLnNz\n" + "bC5iaW5nLmNvbYIQKi5hcHBleC5iaW5nLmNvbYIWKi5wbGF0Zm9ybS5jbi5iaW5n\n" + "LmNvbYINd3AubS5iaW5nLmNvbYIMKi5tLmJpbmcuY29tgg9nbG9iYWwuYmluZy5j\n" + "b22CEXdpbmRvd3NzZWFyY2guY29tgg5zZWFyY2gubXNuLmNvbYIRKi5iaW5nc2Fu\n" + "ZGJveC5jb22CGSouYXBpLnRpbGVzLmRpdHUubGl2ZS5jb22CDyouZGl0dS5saXZl\n" + "LmNvbYIYKi50MC50aWxlcy5kaXR1LmxpdmUuY29tghgqLnQxLnRpbGVzLmRpdHUu\n" + "bGl2ZS5jb22CGCoudDIudGlsZXMuZGl0dS5saXZlLmNvbYIYKi50My50aWxlcy5k\n" + "aXR1LmxpdmUuY29tghUqLnRpbGVzLmRpdHUubGl2ZS5jb22CCzNkLmxpdmUuY29t\n" + "ghNhcGkuc2VhcmNoLmxpdmUuY29tghRiZXRhLnNlYXJjaC5saXZlLmNvbYIVY253\n" + "ZWIuc2VhcmNoLmxpdmUuY29tggxkZXYubGl2ZS5jb22CDWRpdHUubGl2ZS5jb22C\n" + "EWZhcmVjYXN0LmxpdmUuY29tgg5pbWFnZS5saXZlLmNvbYIPaW1hZ2VzLmxpdmUu\n" + "Y29tghFsb2NhbC5saXZlLmNvbS5hdYIUbG9jYWxzZWFyY2gubGl2ZS5jb22CFGxz\n" + "NGQuc2VhcmNoLmxpdmUuY29tgg1tYWlsLmxpdmUuY29tghFtYXBpbmRpYS5saXZl\n" + "LmNvbYIObG9jYWwubGl2ZS5jb22CDW1hcHMubGl2ZS5jb22CEG1hcHMubGl2ZS5j\n" + "b20uYXWCD21pbmRpYS5saXZlLmNvbYINbmV3cy5saXZlLmNvbYIcb3JpZ2luLmNu\n" + "d2ViLnNlYXJjaC5saXZlLmNvbYIWcHJldmlldy5sb2NhbC5saXZlLmNvbYIPc2Vh\n" + "cmNoLmxpdmUuY29tghJ0ZXN0Lm1hcHMubGl2ZS5jb22CDnZpZGVvLmxpdmUuY29t\n" + "gg92aWRlb3MubGl2ZS5jb22CFXZpcnR1YWxlYXJ0aC5saXZlLmNvbYIMd2FwLmxp\n" + "dmUuY29tghJ3ZWJtYXN0ZXIubGl2ZS5jb22CE3dlYm1hc3RlcnMubGl2ZS5jb22C\n" + "FXd3dy5sb2NhbC5saXZlLmNvbS5hdYIUd3d3Lm1hcHMubGl2ZS5jb20uYXUwgawG\n" + "A1UdHwSBpDCBoTCBnqCBm6CBmIZLaHR0cDovL21zY3JsLm1pY3Jvc29mdC5jb20v\n" + "cGtpL21zY29ycC9jcmwvTWljcm9zb2Z0JTIwSVQlMjBUTFMlMjBDQSUyMDIuY3Js\n" + "hklodHRwOi8vY3JsLm1pY3Jvc29mdC5jb20vcGtpL21zY29ycC9jcmwvTWljcm9z\n" + "b2Z0JTIwSVQlMjBUTFMlMjBDQSUyMDIuY3JsME0GA1UdIARGMEQwQgYJKwYBBAGC\n" + "NyoBMDUwMwYIKwYBBQUHAgEWJ2h0dHA6Ly93d3cubWljcm9zb2Z0LmNvbS9wa2kv\n" + "bXNjb3JwL2NwczAfBgNVHSMEGDAWgBSRnjtEbD1XnEJ3KjTXT9HMSpcs2jAdBgNV\n" + "HSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEwDQYJKoZIhvcNAQELBQADggIBAA1g\n" + "NJF5ks5Qrg0/qeOXQbcO3SCs+HKTKxVL8QdaTL3s5gsmWQzcYNS671DmN4lEob2g\n" + "WWZKyKAzQbjDOcf9ndxX4+i+PaCw5K3uONbMOwnuOCwRvDy8YEoCb3OzKFX4sjzh\n" + "1HVL/ljKHUPT+9ap/SpYserNxixibqF2LZYx+9hwr1bcx9GWrg3CoFUFgSZqRQ14\n" + "eiK94iM5kzJLKynKPhez+UOwS5VRev1mxh5nD9hBPzXHHqI9mNWu/lyr7KPUMigi\n" + "QfKKZuqV6W3i1H3BoJi1uDkL3SJo1F39XN3AyGSAZWS9RNn5JzEQQGiJRjrz/PE1\n" + "vTg1BlbsPdKa4gGZGdGBWcj2eXZc+GbLpTy3qWlmJrEn2KGLeomyndlftRPFrBUH\n" + "/5Mio5OeSawjlacBV25fKaoZ1BPc3i+HGKd5ctddCy6kJsgdMD221zGvf/0uW25Z\n" + "ImzDeH7KkOcGbzyWJwBzDgra0RP+qRgK3aYPSWI81OLlnHJ2VOix/UU63NCK2fO/\n" + "URzE8KxoHrgRGXCE52viHv6ksL7QXWelbERU7GEpcZU1suPhDohn4CrfrCYCjpa5\n" + "Ys6ci7Rren82SsXJBfNrgm2U4lxWfzWj+2Ay6yATbdoOPntue8cbbMoTzoNMHQXD\n" + "2DpjtFPs8/RVOFQb0IFVluCrTAnHmI8tTtsmzg6z\n" + "-----END CERTIFICATE-----\n"; + using namespace CKM; BOOST_AUTO_TEST_SUITE(CERTIFICATE_TEST) @@ -87,9 +166,15 @@ POSITIVE_TEST_CASE(constructors) RawBuffer certbase64(CERT_BASE64.begin(), CERT_BASE64.end()); BOOST_REQUIRE_NO_THROW(CertificateImpl(certbase64, DataFormat::FORM_DER_BASE64)); +} +NEGATIVE_TEST_CASE(constructors) +{ RawBuffer dummy({0x0a, 0x0b, 0x0c, 0x0d}); BOOST_REQUIRE(!Certificate::create(dummy, DataFormat::FORM_PEM)); + + RawBuffer certpem(CERT_PEM.begin(), CERT_PEM.end()); + BOOST_REQUIRE(!Certificate::create(certpem, DataFormat::FORM_DER)); } POSITIVE_TEST_CASE(move_semantics) @@ -107,6 +192,16 @@ POSITIVE_TEST_CASE(move_semantics) BOOST_REQUIRE(cert1.getDER() == moveConstructed.getDER()); } +NEGATIVE_TEST_CASE(get_x509) +{ + RawBuffer empty; + + CertificateImpl cert(empty, DataFormat::FORM_PEM); + + BOOST_REQUIRE(cert.empty()); + BOOST_REQUIRE(cert.getX509() == nullptr); +} + POSITIVE_TEST_CASE(get_evp_sh_ptr) { RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); @@ -120,12 +215,47 @@ POSITIVE_TEST_CASE(get_evp_sh_ptr) BOOST_REQUIRE(pubkeyFromCert.getDER() == pubkey.getDER()); } +NEGATIVE_TEST_CASE(get_evp_sh_ptr) +{ + RawBuffer empty; + + CertificateImpl cert(empty, DataFormat::FORM_PEM); + + BOOST_REQUIRE(cert.empty()); + BOOST_REQUIRE(!cert.getEvpShPtr()); +} + +NEGATIVE_TEST_CASE(get_der) +{ + RawBuffer empty; + + CertificateImpl cert(empty, DataFormat::FORM_PEM); + + BOOST_REQUIRE(cert.empty()); + BOOST_REQUIRE(cert.getDER().empty()); +} + POSITIVE_TEST_CASE(get_ocsp_url) { - RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); + RawBuffer certbuf(CERT_OCSP_PEM.begin(), CERT_OCSP_PEM.end()); CertificateImpl cert(certbuf, DataFormat::FORM_PEM); + BOOST_REQUIRE(!cert.getOCSPURL().empty()); +} + +NEGATIVE_TEST_CASE(get_ocsp_url) +{ + RawBuffer empty; + CertificateImpl cert(empty, DataFormat::FORM_PEM); + + BOOST_REQUIRE(cert.empty()); BOOST_REQUIRE(cert.getOCSPURL().empty()); + + RawBuffer certbuf(CERT_PEM.begin(), CERT_PEM.end()); + CertificateImpl cert2(certbuf, DataFormat::FORM_PEM); + + BOOST_REQUIRE(!cert2.empty()); + BOOST_REQUIRE(cert2.getOCSPURL().empty()); } BOOST_AUTO_TEST_SUITE_END() -- 2.7.4 From 22d7ce9b325028cc6d54450753157029a7cd40a3 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Tue, 24 Mar 2020 17:32:26 +0100 Subject: [PATCH 14/16] Replace dpl asserts with libc ones - Libc asserts were already used in few places. Now it's unified. - Libc asserts are disabled in release builds unlike dpl ones. - Code coverage is improved. Change-Id: Ie241b997433b2286d1b6c3f5e24571af5bf5809f --- CMakeLists.txt | 2 + src/CMakeLists.txt | 1 - src/manager/CMakeLists.txt | 1 - src/manager/common/certificate-impl.cpp | 5 +- src/manager/crypto/sw-backend/internals.cpp | 3 +- src/manager/dpl/core/include/dpl/assert.h | 53 --------------------- src/manager/dpl/core/include/dpl/noreturn.h | 27 ----------- src/manager/dpl/core/src/assert.cpp | 54 ---------------------- src/manager/dpl/core/src/binary_queue.cpp | 10 ++-- src/manager/dpl/db/include/dpl/db/sql_connection.h | 3 +- .../dpl/db/src/naive_synchronization_object.cpp | 7 ++- src/manager/dpl/db/src/sql_connection.cpp | 17 ++++--- src/manager/dpl/log/src/log.cpp | 4 +- src/manager/main/socket-manager.cpp | 6 +-- src/manager/service/glib-service.cpp | 3 +- tests/CMakeLists.txt | 2 - tests/test_sql.cpp | 1 - tools/ckm_db_tool/CMakeLists.txt | 1 - 18 files changed, 29 insertions(+), 171 deletions(-) delete mode 100644 src/manager/dpl/core/include/dpl/assert.h delete mode 100644 src/manager/dpl/core/include/dpl/noreturn.h delete mode 100644 src/manager/dpl/core/src/assert.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cfa5cc2..9787428 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,8 @@ ENDIF (DEFINED WATCHDOG_ENABLED) IF (CMAKE_BUILD_TYPE MATCHES "DEBUG") ADD_DEFINITIONS("-DTIZEN_DEBUG_ENABLE") ADD_DEFINITIONS("-DBUILD_TYPE_DEBUG") +ELSE (CMAKE_BUILD_TYPE MATCHES "DEBUG") + ADD_DEFINITIONS("-DNDEBUG=1") ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG") SET(TARGET_KEY_MANAGER "key-manager") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d6fed0d..f69ab36 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,7 +73,6 @@ SET(KEY_MANAGER_SOURCES ${KEY_MANAGER_PATH}/initial-values/NoCharactersHandler.cpp ${KEY_MANAGER_PATH}/initial-values/xml-utils.cpp ${KEY_MANAGER_PATH}/initial-values/initial-value-loader.cpp - ${KEY_MANAGER_PATH}/dpl/core/src/assert.cpp ${KEY_MANAGER_PATH}/dpl/db/src/sql_connection.cpp ${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp ${KEY_MANAGER_PATH}/sqlcipher/sqlcipher.c diff --git a/src/manager/CMakeLists.txt b/src/manager/CMakeLists.txt index c22c6ca..ee0ce5e 100644 --- a/src/manager/CMakeLists.txt +++ b/src/manager/CMakeLists.txt @@ -32,7 +32,6 @@ SET(COMMON_SOURCES ${COMMON_PATH}/dpl/log/src/log.cpp ${COMMON_PATH}/dpl/log/src/old_style_log_provider.cpp ${COMMON_PATH}/dpl/log/src/journal_log_provider.cpp - ${COMMON_PATH}/dpl/core/src/assert.cpp ${COMMON_PATH}/dpl/core/src/binary_queue.cpp ${COMMON_PATH}/dpl/core/src/colors.cpp ${COMMON_PATH}/dpl/core/src/exception.cpp diff --git a/src/manager/common/certificate-impl.cpp b/src/manager/common/certificate-impl.cpp index 6a5f5e8..f81d44a 100644 --- a/src/manager/common/certificate-impl.cpp +++ b/src/manager/common/certificate-impl.cpp @@ -18,12 +18,13 @@ * @version 1.0 * @brief Certificate implementation. */ +#include + #include #include #include #include -#include #include #include @@ -69,7 +70,7 @@ CertificateImpl::CertificateImpl(const RawBuffer &der, DataFormat format) CertificateImpl::CertificateImpl(X509 *x509, bool duplicate) { - Assert(x509); + assert(x509); if (duplicate) m_x509 = X509_dup(x509); diff --git a/src/manager/crypto/sw-backend/internals.cpp b/src/manager/crypto/sw-backend/internals.cpp index a0f0239..a2d1ee8 100644 --- a/src/manager/crypto/sw-backend/internals.cpp +++ b/src/manager/crypto/sw-backend/internals.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015 - 2020 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. @@ -35,7 +35,6 @@ #include #include -#include #include #include diff --git a/src/manager/dpl/core/include/dpl/assert.h b/src/manager/dpl/core/include/dpl/assert.h deleted file mode 100644 index ee865d1..0000000 --- a/src/manager/dpl/core/include/dpl/assert.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2014 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 assert.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of assert - */ -#ifndef CENT_KEY_ASSERT_H -#define CENT_KEY_ASSERT_H - -#include -#include - -namespace CKM { -// Assertion handler procedure -// Do not call directly -// Always use Assert macro -CENT_KEY_NORETURN void AssertProc(const char *condition, - const char *file, - int line, - const char *function); -} // namespace CKM - -#define Assert(Condition) do { if (!(Condition)) { CKM::AssertProc(#Condition, \ - __FILE__, \ - __LINE__, \ - __FUNCTION__); \ - } } while (0) - -#define AssertMsg(Condition, Msg) \ - do { \ - if (!(Condition)) { \ - CKM::AssertProc( \ - (std::string(std::string(#Condition)+" ") + Msg).c_str(), \ - __FILE__, __LINE__, __FUNCTION__); \ - } \ - } while (0) - -#endif // CENT_KEY_ASSERT_H diff --git a/src/manager/dpl/core/include/dpl/noreturn.h b/src/manager/dpl/core/include/dpl/noreturn.h deleted file mode 100644 index b9b85e0..0000000 --- a/src/manager/dpl/core/include/dpl/noreturn.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2011 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 noreturn.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of noreturn - */ -#ifndef CENT_KEY_NORETURN_H -#define CENT_KEY_NORETURN_H - -#define CENT_KEY_NORETURN __attribute__((__noreturn__)) - -#endif // CENT_KEY_NORETURN_H diff --git a/src/manager/dpl/core/src/assert.cpp b/src/manager/dpl/core/src/assert.cpp deleted file mode 100644 index 9fb4fa6..0000000 --- a/src/manager/dpl/core/src/assert.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2011 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 assert.cpp - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of assert - */ -#include "dpl/assert.h" - -#include -#include -#include - -namespace CKM { -void AssertProc(const char *condition, - const char *file, - int line, - const char *function) -{ - try { - LogError( - "################################################################################" - << std::endl << - "### CKM assertion failed! ###" - << std::endl << - "################################################################################" - << std::endl << - "### Condition: " << condition << std::endl << - "### File: " << file << std::endl << - "### Line: " << line << std::endl << - "### Function: " << function << - "################################################################################"); - } catch (...) { - // Just ignore possible double errors - } - - // Fail with c-library abort - abort(); -} -} // namespace CKM diff --git a/src/manager/dpl/core/src/binary_queue.cpp b/src/manager/dpl/core/src/binary_queue.cpp index d30e18b..97f19e1 100644 --- a/src/manager/dpl/core/src/binary_queue.cpp +++ b/src/manager/dpl/core/src/binary_queue.cpp @@ -21,7 +21,7 @@ */ #include #include -#include +#include #include #include #include @@ -197,7 +197,7 @@ void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const size_t bytesLeft = bufferSize; void *ptr = buffer; BucketList::const_iterator bucketIterator = m_buckets.begin(); - Assert(m_buckets.end() != bucketIterator); + assert(m_buckets.end() != bucketIterator); // Flatten data while (bytesLeft > 0) { @@ -250,8 +250,8 @@ BinaryQueue::Bucket::Bucket(const void *data, deleter(dataDeleter), param(userParam) { - Assert(data != NULL); - Assert(deleter != NULL); + assert(data != NULL); + assert(deleter != NULL); } BinaryQueue::Bucket::~Bucket() @@ -280,7 +280,7 @@ void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const void BinaryQueue::VisitBuckets(BucketVisitor *visitor) const { - Assert(visitor != NULL); + assert(visitor != NULL); // Visit all buckets std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor)); diff --git a/src/manager/dpl/db/include/dpl/db/sql_connection.h b/src/manager/dpl/db/include/dpl/db/sql_connection.h index 466ffa0..b0825bc 100644 --- a/src/manager/dpl/db/include/dpl/db/sql_connection.h +++ b/src/manager/dpl/db/include/dpl/db/sql_connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014 - 2020 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. @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include diff --git a/src/manager/dpl/db/src/naive_synchronization_object.cpp b/src/manager/dpl/db/src/naive_synchronization_object.cpp index 809b9e4..1c92e0b 100644 --- a/src/manager/dpl/db/src/naive_synchronization_object.cpp +++ b/src/manager/dpl/db/src/naive_synchronization_object.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014 - 2020 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. @@ -22,8 +22,8 @@ */ #include #include -#include #include +#include namespace { unsigned int seed = time(NULL); @@ -56,8 +56,7 @@ void NanoSleep(uint64_t nanoseconds) if (nanosleep(&requestedTime, &remainingTime) == 0) break; - int error = errno; - Assert(error == EINTR); + assert(errno == EINTR); requestedTime = remainingTime; } diff --git a/src/manager/dpl/db/src/sql_connection.cpp b/src/manager/dpl/db/src/sql_connection.cpp index 15e3cc8..a9ee1b5 100644 --- a/src/manager/dpl/db/src/sql_connection.cpp +++ b/src/manager/dpl/db/src/sql_connection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2014-2020 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. @@ -25,9 +25,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -70,7 +70,7 @@ SqlConnection::DataCommand::DataCommand(SqlConnection *connection, m_masterConnection(connection), m_stmt(NULL) { - Assert(connection != NULL); + assert(connection != NULL); // Notify all after potentially synchronized database connection access ScopedNotifyAll notifyAll(connection->m_synchronizationObject.get()); @@ -735,9 +735,10 @@ void SqlConnection::ResetKey(const RawBuffer &rawPassOld, return; } - AssertMsg(rawPassOld.size() == SQLCIPHER_RAW_DATA_SIZE && - rawPassNew.size() == SQLCIPHER_RAW_DATA_SIZE, - "Binary data for raw password should be 32 bytes long."); + // Binary data for raw password should be 32 bytes long. + assert(rawPassOld.size() == SQLCIPHER_RAW_DATA_SIZE && + rawPassNew.size() == SQLCIPHER_RAW_DATA_SIZE); + // sqlcipher3_rekey requires for key to be already set if (!m_isKeySet) @@ -767,9 +768,7 @@ void SqlConnection::Disconnect() LogPedantic("Disconnecting from DB..."); // All stored data commands must be deleted before disconnect - AssertMsg(m_dataCommandsCount == 0, - "All stored procedures must be deleted" - " before disconnecting SqlConnection"); + assert(m_dataCommandsCount == 0); int result; diff --git a/src/manager/dpl/log/src/log.cpp b/src/manager/dpl/log/src/log.cpp index 37baa3a..8d86312 100644 --- a/src/manager/dpl/log/src/log.cpp +++ b/src/manager/dpl/log/src/log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2011 - 2020 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. @@ -24,8 +24,6 @@ #include -#include - #include #include #include diff --git a/src/manager/main/socket-manager.cpp b/src/manager/main/socket-manager.cpp index 07ce31c..55150fe 100644 --- a/src/manager/main/socket-manager.cpp +++ b/src/manager/main/socket-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014 - 2020 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. @@ -31,12 +31,12 @@ #include #include #include +#include #include #include #include -#include #include #include @@ -456,7 +456,7 @@ void SocketManager::MainLoop() int ret = select(m_maxDesc + 1, &readSet, &writeSet, NULL, ptrTimeout); if (0 == ret) { // timeout - Assert(!m_timeoutQueue.empty()); + assert(!m_timeoutQueue.empty()); Timeout pqTimeout = m_timeoutQueue.top(); m_timeoutQueue.pop(); diff --git a/src/manager/service/glib-service.cpp b/src/manager/service/glib-service.cpp index 3568384..d24833c 100644 --- a/src/manager/service/glib-service.cpp +++ b/src/manager/service/glib-service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016 - 2020 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. @@ -21,6 +21,7 @@ */ #include +#include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b8ee380..40cd751 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -26,7 +26,6 @@ SET(ENCRYPTION_SCHEME_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/encryption-scheme/smack-access.cpp ${CMAKE_CURRENT_SOURCE_DIR}/encryption-scheme/scheme-test.cpp - ${KEY_MANAGER_PATH}/dpl/core/src/assert.cpp ${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp ${KEY_MANAGER_PATH}/dpl/db/src/sql_connection.cpp ${KEY_MANAGER_PATH}/service/db-crypto.cpp @@ -134,7 +133,6 @@ SET(TEST_MERGED_SOURCES ${KEY_MANAGER_PATH}/crypto/sw-backend/internals.cpp ${KEY_MANAGER_PATH}/crypto/sw-backend/obj.cpp ${KEY_MANAGER_PATH}/crypto/sw-backend/store.cpp - ${KEY_MANAGER_PATH}/dpl/core/src/assert.cpp ${KEY_MANAGER_PATH}/dpl/core/src/colors.cpp ${KEY_MANAGER_PATH}/dpl/core/src/errno_string.cpp ${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp diff --git a/tests/test_sql.cpp b/tests/test_sql.cpp index a63455a..d408711 100644 --- a/tests/test_sql.cpp +++ b/tests/test_sql.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include diff --git a/tools/ckm_db_tool/CMakeLists.txt b/tools/ckm_db_tool/CMakeLists.txt index 1068aa0..7833c67 100644 --- a/tools/ckm_db_tool/CMakeLists.txt +++ b/tools/ckm_db_tool/CMakeLists.txt @@ -46,7 +46,6 @@ SET(CKM_DB_TOOLS_SOURCES ${KEY_MANAGER_PATH}/crypto/sw-backend/internals.cpp ${KEY_MANAGER_PATH}/crypto/sw-backend/obj.cpp ${KEY_MANAGER_PATH}/crypto/sw-backend/store.cpp - ${KEY_MANAGER_PATH}/dpl/core/src/assert.cpp ${KEY_MANAGER_PATH}/dpl/db/src/naive_synchronization_object.cpp ${KEY_MANAGER_PATH}/dpl/db/src/sql_connection.cpp ${KEY_MANAGER_PATH}/initial-values/BufferHandler.cpp -- 2.7.4 From 5d910c593a7787cbde88c818c5a3ac8fc753ff0c Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Tue, 17 Mar 2020 13:56:58 +0100 Subject: [PATCH 15/16] Refactor BinaryQueue and tests - Increase code coverage by removing code - Check NULL/0 argument values - Simplify buckets - Adjust tests - 50% negative tests Change-Id: I39bc58b0809798313a26cf13a35668028bbf3be4 --- src/manager/dpl/core/include/dpl/binary_queue.h | 223 +------------------ src/manager/dpl/core/src/binary_queue.cpp | 276 +++--------------------- tests/test_binary-queue.cpp | 124 ++++------- 3 files changed, 72 insertions(+), 551 deletions(-) diff --git a/src/manager/dpl/core/include/dpl/binary_queue.h b/src/manager/dpl/core/include/dpl/binary_queue.h index 7dd928e..404a80c 100644 --- a/src/manager/dpl/core/include/dpl/binary_queue.h +++ b/src/manager/dpl/core/include/dpl/binary_queue.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2011 - 2020 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. @@ -22,27 +22,17 @@ #ifndef CENT_KEY_BINARY_QUEUE_H #define CENT_KEY_BINARY_QUEUE_H -//#include #include -#include #include +#include #include -#include namespace CKM { -/** - * Binary queue auto pointer - */ -class BinaryQueue; -using BinaryQueueUniquePtr = std::unique_ptr; /** * Binary stream implemented as constant size bucket list - * - * @todo Add optimized implementation for FlattenConsume */ -class COMMON_API BinaryQueue { - // : public AbstractInputOutput +class COMMON_API BinaryQueue final { public: class Exception { public: @@ -50,78 +40,20 @@ public: DECLARE_EXCEPTION_TYPE(Base, OutOfData) }; - typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize, - void *userParam); - static void BufferDeleterFree(const void *buffer, - size_t bufferSize, - void *userParam); - - class BucketVisitor { - public: - /** - * Destructor - */ - virtual ~BucketVisitor(); - - /** - * Visit bucket - * - * @return none - * @param[in] buffer Constant pointer to bucket data buffer - * @param[in] bufferSize Number of bytes in bucket - */ - virtual void OnVisitBucket(const void *buffer, size_t bufferSize) = 0; - }; - private: - struct Bucket { - NONCOPYABLE(Bucket); - - const void *buffer; - const void *ptr; - size_t size; - size_t left; - - BufferDeleter deleter; - void *param; + typedef std::vector Bucket; - Bucket(const void *buffer, - size_t bufferSize, - BufferDeleter deleter, - void *userParam); - virtual ~Bucket(); - }; - - typedef std::list BucketList; + typedef std::list BucketList; BucketList m_buckets; size_t m_size; - static void DeleteBucket(Bucket *bucket); - - class BucketVisitorCall { - private: - BucketVisitor *m_visitor; - - public: - BucketVisitorCall(BucketVisitor *visitor); - virtual ~BucketVisitorCall(); - - void operator()(Bucket *bucket) const; - }; - public: /** * Construct empty binary queue */ BinaryQueue(); - /** - * Construct binary queue via bare copy of other binary queue - * - * @param[in] other Other binary queue to copy from - * @warning One cannot assume that bucket structure is preserved during copy - */ - BinaryQueue(const BinaryQueue &other); + BinaryQueue(const BinaryQueue &other) = delete; /** * Construct binary queue by moving data from other binary queue @@ -131,26 +63,6 @@ public: BinaryQueue(BinaryQueue &&) = default; /** - * Destructor - */ - virtual ~BinaryQueue(); - - /** - * Construct binary queue via bare copy of other binary queue - * - * @param[in] other Other binary queue to copy from - * @warning One cannot assume that bucket structure is preserved during copy - */ - const BinaryQueue &operator=(const BinaryQueue &other); - - /** - * Assign data from other binary queue using move semantics - * - * @param[in] other Other binary queue to move from - */ - BinaryQueue &operator=(BinaryQueue &&) = default; - - /** * Append copy of @a bufferSize bytes from memory pointed by @a buffer * to the end of binary queue. Uses default deleter based on free. * @@ -158,78 +70,10 @@ public: * @param[in] buffer Pointer to buffer to copy data from * @param[in] bufferSize Number of bytes to copy * @exception std::bad_alloc Cannot allocate memory to hold additional data - * @see BinaryQueue::BufferDeleterFree */ void AppendCopy(const void *buffer, size_t bufferSize); /** - * Append @a bufferSize bytes from memory pointed by @a buffer - * to the end of binary queue. Uses custom provided deleter. - * Responsibility for deleting provided buffer is transfered to BinaryQueue. - * - * @return none - * @param[in] buffer Pointer to data buffer - * @param[in] bufferSize Number of bytes available in buffer - * @param[in] deleter Pointer to deleter procedure used to free provided - * buffer - * @param[in] userParam User parameter passed to deleter routine - * @exception std::bad_alloc Cannot allocate memory to hold additional data - */ - void AppendUnmanaged( - const void *buffer, - size_t bufferSize, - BufferDeleter deleter = - &BinaryQueue::BufferDeleterFree, - void *userParam = NULL); - - /** - * Append copy of other binary queue to the end of this binary queue - * - * @return none - * @param[in] other Constant reference to other binary queue to copy data - * from - * @exception std::bad_alloc Cannot allocate memory to hold additional data - * @warning One cannot assume that bucket structure is preserved during copy - */ - void AppendCopyFrom(const BinaryQueue &other); - - /** - * Move bytes from other binary queue to the end of this binary queue. - * This also removes all bytes from other binary queue. - * This method is designed to be as fast as possible (only pointer swaps) - * and is suggested over making copies of binary queues. - * Bucket structure is preserved after operation. - * - * @return none - * @param[in] other Reference to other binary queue to move data from - * @exception std::bad_alloc Cannot allocate memory to hold additional data - */ - void AppendMoveFrom(BinaryQueue &other); - - /** - * Append copy of binary queue to the end of other binary queue - * - * @return none - * @param[in] other Constant reference to other binary queue to copy data to - * @exception std::bad_alloc Cannot allocate memory to hold additional data - * @warning One cannot assume that bucket structure is preserved during copy - */ - void AppendCopyTo(BinaryQueue &other) const; - - /** - * Move bytes from binary queue to the end of other binary queue. - * This also removes all bytes from binary queue. - * This method is designed to be as fast as possible (only pointer swaps) - * and is suggested over making copies of binary queues. - * Bucket structure is preserved after operation. - * - * @return none - * @param[in] other Reference to other binary queue to move data to - * @exception std::bad_alloc Cannot allocate memory to hold additional data - */ - void AppendMoveTo(BinaryQueue &other); - - /** * Retrieve total size of all data contained in binary queue * * @return Number of bytes in binary queue @@ -237,42 +81,6 @@ public: size_t Size() const; /** - * Remove all data from binary queue - * - * @return none - */ - void Clear(); - - /** - * Check if binary queue is empty - * - * @return true if binary queue is empty, false otherwise - */ - bool Empty() const; - - /** - * Remove @a size bytes from beginning of binary queue - * - * @return none - * @param[in] size Number of bytes to remove - * @exception BinaryQueue::Exception::OutOfData Number of bytes is larger - * than available bytes in binary queue - */ - void Consume(size_t size); - - /** - * Retrieve @a bufferSize bytes from beginning of binary queue and copy them - * to user supplied buffer - * - * @return none - * @param[in] buffer Pointer to user buffer to receive bytes - * @param[in] bufferSize Size of user buffer pointed by @a buffer - * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten - * is larger than available bytes in binary queue - */ - void Flatten(void *buffer, size_t bufferSize) const; - - /** * Retrieve @a bufferSize bytes from beginning of binary queue, copy them * to user supplied buffer, and remove from binary queue * @@ -283,25 +91,6 @@ public: * is larger than available bytes in binary queue */ void FlattenConsume(void *buffer, size_t bufferSize); - - /** - * Visit each buffer with data using visitor object - * - * @return none - * @param[in] visitor Pointer to bucket visitor - * @see BinaryQueue::BucketVisitor - */ - void VisitBuckets(BucketVisitor *visitor) const; - - /** - * IAbstractInput interface - */ - virtual BinaryQueueUniquePtr Read(size_t size); - - /** - * IAbstractOutput interface - */ - virtual size_t Write(const BinaryQueue &buffer, size_t bufferSize); }; } // namespace CKM diff --git a/src/manager/dpl/core/src/binary_queue.cpp b/src/manager/dpl/core/src/binary_queue.cpp index 97f19e1..ac8db27 100644 --- a/src/manager/dpl/core/src/binary_queue.cpp +++ b/src/manager/dpl/core/src/binary_queue.cpp @@ -19,133 +19,25 @@ * @version 1.0 * @brief This file is the implementation file of binary queue */ -#include #include #include #include -#include -#include #include -#include namespace CKM { BinaryQueue::BinaryQueue() : m_size(0) {} -BinaryQueue::BinaryQueue(const BinaryQueue &other) : - m_size(0) -{ - AppendCopyFrom(other); -} - -BinaryQueue::~BinaryQueue() -{ - // Remove all remainig buckets - Clear(); -} - -const BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other) -{ - if (this != &other) { - Clear(); - AppendCopyFrom(other); - } - - return *this; -} - -void BinaryQueue::AppendCopyFrom(const BinaryQueue &other) -{ - // To speed things up, always copy as one bucket - void *bufferCopy = malloc(other.m_size); - - if (bufferCopy == NULL) - throw std::bad_alloc(); - - try { - other.Flatten(bufferCopy, other.m_size); - AppendUnmanaged(bufferCopy, other.m_size, &BufferDeleterFree, NULL); - } catch (const std::bad_alloc &) { - // Free allocated memory - free(bufferCopy); - throw; - } -} - -void BinaryQueue::AppendMoveFrom(BinaryQueue &other) -{ - // Copy all buckets - std::copy(other.m_buckets.begin(), - other.m_buckets.end(), std::back_inserter(m_buckets)); - m_size += other.m_size; - - // Clear other, but do not free memory - other.m_buckets.clear(); - other.m_size = 0; -} - -void BinaryQueue::AppendCopyTo(BinaryQueue &other) const -{ - other.AppendCopyFrom(*this); -} - -void BinaryQueue::AppendMoveTo(BinaryQueue &other) -{ - other.AppendMoveFrom(*this); -} - -void BinaryQueue::Clear() -{ - std::for_each(m_buckets.begin(), m_buckets.end(), &DeleteBucket); - m_buckets.clear(); - m_size = 0; -} - void BinaryQueue::AppendCopy(const void *buffer, size_t bufferSize) { - // Create data copy with malloc/free - void *bufferCopy = malloc(bufferSize); - - // Check if allocation succeded - if (bufferCopy == NULL) - throw std::bad_alloc(); - - // Copy user data - memcpy(bufferCopy, buffer, bufferSize); - - try { - // Try to append new bucket - AppendUnmanaged(bufferCopy, bufferSize, &BufferDeleterFree, NULL); - } catch (const std::bad_alloc &) { - // Free allocated memory - free(bufferCopy); - throw; - } -} - -void BinaryQueue::AppendUnmanaged(const void *buffer, - size_t bufferSize, - BufferDeleter deleter, - void *userParam) -{ - // Do not attach empty buckets - if (bufferSize == 0) { - deleter(buffer, bufferSize, userParam); + if (bufferSize == 0) return; - } - // Just add new bucket with selected deleter - Bucket *bucket = new Bucket(buffer, bufferSize, deleter, userParam); + assert(buffer != NULL); - try { - m_buckets.push_back(bucket); - } catch (const std::bad_alloc &) { - delete bucket; - throw; - } - - // Increase total queue size + m_buckets.emplace_back(bufferSize); + memcpy(m_buckets.back().data(), buffer, bufferSize); m_size += bufferSize; } @@ -154,163 +46,45 @@ size_t BinaryQueue::Size() const return m_size; } -bool BinaryQueue::Empty() const -{ - return m_size == 0; -} - -void BinaryQueue::Consume(size_t size) -{ - // Check parameters - if (size > m_size) - Throw(Exception::OutOfData); - - size_t bytesLeft = size; - - // Consume data and/or remove buckets - while (bytesLeft > 0) { - // Get consume size - size_t count = std::min(bytesLeft, m_buckets.front()->left); - - m_buckets.front()->ptr = - static_cast(m_buckets.front()->ptr) + count; - m_buckets.front()->left -= count; - bytesLeft -= count; - m_size -= count; - - if (m_buckets.front()->left == 0) { - DeleteBucket(m_buckets.front()); - m_buckets.pop_front(); - } - } -} - -void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const +void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize) { // Check parameters if (bufferSize == 0) return; + assert(buffer != NULL); + if (bufferSize > m_size) Throw(Exception::OutOfData); size_t bytesLeft = bufferSize; void *ptr = buffer; - BucketList::const_iterator bucketIterator = m_buckets.begin(); - assert(m_buckets.end() != bucketIterator); + assert(!m_buckets.empty()); // Flatten data - while (bytesLeft > 0) { + do { + auto& bucket = m_buckets.front(); + // Get consume size - size_t count = std::min(bytesLeft, (*bucketIterator)->left); + size_t bucketSize = bucket.size(); + size_t count = std::min(bytesLeft, bucketSize); // Copy data to user pointer - memcpy(ptr, (*bucketIterator)->ptr, count); + memcpy(ptr, bucket.data(), count); + + // consume + if (count == bucketSize) { + m_buckets.pop_front(); + } else { + bucket.erase(bucket.begin(), bucket.begin() + count); + assert(count == bytesLeft); + break; + } - // Update flattened bytes count bytesLeft -= count; ptr = static_cast(ptr) + count; - - // Take next bucket - ++bucketIterator; - } -} - -void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize) -{ - // FIXME: Optimize - Flatten(buffer, bufferSize); - Consume(bufferSize); -} - -void BinaryQueue::DeleteBucket(BinaryQueue::Bucket *bucket) -{ - delete bucket; + } while (bytesLeft); + m_size -= bufferSize; } -void BinaryQueue::BufferDeleterFree(const void *data, - size_t dataSize, - void *userParam) -{ - (void)dataSize; - (void)userParam; - - // Default free deleter - free(const_cast(data)); -} - -BinaryQueue::Bucket::Bucket(const void *data, - size_t dataSize, - BufferDeleter dataDeleter, - void *userParam) : - buffer(data), - ptr(data), - size(dataSize), - left(dataSize), - deleter(dataDeleter), - param(userParam) -{ - assert(data != NULL); - assert(deleter != NULL); -} - -BinaryQueue::Bucket::~Bucket() -{ - // Invoke deleter on bucket data - deleter(buffer, size, param); -} - -BinaryQueue::BucketVisitor::~BucketVisitor() -{ -} - -BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor) : - m_visitor(visitor) -{ -} - -BinaryQueue::BucketVisitorCall::~BucketVisitorCall() -{ -} - -void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const -{ - m_visitor->OnVisitBucket(bucket->ptr, bucket->left); -} - -void BinaryQueue::VisitBuckets(BucketVisitor *visitor) const -{ - assert(visitor != NULL); - - // Visit all buckets - std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor)); -} - -BinaryQueueUniquePtr BinaryQueue::Read(size_t size) -{ - // Simulate input stream - size_t available = std::min(size, m_size); - - std::unique_ptr> - bufferCopy(malloc(available), free); - - if (!bufferCopy.get()) - throw std::bad_alloc(); - - BinaryQueueUniquePtr result(new BinaryQueue()); - - Flatten(bufferCopy.get(), available); - result->AppendUnmanaged( - bufferCopy.release(), available, &BufferDeleterFree, NULL); - Consume(available); - - return result; -} - -size_t BinaryQueue::Write(const BinaryQueue &buffer, size_t bufferSize) -{ - // Simulate output stream - AppendCopyFrom(buffer); - return bufferSize; -} } // namespace CKM diff --git a/tests/test_binary-queue.cpp b/tests/test_binary-queue.cpp index 28b1833..779eab4 100644 --- a/tests/test_binary-queue.cpp +++ b/tests/test_binary-queue.cpp @@ -22,121 +22,79 @@ using namespace CKM; namespace { -RawBuffer buf({0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}); +constexpr unsigned char buf[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; +constexpr size_t bufSize = sizeof(buf); } // namespace anonymous BOOST_AUTO_TEST_SUITE(BINARY_QUEUE_TEST) -POSITIVE_TEST_CASE(copy_assignment) +POSITIVE_TEST_CASE(append_copy) { - BinaryQueue bq1; - bq1.AppendCopy(buf.data(), buf.size()); - - BinaryQueue bq2; - bq2 = bq1; - - BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size()); - - RawBuffer buf1(bq1.Size(), 0x00); - RawBuffer buf2(bq2.Size(), 0x00); - - bq1.Flatten(buf1.data(), buf1.size()); - bq2.Flatten(buf2.data(), buf2.size()); - - BOOST_REQUIRE(buf1 == buf2); -} - -POSITIVE_TEST_CASE(append_copy_to) -{ - BinaryQueue bq1; - bq1.AppendCopy(buf.data(), buf.size()); - - BinaryQueue bq2; - bq1.AppendCopyTo(bq2); + BinaryQueue bq; + BOOST_REQUIRE(bq.Size() == 0); - BOOST_REQUIRE(bq1.Size() == bq2.Size() && bq1.Size() == buf.size()); + bq.AppendCopy(buf, bufSize); - RawBuffer buf1(bq1.Size(), 0x00); - RawBuffer buf2(bq2.Size(), 0x00); + BOOST_REQUIRE(bq.Size() == bufSize); - bq1.Flatten(buf1.data(), buf1.size()); - bq2.Flatten(buf2.data(), buf2.size()); + bq.AppendCopy(buf, bufSize); - BOOST_REQUIRE(buf1 == buf2); + BOOST_REQUIRE(bq.Size() == 2 * bufSize); } -POSITIVE_TEST_CASE(append_move_to) +NEGATIVE_TEST_CASE(append_copy) { - BinaryQueue bq1; - bq1.AppendCopy(buf.data(), buf.size()); - - BinaryQueue bq2; - bq1.AppendMoveTo(bq2); + BinaryQueue bq; - BOOST_REQUIRE(bq2.Size() == buf.size() && bq1.Empty()); + bq.AppendCopy(buf, 0); - RawBuffer buf2(bq2.Size(), 0x00); - bq2.Flatten(buf2.data(), buf2.size()); - BOOST_REQUIRE(buf == buf2); + BOOST_REQUIRE(bq.Size() == 0); } -POSITIVE_TEST_CASE(read) +POSITIVE_TEST_CASE(flatten_consume) { - BinaryQueue bq1; - bq1.AppendCopy(buf.data(), buf.size()); + BinaryQueue bq; - auto bq2 = bq1.Read(buf.size()); - BOOST_REQUIRE(bq1.Empty()); + constexpr size_t part1Size = bufSize / 2; - RawBuffer buf2(bq2->Size(), 0x00); - bq2->Flatten(buf2.data(), buf2.size()); - BOOST_REQUIRE(buf == buf2); -} + bq.AppendCopy(buf, part1Size); + bq.AppendCopy(buf + part1Size, bufSize - part1Size); -POSITIVE_TEST_CASE(write) -{ - BinaryQueue bq1; - bq1.AppendCopy(buf.data(), buf.size()); + char out[3 * bufSize / 4]; + bq.FlattenConsume(out, sizeof(out)); + BOOST_REQUIRE(memcmp(buf, out, sizeof(out)) == 0); - BinaryQueue bq2; - bq2.Write(bq1, bq1.Size()); + constexpr size_t remainingSize = bufSize - sizeof(out); + BOOST_REQUIRE(bq.Size() == remainingSize); - RawBuffer buf1(bq1.Size(), 0x00); - RawBuffer buf2(bq2.Size(), 0x00); + bq.FlattenConsume(out, remainingSize); - bq1.Flatten(buf1.data(), buf1.size()); - bq2.Flatten(buf2.data(), buf2.size()); + BOOST_REQUIRE(memcmp(buf + sizeof(out), out, remainingSize) == 0); + BOOST_REQUIRE(memcmp(buf + remainingSize, + out + remainingSize, + sizeof(out) - remainingSize) == 0); - BOOST_REQUIRE(buf1 == buf2); + BOOST_REQUIRE(bq.Size() == 0); } -POSITIVE_TEST_CASE(bucket_visitor) +NEGATIVE_TEST_CASE(flatten_consume) { - static std::vector globalBuf; - - class BucketVisitorTest : public BinaryQueue::BucketVisitor { - public: - virtual void OnVisitBucket(const void *buffer, size_t bufferSize) override - { - for (size_t i = 0; i < bufferSize; ++i) - globalBuf.push_back(static_cast(buffer)[i]); - } - }; + BinaryQueue bq; - BucketVisitorTest visitor; + char out; + BOOST_REQUIRE_THROW(bq.FlattenConsume(&out, sizeof(out)), + BinaryQueue::Exception::OutOfData); - constexpr size_t BucketNum = 3; - BinaryQueue bq; - for (size_t i = 0; i < BucketNum; ++i) - bq.AppendCopy(buf.data(), buf.size()); + bq.AppendCopy(buf, bufSize); - bq.VisitBuckets(&visitor); + bq.FlattenConsume(&out, 0); + BOOST_REQUIRE(bq.Size() == bufSize); - BOOST_REQUIRE(globalBuf.size() == buf.size() * BucketNum); - for (size_t i = 0; i < BucketNum; ++i) - for (size_t j = 0; j < buf.size(); ++j) - BOOST_REQUIRE(globalBuf[i * buf.size() + j] == buf[j]); + char out2[bufSize + 1]; + BOOST_REQUIRE_THROW(bq.FlattenConsume(out2, sizeof(out2)), + BinaryQueue::Exception::OutOfData); + BOOST_REQUIRE(bq.Size() == bufSize); } BOOST_AUTO_TEST_SUITE_END() -- 2.7.4 From 0aef0dd9a2687ff9943a033e83e0656c71217f9d Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Wed, 25 Mar 2020 16:31:51 +0100 Subject: [PATCH 16/16] Add negative CommunicationManager test Invalid usage simply won't compile. Not much that can be done to reach the 50% ratio except for merging all positive tests into one. Change-Id: I99b8b97397a7d4ccdf762fc96dbf7d8648ad9a17 --- tests/test_comm-manager.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/test_comm-manager.cpp b/tests/test_comm-manager.cpp index a6216a9..961451c 100644 --- a/tests/test_comm-manager.cpp +++ b/tests/test_comm-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved + * Copyright (c) 2015-2020 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. @@ -25,6 +25,7 @@ #include #include #include +#include namespace { struct MessageA { @@ -206,6 +207,28 @@ POSITIVE_TEST_CASE(TMM_0060_Stress) BOOST_REQUIRE_MESSAGE(c == 0, "Unexpected number of MessageC: " << c); } +NEGATIVE_TEST_CASE(TMM_0070_ThrowingListener) +{ + CKM::CommunicationManager mgr; + + bool called[3] = {}; + mgr.Register([&](const char&) { + called[0] = true; + }); + mgr.Register([&](const char&) { + called[1] = true; + throw std::runtime_error("Everything is awesome!"); + }); + mgr.Register([&](const char&) { + called[2] = true; + }); + + BOOST_REQUIRE_THROW(mgr.SendMessage('|'), std::runtime_error); + BOOST_REQUIRE_MESSAGE(called[0], "1st listener not called"); + BOOST_REQUIRE_MESSAGE(called[1], "2nd listener not called"); + BOOST_REQUIRE_MESSAGE(!called[2], "3rd listener called"); +} + BOOST_AUTO_TEST_SUITE_END() -- 2.7.4