blob: 1d9793cbddc7e7a09699c412520783c7b8b518c7 [file] [log] [blame]
Shuo Chen7c6b4d72014-03-26 15:20:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "../storage/sqlite/sqlite-handle.hpp"
8
9#include <ndn-cpp-dev/security/key-chain.hpp>
10
11#include <boost/filesystem.hpp>
12#include <boost/test/unit_test.hpp>
13#include <boost/test/output_test_stream.hpp>
14
15namespace repo {
16
17class TestDbCreateDestroy
18{
19public:
20 TestDbCreateDestroy()
21 {
22 boost::filesystem::path testPath("unittestdb");
23 boost::filesystem::file_status testPathStatus = boost::filesystem::status(testPath);
24 if (!boost::filesystem::is_directory(testPathStatus)) {
25 if (!boost::filesystem::create_directory(boost::filesystem::path(testPath))) {
26 BOOST_FAIL("Cannot create unittestdb folder");
27 }
28 }
29 handle = new SqliteHandle("unittestdb");
30 }
31
32 ~TestDbCreateDestroy()
33 {
34 delete handle;
35 boost::filesystem::remove_all(boost::filesystem::path("unittestdb"));
36 }
37
38public:
39 SqliteHandle* handle;
40};
41
42BOOST_FIXTURE_TEST_SUITE(TestSqlite, TestDbCreateDestroy)
43
44static inline ndn::shared_ptr<ndn::Data>
45createData(const ndn::Name& name)
46{
47 static ndn::KeyChain keyChain;
48 static std::vector<uint8_t> content(1500, '-');
49
50 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
51 data->setName(name);
52 data->setContent(&content[0], content.size());
53 keyChain.sign(*data);
54
55 return data;
56}
57
58class FixtureBase : public TestDbCreateDestroy
59{
60public:
61 typedef std::list<ndn::shared_ptr<ndn::Data> > DataContainer;
62 DataContainer datas;
63
64 typedef std::list<std::pair<ndn::Interest, ndn::shared_ptr<ndn::Data> > > InterestContainer;
65 InterestContainer interests;
66};
67
68template<size_t N>
69class BaseSmoketestFixture : public FixtureBase
70{
71public:
72 BaseSmoketestFixture()
73 {
74 ndn::Name baseName("/x/y/z/test/1");
75 for (size_t i = 0; i < N; i++) {
76 ndn::Name name(baseName);
77 name.appendSegment(i);
78 ndn::shared_ptr<Data> data = createData(name);
79 this->datas.push_back(data);
80
81 this->interests.push_back(std::make_pair(Interest(name), data));
82 }
83 }
84};
85
86class BaseTestFixture : public FixtureBase
87{
88public:
89 BaseTestFixture()
90 {
91 datas.push_back(createData("/a"));
92 interests.push_back(std::make_pair(Interest("/a"), datas.back()));
93
94 datas.push_back(createData("/a/b"));
95 interests.push_back(std::make_pair(Interest("/a/b"), datas.back()));
96
97 datas.push_back(createData("/a/b/c"));
98 interests.push_back(std::make_pair(Interest("/a/b/c"), datas.back()));
99
100 datas.push_back(createData("/a/b/c/d"));
101 interests.push_back(std::make_pair(Interest("/a/b/c/d"), datas.back()));
102 }
103};
104
105class SelectorTestFixture : public FixtureBase
106{
107public:
108 SelectorTestFixture()
109 {
110 datas.push_back(createData("/a/1"));
111 datas.push_back(createData("/b/1"));
112 interests.push_back(std::make_pair(Interest()
113 .setName("/b")
114 .setSelectors(Selectors()
115 .setChildSelector(0)),
116 datas.back()));
117
118 datas.push_back(createData("/c/1"));
119 datas.push_back(createData("/b/99"));
120 interests.push_back(std::make_pair(Interest()
121 .setName("/b")
122 .setSelectors(Selectors()
123 .setChildSelector(1)),
124 datas.back()));
125 datas.push_back(createData("/b/5"));
126 datas.push_back(createData("/b/55"));
127 }
128};
129
130typedef boost::mpl::vector< BaseTestFixture,
131 SelectorTestFixture,
132 BaseSmoketestFixture<1>,
133 BaseSmoketestFixture<100> > Fixtures;
134
135BOOST_FIXTURE_TEST_CASE_TEMPLATE(InsertReadDelete, T, Fixtures, T)
136{
137 // Insert
138 for (typename T::DataContainer::iterator i = this->datas.begin();
139 i != this->datas.end(); ++i) {
140 BOOST_CHECK_EQUAL(this->handle->insertData(**i), true);
141 }
142
143 // Read (all items should exist)
144 for (typename T::InterestContainer::iterator i = this->interests.begin();
145 i != this->interests.end(); ++i) {
146 ndn::Data retrievedData;
147 BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), true);
148 BOOST_CHECK_EQUAL(retrievedData, *i->second);
149 }
150
151 // Delete
152 for (typename T::DataContainer::iterator i = this->datas.begin();
153 i != this->datas.end(); ++i) {
154 BOOST_CHECK_EQUAL(this->handle->deleteData((*i)->getName()), true);
155 }
156
157 // Read (none of the items should exist)
158 for (typename T::InterestContainer::iterator i = this->interests.begin();
159 i != this->interests.end(); ++i) {
160 ndn::Data retrievedData;
161 BOOST_REQUIRE_EQUAL(this->handle->readData(i->first, retrievedData), false);
162 }
163}
164
165BOOST_AUTO_TEST_SUITE_END()
166
167}// namespace repo