blob: 2428df3503a9e93155c5b32f03f1a745bf1c88cd [file] [log] [blame]
Yukai Tud96dfb82016-10-24 13:48:01 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017, Regents of the University of California.
4 *
5 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
6 *
7 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
19 */
20
21#include "object-db.hpp"
22
23#include "test-common.hpp"
24
25namespace ndn {
26namespace chronoshare {
27namespace tests {
28
29namespace fs = boost::filesystem;
30
31class TestObjectDbFixture : public IdentityManagementTimeFixture
32{
33public:
34 TestObjectDbFixture()
35 {
36 tmpdir = fs::unique_path(UNIT_TEST_CONFIG_PATH) / "TestObjectDb";
37 if (exists(tmpdir)) {
38 remove_all(tmpdir);
39 }
40 }
41
42public:
43 fs::path tmpdir;
44};
45
46BOOST_FIXTURE_TEST_SUITE(TestObjectDb, TestObjectDbFixture)
47
48BOOST_AUTO_TEST_CASE(Basic)
49{
50 BOOST_CHECK_EQUAL(ObjectDb::doesExist(tmpdir, "/my-device", "abcdef"), false);
51
52 Data data("/hello/world");
53 data.setContent(Name("/some/content").wireEncode());
54 m_keyChain.sign(data);
55
56 // Saving object
57 {
58 ObjectDb object(tmpdir, "abcdef");
59 BOOST_CHECK_EQUAL(object.getLastUsed(), time::steady_clock::now());
60
61 BOOST_CHECK_EQUAL(ObjectDb::doesExist(tmpdir, "/my-device", "abcdef"), false);
62
63 advanceClocks(time::hours(1));
64 BOOST_CHECK_NE(object.getLastUsed(), time::steady_clock::now());
65
66 object.saveContentObject("/my-device", 0, data);
67 }
68
69 // Retrieving object
70 {
71 ObjectDb object(tmpdir, "abcdef");
72
73 BOOST_CHECK_EQUAL(ObjectDb::doesExist(tmpdir, "/my-device", "abcdef"), true);
74 BOOST_CHECK(exists(tmpdir / "objects" / "ab" / "cdef"));
75 BOOST_CHECK_EQUAL(object.getLastUsed(), time::steady_clock::now());
76
77 advanceClocks(time::hours(1));
78 BOOST_CHECK_NE(object.getLastUsed(), time::steady_clock::now());
79
80 BOOST_CHECK(object.fetchSegment("/my-device", 0) != nullptr);
81 auto retrievedData = object.fetchSegment("/my-device", 0);
82 BOOST_REQUIRE(retrievedData != nullptr);
83 BOOST_CHECK_EQUAL(*retrievedData, data);
84 BOOST_CHECK_EQUAL(object.getLastUsed(), time::steady_clock::now());
85
86 advanceClocks(time::hours(1));
87 BOOST_CHECK_NE(object.getLastUsed(), time::steady_clock::now());
88
89 BOOST_CHECK(object.fetchSegment("/my-device", 1) == nullptr);
90 BOOST_CHECK_EQUAL(object.getLastUsed(), time::steady_clock::now());
91 }
92}
93
94BOOST_AUTO_TEST_SUITE_END()
95
96} // namespace tests
97} // namespace chronoshare
98} // namespace ndn