blob: 09fdc9f7a33a306f2b320481e60504116fb6e70d [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
Shock Jiang3016c982014-11-11 11:35:17 -080020#include "daemon/db-mgr.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080021
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080022#include <algorithm>
Shock Jiang3016c982014-11-11 11:35:17 -080023
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080024#include "test-common.hpp"
Shock Jiang3016c982014-11-11 11:35:17 -080025
26namespace ndn {
27namespace ndns {
28namespace tests {
29
30NDNS_LOG_INIT("DbMgrTest")
31
32BOOST_AUTO_TEST_SUITE(DbMgr)
33
34static const boost::filesystem::path TEST_DATABASE2 = TEST_CONFIG_PATH "/" "test-ndns.db";
35
36class DbMgrFixture
37{
38public:
39 DbMgrFixture()
40 : session(TEST_DATABASE2.string())
41 {
42 }
43
44 ~DbMgrFixture()
45 {
46 session.close();
47 boost::filesystem::remove(TEST_DATABASE2);
48 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
49 }
50
51public:
52 ndns::DbMgr session;
53};
54
55
56
57BOOST_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
84BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
85{
86 Zone zone("/net");
87 Rrset rrset1(&zone);
88
89 // Add
90
91 rrset1.setLabel("/net/ksk-123");
92 rrset1.setType(name::Component("ID-CERT"));
93 rrset1.setVersion(name::Component::fromVersion(567));
94 rrset1.setTtl(time::seconds(4600));
95
96 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +000097 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -080098
99 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
100 BOOST_CHECK_NO_THROW(session.insert(rrset1));
101 BOOST_CHECK_GT(rrset1.getId(), 0);
102 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
103
104 // Lookup
105
106 Rrset rrset2(&zone);
107 rrset2.setLabel("/net/ksk-123");
108 rrset2.setType(name::Component("ID-CERT"));
109
110 bool isFound = false;
111 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
112 BOOST_CHECK_EQUAL(isFound, true);
113
114 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
115 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
116 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
117 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
118 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
119 BOOST_CHECK(rrset2.getData() == rrset1.getData());
120
121 // Replace
122
123 rrset1.setVersion(name::Component::fromVersion(890));
124 static const std::string DATA2 = "ANOTHER DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000125 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800126
127 BOOST_CHECK_NO_THROW(session.update(rrset1));
128
129 rrset2 = Rrset(&zone);
130 rrset2.setLabel("/net/ksk-123");
131 rrset2.setType(name::Component("ID-CERT"));
132
133 isFound = false;
134 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
135 BOOST_CHECK_EQUAL(isFound, true);
136
137 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
138 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
139 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
140 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
141 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
142 BOOST_CHECK(rrset2.getData() == rrset1.getData());
143
144 // Remove
145
146 BOOST_CHECK_NO_THROW(session.remove(rrset1));
147
148 rrset2 = Rrset(&zone);
149 rrset2.setLabel("/net/ksk-123");
150 rrset2.setType(name::Component("ID-CERT"));
151
152 isFound = false;
153 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
154 BOOST_CHECK_EQUAL(isFound, false);
155
156 // Check error handling
157
158 rrset1 = Rrset();
159 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
160 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
161
162 rrset1.setId(1);
163 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
164
165 rrset1.setId(0);
166 rrset1.setZone(&zone);
167 BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
168
169 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
170
171 rrset1.setId(1);
172 BOOST_CHECK_NO_THROW(session.remove(rrset1));
173
174 rrset1.setZone(0);
175 rrset1.setId(1);
176 BOOST_CHECK_NO_THROW(session.remove(rrset1));
177}
178
Jiewen Tan870b29b2014-11-17 19:09:49 -0800179BOOST_FIXTURE_TEST_CASE(FindAllZones, DbMgrFixture)
180{
181 Zone zone("/ndn");
182 zone.setTtl(time::seconds(1600));
183 session.insert(zone);
184
185 Zone zone2("/ndn/ucla");
186 zone2.setTtl(time::seconds(2600));
187 session.insert(zone2);
188
189 std::vector<Zone> vec = session.listZones();
190 BOOST_CHECK_EQUAL(vec.size(), 2);
191
192 std::sort(vec.begin(),
193 vec.end(),
194 [] (const Zone& n1, const Zone& n2) {
195 return n1.getName().size() < n2.getName().size();
196 });
197 BOOST_CHECK_EQUAL(vec[0].getId(), zone.getId());
198 BOOST_CHECK_EQUAL(vec[0].getName(), "/ndn");
199 BOOST_CHECK_EQUAL(vec[0].getTtl(), time::seconds(1600));
200
201 BOOST_CHECK_EQUAL(vec[1].getId(), zone2.getId());
202 BOOST_CHECK_EQUAL(vec[1].getName(), "/ndn/ucla");
203 BOOST_CHECK_EQUAL(vec[1].getTtl(), time::seconds(2600));
204
205}
Shock Jiang3016c982014-11-11 11:35:17 -0800206
207BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
208{
209 Zone zone("/");
210 Rrset rrset1(&zone);
211 rrset1.setLabel("/net/ksk-123");
212 rrset1.setType(name::Component("ID-CERT"));
213 rrset1.setVersion(name::Component::fromVersion(567));
214 rrset1.setTtl(time::seconds(4600));
215
216 static const std::string DATA1 = "SOME DATA";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000217 rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800218 session.insert(rrset1);
219
220 Rrset rrset2(&zone);
221 rrset2.setLabel("/net");
222 rrset2.setType(name::Component("NS"));
223 rrset2.setVersion(name::Component::fromVersion(232));
224 rrset2.setTtl(time::seconds(2100));
225 std::string data2 = "host1.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000226 rrset2.setData(makeBinaryBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800227 session.insert(rrset2);
228
229 std::vector<Rrset> vec = session.findRrsets(zone);
230 BOOST_CHECK_EQUAL(vec.size(), 2);
231
232 std::sort(vec.begin(),
233 vec.end(),
234 [] (const Rrset& n1, const Rrset& n2) {
235 return n1.getLabel().size() < n2.getLabel().size();
236 });
237 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
238 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
239}
240
241BOOST_AUTO_TEST_SUITE_END()
242
243} // namespace tests
244} // namespace ndns
245} // namespace ndn