Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [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 | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 5 | * This file is part of NAC (Name-Based Access Control for NDN). |
| 6 | * See AUTHORS.md for complete list of NAC authors and contributors. |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 8 | * NAC is free software: you can redistribute it and/or modify it under the terms |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 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 | * |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 12 | * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 17 | * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 18 | * |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 19 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "group-manager-db.hpp" |
| 23 | #include "boost-test.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 24 | #include "algo/rsa.hpp" |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
| 26 | |
| 27 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 28 | namespace nac { |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 29 | namespace tests { |
| 30 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 31 | const uint8_t SCHEDULE[] = { |
| 32 | 0x8f, 0xc4,// Schedule |
| 33 | 0x8d, 0x90,// WhiteIntervalList |
| 34 | 0x8c, 0x2e, // RepetitiveInterval |
| 35 | 0x86, 0x0f, |
| 36 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 37 | 0x87, 0x0f, |
| 38 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 39 | 0x88, 0x01, |
| 40 | 0x05, |
| 41 | 0x89, 0x01, |
| 42 | 0x0a, |
| 43 | 0x8a, 0x01, |
| 44 | 0x02, |
| 45 | 0x8b, 0x01, |
| 46 | 0x01, |
| 47 | 0x8c, 0x2e, // RepetitiveInterval |
| 48 | 0x86, 0x0f, |
| 49 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 50 | 0x87, 0x0f, |
| 51 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x38, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 52 | 0x88, 0x01, |
| 53 | 0x06, |
| 54 | 0x89, 0x01, |
| 55 | 0x08, |
| 56 | 0x8a, 0x01, |
| 57 | 0x01, |
| 58 | 0x8b, 0x01, |
| 59 | 0x01, |
| 60 | 0x8c, 0x2e, // RepetitiveInterval |
| 61 | 0x86, 0x0f, |
| 62 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 63 | 0x87, 0x0f, |
| 64 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 65 | 0x88, 0x01, |
| 66 | 0x04, |
| 67 | 0x89, 0x01, |
| 68 | 0x07, |
| 69 | 0x8a, 0x01, |
| 70 | 0x00, |
| 71 | 0x8b, 0x01, |
| 72 | 0x00, |
| 73 | 0x8e, 0x30, // BlackIntervalList |
| 74 | 0x8c, 0x2e, // RepetitiveInterval |
| 75 | 0x86, 0x0f, |
| 76 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 77 | 0x87, 0x0f, |
| 78 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x37, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 79 | 0x88, 0x01, |
| 80 | 0x07, |
| 81 | 0x89, 0x01, |
| 82 | 0x08, |
| 83 | 0x8a, 0x01, |
| 84 | 0x00, |
| 85 | 0x8b, 0x01, |
| 86 | 0x00 |
| 87 | }; |
| 88 | |
| 89 | const uint8_t REPETITIVE_INTERVAL[] = { |
| 90 | 0x8c, 0x2e, // RepetitiveInterval |
| 91 | 0x86, 0x0f, |
| 92 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 93 | 0x87, 0x0f, |
| 94 | 0x32, 0x30, 0x31, 0x35, 0x30, 0x39, 0x32, 0x31, 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 95 | 0x88, 0x01, |
| 96 | 0x02, |
| 97 | 0x89, 0x01, |
| 98 | 0x0a, |
| 99 | 0x8a, 0x01, |
| 100 | 0x05, |
| 101 | 0x8b, 0x01, |
| 102 | 0x01 |
| 103 | }; |
| 104 | |
| 105 | class GroupManagerDBFixture |
| 106 | { |
| 107 | public: |
| 108 | GroupManagerDBFixture() |
| 109 | : tmpPath(boost::filesystem::path(TMP_TESTS_PATH)) |
| 110 | { |
| 111 | boost::filesystem::create_directories(tmpPath); |
| 112 | } |
| 113 | |
| 114 | ~GroupManagerDBFixture() |
| 115 | { |
| 116 | boost::filesystem::remove_all(tmpPath); |
| 117 | } |
| 118 | |
| 119 | public: |
| 120 | boost::filesystem::path tmpPath; |
| 121 | }; |
| 122 | |
| 123 | BOOST_FIXTURE_TEST_SUITE(TestGroupManagerDB, GroupManagerDBFixture) |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(DatabaseFunctions) |
| 126 | { |
| 127 | // construction |
| 128 | std::string dbDir = tmpPath.c_str(); |
| 129 | dbDir += "/test.db"; |
| 130 | GroupManagerDB db(dbDir); |
| 131 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 132 | Block scheduleBlock(SCHEDULE, sizeof(SCHEDULE)); |
| 133 | |
| 134 | // create schedule |
| 135 | Schedule schedule(scheduleBlock); |
| 136 | |
| 137 | // create member |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 138 | RsaKeyParams params; |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 139 | DecryptKey<algo::Rsa> decryptKey = algo::Rsa::generateKey(params); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 140 | EncryptKey<algo::Rsa> encryptKey = algo::Rsa::deriveEncryptKey(decryptKey.getKeyBits()); |
| 141 | Buffer keyBuf = encryptKey.getKeyBits(); |
| 142 | |
| 143 | Name name1("/ndn/BoyA/ksk-123"); |
| 144 | Name name2("/ndn/BoyB/ksk-1233"); |
| 145 | Name name3("/ndn/GirlC/ksk-123"); |
| 146 | Name name4("/ndn/GirlD/ksk-123"); |
| 147 | Name name5("/ndn/Hello/ksk-123"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 148 | |
| 149 | // add schedules into the database |
| 150 | BOOST_CHECK_NO_THROW(db.addSchedule("work-time", schedule)); |
| 151 | BOOST_CHECK_NO_THROW(db.addSchedule("rest-time", schedule)); |
| 152 | BOOST_CHECK_NO_THROW(db.addSchedule("play-time", schedule)); |
| 153 | BOOST_CHECK_NO_THROW(db.addSchedule("boelter-time", schedule)); |
| 154 | |
| 155 | // throw exception when adding a schedule called an existing name |
| 156 | BOOST_CHECK_THROW(db.addSchedule("boelter-time", schedule), GroupManagerDB::Error); |
| 157 | |
| 158 | // add members into the database |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 159 | BOOST_CHECK_NO_THROW(db.addMember("work-time", name1, keyBuf)); |
| 160 | BOOST_CHECK_NO_THROW(db.addMember("rest-time", name2, keyBuf)); |
| 161 | BOOST_CHECK_NO_THROW(db.addMember("play-time", name3, keyBuf)); |
| 162 | BOOST_CHECK_NO_THROW(db.addMember("play-time", name4, keyBuf)); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 163 | |
| 164 | // throw exception when adding a member having a not existing schedule name |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 165 | BOOST_CHECK_THROW(db.addMember("false-time", name5, keyBuf), GroupManagerDB::Error); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 166 | |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 167 | BOOST_CHECK_NO_THROW(db.addMember("boelter-time", name5, keyBuf)); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 168 | |
| 169 | // throw exception when adding a member having an existing identity |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 170 | BOOST_CHECK_THROW(db.addMember("work-time", name5, keyBuf), GroupManagerDB::Error); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 171 | |
| 172 | // has function |
| 173 | BOOST_CHECK_EQUAL(db.hasSchedule("work-time"), true); |
| 174 | BOOST_CHECK_EQUAL(db.hasSchedule("rest-time"), true); |
| 175 | BOOST_CHECK_EQUAL(db.hasSchedule("play-time"), true); |
| 176 | BOOST_CHECK_EQUAL(db.hasSchedule("sleep-time"), false); |
| 177 | BOOST_CHECK_EQUAL(db.hasSchedule(""), false); |
| 178 | |
| 179 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyA")), true); |
| 180 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyB")), true); |
| 181 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/BoyC")), false); |
| 182 | |
| 183 | // get schedule |
| 184 | Schedule scheduleResult = db.getSchedule("work-time"); |
| 185 | BOOST_CHECK(scheduleResult.wireEncode() == scheduleBlock); |
| 186 | |
| 187 | scheduleResult = db.getSchedule("play-time"); |
| 188 | BOOST_CHECK(scheduleResult.wireEncode() == scheduleBlock); |
| 189 | |
| 190 | // throw exception when there is no such schedule in database |
| 191 | BOOST_CHECK_THROW(db.getSchedule("work-time-11"), GroupManagerDB::Error); |
| 192 | |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 193 | // list all schedule names |
| 194 | std::list<std::string> names = db.listAllScheduleNames(); |
| 195 | BOOST_CHECK(std::find(names.begin(), names.end(), "work-time") != names.end()); |
| 196 | BOOST_CHECK(std::find(names.begin(), names.end(), "play-time") != names.end()); |
| 197 | BOOST_CHECK(std::find(names.begin(), names.end(), "rest-time") != names.end()); |
| 198 | BOOST_CHECK(std::find(names.begin(), names.end(), "sleep-time") == names.end()); |
| 199 | |
| 200 | // list members of a schedule |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 201 | std::map<Name, Buffer> memberMap = db.getScheduleMembers("play-time"); |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 202 | BOOST_CHECK(memberMap.size() != 0); |
| 203 | |
| 204 | // when there's no such schedule, the return list's size is 0 |
| 205 | BOOST_CHECK_EQUAL(db.getScheduleMembers("sleep-time").size(), 0); |
| 206 | |
| 207 | // list all members |
| 208 | std::list<Name> members = db.listAllMembers(); |
| 209 | BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/GirlC")) != members.end()); |
| 210 | BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/GirlD")) != members.end()); |
| 211 | BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/BoyA")) != members.end()); |
| 212 | BOOST_CHECK(std::find(members.begin(), members.end(), Name("/ndn/BoyB")) != members.end()); |
| 213 | |
| 214 | // rename schedule |
| 215 | BOOST_CHECK_EQUAL(db.hasSchedule("boelter-time"), true); |
| 216 | db.renameSchedule("boelter-time", "rieber-time"); |
| 217 | BOOST_CHECK_EQUAL(db.hasSchedule("boelter-time"), false); |
| 218 | BOOST_CHECK_EQUAL(db.hasSchedule("rieber-time"), true); |
| 219 | BOOST_CHECK_EQUAL(db.getMemberSchedule("/ndn/Hello"), "rieber-time"); |
| 220 | |
| 221 | // update schedule |
| 222 | Schedule newSchedule(scheduleBlock); |
| 223 | Block repIntervalBlock(REPETITIVE_INTERVAL, sizeof(REPETITIVE_INTERVAL)); |
| 224 | newSchedule.addWhiteInterval(RepetitiveInterval(repIntervalBlock)); |
| 225 | db.updateSchedule("rieber-time", newSchedule); |
| 226 | scheduleResult = db.getSchedule("rieber-time"); |
| 227 | BOOST_CHECK(scheduleResult.wireEncode() != scheduleBlock); |
| 228 | BOOST_CHECK(scheduleResult.wireEncode() == newSchedule.wireEncode()); |
| 229 | |
| 230 | // add a new schedule when update a not existing schedule |
| 231 | BOOST_CHECK_EQUAL(db.hasSchedule("ralphs-time"), false); |
| 232 | db.updateSchedule("ralphs-time", newSchedule); |
| 233 | BOOST_CHECK_EQUAL(db.hasSchedule("ralphs-time"), true); |
| 234 | |
| 235 | // update schedule of member |
| 236 | db.updateMemberSchedule(Name("/ndn/Hello"), "play-time"); |
| 237 | BOOST_CHECK_EQUAL(db.getMemberSchedule(Name("/ndn/Hello")), "play-time"); |
| 238 | |
| 239 | // delete member |
| 240 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/Hello")), true); |
| 241 | db.deleteMember(Name("/ndn/Hello")); |
| 242 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/Hello")), false); |
| 243 | |
| 244 | // delete a not existing member |
| 245 | BOOST_CHECK_NO_THROW(db.deleteMember(Name("/ndn/notExisting"))); |
| 246 | |
| 247 | // delete the schedule and all the members using this schedule should be deleted |
| 248 | db.deleteSchedule("play-time"); |
| 249 | BOOST_CHECK_EQUAL(db.hasSchedule("play-time"), false); |
| 250 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/GirlC")), false); |
| 251 | BOOST_CHECK_EQUAL(db.hasMember(Name("/ndn/GirlD")), false); |
| 252 | |
| 253 | // delete a not existing schedule |
| 254 | BOOST_CHECK_NO_THROW(db.deleteSchedule("not-existing-time")); |
| 255 | } |
| 256 | |
| 257 | BOOST_AUTO_TEST_SUITE_END() |
| 258 | |
| 259 | } // namespace tests |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 260 | } // namespace nac |
Zhiyi Zhang | 7cc09fc | 2015-09-01 13:40:32 -0700 | [diff] [blame] | 261 | } // namespace ndn |