/* * Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ #include #include #include #include #include using namespace CKM; namespace { class GObjTest : public Crypto::GObj { public: GObjTest() : Crypto::GObj() {} }; class GStoreTest : public Crypto::GStore { public: GStoreTest(CryptoBackend backendId) : Crypto::GStore(backendId) {} }; struct TestException : public std::exception {}; class ThrowingHandlerTest { public: static void Handle(const std::string &) { throw TestException(); } }; } // namespace anonymous BOOST_AUTO_TEST_SUITE(GENERIC_BACKEND_TEST) BOOST_AUTO_TEST_CASE(gobj) { GObjTest obj; BOOST_REQUIRE_THROW(obj.getBinary(), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(obj.encrypt(CryptoAlgorithm(), RawBuffer()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(obj.decrypt(CryptoAlgorithm(), RawBuffer()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(obj.sign(CryptoAlgorithm(), RawBuffer()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(obj.verify(CryptoAlgorithm(), RawBuffer(), RawBuffer()), Exc::Crypto::OperationNotSupported); } BOOST_AUTO_TEST_CASE(gstore) { GStoreTest store(static_cast(0)); BOOST_REQUIRE_THROW(store.getObject(Token(), Password()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(store.generateAKey(CryptoAlgorithm(), Password(), Password()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(store.generateSKey(CryptoAlgorithm(), Password()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password(), Crypto::EncryptionParams()), Exc::Crypto::OperationNotSupported); BOOST_REQUIRE_THROW(store.destroy(Token()), Exc::Crypto::OperationNotSupported); } #if 0 BOOST_AUTO_TEST_CASE(algo_validation_not_mandatory) { constexpr ParamName pn = ParamName::ALGO_TYPE; using Checker = Crypto::ParamCheck< pn, int, false, Crypto::Type::Equals<0, 1, 2, 3, 4>, Crypto::DefaultGetter>; Checker checker; CryptoAlgorithm ca; BOOST_REQUIRE_NO_THROW(checker.Check(ca)); for (int i = 0; i < 5; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_NO_THROW(checker.Check(ca)); } for (int i = 5; i < 10; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam); } } BOOST_AUTO_TEST_CASE(algo_validation_mandatory) { constexpr ParamName pn = ParamName::ALGO_TYPE; using Checker = Crypto::ParamCheck< pn, int, true, Crypto::Type::Equals<0, 1, 2, 3, 4>, Crypto::DefaultGetter>; Checker checker; CryptoAlgorithm ca; BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam); for (int i = 0; i < 5; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_NO_THROW(checker.Check(ca)); } for (int i = 5; i < 10; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_THROW(checker.Check(ca), Exc::Crypto::InputParam); } } BOOST_AUTO_TEST_CASE(algo_validation_throwing_handler) { constexpr ParamName pn = ParamName::ALGO_TYPE; using Checker = Crypto::ParamCheck< pn, int, true, Crypto::Type::Equals<0, 1, 2, 3, 4>, Crypto::DefaultGetter, ThrowingHandlerTest>; Checker checker; CryptoAlgorithm ca; BOOST_REQUIRE_THROW(checker.Check(ca), TestException); for (int i = 0; i < 5; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_NO_THROW(checker.Check(ca)); } for (int i = 5; i < 10; ++i) { ca.setParam(pn, i); BOOST_REQUIRE_THROW(checker.Check(ca), TestException); } } #endif BOOST_AUTO_TEST_SUITE_END()