blob: cf19abb4b3416cc07301618ca2d4fe72d617a834 [file] [log] [blame]
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * 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 Swaminathanb2105902015-08-20 14:28:54 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanth Swaminathanb2105902015-08-20 14:28:54 -07009 * 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 Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070013 * 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 Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070018 *
19 * @author Prashanth Swaminathan <prashanthsw@gmail.com>
20 */
21
22#include "producer-db.hpp"
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070023#include "boost-test.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070024#include "algo/aes.hpp"
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070025
26#include <boost/filesystem.hpp>
27
28namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040029namespace nac {
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070030namespace tests {
31
32using time::system_clock;
33
34class ProducerDBFixture
35{
36public:
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
48public:
49 boost::filesystem::path tmpPath;
50};
51
52BOOST_FIXTURE_TEST_SUITE(TestProducerDB, ProducerDBFixture)
53
54BOOST_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 Swaminathanb2105902015-08-20 14:28:54 -070062 AesKeyParams params(128);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070063 Buffer keyBuf1 = algo::Aes::generateKey(params).getKeyBits();
64 Buffer keyBuf2 = algo::Aes::generateKey(params).getKeyBits();
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070065
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 Zhang19a11d22018-04-12 22:58:20 -070087 BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), keyResult.end(), keyBuf1.begin(), keyBuf1.end());
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070088
89 keyResult = db.getContentKey(point3);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070090 BOOST_CHECK_EQUAL_COLLECTIONS(keyResult.begin(), keyResult.end(), keyBuf2.begin(), keyBuf2.end());
Prashanth Swaminathanb2105902015-08-20 14:28:54 -070091
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
104BOOST_AUTO_TEST_SUITE_END()
105
106} // namespace tests
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400107} // namespace nac
Prashanth Swaminathanb2105902015-08-20 14:28:54 -0700108} // namespace ndn