Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
testcode.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 #include "commonincludes.hpp"
19 #include "stuncore.h"
20 
21 #include "testdatastream.h"
22 #include "testreader.h"
23 #include "testbuilder.h"
24 #include "testmessagehandler.h"
25 #include "testcmdline.h"
26 #include "testintegrity.h"
27 #include "testclientlogic.h"
28 #include "testrecvfromex.h"
29 #include "testfasthash.h"
30 #include "cmdlineparser.h"
31 #include "oshelper.h"
32 #include "prettyprint.h"
33 #include "polling.h"
34 #include "testpolling.h"
35 #include "testatomichelpers.h"
36 #include "testratelimiter.h"
37 
39 {
40  uint8_t msgbytes[1500];
41  ssize_t messagesize;
42  CStunMessageReader reader;
43  int ret;
44 
45 
46  messagesize=0;
47  while (messagesize < 1500)
48  {
49  ret = getchar();
50  if (ret < 0)
51  {
52  break;
53  }
54 
55  unsigned char ch = (unsigned char)ret;
56  msgbytes[messagesize] = ch;
57  messagesize++;
58  }
59 
60  printf("Processing %d bytes\n", (int)messagesize);
61  reader.AddBytes(msgbytes, messagesize);
62 
63 
64  printf("Test Run (%u)\n", GetMillisecondCounter());
65 
66 
67  exit(0);
68 
69 }
70 
71 
73 {
74  std::vector<IUnitTest*> vecTests;
75 
76  boost::shared_ptr<CTestDataStream> spTestDataStream(new CTestDataStream);
77  boost::shared_ptr<CTestReader> spTestReader(new CTestReader);
78  boost::shared_ptr<CTestBuilder> spTestBuilder(new CTestBuilder);
79  boost::shared_ptr<CTestIntegrity> spTestIntegrity(new CTestIntegrity);
80  boost::shared_ptr<CTestMessageHandler> spTestMessageHandler(new CTestMessageHandler);
81  boost::shared_ptr<CTestCmdLineParser> spTestCmdLineParser(new CTestCmdLineParser);
82  boost::shared_ptr<CTestClientLogic> spTestClientLogic(new CTestClientLogic);
83  boost::shared_ptr<CTestRecvFromExIPV4> spTestRecvFromEx4(new CTestRecvFromExIPV4);
84  boost::shared_ptr<CTestRecvFromExIPV6> spTestRecvFromEx6(new CTestRecvFromExIPV6);
85  boost::shared_ptr<CTestFastHash> spTestFastHash(new CTestFastHash);
86  boost::shared_ptr<CTestPolling> spTestPolling(new CTestPolling);
87  boost::shared_ptr<CTestAtomicHelpers> spTestAtomicHelpers(new CTestAtomicHelpers);
88  boost::shared_ptr<CTestRateLimiter> spTestRateLimiter(new CTestRateLimiter);
89 
90  vecTests.push_back(spTestDataStream.get());
91  vecTests.push_back(spTestReader.get());
92  vecTests.push_back(spTestBuilder.get());
93  vecTests.push_back(spTestIntegrity.get());
94  vecTests.push_back(spTestMessageHandler.get());
95  vecTests.push_back(spTestCmdLineParser.get());
96  vecTests.push_back(spTestClientLogic.get());
97  vecTests.push_back(spTestRecvFromEx4.get());
98  vecTests.push_back(spTestRecvFromEx6.get());
99  vecTests.push_back(spTestFastHash.get());
100  vecTests.push_back(spTestPolling.get());
101  vecTests.push_back(spTestAtomicHelpers.get());
102  vecTests.push_back(spTestRateLimiter.get());
103 
104 
105  for (size_t index = 0; index < vecTests.size(); index++)
106  {
107  HRESULT hr = vecTests[index]->Run();
108  printf("Result of %s: %s\n", vecTests[index]->GetName(), SUCCEEDED(hr)?"PASS": "FAIL");
109  }
110 }
111 
112 
114 {
115  const size_t MAX_TEXT_SIZE = 100000;
116  char* buffer = new char[MAX_TEXT_SIZE];
117 
118  size_t messagesize = 0;
119 
120  while (messagesize < MAX_TEXT_SIZE)
121  {
122  int ret = getchar();
123  if (ret < 0)
124  {
125  break;
126  }
127 
128  buffer[messagesize] = (signed char)(ret);
129  messagesize++;
130  }
131 
132  ::PrettyPrint(buffer, 78);
133 
134  delete [] buffer;
135 }
136 
137 int main(int argc, char** argv)
138 {
139  CCmdLineParser cmdline;
140  std::string strFuzz;
141  std::string strPP;
142  bool fParseError = false;
143 
144 
145  cmdline.AddOption("fuzz", no_argument, &strFuzz);
146  cmdline.AddOption("pp", no_argument, &strPP);
147 
148  cmdline.ParseCommandLine(argc, argv, 1, &fParseError);
149 
150  if (strFuzz.size() > 0)
151  {
152  ReaderFuzzTest();
153  }
154  else if (strPP.size() > 0)
155  {
156  PrettyPrintTest();
157  }
158  else
159  {
160  RunUnitTests();
161  }
162 
163  return 0;
164 }
165 
166 
167 
168 
void PrettyPrintTest()
Definition: testcode.cpp:113
void RunUnitTests()
Definition: testcode.cpp:72
uint32_t GetMillisecondCounter()
ReaderParseState AddBytes(const uint8_t *pData, uint32_t size)
Definition: stunreader.cpp:750
#define SUCCEEDED(hr)
Definition: hresult.h:28
int32_t HRESULT
Definition: hresult.h:22
HRESULT ParseCommandLine(int argc, char **argv, int startindex, bool *fParseError)
void ReaderFuzzTest()
Definition: testcode.cpp:38
int main(int argc, char **argv)
Definition: testcode.cpp:137
HRESULT AddOption(const char *pszName, int has_arg, std::string *pStrResult)
void PrettyPrint(const char *pszInput, size_t width)