blob: 611ac2ed386fd56134d898e1c1ce4eccfd413b39 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu21157162014-02-28 13:02:34 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Yingdi Yu21157162014-02-28 13:02:34 -080011 */
12
Yingdi Yu21157162014-02-28 13:02:34 -080013#include "security/key-chain.hpp"
14#include "security/validator.hpp"
15
Junxiao Shi482ccc52014-03-31 13:05:24 -070016#include "security/cryptopp.hpp"
Yingdi Yu21157162014-02-28 13:02:34 -080017
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070018#include "boost-test.hpp"
19
Yingdi Yu21157162014-02-28 13:02:34 -080020using namespace std;
21namespace ndn {
22
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070023BOOST_AUTO_TEST_SUITE(SecurityTestSignatureSha256)
Yingdi Yu21157162014-02-28 13:02:34 -080024
25string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4");
26
27BOOST_AUTO_TEST_CASE (Sha256)
28{
29 using namespace CryptoPP;
30
31 char content[6] = "1234\n";
32 ConstBufferPtr buf = crypto::sha256(reinterpret_cast<uint8_t*>(content), 5);
33 string result;
34 StringSource(buf->buf(), buf->size(), true, new HexEncoder(new StringSink(result), false));
35
36 BOOST_REQUIRE_EQUAL(SHA256_RESULT, result);
37}
38
39BOOST_AUTO_TEST_CASE (Signature)
40{
41 using namespace CryptoPP;
42
43 Name name("/TestSignatureSha/Basic");
44 Data testData(name);
45 char content[5] = "1234";
46 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070047
Yingdi Yuf56c68f2014-04-24 21:50:13 -070048 BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
49 KeyChain keyChain("sqlite3", "file");
50
51 keyChain.signWithSha256(testData);
Yingdi Yu21157162014-02-28 13:02:34 -080052
53 testData.wireEncode();
54
55 SignatureSha256 sig(testData.getSignature());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070056
Yingdi Yu21157162014-02-28 13:02:34 -080057 BOOST_REQUIRE(Validator::verifySignature(testData, sig));
58}
59
60BOOST_AUTO_TEST_SUITE_END()
61
62} // namespace ndn