Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 13 | #include "security/key-chain.hpp" |
| 14 | #include "security/validator.hpp" |
| 15 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 16 | #include "security/cryptopp.hpp" |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 18 | #include "boost-test.hpp" |
| 19 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 20 | using namespace std; |
| 21 | namespace ndn { |
| 22 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 23 | BOOST_AUTO_TEST_SUITE(SecurityTestSignatureSha256) |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 24 | |
| 25 | string SHA256_RESULT("a883dafc480d466ee04e0d6da986bd78eb1fdd2178d04693723da3a8f95d42f4"); |
| 26 | |
| 27 | BOOST_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 | |
| 39 | BOOST_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 Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 48 | BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file")); |
| 49 | KeyChain keyChain("sqlite3", "file"); |
| 50 | |
| 51 | keyChain.signWithSha256(testData); |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 52 | |
| 53 | testData.wireEncode(); |
| 54 | |
| 55 | SignatureSha256 sig(testData.getSignature()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 56 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 57 | BOOST_REQUIRE(Validator::verifySignature(testData, sig)); |
| 58 | } |
| 59 | |
| 60 | BOOST_AUTO_TEST_SUITE_END() |
| 61 | |
| 62 | } // namespace ndn |