blob: 4f730b978286053e7e70c92e5bd591414774b0ab [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
3 * Copyright (c) 2014-2017, 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
Shock Jiang3016c982014-11-11 11:35:17 -080020#include "daemon/db-mgr.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080021#include "test-common.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080022
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080023#include <algorithm>
Shock Jiang3016c982014-11-11 11:35:17 -080024
Shock Jiang3016c982014-11-11 11:35:17 -080025namespace ndn {
26namespace ndns {
27namespace tests {
28
29NDNS_LOG_INIT("DbMgrTest")
30
31BOOST_AUTO_TEST_SUITE(DbMgr)
32
33static const boost::filesystem::path TEST_DATABASE2 = TEST_CONFIG_PATH "/" "test-ndns.db";
34
35class DbMgrFixture
36{
37public:
38 DbMgrFixture()
39 : session(TEST_DATABASE2.string())
40 {
41 }
42
43 ~DbMgrFixture()
44 {
45 session.close();
46 boost::filesystem::remove(TEST_DATABASE2);
47 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
48 }
49
50public:
51 ndns::DbMgr session;
52};
53
54
55
56BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
57{
58 Zone zone1;
59 zone1.setName("/net");
60 zone1.setTtl(time::seconds(4600));
61 BOOST_CHECK_NO_THROW(session.insert(zone1));
62 BOOST_CHECK_GT(zone1.getId(), 0);
63
64 Zone zone2;
65 zone2.setName("/net");
66 session.find(zone2);
67 BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
68 BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
69
70 BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
71
72 zone2.setId(0);
73 BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
74
75 BOOST_CHECK_NO_THROW(session.remove(zone1));
76 BOOST_CHECK_EQUAL(zone1.getId(), 0);
77
78 // record shouldn't exist at this point
79 BOOST_CHECK_NO_THROW(session.find(zone2));
80 BOOST_CHECK_EQUAL(zone2.getId(), 0);
81}
82
Yumin Xia2c509c22017-02-09 14:37:36 -080083BOOST_FIXTURE_TEST_CASE(ZoneInfo, DbMgrFixture)
84{
85 Zone zone;
86 zone.setName("/net");
87 BOOST_CHECK_NO_THROW(session.insert(zone));
88
89 Name name1 = Name("/ndn/test");
90 Name name2 = Name("/ndn/zzzzz");
91
92 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "dsk", name1.wireEncode()));
93 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "ksk", name2.wireEncode()));
94
95 std::map<std::string, Block> zoneInfo;
96 zoneInfo = session.getZoneInfo(zone);
97
98 BOOST_CHECK_EQUAL(Name(zoneInfo["dsk"]), name1);
99 BOOST_CHECK_EQUAL(Name(zoneInfo["ksk"]), name2);
100}
101
Shock Jiang3016c982014-11-11 11:35:17 -0800102BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
103{
104 Zone zone("/net");
105 Rrset rrset1(&zone);
106
107 // Add
108
109 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800110 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800111 rrset1.setVersion(name::Component::fromVersion(567));
112 rrset1.setTtl(time::seconds(4600));
113
114 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000115 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800116
117 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
118 BOOST_CHECK_NO_THROW(session.insert(rrset1));
119 BOOST_CHECK_GT(rrset1.getId(), 0);
120 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
121
122 // Lookup
123
124 Rrset rrset2(&zone);
125 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800126 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800127
128 bool isFound = false;
129 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
130 BOOST_CHECK_EQUAL(isFound, true);
131
132 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
133 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
134 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
135 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
136 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
137 BOOST_CHECK(rrset2.getData() == rrset1.getData());
138
139 // Replace
140
141 rrset1.setVersion(name::Component::fromVersion(890));
142 static const std::string DATA2 = "ANOTHER DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000143 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800144
145 BOOST_CHECK_NO_THROW(session.update(rrset1));
146
147 rrset2 = Rrset(&zone);
148 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800149 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800150
151 isFound = false;
152 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
153 BOOST_CHECK_EQUAL(isFound, true);
154
155 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
156 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
157 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
158 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
159 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
160 BOOST_CHECK(rrset2.getData() == rrset1.getData());
161
162 // Remove
163
164 BOOST_CHECK_NO_THROW(session.remove(rrset1));
165
166 rrset2 = Rrset(&zone);
167 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800168 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800169
170 isFound = false;
171 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
172 BOOST_CHECK_EQUAL(isFound, false);
173
174 // Check error handling
175
176 rrset1 = Rrset();
177 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
178 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
179
180 rrset1.setId(1);
181 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
182
183 rrset1.setId(0);
184 rrset1.setZone(&zone);
185 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
186
187 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
188
189 rrset1.setId(1);
190 BOOST_CHECK_NO_THROW(session.remove(rrset1));
191
192 rrset1.setZone(0);
193 rrset1.setId(1);
194 BOOST_CHECK_NO_THROW(session.remove(rrset1));
195}
196
Jiewen Tan870b29b2014-11-17 19:09:49 -0800197BOOST_FIXTURE_TEST_CASE(FindAllZones, DbMgrFixture)
198{
199 Zone zone("/ndn");
200 zone.setTtl(time::seconds(1600));
201 session.insert(zone);
202
203 Zone zone2("/ndn/ucla");
204 zone2.setTtl(time::seconds(2600));
205 session.insert(zone2);
206
207 std::vector<Zone> vec = session.listZones();
208 BOOST_CHECK_EQUAL(vec.size(), 2);
209
210 std::sort(vec.begin(),
211 vec.end(),
212 [] (const Zone& n1, const Zone& n2) {
213 return n1.getName().size() < n2.getName().size();
214 });
215 BOOST_CHECK_EQUAL(vec[0].getId(), zone.getId());
216 BOOST_CHECK_EQUAL(vec[0].getName(), "/ndn");
217 BOOST_CHECK_EQUAL(vec[0].getTtl(), time::seconds(1600));
218
219 BOOST_CHECK_EQUAL(vec[1].getId(), zone2.getId());
220 BOOST_CHECK_EQUAL(vec[1].getName(), "/ndn/ucla");
221 BOOST_CHECK_EQUAL(vec[1].getTtl(), time::seconds(2600));
222
223}
Shock Jiang3016c982014-11-11 11:35:17 -0800224
225BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
226{
227 Zone zone("/");
228 Rrset rrset1(&zone);
229 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800230 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800231 rrset1.setVersion(name::Component::fromVersion(567));
232 rrset1.setTtl(time::seconds(4600));
233
234 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000235 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800236 session.insert(rrset1);
237
238 Rrset rrset2(&zone);
239 rrset2.setLabel("/net");
240 rrset2.setType(name::Component("NS"));
241 rrset2.setVersion(name::Component::fromVersion(232));
242 rrset2.setTtl(time::seconds(2100));
243 std::string data2 = "host1.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000244 rrset2.setData(makeBinaryBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800245 session.insert(rrset2);
246
247 std::vector<Rrset> vec = session.findRrsets(zone);
248 BOOST_CHECK_EQUAL(vec.size(), 2);
249
250 std::sort(vec.begin(),
251 vec.end(),
252 [] (const Rrset& n1, const Rrset& n2) {
253 return n1.getLabel().size() < n2.getLabel().size();
254 });
255 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
256 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
257}
258
259BOOST_AUTO_TEST_SUITE_END()
260
261} // namespace tests
262} // namespace ndns
263} // namespace ndn