blob: c98df6fee57ad1b3c60b121f8a7c67d9137d1b3e [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080011 */
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "face.hpp"
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080014#include "util/scheduler.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080015
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070016#include "boost-test.hpp"
17
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080018namespace ndn {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080019
20BOOST_AUTO_TEST_SUITE(TestFaces)
21
22struct FacesFixture
23{
24 FacesFixture()
25 : dataCount(0)
26 , timeoutCount(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080027 , regPrefixId(0)
28 , inInterestCount(0)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080029 , inInterestCount2(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080030 , regFailedCount(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080031 {
32 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080034 void
35 onData()
36 {
37 ++dataCount;
38 }
39
40 void
41 onTimeout()
42 {
43 ++timeoutCount;
44 }
45
46 void
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080047 onInterest(Face& face)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080048 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080049 ++inInterestCount;
50
51 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080052 }
53
54 void
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080055 onInterest2(Face& face)
56 {
57 ++inInterestCount2;
58
59 face.unsetInterestFilter(regPrefixId2);
60 }
61
62 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080063 onRegFailed()
64 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080065 ++regFailedCount;
66 }
67
68 void
69 expressInterest(Face& face, const Name& name)
70 {
71 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080073 face.expressInterest(i,
74 bind(&FacesFixture::onData, this),
75 bind(&FacesFixture::onTimeout, this));
76 }
77
78 void
79 terminate(Face& face)
80 {
81 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080082 }
83
84 uint32_t dataCount;
85 uint32_t timeoutCount;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080086
87 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080088 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080089 uint32_t inInterestCount;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080090 uint32_t inInterestCount2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080091 uint32_t regFailedCount;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080092};
93
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070094BOOST_FIXTURE_TEST_CASE(Unix, FacesFixture)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080095{
96 Face face;
97
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070098 face.expressInterest(Interest("/", time::milliseconds(1000)),
99 bind(&FacesFixture::onData, this),
100 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800101
102 BOOST_REQUIRE_NO_THROW(face.processEvents());
103
104 BOOST_CHECK_EQUAL(dataCount, 1);
105 BOOST_CHECK_EQUAL(timeoutCount, 0);
106
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700107 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
108 time::milliseconds(50)),
109 bind(&FacesFixture::onData, this),
110 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700111
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800112 BOOST_REQUIRE_NO_THROW(face.processEvents());
113
114 BOOST_CHECK_EQUAL(dataCount, 1);
115 BOOST_CHECK_EQUAL(timeoutCount, 1);
116}
117
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700118BOOST_FIXTURE_TEST_CASE(Tcp, FacesFixture)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800119{
120 Face face("localhost");
121
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700122 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800123 bind(&FacesFixture::onData, this),
124 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800125
126 BOOST_REQUIRE_NO_THROW(face.processEvents());
127
128 BOOST_CHECK_EQUAL(dataCount, 1);
129 BOOST_CHECK_EQUAL(timeoutCount, 0);
130
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700131 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
132 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800133 bind(&FacesFixture::onData, this),
134 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800135
136 BOOST_REQUIRE_NO_THROW(face.processEvents());
137
138 BOOST_CHECK_EQUAL(dataCount, 1);
139 BOOST_CHECK_EQUAL(timeoutCount, 1);
140}
141
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800142
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700143BOOST_FIXTURE_TEST_CASE(SetFilter, FacesFixture)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800144{
145 Face face;
146 Face face2(face.ioService());
147 Scheduler scheduler(*face.ioService());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148 scheduler.scheduleEvent(time::milliseconds(300),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700149 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700150
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800151 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700152 bind(&FacesFixture::onInterest, this, ref(face)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800153 bind(&FacesFixture::onRegFailed, this));
154
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700155 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800156 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700157 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700158
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800159 BOOST_REQUIRE_NO_THROW(face.processEvents());
160
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700161 BOOST_CHECK_EQUAL(regFailedCount, 0);
162 BOOST_CHECK_EQUAL(inInterestCount, 1);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800163 BOOST_CHECK_EQUAL(timeoutCount, 1);
164 BOOST_CHECK_EQUAL(dataCount, 0);
165}
166
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700167BOOST_FIXTURE_TEST_CASE(SetTwoFilters, FacesFixture)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800168{
169 Face face;
170 Face face2(face.ioService());
171 Scheduler scheduler(*face.ioService());
Obaid6e7f5f12014-03-11 14:46:10 -0500172 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700173 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700174
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800175 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700176 bind(&FacesFixture::onInterest, this, ref(face)),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800177 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700178
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800179 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700180 bind(&FacesFixture::onInterest2, this, ref(face)),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800181 bind(&FacesFixture::onRegFailed, this));
182
183
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700184 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800185 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700186 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700187
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800188 BOOST_REQUIRE_NO_THROW(face.processEvents());
189
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700190 BOOST_CHECK_EQUAL(regFailedCount, 0);
191 BOOST_CHECK_EQUAL(inInterestCount, 1);
192 BOOST_CHECK_EQUAL(inInterestCount2, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800193 BOOST_CHECK_EQUAL(timeoutCount, 1);
194 BOOST_CHECK_EQUAL(dataCount, 0);
195}
196
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800197BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800198
199} // namespace ndn