blob: af5a879757cc5fb74e16c5d006a34c1f2172c071 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "table/strategy-choice.hpp"
8#include "fw/forwarder.hpp"
9
10#include "tests/test-common.hpp"
11
12namespace nfd {
13namespace tests {
14
15BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture)
16
17using fw::Strategy;
18
19class StrategyChoiceTestDummyStrategy : public Strategy
20{
21public:
22 StrategyChoiceTestDummyStrategy(Forwarder& forwarder, const Name& name)
23 : Strategy(forwarder, name)
24 {
25 }
26
27 virtual
28 ~StrategyChoiceTestDummyStrategy()
29 {
30 }
31
32 virtual void
33 afterReceiveInterest(const Face& inFace,
34 const Interest& interest,
35 shared_ptr<fib::Entry> fibEntry,
36 shared_ptr<pit::Entry> pitEntry)
37 {
38 }
39};
40
41BOOST_AUTO_TEST_CASE(Effective)
42{
43 Forwarder forwarder;
44 Name nameP("ndn:/strategy/P");
45 Name nameQ("ndn:/strategy/Q");
46 Name nameZ("ndn:/strategy/Z");
47 shared_ptr<Strategy> strategyP = make_shared<StrategyChoiceTestDummyStrategy>(
48 boost::ref(forwarder), nameP);
49 shared_ptr<Strategy> strategyQ = make_shared<StrategyChoiceTestDummyStrategy>(
50 boost::ref(forwarder), nameQ);
51
52 StrategyChoice& table = forwarder.getStrategyChoice();
53
54 // install
55 BOOST_CHECK_EQUAL(table.install(strategyP), true);
56 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
57 BOOST_CHECK_EQUAL(table.install(strategyQ), false);
58
59 BOOST_CHECK(table.insert("ndn:/", "ndn:/strategy/P"));
60 // { '/'=>P }
61
62 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
63 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
64 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
65
66 BOOST_CHECK(table.insert("ndn:/A/B", "ndn:/strategy/P"));
67 // { '/'=>P, '/A/B'=>P }
68
69 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
70 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
71 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
72 // same instance
73 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get());
74 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get());
75 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
76
77 table.erase("ndn:/A"); // no effect
78 // { '/'=>P, '/A/B'=>P }
79
80 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
81 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
82 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
83
84 BOOST_CHECK(table.insert("ndn:/A", "ndn:/strategy/Q"));
85 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
86
87 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
88 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
89 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
90
91 table.erase("ndn:/A/B");
92 // { '/'=>P, '/A'=>Q }
93
94 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
95 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
96 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
97
98 BOOST_CHECK(!table.insert("ndn:/", "ndn:/strategy/Z")); // non existent strategy
99
100 BOOST_CHECK(table.insert("ndn:/", "ndn:/strategy/Q"));
101 BOOST_CHECK(table.insert("ndn:/A", "ndn:/strategy/P"));
102 // { '/'=>Q, '/A'=>P }
103
104 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
105 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
106 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
107 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
108}
109
110BOOST_AUTO_TEST_SUITE_END()
111
112} // namespace tests
113} // namespace nfd