blob: f59d6e7da02958c038cd1db1eefb6498591b40ce [file] [log] [blame]
Alexander Afanasyev7e721412017-01-11 13:36:08 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyev7e721412017-01-11 13:36:08 -08004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Alexander Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/validation-error.hpp"
Alexander Afanasyev7e721412017-01-11 13:36:08 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento637ea3b2022-09-10 00:11:34 -040025
Alexander Afanasyev7e721412017-01-11 13:36:08 -080026#include <boost/lexical_cast.hpp>
27
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
29
30using ndn::security::ValidationError;
Alexander Afanasyev7e721412017-01-11 13:36:08 -080031
32BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev7e721412017-01-11 13:36:08 -080033BOOST_AUTO_TEST_SUITE(TestValidationError)
34
35BOOST_AUTO_TEST_CASE(Basic)
36{
Davide Pesavento87208f92022-08-30 19:51:23 -040037 ValidationError e1{ValidationError::INVALID_SIGNATURE};
Alexander Afanasyev7e721412017-01-11 13:36:08 -080038 BOOST_CHECK_EQUAL(e1.getCode(), 1);
39 BOOST_CHECK_EQUAL(e1.getInfo(), "");
Davide Pesavento637ea3b2022-09-10 00:11:34 -040040 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(e1), "Signature verification failed");
Alexander Afanasyev7e721412017-01-11 13:36:08 -080041
Davide Pesavento637ea3b2022-09-10 00:11:34 -040042 ValidationError e2{ValidationError::MALFORMED_SIGNATURE, "message"};
Alexander Afanasyev7e721412017-01-11 13:36:08 -080043 BOOST_CHECK_EQUAL(e2.getCode(), 2);
44 BOOST_CHECK_EQUAL(e2.getInfo(), "message");
Davide Pesavento637ea3b2022-09-10 00:11:34 -040045 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(e2), "Missing or malformed signature (message)");
Alexander Afanasyev7e721412017-01-11 13:36:08 -080046
47 ValidationError e3{65535, "other message"};
48 BOOST_CHECK_EQUAL(e3.getCode(), 65535);
49 BOOST_CHECK_EQUAL(e3.getInfo(), "other message");
50 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(e3), "Custom error code 65535 (other message)");
Davide Pesavento637ea3b2022-09-10 00:11:34 -040051
52 ValidationError e4{200};
53 BOOST_CHECK_EQUAL(e4.getCode(), 200);
54 BOOST_CHECK_EQUAL(e4.getInfo(), "");
55 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(e4), "Unknown error code 200");
Alexander Afanasyev7e721412017-01-11 13:36:08 -080056}
57
58BOOST_AUTO_TEST_SUITE_END() // TestValidationError
Alexander Afanasyev7e721412017-01-11 13:36:08 -080059BOOST_AUTO_TEST_SUITE_END() // Security
60
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040061} // namespace ndn::tests