blob: 4b628b3aa91aba1f73d1c9815add238144a167b0 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, Regents of the University of California.
Shock Jiang3016c982014-11-11 11:35:17 -08004 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS 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 * NDNS 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 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config.hpp"
21#include "daemon/db-mgr.hpp"
22#include "logger.hpp"
23
24#include "../../boost-test.hpp"
25#include <boost/filesystem.hpp>
26
27#include <algorithm> // std::sort
28
29namespace ndn {
30namespace ndns {
31namespace tests {
32
33NDNS_LOG_INIT("DbMgrTest")
34
35BOOST_AUTO_TEST_SUITE(DbMgr)
36
37static const boost::filesystem::path TEST_DATABASE2 = TEST_CONFIG_PATH "/" "test-ndns.db";
38
39class DbMgrFixture
40{
41public:
42 DbMgrFixture()
43 : session(TEST_DATABASE2.string())
44 {
45 }
46
47 ~DbMgrFixture()
48 {
49 session.close();
50 boost::filesystem::remove(TEST_DATABASE2);
51 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
52 }
53
54public:
55 ndns::DbMgr session;
56};
57
58
59
60BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
61{
62 Zone zone1;
63 zone1.setName("/net");
64 zone1.setTtl(time::seconds(4600));
65 BOOST_CHECK_NO_THROW(session.insert(zone1));
66 BOOST_CHECK_GT(zone1.getId(), 0);
67
68 Zone zone2;
69 zone2.setName("/net");
70 session.find(zone2);
71 BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
72 BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
73
74 BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
75
76 zone2.setId(0);
77 BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
78
79 BOOST_CHECK_NO_THROW(session.remove(zone1));
80 BOOST_CHECK_EQUAL(zone1.getId(), 0);
81
82 // record shouldn't exist at this point
83 BOOST_CHECK_NO_THROW(session.find(zone2));
84 BOOST_CHECK_EQUAL(zone2.getId(), 0);
85}
86
87BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
88{
89 Zone zone("/net");
90 Rrset rrset1(&zone);
91
92 // Add
93
94 rrset1.setLabel("/net/ksk-123");
95 rrset1.setType(name::Component("ID-CERT"));
96 rrset1.setVersion(name::Component::fromVersion(567));
97 rrset1.setTtl(time::seconds(4600));
98
99 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000100 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800101
102 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
103 BOOST_CHECK_NO_THROW(session.insert(rrset1));
104 BOOST_CHECK_GT(rrset1.getId(), 0);
105 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
106
107 // Lookup
108
109 Rrset rrset2(&zone);
110 rrset2.setLabel("/net/ksk-123");
111 rrset2.setType(name::Component("ID-CERT"));
112
113 bool isFound = false;
114 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
115 BOOST_CHECK_EQUAL(isFound, true);
116
117 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
118 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
119 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
120 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
121 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
122 BOOST_CHECK(rrset2.getData() == rrset1.getData());
123
124 // Replace
125
126 rrset1.setVersion(name::Component::fromVersion(890));
127 static const std::string DATA2 = "ANOTHER DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000128 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800129
130 BOOST_CHECK_NO_THROW(session.update(rrset1));
131
132 rrset2 = Rrset(&zone);
133 rrset2.setLabel("/net/ksk-123");
134 rrset2.setType(name::Component("ID-CERT"));
135
136 isFound = false;
137 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
138 BOOST_CHECK_EQUAL(isFound, true);
139
140 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
141 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
142 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
143 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
144 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
145 BOOST_CHECK(rrset2.getData() == rrset1.getData());
146
147 // Remove
148
149 BOOST_CHECK_NO_THROW(session.remove(rrset1));
150
151 rrset2 = Rrset(&zone);
152 rrset2.setLabel("/net/ksk-123");
153 rrset2.setType(name::Component("ID-CERT"));
154
155 isFound = false;
156 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
157 BOOST_CHECK_EQUAL(isFound, false);
158
159 // Check error handling
160
161 rrset1 = Rrset();
162 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
163 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
164
165 rrset1.setId(1);
166 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
167
168 rrset1.setId(0);
169 rrset1.setZone(&zone);
170 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
171
172 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
173
174 rrset1.setId(1);
175 BOOST_CHECK_NO_THROW(session.remove(rrset1));
176
177 rrset1.setZone(0);
178 rrset1.setId(1);
179 BOOST_CHECK_NO_THROW(session.remove(rrset1));
180}
181
Jiewen Tan870b29b2014-11-17 19:09:49 -0800182BOOST_FIXTURE_TEST_CASE(FindAllZones, DbMgrFixture)
183{
184 Zone zone("/ndn");
185 zone.setTtl(time::seconds(1600));
186 session.insert(zone);
187
188 Zone zone2("/ndn/ucla");
189 zone2.setTtl(time::seconds(2600));
190 session.insert(zone2);
191
192 std::vector<Zone> vec = session.listZones();
193 BOOST_CHECK_EQUAL(vec.size(), 2);
194
195 std::sort(vec.begin(),
196 vec.end(),
197 [] (const Zone& n1, const Zone& n2) {
198 return n1.getName().size() < n2.getName().size();
199 });
200 BOOST_CHECK_EQUAL(vec[0].getId(), zone.getId());
201 BOOST_CHECK_EQUAL(vec[0].getName(), "/ndn");
202 BOOST_CHECK_EQUAL(vec[0].getTtl(), time::seconds(1600));
203
204 BOOST_CHECK_EQUAL(vec[1].getId(), zone2.getId());
205 BOOST_CHECK_EQUAL(vec[1].getName(), "/ndn/ucla");
206 BOOST_CHECK_EQUAL(vec[1].getTtl(), time::seconds(2600));
207
208}
Shock Jiang3016c982014-11-11 11:35:17 -0800209
210BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
211{
212 Zone zone("/");
213 Rrset rrset1(&zone);
214 rrset1.setLabel("/net/ksk-123");
215 rrset1.setType(name::Component("ID-CERT"));
216 rrset1.setVersion(name::Component::fromVersion(567));
217 rrset1.setTtl(time::seconds(4600));
218
219 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000220 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800221 session.insert(rrset1);
222
223 Rrset rrset2(&zone);
224 rrset2.setLabel("/net");
225 rrset2.setType(name::Component("NS"));
226 rrset2.setVersion(name::Component::fromVersion(232));
227 rrset2.setTtl(time::seconds(2100));
228 std::string data2 = "host1.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000229 rrset2.setData(makeBinaryBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800230 session.insert(rrset2);
231
232 std::vector<Rrset> vec = session.findRrsets(zone);
233 BOOST_CHECK_EQUAL(vec.size(), 2);
234
235 std::sort(vec.begin(),
236 vec.end(),
237 [] (const Rrset& n1, const Rrset& n2) {
238 return n1.getLabel().size() < n2.getLabel().size();
239 });
240 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
241 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
242}
243
244BOOST_AUTO_TEST_SUITE_END()
245
246} // namespace tests
247} // namespace ndns
248} // namespace ndn