Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -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 |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -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. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -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 |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -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; |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -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/>. |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 18 | * |
| 19 | * @author Prashanth Swaminathan <prashanthsw@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "producer-db.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 23 | #include "boost-test.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 24 | #include "algo/aes.hpp" |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/filesystem.hpp> |
| 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 29 | namespace nac { |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 30 | namespace tests { |
| 31 | |
| 32 | using time::system_clock; |
| 33 | |
| 34 | class ProducerDBFixture |
| 35 | { |
| 36 | public: |
| 37 | ProducerDBFixture() |
| 38 | : tmpPath(boost::filesystem::path(TMP_TESTS_PATH)) |
| 39 | { |
| 40 | boost::filesystem::create_directories(tmpPath); |
| 41 | } |
| 42 | |
| 43 | ~ProducerDBFixture() |
| 44 | { |
| 45 | boost::filesystem::remove_all(tmpPath); |
| 46 | } |
| 47 | |
| 48 | public: |
| 49 | boost::filesystem::path tmpPath; |
| 50 | }; |
| 51 | |
| 52 | BOOST_FIXTURE_TEST_SUITE(TestProducerDB, ProducerDBFixture) |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE(DatabaseFunctions) |
| 55 | { |
| 56 | // construction |
| 57 | std::string dbDir = tmpPath.c_str(); |
| 58 | dbDir += "/test.db"; |
| 59 | ProducerDB db(dbDir); |
| 60 | |
| 61 | // create member |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 62 | AesKeyParams params(128); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 63 | Buffer keyBuf1 = algo::Aes::generateKey(params).getKeyBits(); |
| 64 | Buffer keyBuf2 = algo::Aes::generateKey(params).getKeyBits(); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 65 | |
| 66 | system_clock::TimePoint point1(time::fromIsoString("20150101T100000")); |
| 67 | system_clock::TimePoint point2(time::fromIsoString("20150102T100000")); |
| 68 | system_clock::TimePoint point3(time::fromIsoString("20150103T100000")); |
| 69 | system_clock::TimePoint point4(time::fromIsoString("20150104T100000")); |
| 70 | |
| 71 | // add keys into the database |
| 72 | BOOST_CHECK_NO_THROW(db.addContentKey(point1, keyBuf1)); |
| 73 | BOOST_CHECK_NO_THROW(db.addContentKey(point2, keyBuf1)); |
| 74 | BOOST_CHECK_NO_THROW(db.addContentKey(point3, keyBuf2)); |
| 75 | |
| 76 | // throw exception when adding a key to an existing timeslot |
| 77 | BOOST_CHECK_THROW(db.addContentKey(point1, keyBuf1), ProducerDB::Error); |
| 78 | |
| 79 | // has function |
| 80 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), true); |
| 81 | BOOST_CHECK_EQUAL(db.hasContentKey(point2), true); |
| 82 | BOOST_CHECK_EQUAL(db.hasContentKey(point3), true); |
| 83 | BOOST_CHECK_EQUAL(db.hasContentKey(point4), false); |
| 84 | |
| 85 | // get content key |
| 86 | Buffer keyResult = db.getContentKey(point1); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), keyResult.end(), keyBuf1.begin(), keyBuf1.end()); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 88 | |
| 89 | keyResult = db.getContentKey(point3); |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), keyResult.end(), keyBuf2.begin(), keyBuf2.end()); |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 91 | |
| 92 | // throw exception when there is no such timeslot in database |
| 93 | BOOST_CHECK_THROW(db.getContentKey(point4), ProducerDB::Error); |
| 94 | |
| 95 | // delete content key |
| 96 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), true); |
| 97 | db.deleteContentKey(point1); |
| 98 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), false); |
| 99 | |
| 100 | // delete at a non-existing timeslot |
| 101 | BOOST_CHECK_NO_THROW(db.deleteContentKey(point4)); |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_SUITE_END() |
| 105 | |
| 106 | } // namespace tests |
Alexander Afanasyev | 9091d83 | 2018-04-18 17:21:08 -0400 | [diff] [blame] | 107 | } // namespace nac |
Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 108 | } // namespace ndn |