Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 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 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 20 | #include "daemon/db-mgr.hpp" |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 21 | #include "logger.hpp" |
| 22 | |
| 23 | #include "boost-test.hpp" |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 25 | #include <algorithm> |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 26 | #include <boost/filesystem.hpp> |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 27 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | namespace tests { |
| 31 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 32 | NDNS_LOG_INIT(DbMgrTest); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(DbMgr) |
| 35 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 36 | const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 37 | |
| 38 | class DbMgrFixture |
| 39 | { |
| 40 | public: |
| 41 | DbMgrFixture() |
| 42 | : session(TEST_DATABASE2.string()) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | ~DbMgrFixture() |
| 47 | { |
| 48 | session.close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 49 | NDNS_LOG_INFO("remove database " << TEST_DATABASE2); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 50 | boost::filesystem::remove(TEST_DATABASE2); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public: |
| 54 | ndns::DbMgr session; |
| 55 | }; |
| 56 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 57 | BOOST_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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 84 | BOOST_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 Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 103 | BOOST_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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 111 | rrset1.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 112 | rrset1.setVersion(name::Component::fromVersion(567)); |
| 113 | rrset1.setTtl(time::seconds(4600)); |
| 114 | |
| 115 | static const std::string DATA1 = "SOME DATA"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 116 | rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size())); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 117 | |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 127 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 128 | |
| 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 144 | rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size())); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 145 | |
| 146 | BOOST_CHECK_NO_THROW(session.update(rrset1)); |
| 147 | |
| 148 | rrset2 = Rrset(&zone); |
| 149 | rrset2.setLabel("/net/ksk-123"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 150 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 151 | |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 169 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 170 | |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 198 | BOOST_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 Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 225 | |
| 226 | BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture) |
| 227 | { |
| 228 | Zone zone("/"); |
| 229 | Rrset rrset1(&zone); |
| 230 | rrset1.setLabel("/net/ksk-123"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 231 | rrset1.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 232 | rrset1.setVersion(name::Component::fromVersion(567)); |
| 233 | rrset1.setTtl(time::seconds(4600)); |
| 234 | |
| 235 | static const std::string DATA1 = "SOME DATA"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 236 | rrset1.setData(makeBinaryBlock(ndn::tlv::Content, DATA1.data(), DATA1.size())); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 237 | 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 Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 245 | rrset2.setData(makeBinaryBlock(ndn::tlv::Content, data2.c_str(), data2.size())); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 246 | 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 | |
| 260 | BOOST_AUTO_TEST_SUITE_END() |
| 261 | |
| 262 | } // namespace tests |
| 263 | } // namespace ndns |
| 264 | } // namespace ndn |