blob: 4116ffb811818401fc2b8e2101e9037d9cacb191 [file] [log] [blame]
Shock Jiang4e0ab7c2014-09-11 16:23:21 -07001/* -*- 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 "db-mgr.hpp"
21#include "../boost-test.hpp"
22
23#include <boost/filesystem.hpp>
24
25namespace ndn {
26namespace ndns {
27namespace tests {
28
29BOOST_AUTO_TEST_SUITE(DbMgr)
30
31static const boost::filesystem::path TEST_DATABASE = BUILDDIR "/tests/unit/db-mgr-ndns.db";
32
33class DbMgrFixture
34{
35public:
36 DbMgrFixture()
37 : session(TEST_DATABASE.string().c_str())
38 {
39 }
40
41 ~DbMgrFixture()
42 {
43 session.close();
44 boost::filesystem::remove(TEST_DATABASE);
45 }
46
47public:
48 ndns::DbMgr session;
49};
50
51
52BOOST_FIXTURE_TEST_CASE(Basic, DbMgrFixture)
53{
54 BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED);
55
56 session.close();
57 BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CLOSED);
58
59 // reopen
60 session.open();
61 BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED);
62}
63
64BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
65{
66 Zone zone1;
67 zone1.setName("/net");
68 zone1.setTtl(time::seconds(4600));
69 BOOST_CHECK_NO_THROW(session.insert(zone1));
70 BOOST_CHECK_GT(zone1.getId(), 0);
71
72 Zone zone2;
73 zone2.setName("/net");
Shock Jiangbcdef862014-10-07 14:13:55 -070074 session.find(zone2);
Shock Jiang4e0ab7c2014-09-11 16:23:21 -070075 BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
76 BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
77
78 BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
79
80 zone2.setId(0);
81 BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
82
83 BOOST_CHECK_NO_THROW(session.remove(zone1));
84 BOOST_CHECK_EQUAL(zone1.getId(), 0);
85
86 // record shouldn't exist at this point
Shock Jiangbcdef862014-10-07 14:13:55 -070087 BOOST_CHECK_NO_THROW(session.find(zone2));
Shock Jiang4e0ab7c2014-09-11 16:23:21 -070088 BOOST_CHECK_EQUAL(zone2.getId(), 0);
89}
90
Shock Jiangbcdef862014-10-07 14:13:55 -070091BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
92{
93 Zone zone("/net");
94 Rrset rrset1(&zone);
95
96 // Add
97
98 rrset1.setLabel("/net/ksk-123");
99 rrset1.setType(name::Component("ID-CERT"));
100 rrset1.setVersion(name::Component::fromVersion(567));
101 rrset1.setTtl(time::seconds(4600));
102
103 static const std::string DATA1 = "SOME DATA";
104 rrset1.setData(dataBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
105
106 BOOST_CHECK_EQUAL(rrset1.getId(), 0);
107 BOOST_CHECK_NO_THROW(session.insert(rrset1));
108 BOOST_CHECK_GT(rrset1.getId(), 0);
109 BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
110
111 // Lookup
112
113 Rrset rrset2(&zone);
114 rrset2.setLabel("/net/ksk-123");
115 rrset2.setType(name::Component("ID-CERT"));
116
117 bool isFound = false;
118 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
119 BOOST_CHECK_EQUAL(isFound, true);
120
121 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
122 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
123 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
124 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
125 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
126 BOOST_CHECK(rrset2.getData() == rrset1.getData());
127
128 // Replace
129
130 rrset1.setVersion(name::Component::fromVersion(890));
131 static const std::string DATA2 = "ANOTHER DATA";
132 rrset1.setData(dataBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
133
134 BOOST_CHECK_NO_THROW(session.modify(rrset1));
135
136 rrset2 = Rrset(&zone);
137 rrset2.setLabel("/net/ksk-123");
138 rrset2.setType(name::Component("ID-CERT"));
139
140 isFound = false;
141 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
142 BOOST_CHECK_EQUAL(isFound, true);
143
144 BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
145 BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
146 BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
147 BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
148 BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
149 BOOST_CHECK(rrset2.getData() == rrset1.getData());
150
151 // Remove
152
153 BOOST_CHECK_NO_THROW(session.remove(rrset1));
154
155 rrset2 = Rrset(&zone);
156 rrset2.setLabel("/net/ksk-123");
157 rrset2.setType(name::Component("ID-CERT"));
158
159 isFound = false;
160 BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
161 BOOST_CHECK_EQUAL(isFound, false);
162
163 // Check error handling
164
165 rrset1 = Rrset();
166 BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
167 BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
168
169 rrset1.setId(1);
170 BOOST_CHECK_THROW(session.modify(rrset1), ndns::DbMgr::RrsetError);
171
172 rrset1.setId(0);
173 rrset1.setZone(&zone);
174 BOOST_CHECK_THROW(session.modify(rrset1), ndns::DbMgr::RrsetError);
175
176 BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
177
178 rrset1.setId(1);
179 BOOST_CHECK_NO_THROW(session.remove(rrset1));
180
181 rrset1.setZone(0);
182 rrset1.setId(1);
183 BOOST_CHECK_NO_THROW(session.remove(rrset1));
184}
185
Shock Jiang4e0ab7c2014-09-11 16:23:21 -0700186BOOST_AUTO_TEST_SUITE_END()
187
188} // namespace tests
189} // namespace ndns
190} // namespace ndn