blob: 16a2c8102863245526667a291e410e502ce33eb2 [file] [log] [blame]
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -07001/* -*- 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 "group-manager-db.hpp"
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080023#include "algo/rsa.hpp"
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070024#include "boost-test.hpp"
25
26#include <boost/filesystem.hpp>
27
28namespace ndn {
29namespace gep {
30namespace tests {
31
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -070032const uint8_t SCHEDULE[] = {
33 0x8f, 0xc4,// Schedule
34 0x8d, 0x90,// WhiteIntervalList
35 0x8c, 0x2e, // RepetitiveInterval
36 0x86, 0x0f,
37 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
38 0x87, 0x0f,
39 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
40 0x88, 0x01,
41 0x05,
42 0x89, 0x01,
43 0x0a,
44 0x8a, 0x01,
45 0x02,
46 0x8b, 0x01,
47 0x01,
48 0x8c, 0x2e, // RepetitiveInterval
49 0x86, 0x0f,
50 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
51 0x87, 0x0f,
52 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
53 0x88, 0x01,
54 0x06,
55 0x89, 0x01,
56 0x08,
57 0x8a, 0x01,
58 0x01,
59 0x8b, 0x01,
60 0x01,
61 0x8c, 0x2e, // RepetitiveInterval
62 0x86, 0x0f,
63 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
64 0x87, 0x0f,
65 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
66 0x88, 0x01,
67 0x04,
68 0x89, 0x01,
69 0x07,
70 0x8a, 0x01,
71 0x00,
72 0x8b, 0x01,
73 0x00,
74 0x8e, 0x30, // BlackIntervalList
75 0x8c, 0x2e, // RepetitiveInterval
76 0x86, 0x0f,
77 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
78 0x87, 0x0f,
79 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
80 0x88, 0x01,
81 0x07,
82 0x89, 0x01,
83 0x08,
84 0x8a, 0x01,
85 0x00,
86 0x8b, 0x01,
87 0x00
88};
89
90const uint8_t REPETITIVE_INTERVAL[] = {
91 0x8c, 0x2e, // RepetitiveInterval
92 0x86, 0x0f,
93 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
94 0x87, 0x0f,
95 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, 0x32, 0x31, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
96 0x88, 0x01,
97 0x02,
98 0x89, 0x01,
99 0x0a,
100 0x8a, 0x01,
101 0x05,
102 0x8b, 0x01,
103 0x01
104};
105
106class GroupManagerDBFixture
107{
108public:
109 GroupManagerDBFixture()
110 : tmpPath(boost::filesystem::path(TMP_TESTS_PATH))
111 {
112 boost::filesystem::create_directories(tmpPath);
113 }
114
115 ~GroupManagerDBFixture()
116 {
117 boost::filesystem::remove_all(tmpPath);
118 }
119
120public:
121 boost::filesystem::path tmpPath;
122};
123
124BOOST_FIXTURE_TEST_SUITE(TestGroupManagerDB, GroupManagerDBFixture)
125
126BOOST_AUTO_TEST_CASE(DatabaseFunctions)
127{
128 // construction
129 std::string dbDir = tmpPath.c_str();
130 dbDir += "/test.db";
131 GroupManagerDB db(dbDir);
132
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700133 Block scheduleBlock(SCHEDULE, sizeof(SCHEDULE));
134
135 // create schedule
136 Schedule schedule(scheduleBlock);
137
138 // create member
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800139 RandomNumberGenerator rng;
140 RsaKeyParams params;
141 DecryptKey<algo::Rsa> decryptKey = algo::Rsa::generateKey(rng, params);
142 EncryptKey<algo::Rsa> encryptKey = algo::Rsa::deriveEncryptKey(decryptKey.getKeyBits());
143 Buffer keyBuf = encryptKey.getKeyBits();
144
145 Name name1("/ndn/BoyA/ksk-123");
146 Name name2("/ndn/BoyB/ksk-1233");
147 Name name3("/ndn/GirlC/ksk-123");
148 Name name4("/ndn/GirlD/ksk-123");
149 Name name5("/ndn/Hello/ksk-123");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700150
151 // add schedules into the database
152 BOOST_CHECK_NO_THROW(db.addSchedule("work-time", schedule));
153 BOOST_CHECK_NO_THROW(db.addSchedule("rest-time", schedule));
154 BOOST_CHECK_NO_THROW(db.addSchedule("play-time", schedule));
155 BOOST_CHECK_NO_THROW(db.addSchedule("boelter-time", schedule));
156
157 // throw exception when adding a schedule called an existing name
158 BOOST_CHECK_THROW(db.addSchedule("boelter-time", schedule), GroupManagerDB::Error);
159
160 // add members into the database
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800161 BOOST_CHECK_NO_THROW(db.addMember("work-time", name1, keyBuf));
162 BOOST_CHECK_NO_THROW(db.addMember("rest-time", name2, keyBuf));
163 BOOST_CHECK_NO_THROW(db.addMember("play-time", name3, keyBuf));
164 BOOST_CHECK_NO_THROW(db.addMember("play-time", name4, keyBuf));
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700165
166 // throw exception when adding a member having a not existing schedule name
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800167 BOOST_CHECK_THROW(db.addMember("false-time", name5, keyBuf), GroupManagerDB::Error);
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700168
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800169 BOOST_CHECK_NO_THROW(db.addMember("boelter-time", name5, keyBuf));
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700170
171 // throw exception when adding a member having an existing identity
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800172 BOOST_CHECK_THROW(db.addMember("work-time", name5, keyBuf), GroupManagerDB::Error);
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700173
174 // has function
175 BOOST_CHECK_EQUAL(db.hasSchedule("work-time"), true);
176 BOOST_CHECK_EQUAL(db.hasSchedule("rest-time"), true);
177 BOOST_CHECK_EQUAL(db.hasSchedule("play-time"), true);
178 BOOST_CHECK_EQUAL(db.hasSchedule("sleep-time"), false);
179 BOOST_CHECK_EQUAL(db.hasSchedule(""), false);
180
181 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyA")), true);
182 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyB")), true);
183 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyC")), false);
184
185 // get schedule
186 Schedule scheduleResult = db.getSchedule("work-time");
187 BOOST_CHECK(scheduleResult.wireEncode() == scheduleBlock);
188
189 scheduleResult = db.getSchedule("play-time");
190 BOOST_CHECK(scheduleResult.wireEncode() == scheduleBlock);
191
192 // throw exception when there is no such schedule in database
193 BOOST_CHECK_THROW(db.getSchedule("work-time-11"), GroupManagerDB::Error);
194
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700195 // list all schedule names
196 std::list<std::string> names = db.listAllScheduleNames();
197 BOOST_CHECK(std::find(names.begin(), names.end(), "work-time") != names.end());
198 BOOST_CHECK(std::find(names.begin(), names.end(), "play-time") != names.end());
199 BOOST_CHECK(std::find(names.begin(), names.end(), "rest-time") != names.end());
200 BOOST_CHECK(std::find(names.begin(), names.end(), "sleep-time") == names.end());
201
202 // list members of a schedule
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800203 std::map<Name, Buffer> memberMap = db.getScheduleMembers("play-time");
Zhiyi Zhang7cc09fc2015-09-01 13:40:32 -0700204 BOOST_CHECK(memberMap.size() != 0);
205
206 // when there's no such schedule, the return list's size is 0
207 BOOST_CHECK_EQUAL(db.getScheduleMembers("sleep-time").size(), 0);
208
209 // list all members
210 std::list<Name> members = db.listAllMembers();
211 BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/GirlC")) != members.end());
212 BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/GirlD")) != members.end());
213 BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/BoyA")) != members.end());
214 BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/BoyB")) != members.end());
215
216 // rename schedule
217 BOOST_CHECK_EQUAL(db.hasSchedule("boelter-time"), true);
218 db.renameSchedule("boelter-time", "rieber-time");
219 BOOST_CHECK_EQUAL(db.hasSchedule("boelter-time"), false);
220 BOOST_CHECK_EQUAL(db.hasSchedule("rieber-time"), true);
221 BOOST_CHECK_EQUAL(db.getMemberSchedule("/ndn/Hello"), "rieber-time");
222
223 // update schedule
224 Schedule newSchedule(scheduleBlock);
225 Block repIntervalBlock(REPETITIVE_INTERVAL, sizeof(REPETITIVE_INTERVAL));
226 newSchedule.addWhiteInterval(RepetitiveInterval(repIntervalBlock));
227 db.updateSchedule("rieber-time", newSchedule);
228 scheduleResult = db.getSchedule("rieber-time");
229 BOOST_CHECK(scheduleResult.wireEncode() != scheduleBlock);
230 BOOST_CHECK(scheduleResult.wireEncode() == newSchedule.wireEncode());
231
232 // add a new schedule when update a not existing schedule
233 BOOST_CHECK_EQUAL(db.hasSchedule("ralphs-time"), false);
234 db.updateSchedule("ralphs-time", newSchedule);
235 BOOST_CHECK_EQUAL(db.hasSchedule("ralphs-time"), true);
236
237 // update schedule of member
238 db.updateMemberSchedule(Name("/ndn/Hello"), "play-time");
239 BOOST_CHECK_EQUAL(db.getMemberSchedule(Name("/ndn/Hello")), "play-time");
240
241 // delete member
242 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/Hello")), true);
243 db.deleteMember(Name("/ndn/Hello"));
244 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/Hello")), false);
245
246 // delete a not existing member
247 BOOST_CHECK_NO_THROW(db.deleteMember(Name("/ndn/notExisting")));
248
249 // delete the schedule and all the members using this schedule should be deleted
250 db.deleteSchedule("play-time");
251 BOOST_CHECK_EQUAL(db.hasSchedule("play-time"), false);
252 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/GirlC")), false);
253 BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/GirlD")), false);
254
255 // delete a not existing schedule
256 BOOST_CHECK_NO_THROW(db.deleteSchedule("not-existing-time"));
257}
258
259BOOST_AUTO_TEST_SUITE_END()
260
261} // namespace tests
262} // namespace gep
263} // namespace ndn