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 | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
| 22 | #include "boost-test.hpp" |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 24 | #include <algorithm> |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 25 | #include <boost/filesystem.hpp> |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 26 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace ndns { |
| 29 | namespace tests { |
| 30 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(DbMgr) |
| 32 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 33 | 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] | 34 | |
| 35 | class DbMgrFixture |
| 36 | { |
| 37 | public: |
| 38 | DbMgrFixture() |
| 39 | : session(TEST_DATABASE2.string()) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | ~DbMgrFixture() |
| 44 | { |
| 45 | session.close(); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 46 | boost::filesystem::remove(TEST_DATABASE2); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public: |
| 50 | ndns::DbMgr session; |
| 51 | }; |
| 52 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 53 | BOOST_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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 80 | BOOST_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 Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 99 | BOOST_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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 107 | rrset1.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 108 | rrset1.setVersion(name::Component::fromVersion(567)); |
| 109 | rrset1.setTtl(time::seconds(4600)); |
| 110 | |
| 111 | static const std::string DATA1 = "SOME DATA"; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 112 | rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA1)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 113 | |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 123 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 124 | |
| 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 Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 140 | rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA2)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 141 | |
| 142 | BOOST_CHECK_NO_THROW(session.update(rrset1)); |
| 143 | |
| 144 | rrset2 = Rrset(&zone); |
| 145 | rrset2.setLabel("/net/ksk-123"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 146 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 147 | |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 165 | rrset2.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 166 | |
| 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 Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 189 | rrset1.setZone(nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 190 | rrset1.setId(1); |
| 191 | BOOST_CHECK_NO_THROW(session.remove(rrset1)); |
| 192 | } |
| 193 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 194 | BOOST_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 Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 221 | |
| 222 | BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture) |
| 223 | { |
| 224 | Zone zone("/"); |
| 225 | Rrset rrset1(&zone); |
| 226 | rrset1.setLabel("/net/ksk-123"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 227 | rrset1.setType(name::Component("CERT")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 228 | rrset1.setVersion(name::Component::fromVersion(567)); |
| 229 | rrset1.setTtl(time::seconds(4600)); |
| 230 | |
| 231 | static const std::string DATA1 = "SOME DATA"; |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 232 | rrset1.setData(makeStringBlock(ndn::tlv::Content, DATA1)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 233 | 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 Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 241 | rrset2.setData(makeStringBlock(ndn::tlv::Content, data2)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 242 | session.insert(rrset2); |
| 243 | |
| 244 | std::vector<Rrset> vec = session.findRrsets(zone); |
| 245 | BOOST_CHECK_EQUAL(vec.size(), 2); |
| 246 | |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 247 | std::sort(vec.begin(), vec.end(), |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 248 | [] (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 | |
| 255 | BOOST_AUTO_TEST_SUITE_END() |
| 256 | |
| 257 | } // namespace tests |
| 258 | } // namespace ndns |
| 259 | } // namespace ndn |