/[resiprocate]/main/resip/stack/test/testConnectionBase.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testConnectionBase.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10222 - (show annotations) (download)
Tue May 28 12:56:06 2013 UTC (6 years, 5 months ago) by Dpocock
File MIME type: text/plain
File size: 9722 byte(s)
resip/stack: testConnectionBase refactoring to handle WebSockets, contributed by Ng Zhi An
1 #include <iostream>
2 #include <memory>
3
4 #include "rutil/DataStream.hxx"
5 #include "resip/stack/SipMessage.hxx"
6 #include "resip/stack/ConnectionBase.hxx"
7
8 #include "resip/stack/Helper.hxx"
9 #include "resip/stack/Uri.hxx"
10 #include "resip/stack/Helper.hxx"
11 #include "resip/stack/Transport.hxx"
12 #include "resip/stack/SdpContents.hxx"
13 #include "resip/stack/test/TestSupport.hxx"
14 #include "resip/stack/PlainContents.hxx"
15 #include "resip/stack/UnknownHeaderType.hxx"
16 #include "resip/stack/UnknownParameterType.hxx"
17 #include "rutil/Logger.hxx"
18 #include "rutil/ParseBuffer.hxx"
19 #include "rutil/Random.hxx"
20
21
22 using namespace resip;
23 using namespace std;
24
25 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
26 #define CRLF "\r\n"
27
28 class FakeTCPTransport : public Transport
29 {
30 public:
31 FakeTCPTransport(Fifo<TransactionMessage>& rxFifo,
32 int portNum,
33 IpVersion version,
34 const Data& interfaceObj,
35 const Data& tlsDomain = Data::Empty) :
36 Transport(rxFifo, portNum, version, interfaceObj, tlsDomain)
37 {}
38
39 virtual TransportType transport() const { return TCP; }
40 virtual bool isFinished() const { assert(0); return true; }
41 virtual void process(FdSet& fdset) { assert(0); }
42 virtual void process() { assert(0); }
43 virtual void processTransmitQueue() { assert(0); }
44 virtual void buildFdSet( FdSet& fdset) { assert(0); }
45
46 virtual bool isReliable() const { assert(0); return true; }
47 virtual bool isDatagram() const { assert(0); return true; }
48 virtual bool shareStackProcessAndSelect() const { assert(0); return true;}
49 virtual void startOwnProcessing() { assert(0); }
50 virtual bool hasDataToSend() const { assert(0); return false; }
51 virtual void setPollGrp(FdPollGrp* grp) { assert(0); }
52 virtual unsigned int getFifoSize() const { assert(0); return 0; }
53
54 virtual void send(std::auto_ptr<SendData> data) { assert(0); }
55 void flush() {mStateMachineFifo.flush();}
56 };
57
58 class FakeWSTransport : public Transport
59 {
60 public:
61 FakeWSTransport(Fifo<TransactionMessage>& rxFifo,
62 int portNum,
63 IpVersion version,
64 const Data& interfaceObj,
65 const Data& tlsDomain = Data::Empty) :
66 Transport(rxFifo, portNum, version, interfaceObj, tlsDomain)
67 {}
68
69 virtual TransportType transport() const { return WS; }
70 virtual bool isFinished() const { assert(0); return true; }
71 virtual void process(FdSet& fdset) { assert(0); }
72 virtual void process() { assert(0); }
73 virtual void processTransmitQueue() { assert(0); }
74 virtual void buildFdSet( FdSet& fdset) { assert(0); }
75
76 virtual bool isReliable() const { assert(0); return true; }
77 virtual bool isDatagram() const { assert(0); return true; }
78 virtual bool shareStackProcessAndSelect() const { assert(0); return true;}
79 virtual void startOwnProcessing() { assert(0); }
80 virtual bool hasDataToSend() const { assert(0); return false; }
81 virtual void setPollGrp(FdPollGrp* grp) { assert(0); }
82 virtual unsigned int getFifoSize() const { assert(0); return 0; }
83
84 virtual void send(std::auto_ptr<SendData> data) { assert(0); }
85 void flush() { mStateMachineFifo.flush(); }
86 };
87
88 class TestConnection : public ConnectionBase
89 {
90 public:
91 TestConnection(Transport* transport,const Tuple& who, const Data& bytes) :
92 ConnectionBase(transport,who),
93 mTestStream(bytes),
94 mStreamPos(0)
95 {}
96
97 bool read(unsigned int minChunkSize, unsigned int maxChunkSize)
98 {
99
100 unsigned int chunk = chooseChunkSize(minChunkSize, maxChunkSize);
101 assert(chunk > 0);
102 std::pair<char*, size_t> writePair = getWriteBuffer();
103 memcpy(writePair.first, mTestStream.data() + mStreamPos, chunk);
104 mStreamPos += chunk;
105 assert(mStreamPos <= mTestStream.size());
106 preparseNewBytes(chunk);
107 return mStreamPos != mTestStream.size();
108 }
109
110 private:
111 unsigned int chooseChunkSize(unsigned int min, unsigned int max)
112 {
113 unsigned int chunk = Random::getRandom() % max;
114 chunk = resipMax(chunk, min);
115 chunk = resipMin(chunk, max);
116 unsigned int chunkPos = mTestStream.size() - mStreamPos;
117 chunk = resipMin(chunk, chunkPos);
118 return chunk;
119 }
120 Data mTestStream;
121 unsigned int mStreamPos;
122 // Fifo<TransactionMessage>& mRxFifo;
123 };
124
125 bool
126 testTCPConnection()
127 {
128 Data bytes("INVITE sip:192.168.2.92:5100;q=1 SIP/2.0\r\n"
129 "To: <sip:yiwen_AT_meet2talk.com@whistler.gloo.net>\r\n"
130 "From: Jason Fischl<sip:jason_AT_meet2talk.com@whistler.gloo.net>;tag=ba1aee2d\r\n"
131 "Via: SIP/2.0/UDP 192.168.2.220:5060;branch=z9hG4bK-c87542-da4d3e6a.0-1--c87542-;rport=5060;received=192.168.2.220;stid=579667358\r\n"
132 "Via: SIP/2.0/UDP 192.168.2.15:5100;branch=z9hG4bK-c87542-579667358-1--c87542-;rport=5100;received=192.168.2.15\r\n"
133 "Call-ID: 6c64b42fce01b007\r\n"
134 "CSeq: 2 INVITE\r\n"
135 "Route: <sip:proxy@192.168.2.220:5060;lr>\r\n"
136 "Contact: <sip:192.168.2.15:5100>\r\n"
137 "Content-Length: 0\r\n"
138 "\r\n"
139 "\r\n\r\n" //keepalive
140 "INVITE sip:192.168.2.92:5100;q=1 SIP/2.0\r\n"
141 "To: <sip:yiwen_AT_meet2talk.com@whistler.gloo.net>\r\n"
142 "From: Jason Fischl<sip:jason_AT_meet2talk.com@whistler.gloo.net>;tag=ba1aee2d\r\n"
143 "Via: SIP/2.0/UDP 192.168.2.220:5060;branch=z9hG4bK-c87542-da4d3e6a.0-1--c87542-;rport=5060;received=192.168.2.220;stid=579667358\r\n"
144 "Via: SIP/2.0/UDP 192.168.2.15:5100;branch=z9hG4bK-c87542-579667358-1--c87542-;rport=5100;received=192.168.2.15\r\n"
145 "Call-ID: 6c64b42fce01b007\r\n"
146 "CSeq: 2 INVITE\r\n"
147 "Record-Route: <sip:proxy@192.168.2.220:5060;lr>\r\n"
148 "Contact: <sip:192.168.2.15:5100>\r\n"
149 "Max-Forwards: 69\r\n"
150 "Content-Type: application/sdp\r\n"
151 "Content-Length: 307\r\n"
152 "\r\n"
153 "v=0\r\n"
154 "o=M2TUA 1589993278 1032390928 IN IP4 192.168.2.15\r\n"
155 "s=-\r\n"
156 "c=IN IP4 192.168.2.15\r\n"
157 "t=0 0\r\n"
158 "m=audio 9000 RTP/AVP 103 97 100 101 0 8 102\r\n"
159 "a=rtpmap:103 ISAC/16000\r\n"
160 "a=rtpmap:97 IPCMWB/16000\r\n"
161 "a=rtpmap:100 EG711U/8000\r\n"
162 "a=rtpmap:101 EG711A/8000\r\n"
163 "a=rtpmap:0 PCMU/8000\r\n"
164 "a=rtpmap:8 PCMA/8000\r\n"
165 "a=rtpmap:102 iLBC/8000\r\n");
166
167 Fifo<TransactionMessage> testRxFifo;
168 FakeTCPTransport fake(testRxFifo, 5060, V4, Data::Empty);
169 Tuple who(fake.getTuple());
170
171 int chunkRange = 700;
172 unsigned int runs = 100;
173
174 for (unsigned int i=0; i < runs; i++)
175 {
176 TestConnection cBase(&fake,who, bytes);
177 int minChunk = (Random::getRandom() % chunkRange)+1;
178 int maxChunk = (Random::getRandom() % chunkRange)+1;
179 if (maxChunk < minChunk) swap(maxChunk, minChunk);
180 while(cBase.read(minChunk, maxChunk));
181 }
182 fake.flush();
183 return testRxFifo.size() == runs * 3;
184 }
185 int
186 main(int argc, char** argv)
187 {
188 Log::initialize(Log::Cout, Log::Info, argv[0]);
189
190 assert(testTCPConnection());
191 cerr << "testTCPConnection OK" << endl;
192
193 cerr << "ALL OK" << endl;
194 return 0;
195 }
196
197 /* ====================================================================
198 * The Vovida Software License, Version 1.0
199 *
200 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
201 *
202 * Redistribution and use in source and binary forms, with or without
203 * modification, are permitted provided that the following conditions
204 * are met:
205 *
206 * 1. Redistributions of source code must retain the above copyright
207 * notice, this list of conditions and the following disclaimer.
208 *
209 * 2. Redistributions in binary form must reproduce the above copyright
210 * notice, this list of conditions and the following disclaimer in
211 * the documentation and/or other materials provided with the
212 * distribution.
213 *
214 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
215 * and "Vovida Open Communication Application Library (VOCAL)" must
216 * not be used to endorse or promote products derived from this
217 * software without prior written permission. For written
218 * permission, please contact vocal@vovida.org.
219 *
220 * 4. Products derived from this software may not be called "VOCAL", nor
221 * may "VOCAL" appear in their name, without prior written
222 * permission of Vovida Networks, Inc.
223 *
224 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
225 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
226 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
227 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
228 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
229 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
230 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
231 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
232 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
233 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
235 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
236 * DAMAGE.
237 *
238 * ====================================================================
239 *
240 * This software consists of voluntary contributions made by Vovida
241 * Networks, Inc. and many individuals on behalf of Vovida Networks,
242 * Inc. For more information on Vovida Networks, Inc., please see
243 * <http://www.vovida.org/>.
244 *
245 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27