blob: 0b693307bc665ff9271f2fbc526c2d94587f58ae [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/*
Davide Pesaventobdd88c12020-11-26 00:35:08 -05003 * Copyright (c) 2014-2020, 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"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050021#include "logger.hpp"
22
23#include "boost-test.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080024
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080025#include <algorithm>
Davide Pesaventobdd88c12020-11-26 00:35:08 -050026#include <boost/filesystem.hpp>
Shock Jiang3016c982014-11-11 11:35:17 -080027
Shock Jiang3016c982014-11-11 11:35:17 -080028namespace ndn {
29namespace ndns {
30namespace tests {
31
Alexander Afanasyev08d18742018-03-15 16:31:28 -040032NDNS_LOG_INIT(DbMgrTest);
Shock Jiang3016c982014-11-11 11:35:17 -080033
34BOOST_AUTO_TEST_SUITE(DbMgr)
35
Davide Pesaventobdd88c12020-11-26 00:35:08 -050036const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
Shock Jiang3016c982014-11-11 11:35:17 -080037
38class DbMgrFixture
39{
40public:
41 DbMgrFixture()
42 : session(TEST_DATABASE2.string())
43 {
44 }
45
46 ~DbMgrFixture()
47 {
48 session.close();
Shock Jiang3016c982014-11-11 11:35:17 -080049 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
Davide Pesaventobdd88c12020-11-26 00:35:08 -050050 boost::filesystem::remove(TEST_DATABASE2);
Shock Jiang3016c982014-11-11 11:35:17 -080051 }
52
53public:
54 ndns::DbMgr session;
55};
56
Shock Jiang3016c982014-11-11 11:35:17 -080057BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
58{
59 Zone zone1;
60 zone1.setName("/net");
61 zone1.setTtl(time::seconds(4600));
62 BOOST_CHECK_NO_THROW(session.insert(zone1));
63 BOOST_CHECK_GT(zone1.getId(), 0);
64
65 Zone zone2;
66 zone2.setName("/net");
67 session.find(zone2);
68 BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
69 BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
70
71 BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
72
73 zone2.setId(0);
74 BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
75
76 BOOST_CHECK_NO_THROW(session.remove(zone1));
77 BOOST_CHECK_EQUAL(zone1.getId(), 0);
78
79 // record shouldn't exist at this point
80 BOOST_CHECK_NO_THROW(session.find(zone2));
81 BOOST_CHECK_EQUAL(zone2.getId(), 0);
82}
83
Yumin Xia2c509c22017-02-09 14:37:36 -080084BOOST_FIXTURE_TEST_CASE(ZoneInfo, DbMgrFixture)
85{
86 Zone zone;
87 zone.setName("/net");
88 BOOST_CHECK_NO_THROW(session.insert(zone));
89
90 Name name1 = Name("/ndn/test");
91 Name name2 = Name("/ndn/zzzzz");
92
93 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "dsk", name1.wireEncode()));
94 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "ksk", name2.wireEncode()));
95
96 std::map<std::string, Block> zoneInfo;
97 zoneInfo = session.getZoneInfo(zone);
98
99 BOOST_CHECK_EQUAL(Name(zoneInfo["dsk"]), name1);
100 BOOST_CHECK_EQUAL(Name(zoneInfo["ksk"]), name2);
101}
102
Shock Jiang3016c982014-11-11 11:35:17 -0800103BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
104{
105 Zone zone("/net");
106 Rrset rrset1(&zone);
107
108 // Add
109
110 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800111 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800112 rrset1.setVersion(name::Component::fromVersion(567));
113 rrset1.setTtl(time::seconds(4600));
114
115 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000116 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800117
118 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
119 BOOST_CHECK_NO_THROW(session.insert(rrset1));
120 BOOST_CHECK_GT(rrset1.getId(), 0);
121 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
122
123 // Lookup
124
125 Rrset rrset2(&zone);
126 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800127 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800128
129 bool isFound = false;
130 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
131 BOOST_CHECK_EQUAL(isFound, true);
132
133 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
134 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
135 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
136 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
137 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
138 BOOST_CHECK(rrset2.getData() == rrset1.getData());
139
140 // Replace
141
142 rrset1.setVersion(name::Component::fromVersion(890));
143 static const std::string DATA2 = "ANOTHER DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000144 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800145
146 BOOST_CHECK_NO_THROW(session.update(rrset1));
147
148 rrset2 = Rrset(&zone);
149 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800150 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800151
152 isFound = false;
153 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
154 BOOST_CHECK_EQUAL(isFound, true);
155
156 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
157 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
158 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
159 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
160 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
161 BOOST_CHECK(rrset2.getData() == rrset1.getData());
162
163 // Remove
164
165 BOOST_CHECK_NO_THROW(session.remove(rrset1));
166
167 rrset2 = Rrset(&zone);
168 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800169 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800170
171 isFound = false;
172 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
173 BOOST_CHECK_EQUAL(isFound, false);
174
175 // Check error handling
176
177 rrset1 = Rrset();
178 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
179 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
180
181 rrset1.setId(1);
182 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
183
184 rrset1.setId(0);
185 rrset1.setZone(&zone);
186 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
187
188 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
189
190 rrset1.setId(1);
191 BOOST_CHECK_NO_THROW(session.remove(rrset1));
192
193 rrset1.setZone(0);
194 rrset1.setId(1);
195 BOOST_CHECK_NO_THROW(session.remove(rrset1));
196}
197
Jiewen Tan870b29b2014-11-17 19:09:49 -0800198BOOST_FIXTURE_TEST_CASE(FindAllZones, DbMgrFixture)
199{
200 Zone zone("/ndn");
201 zone.setTtl(time::seconds(1600));
202 session.insert(zone);
203
204 Zone zone2("/ndn/ucla");
205 zone2.setTtl(time::seconds(2600));
206 session.insert(zone2);
207
208 std::vector<Zone> vec = session.listZones();
209 BOOST_CHECK_EQUAL(vec.size(), 2);
210
211 std::sort(vec.begin(),
212 vec.end(),
213 [] (const Zone& n1, const Zone& n2) {
214 return n1.getName().size() < n2.getName().size();
215 });
216 BOOST_CHECK_EQUAL(vec[0].getId(), zone.getId());
217 BOOST_CHECK_EQUAL(vec[0].getName(), "/ndn");
218 BOOST_CHECK_EQUAL(vec[0].getTtl(), time::seconds(1600));
219
220 BOOST_CHECK_EQUAL(vec[1].getId(), zone2.getId());
221 BOOST_CHECK_EQUAL(vec[1].getName(), "/ndn/ucla");
222 BOOST_CHECK_EQUAL(vec[1].getTtl(), time::seconds(2600));
223
224}
Shock Jiang3016c982014-11-11 11:35:17 -0800225
226BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
227{
228 Zone zone("/");
229 Rrset rrset1(&zone);
230 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800231 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800232 rrset1.setVersion(name::Component::fromVersion(567));
233 rrset1.setTtl(time::seconds(4600));
234
235 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000236 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800237 session.insert(rrset1);
238
239 Rrset rrset2(&zone);
240 rrset2.setLabel("/net");
241 rrset2.setType(name::Component("NS"));
242 rrset2.setVersion(name::Component::fromVersion(232));
243 rrset2.setTtl(time::seconds(2100));
244 std::string data2 = "host1.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000245 rrset2.setData(makeBinaryBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800246 session.insert(rrset2);
247
248 std::vector<Rrset> vec = session.findRrsets(zone);
249 BOOST_CHECK_EQUAL(vec.size(), 2);
250
251 std::sort(vec.begin(),
252 vec.end(),
253 [] (const Rrset& n1, const Rrset& n2) {
254 return n1.getLabel().size() < n2.getLabel().size();
255 });
256 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
257 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
258}
259
260BOOST_AUTO_TEST_SUITE_END()
261
262} // namespace tests
263} // namespace ndns
264} // namespace ndn