blob: fb621d2c4c9b13f981fad41e7ba2439a71d37fd9 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
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";
100 rrset1.setData(dataBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
101
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";
128 rrset1.setData(dataBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
129
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
182
183BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
184{
185 Zone zone("/");
186 Rrset rrset1(&zone);
187 rrset1.setLabel("/net/ksk-123");
188 rrset1.setType(name::Component("ID-CERT"));
189 rrset1.setVersion(name::Component::fromVersion(567));
190 rrset1.setTtl(time::seconds(4600));
191
192 static const std::string DATA1 = "SOME DATA";
193 rrset1.setData(dataBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
194 session.insert(rrset1);
195
196 Rrset rrset2(&zone);
197 rrset2.setLabel("/net");
198 rrset2.setType(name::Component("NS"));
199 rrset2.setVersion(name::Component::fromVersion(232));
200 rrset2.setTtl(time::seconds(2100));
201 std::string data2 = "host1.net";
202 rrset2.setData(dataBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
203 session.insert(rrset2);
204
205 std::vector<Rrset> vec = session.findRrsets(zone);
206 BOOST_CHECK_EQUAL(vec.size(), 2);
207
208 std::sort(vec.begin(),
209 vec.end(),
210 [] (const Rrset& n1, const Rrset& n2) {
211 return n1.getLabel().size() < n2.getLabel().size();
212 });
213 BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
214 BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
215}
216
217BOOST_AUTO_TEST_SUITE_END()
218
219} // namespace tests
220} // namespace ndns
221} // namespace ndn