blob: a30f2dc90eb07f8810133c89a4169597cab27b63 [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 Pesavento38fd3982022-04-18 22:22:02 -04003 * Copyright (c) 2014-2022, 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
22#include "boost-test.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080023
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080024#include <algorithm>
Davide Pesaventobdd88c12020-11-26 00:35:08 -050025#include <boost/filesystem.hpp>
Shock Jiang3016c982014-11-11 11:35:17 -080026
Shock Jiang3016c982014-11-11 11:35:17 -080027namespace ndn {
28namespace ndns {
29namespace tests {
30
Shock Jiang3016c982014-11-11 11:35:17 -080031BOOST_AUTO_TEST_SUITE(DbMgr)
32
Davide Pesaventobdd88c12020-11-26 00:35:08 -050033const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
Shock Jiang3016c982014-11-11 11:35:17 -080034
35class DbMgrFixture
36{
37public:
38 DbMgrFixture()
39 : session(TEST_DATABASE2.string())
40 {
41 }
42
43 ~DbMgrFixture()
44 {
45 session.close();
Davide Pesaventobdd88c12020-11-26 00:35:08 -050046 boost::filesystem::remove(TEST_DATABASE2);
Shock Jiang3016c982014-11-11 11:35:17 -080047 }
48
49public:
50 ndns::DbMgr session;
51};
52
Shock Jiang3016c982014-11-11 11:35:17 -080053BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
54{
55 Zone zone1;
56 zone1.setName("/net");
57 zone1.setTtl(time::seconds(4600));
58 BOOST_CHECK_NO_THROW(session.insert(zone1));
59 BOOST_CHECK_GT(zone1.getId(), 0);
60
61 Zone zone2;
62 zone2.setName("/net");
63 session.find(zone2);
64 BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
65 BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
66
67 BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
68
69 zone2.setId(0);
70 BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
71
72 BOOST_CHECK_NO_THROW(session.remove(zone1));
73 BOOST_CHECK_EQUAL(zone1.getId(), 0);
74
75 // record shouldn't exist at this point
76 BOOST_CHECK_NO_THROW(session.find(zone2));
77 BOOST_CHECK_EQUAL(zone2.getId(), 0);
78}
79
Yumin Xia2c509c22017-02-09 14:37:36 -080080BOOST_FIXTURE_TEST_CASE(ZoneInfo, DbMgrFixture)
81{
82 Zone zone;
83 zone.setName("/net");
84 BOOST_CHECK_NO_THROW(session.insert(zone));
85
86 Name name1 = Name("/ndn/test");
87 Name name2 = Name("/ndn/zzzzz");
88
89 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "dsk", name1.wireEncode()));
90 BOOST_CHECK_NO_THROW(session.setZoneInfo(zone, "ksk", name2.wireEncode()));
91
92 std::map<std::string, Block> zoneInfo;
93 zoneInfo = session.getZoneInfo(zone);
94
95 BOOST_CHECK_EQUAL(Name(zoneInfo["dsk"]), name1);
96 BOOST_CHECK_EQUAL(Name(zoneInfo["ksk"]), name2);
97}
98
Shock Jiang3016c982014-11-11 11:35:17 -080099BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
100{
101 Zone zone("/net");
102 Rrset rrset1(&zone);
103
104 // Add
105
106 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800107 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800108 rrset1.setVersion(name::Component::fromVersion(567));
109 rrset1.setTtl(time::seconds(4600));
110
111 static const std::string DATA1 = "SOME DATA";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400112 rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA1));
Shock Jiang3016c982014-11-11 11:35:17 -0800113
114 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
115 BOOST_CHECK_NO_THROW(session.insert(rrset1));
116 BOOST_CHECK_GT(rrset1.getId(), 0);
117 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
118
119 // Lookup
120
121 Rrset rrset2(&zone);
122 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800123 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800124
125 bool isFound = false;
126 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
127 BOOST_CHECK_EQUAL(isFound, true);
128
129 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
130 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
131 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
132 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
133 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
134 BOOST_CHECK(rrset2.getData() == rrset1.getData());
135
136 // Replace
137
138 rrset1.setVersion(name::Component::fromVersion(890));
139 static const std::string DATA2 = "ANOTHER DATA";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400140 rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA2));
Shock Jiang3016c982014-11-11 11:35:17 -0800141
142 BOOST_CHECK_NO_THROW(session.update(rrset1));
143
144 rrset2 = Rrset(&zone);
145 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800146 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800147
148 isFound = false;
149 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
150 BOOST_CHECK_EQUAL(isFound, true);
151
152 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
153 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
154 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
155 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
156 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
157 BOOST_CHECK(rrset2.getData() == rrset1.getData());
158
159 // Remove
160
161 BOOST_CHECK_NO_THROW(session.remove(rrset1));
162
163 rrset2 = Rrset(&zone);
164 rrset2.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800165 rrset2.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800166
167 isFound = false;
168 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
169 BOOST_CHECK_EQUAL(isFound, false);
170
171 // Check error handling
172
173 rrset1 = Rrset();
174 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
175 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
176
177 rrset1.setId(1);
178 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
179
180 rrset1.setId(0);
181 rrset1.setZone(&zone);
182 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
183
184 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
185
186 rrset1.setId(1);
187 BOOST_CHECK_NO_THROW(session.remove(rrset1));
188
Davide Pesavento38fd3982022-04-18 22:22:02 -0400189 rrset1.setZone(nullptr);
Shock Jiang3016c982014-11-11 11:35:17 -0800190 rrset1.setId(1);
191 BOOST_CHECK_NO_THROW(session.remove(rrset1));
192}
193
Jiewen Tan870b29b2014-11-17 19:09:49 -0800194BOOST_FIXTURE_TEST_CASE(FindAllZones, DbMgrFixture)
195{
196 Zone zone("/ndn");
197 zone.setTtl(time::seconds(1600));
198 session.insert(zone);
199
200 Zone zone2("/ndn/ucla");
201 zone2.setTtl(time::seconds(2600));
202 session.insert(zone2);
203
204 std::vector<Zone> vec = session.listZones();
205 BOOST_CHECK_EQUAL(vec.size(), 2);
206
207 std::sort(vec.begin(),
208 vec.end(),
209 [] (const Zone& n1, const Zone& n2) {
210 return n1.getName().size() < n2.getName().size();
211 });
212 BOOST_CHECK_EQUAL(vec[0].getId(), zone.getId());
213 BOOST_CHECK_EQUAL(vec[0].getName(), "/ndn");
214 BOOST_CHECK_EQUAL(vec[0].getTtl(), time::seconds(1600));
215
216 BOOST_CHECK_EQUAL(vec[1].getId(), zone2.getId());
217 BOOST_CHECK_EQUAL(vec[1].getName(), "/ndn/ucla");
218 BOOST_CHECK_EQUAL(vec[1].getTtl(), time::seconds(2600));
219
220}
Shock Jiang3016c982014-11-11 11:35:17 -0800221
222BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
223{
224 Zone zone("/");
225 Rrset rrset1(&zone);
226 rrset1.setLabel("/net/ksk-123");
Yumin Xia2c509c22017-02-09 14:37:36 -0800227 rrset1.setType(name::Component("CERT"));
Shock Jiang3016c982014-11-11 11:35:17 -0800228 rrset1.setVersion(name::Component::fromVersion(567));
229 rrset1.setTtl(time::seconds(4600));
230
231 static const std::string DATA1 = "SOME DATA";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400232 rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA1));
Shock Jiang3016c982014-11-11 11:35:17 -0800233 session.insert(rrset1);
234
235 Rrset rrset2(&zone);
236 rrset2.setLabel("/net");
237 rrset2.setType(name::Component("NS"));
238 rrset2.setVersion(name::Component::fromVersion(232));
239 rrset2.setTtl(time::seconds(2100));
240 std::string data2 = "host1.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400241 rrset2.setData(makeStringBlock(ndn::tlv::Content, data2));
Shock Jiang3016c982014-11-11 11:35:17 -0800242 session.insert(rrset2);
243
244 std::vector<Rrset> vec = session.findRrsets(zone);
245 BOOST_CHECK_EQUAL(vec.size(), 2);
246
Davide Pesavento38fd3982022-04-18 22:22:02 -0400247 std::sort(vec.begin(), vec.end(),
Shock Jiang3016c982014-11-11 11:35:17 -0800248 [] (const Rrset& n1, const Rrset& n2) {
249 return n1.getLabel().size() < n2.getLabel().size();
250 });
251 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
252 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
253}
254
255BOOST_AUTO_TEST_SUITE_END()
256
257} // namespace tests
258} // namespace ndns
259} // namespace ndn