Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 19 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "consumer-db.hpp" |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 23 | #include "boost-test.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 24 | #include "algo/aes.hpp" |
| 25 | #include "algo/rsa.hpp" |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 26 | |
| 27 | #include <boost/filesystem.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace gep { |
| 31 | namespace tests { |
| 32 | |
| 33 | class ConsumerDBFixture |
| 34 | { |
| 35 | public: |
| 36 | ConsumerDBFixture() |
| 37 | : tmpPath(boost::filesystem::path(TMP_TESTS_PATH)) |
| 38 | { |
| 39 | boost::filesystem::create_directories(tmpPath); |
| 40 | } |
| 41 | |
| 42 | ~ConsumerDBFixture() |
| 43 | { |
| 44 | boost::filesystem::remove_all(tmpPath); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | generateRsaKey(Buffer& encryptionKeyBuf, Buffer& decryptionKeyBuf) |
| 49 | { |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 50 | RsaKeyParams params; |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 51 | DecryptKey<algo::Rsa> dKey = algo::Rsa::generateKey(params); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 52 | decryptionKeyBuf = dKey.getKeyBits(); |
| 53 | EncryptKey<algo::Rsa> eKey = algo::Rsa::deriveEncryptKey(decryptionKeyBuf); |
| 54 | encryptionKeyBuf = eKey.getKeyBits(); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | generateAesKey(Buffer& encryptionKeyBuf, Buffer& decryptionKeyBuf) |
| 59 | { |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 60 | AesKeyParams params; |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 61 | DecryptKey<algo::Aes> memberDecryptKey = algo::Aes::generateKey(params); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 62 | decryptionKeyBuf = memberDecryptKey.getKeyBits(); |
| 63 | EncryptKey<algo::Aes> memberEncryptKey = algo::Aes::deriveEncryptKey(decryptionKeyBuf); |
| 64 | encryptionKeyBuf = memberEncryptKey.getKeyBits(); |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | boost::filesystem::path tmpPath; |
| 69 | }; |
| 70 | |
| 71 | BOOST_FIXTURE_TEST_SUITE(TestConsumerDB, ConsumerDBFixture) |
| 72 | |
| 73 | BOOST_AUTO_TEST_CASE(OperateAesDecryptionKey) |
| 74 | { |
| 75 | // construction |
| 76 | std::string dbDir = tmpPath.c_str(); |
| 77 | dbDir += "/test.db"; |
| 78 | ConsumerDB db(dbDir); |
| 79 | |
| 80 | // generate key buffer |
| 81 | Buffer eKeyBuf; |
| 82 | Buffer dKeyBuf; |
| 83 | generateAesKey(eKeyBuf, dKeyBuf); |
| 84 | |
| 85 | Name keyName("/alice/health/samples/activity/steps/C-KEY/20150928080000/20150928090000!"); |
| 86 | keyName.append("FOR/alice/health/read/activity!"); |
| 87 | db.addKey(keyName, dKeyBuf); |
| 88 | Buffer resultBuf = db.getKey(keyName); |
| 89 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 90 | BOOST_CHECK_EQUAL_COLLECTIONS(dKeyBuf.begin(), dKeyBuf.end(), resultBuf.begin(), resultBuf.end()); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 91 | |
| 92 | db.deleteKey(keyName); |
| 93 | resultBuf = db.getKey(keyName); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(resultBuf.size(), 0); |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(OperateRsaDecryptionKey) |
| 99 | { |
| 100 | // construction |
| 101 | std::string dbDir = tmpPath.c_str(); |
| 102 | dbDir += "/test.db"; |
| 103 | ConsumerDB db(dbDir); |
| 104 | |
| 105 | // generate key buffer |
| 106 | Buffer eKeyBuf; |
| 107 | Buffer dKeyBuf; |
| 108 | generateRsaKey(eKeyBuf, dKeyBuf); |
| 109 | |
| 110 | Name keyName("/alice/health/samples/activity/steps/D-KEY/20150928080000/20150928090000!"); |
| 111 | keyName.append("FOR/test/member/KEY/123!"); |
| 112 | db.addKey(keyName, dKeyBuf); |
| 113 | Buffer resultBuf = db.getKey(keyName); |
| 114 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 115 | BOOST_CHECK_EQUAL_COLLECTIONS(dKeyBuf.begin(), dKeyBuf.end(), resultBuf.begin(), resultBuf.end()); |
Zhiyi Zhang | 5f13362 | 2015-10-17 08:49:54 +0800 | [diff] [blame] | 116 | |
| 117 | db.deleteKey(keyName); |
| 118 | resultBuf = db.getKey(keyName); |
| 119 | |
| 120 | BOOST_CHECK_EQUAL(resultBuf.size(), 0); |
| 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_SUITE_END() |
| 124 | |
| 125 | } // namespace tests |
| 126 | } // namespace gep |
| 127 | } // namespace ndn |