blob: bad6213cf71364d164838d283791437b7eef3ed6 [file] [log] [blame]
Zhiyi Zhang5f133622015-10-17 08:49:54 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
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 *
19 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
20 */
21
22#include "consumer-db.hpp"
23#include "algo/rsa.hpp"
24#include "algo/aes.hpp"
25#include "boost-test.hpp"
26
27#include <boost/filesystem.hpp>
28
29namespace ndn {
30namespace gep {
31namespace tests {
32
33class ConsumerDBFixture
34{
35public:
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 {
50 RandomNumberGenerator rng;
51 RsaKeyParams params;
52 DecryptKey<algo::Rsa> dKey = algo::Rsa::generateKey(rng, params);
53 decryptionKeyBuf = dKey.getKeyBits();
54 EncryptKey<algo::Rsa> eKey = algo::Rsa::deriveEncryptKey(decryptionKeyBuf);
55 encryptionKeyBuf = eKey.getKeyBits();
56 }
57
58 void
59 generateAesKey(Buffer& encryptionKeyBuf, Buffer& decryptionKeyBuf)
60 {
61 RandomNumberGenerator rng;
62 AesKeyParams params;
63 DecryptKey<algo::Aes> memberDecryptKey = algo::Aes::generateKey(rng, params);
64 decryptionKeyBuf = memberDecryptKey.getKeyBits();
65 EncryptKey<algo::Aes> memberEncryptKey = algo::Aes::deriveEncryptKey(decryptionKeyBuf);
66 encryptionKeyBuf = memberEncryptKey.getKeyBits();
67 }
68
69public:
70 boost::filesystem::path tmpPath;
71};
72
73BOOST_FIXTURE_TEST_SUITE(TestConsumerDB, ConsumerDBFixture)
74
75BOOST_AUTO_TEST_CASE(OperateAesDecryptionKey)
76{
77 // construction
78 std::string dbDir = tmpPath.c_str();
79 dbDir += "/test.db";
80 ConsumerDB db(dbDir);
81
82 // generate key buffer
83 Buffer eKeyBuf;
84 Buffer dKeyBuf;
85 generateAesKey(eKeyBuf, dKeyBuf);
86
87 Name keyName("/alice/health/samples/activity/steps/C-KEY/20150928080000/20150928090000!");
88 keyName.append("FOR/alice/health/read/activity!");
89 db.addKey(keyName, dKeyBuf);
90 Buffer resultBuf = db.getKey(keyName);
91
92 BOOST_CHECK_EQUAL_COLLECTIONS(dKeyBuf.begin(), dKeyBuf.end(),
93 resultBuf.begin(), resultBuf.end());
94
95 db.deleteKey(keyName);
96 resultBuf = db.getKey(keyName);
97
98 BOOST_CHECK_EQUAL(resultBuf.size(), 0);
99}
100
101BOOST_AUTO_TEST_CASE(OperateRsaDecryptionKey)
102{
103 // construction
104 std::string dbDir = tmpPath.c_str();
105 dbDir += "/test.db";
106 ConsumerDB db(dbDir);
107
108 // generate key buffer
109 Buffer eKeyBuf;
110 Buffer dKeyBuf;
111 generateRsaKey(eKeyBuf, dKeyBuf);
112
113 Name keyName("/alice/health/samples/activity/steps/D-KEY/20150928080000/20150928090000!");
114 keyName.append("FOR/test/member/KEY/123!");
115 db.addKey(keyName, dKeyBuf);
116 Buffer resultBuf = db.getKey(keyName);
117
118 BOOST_CHECK_EQUAL_COLLECTIONS(dKeyBuf.begin(), dKeyBuf.end(),
119 resultBuf.begin(), resultBuf.end());
120
121 db.deleteKey(keyName);
122 resultBuf = db.getKey(keyName);
123
124 BOOST_CHECK_EQUAL(resultBuf.size(), 0);
125}
126
127BOOST_AUTO_TEST_SUITE_END()
128
129} // namespace tests
130} // namespace gep
131} // namespace ndn