blob: 264078c72f68a9fd7df94f83df29f6b9acfb1886 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu21157162014-02-28 13:02:34 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu21157162014-02-28 13:02:34 -080020 */
21
Yingdi Yu21157162014-02-28 13:02:34 -080022#include "security/key-chain.hpp"
23#include "security/validator.hpp"
24
Junxiao Shi482ccc52014-03-31 13:05:24 -070025#include "security/cryptopp.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080026
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
28
Yingdi Yu21157162014-02-28 13:02:34 -080029namespace ndn {
30
Yingdi Yubf6a2812014-06-17 15:32:11 -070031BOOST_AUTO_TEST_SUITE(SecurityTestDigestSha256)
Yingdi Yu21157162014-02-28 13:02:34 -080032
Yingdi Yu4a557052014-07-09 16:40:37 -070033std::string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
Yingdi Yu21157162014-02-28 13:02:34 -080034
Yingdi Yu4a557052014-07-09 16:40:37 -070035BOOST_AUTO_TEST_CASE(Sha256)
Yingdi Yu21157162014-02-28 13:02:34 -080036{
37 using namespace CryptoPP;
38
39 char content[6] = "1234\n";
40 ConstBufferPtr buf = crypto::sha256(reinterpret_cast<uint8_t*>(content), 5);
Yingdi Yu4a557052014-07-09 16:40:37 -070041 std::string result;
Yingdi Yu21157162014-02-28 13:02:34 -080042 StringSource(buf->buf(), buf->size(), true, new HexEncoder(new StringSink(result), false));
43
Yingdi Yu4a557052014-07-09 16:40:37 -070044 BOOST_CHECK_EQUAL(SHA256_RESULT, result);
Yingdi Yu21157162014-02-28 13:02:34 -080045}
46
Yingdi Yu4a557052014-07-09 16:40:37 -070047BOOST_AUTO_TEST_CASE(Signature)
Yingdi Yu21157162014-02-28 13:02:34 -080048{
49 using namespace CryptoPP;
50
51 Name name("/TestSignatureSha/Basic");
52 Data testData(name);
53 char content[5] = "1234";
54 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070055
Yingdi Yuf56c68f2014-04-24 21:50:13 -070056 BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
57 KeyChain keyChain("sqlite3", "file");
58
59 keyChain.signWithSha256(testData);
Yingdi Yu21157162014-02-28 13:02:34 -080060
61 testData.wireEncode();
62
Yingdi Yubf6a2812014-06-17 15:32:11 -070063 DigestSha256 sig(testData.getSignature());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070064
Yingdi Yu4a557052014-07-09 16:40:37 -070065 BOOST_CHECK(Validator::verifySignature(testData, sig));
66
67 BOOST_CHECK_THROW(sig.getKeyLocator(), ndn::SignatureInfo::Error);
Yingdi Yu21157162014-02-28 13:02:34 -080068}
69
70BOOST_AUTO_TEST_SUITE_END()
71
72} // namespace ndn