Prashanth Swaminathan | b210590 | 2015-08-20 14:28:54 -0700 | [diff] [blame] | 1 | /* -*- 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 Prashanth Swaminathan <prashanthsw@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "producer-db.hpp" |
| 23 | #include "algo/aes.hpp" |
| 24 | #include "boost-test.hpp" |
| 25 | |
| 26 | #include <boost/filesystem.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 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 |
| 62 | RandomNumberGenerator rng; |
| 63 | AesKeyParams params(128); |
| 64 | Buffer keyBuf1 = algo::Aes::generateKey(rng, params).getKeyBits(); |
| 65 | Buffer keyBuf2 = algo::Aes::generateKey(rng, params).getKeyBits(); |
| 66 | |
| 67 | system_clock::TimePoint point1(time::fromIsoString("20150101T100000")); |
| 68 | system_clock::TimePoint point2(time::fromIsoString("20150102T100000")); |
| 69 | system_clock::TimePoint point3(time::fromIsoString("20150103T100000")); |
| 70 | system_clock::TimePoint point4(time::fromIsoString("20150104T100000")); |
| 71 | |
| 72 | // add keys into the database |
| 73 | BOOST_CHECK_NO_THROW(db.addContentKey(point1, keyBuf1)); |
| 74 | BOOST_CHECK_NO_THROW(db.addContentKey(point2, keyBuf1)); |
| 75 | BOOST_CHECK_NO_THROW(db.addContentKey(point3, keyBuf2)); |
| 76 | |
| 77 | // throw exception when adding a key to an existing timeslot |
| 78 | BOOST_CHECK_THROW(db.addContentKey(point1, keyBuf1), ProducerDB::Error); |
| 79 | |
| 80 | // has function |
| 81 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), true); |
| 82 | BOOST_CHECK_EQUAL(db.hasContentKey(point2), true); |
| 83 | BOOST_CHECK_EQUAL(db.hasContentKey(point3), true); |
| 84 | BOOST_CHECK_EQUAL(db.hasContentKey(point4), false); |
| 85 | |
| 86 | // get content key |
| 87 | Buffer keyResult = db.getContentKey(point1); |
| 88 | BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), |
| 89 | keyResult.end(), |
| 90 | keyBuf1.begin(), |
| 91 | keyBuf1.end()); |
| 92 | |
| 93 | keyResult = db.getContentKey(point3); |
| 94 | BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), |
| 95 | keyResult.end(), |
| 96 | keyBuf2.begin(), |
| 97 | keyBuf2.end()); |
| 98 | |
| 99 | // throw exception when there is no such timeslot in database |
| 100 | BOOST_CHECK_THROW(db.getContentKey(point4), ProducerDB::Error); |
| 101 | |
| 102 | // delete content key |
| 103 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), true); |
| 104 | db.deleteContentKey(point1); |
| 105 | BOOST_CHECK_EQUAL(db.hasContentKey(point1), false); |
| 106 | |
| 107 | // delete at a non-existing timeslot |
| 108 | BOOST_CHECK_NO_THROW(db.deleteContentKey(point4)); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_SUITE_END() |
| 112 | |
| 113 | } // namespace tests |
| 114 | } // namespace gep |
| 115 | } // namespace ndn |