| 1 | /* |
|---|
| 2 | phtests - test tool for phapi |
|---|
| 3 | Copyright (c) 2010, MBDSYS |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that |
|---|
| 7 | the following conditions are met: |
|---|
| 8 | |
|---|
| 9 | * Redistributions of source code must retain the above copyright notice, this list of conditions |
|---|
| 10 | and the following disclaimer. |
|---|
| 11 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
|---|
| 12 | following disclaimer in the documentation and/or other materials provided with the distribution. |
|---|
| 13 | * Neither the name of the MBDSYS nor the names of its contributors may be used to endorse or promote products |
|---|
| 14 | derived from this software without specific prior written permission. |
|---|
| 15 | |
|---|
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
|---|
| 17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 18 | IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|---|
| 19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 21 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|---|
| 22 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 23 | */ |
|---|
| 24 | #include <string> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include <time.h> |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include <stdarg.h> |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef _MSC_VER |
|---|
| 33 | #include <winsock2.h> |
|---|
| 34 | #include <windows.h> |
|---|
| 35 | #define sleep(x) Sleep((x)*1000) |
|---|
| 36 | # define VSNPRINTF _vsnprintf |
|---|
| 37 | #else |
|---|
| 38 | # include <sys/time.h> |
|---|
| 39 | # define VSNPRINTF vsnprintf |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | #include <osipparser2/osip_port.h> |
|---|
| 43 | #include <osip2/osip_time.h> |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | using namespace std; |
|---|
| 47 | |
|---|
| 48 | #include "phapipp.h" |
|---|
| 49 | #include "phtests.h" |
|---|
| 50 | #include "phtestslog.h" |
|---|
| 51 | |
|---|
| 52 | string extract_uri(const char *s) |
|---|
| 53 | { |
|---|
| 54 | string tmp = s; |
|---|
| 55 | int lt = tmp.find('<'); |
|---|
| 56 | int gt = tmp.find('>'); |
|---|
| 57 | |
|---|
| 58 | return tmp.substr(lt+1, gt-lt-1); |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | class myphapi:public verona::stdphapi |
|---|
| 63 | { |
|---|
| 64 | public: |
|---|
| 65 | myphapi():_conf(0){} |
|---|
| 66 | void conf(testConf *extconf){ _conf = extconf;} |
|---|
| 67 | private: |
|---|
| 68 | testConf *_conf; |
|---|
| 69 | static const char *nz(const char *s) { return s ? s : ""; } |
|---|
| 70 | |
|---|
| 71 | void onRegProgress(int rid, int status) |
|---|
| 72 | { |
|---|
| 73 | DBG_LOG("Reg: %d Status: %d\n", rid, status); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | virtual void onMsgProgress(int mid, const phMsgStateInfo_t *info) |
|---|
| 78 | { |
|---|
| 79 | if (info->event == phMsgNew) |
|---|
| 80 | { |
|---|
| 81 | DBG_LOG("Got Message: to=%s from=%s type=%s\n======\n%s=======\n", |
|---|
| 82 | info->to, info->from, info->rawctt, info->content); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | virtual void onConnectionLost(const char *host, int port) |
|---|
| 87 | { |
|---|
| 88 | DBG_LOG("Connection lost %s, %d\n", host, port); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | virtual void onNotifyProgress (const char* event, const char* from, const char *ctt, const char* content) |
|---|
| 92 | { |
|---|
| 93 | DBG_LOG("Got notification: from=%s evt=%s ctt=%s content:\n====\n%s\n====\n", from, event, ctt, content); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | virtual void onPhLogCsl(const char *msg) |
|---|
| 97 | { |
|---|
| 98 | DBG_LOG("csl log [%s]", msg); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void onCallProgress(int cid, const phCallStateInfo_t *info) |
|---|
| 102 | { |
|---|
| 103 | _conf->callStatus = info->event; |
|---|
| 104 | switch (info->event) |
|---|
| 105 | { |
|---|
| 106 | case phDIALING: |
|---|
| 107 | DBG_LOG("DIALING line=%d cid=%d uri=%s\n", info->vlid, cid, info->remoteUri); |
|---|
| 108 | break; |
|---|
| 109 | |
|---|
| 110 | case phRINGING: |
|---|
| 111 | DBG_LOG("RINGING cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 112 | break; |
|---|
| 113 | |
|---|
| 114 | case phRINGandSTART: |
|---|
| 115 | DBG_LOG("RINGINGandSTART cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 116 | break; |
|---|
| 117 | |
|---|
| 118 | case phRINGandSTOP: |
|---|
| 119 | DBG_LOG("RINGandSTOP cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 120 | break; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | case phNOANSWER: |
|---|
| 124 | DBG_LOG("NOANSWER cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 125 | break; |
|---|
| 126 | |
|---|
| 127 | case phCALLBUSY: |
|---|
| 128 | DBG_LOG("BUSY cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 129 | break; |
|---|
| 130 | |
|---|
| 131 | case phCALLREDIRECTED: |
|---|
| 132 | DBG_LOG("REDIRECTED cid=%d newcid=%d to=%s\n", cid, info->newcid, info->remoteUri); |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | case phCALLOK: |
|---|
| 136 | DBG_LOG("CALLOK cid=%d uri=%s\n", cid, info->remoteUri); |
|---|
| 137 | break; |
|---|
| 138 | |
|---|
| 139 | case phCALLHELD: |
|---|
| 140 | DBG_LOG("CALLHELD cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 141 | break; |
|---|
| 142 | |
|---|
| 143 | case phCALLRESUMED: |
|---|
| 144 | DBG_LOG("CALLRESUMED cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 145 | break; |
|---|
| 146 | |
|---|
| 147 | case phHOLDOK: |
|---|
| 148 | DBG_LOG("HOLDOK cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 149 | break; |
|---|
| 150 | |
|---|
| 151 | case phRESUMEOK: |
|---|
| 152 | DBG_LOG("RESUMEOK cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 153 | break; |
|---|
| 154 | |
|---|
| 155 | case phINCALL: |
|---|
| 156 | printf("INCALL line=%d cid=%d to=%s from=%s streams=%x callinfo=%s\n", info->vlid, cid, |
|---|
| 157 | info->localUri, info->remoteUri, info->streams, nz(info->callinfo)); |
|---|
| 158 | this->ringingCall(cid); |
|---|
| 159 | break; |
|---|
| 160 | |
|---|
| 161 | case phINCALLREADY: |
|---|
| 162 | DBG_LOG("INCALLREADY cid=%d\n", cid); |
|---|
| 163 | this->acceptCall(cid, 0, PH_STREAM_AUDIO); |
|---|
| 164 | break; |
|---|
| 165 | |
|---|
| 166 | case phCALLCLOSED: |
|---|
| 167 | DBG_LOG("CALLCLOSED cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 168 | break; |
|---|
| 169 | |
|---|
| 170 | case phCALLCLOSEDandSTOPRING: |
|---|
| 171 | DBG_LOG("CALLCLOSEDandSTOPRING cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 172 | break; |
|---|
| 173 | |
|---|
| 174 | case phCALLERROR: |
|---|
| 175 | DBG_LOG("CALLERROR cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 176 | _conf->cid = -1; |
|---|
| 177 | break; |
|---|
| 178 | |
|---|
| 179 | case phDTMF: |
|---|
| 180 | DBG_LOG("DTMF cid=%d digit=%c\n", cid, info->dtmfDigit); |
|---|
| 181 | break; |
|---|
| 182 | |
|---|
| 183 | case phXFERREQ: |
|---|
| 184 | DBG_LOG("XFERREQ line=%d cid=%d newcid=%d to=%s", info->vlid, cid, info->newcid, info->remoteUri); |
|---|
| 185 | break; |
|---|
| 186 | |
|---|
| 187 | case phXFERPROGRESS: |
|---|
| 188 | DBG_LOG("XFERPROGRESS cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 189 | break; |
|---|
| 190 | |
|---|
| 191 | case phXFEROK: |
|---|
| 192 | DBG_LOG("XFEROK cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 193 | break; |
|---|
| 194 | |
|---|
| 195 | case phXFERFAIL: |
|---|
| 196 | DBG_LOG("XFERFAIL cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 197 | break; |
|---|
| 198 | |
|---|
| 199 | case phCALLREPLACED: |
|---|
| 200 | DBG_LOG("CALLREPLACED cid=%d newcid=%d uri=%s streams=%x, callinfo=%s\n", cid, info->newcid, |
|---|
| 201 | info->remoteUri, info->streams, nz(info->callinfo)); |
|---|
| 202 | break; |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | myphapi testsphapi; |
|---|
| 210 | |
|---|
| 211 | struct testsFunc |
|---|
| 212 | { |
|---|
| 213 | testsFunc(testFunc func, int proba, int time): func(func), funcM(NULL), proba(proba), time(time), data(NULL) {}; |
|---|
| 214 | testsFunc(phtestFunc func, int proba, int time, phTestsCore *data): func(NULL), funcM(func), proba(proba), time(time), data(data) {}; |
|---|
| 215 | int proba; |
|---|
| 216 | int time; |
|---|
| 217 | testFunc func; |
|---|
| 218 | phtestFunc funcM; |
|---|
| 219 | phTestsCore *data; |
|---|
| 220 | }; |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | phTestsCore::phTestsCore(int nbFunc, bool rand, int nbLoop, const char *name):name(name),nbFunc(nbFunc),curFunc(0),totalProb(0),ready(false),rand(rand), nbLoop(nbLoop) |
|---|
| 224 | { |
|---|
| 225 | funcTab = new testsFunc*[nbFunc]; |
|---|
| 226 | memset(funcTab, 0, nbFunc); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | phTestsCore::~phTestsCore() |
|---|
| 230 | { |
|---|
| 231 | for (int i = 0; i < nbFunc && funcTab[i]; ++i) |
|---|
| 232 | delete funcTab[i]; |
|---|
| 233 | delete [] funcTab; |
|---|
| 234 | delete [] name; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | const char* phTestsCore::getName() const |
|---|
| 238 | { |
|---|
| 239 | return name; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | bool phTestsCore::addFunc(testFunc func, int proba, int time) |
|---|
| 243 | { |
|---|
| 244 | if (curFunc == nbFunc) |
|---|
| 245 | return false; |
|---|
| 246 | |
|---|
| 247 | funcTab[curFunc++] = new testsFunc(func, proba, time); |
|---|
| 248 | totalProb += proba; |
|---|
| 249 | |
|---|
| 250 | return true; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | bool phTestsCore::addFunc(phtestFunc func, int proba, int time, phTestsCore *data) |
|---|
| 254 | { |
|---|
| 255 | if (curFunc == nbFunc) |
|---|
| 256 | return false; |
|---|
| 257 | |
|---|
| 258 | funcTab[curFunc++] = new testsFunc(func, proba, time, data); |
|---|
| 259 | totalProb += proba; |
|---|
| 260 | |
|---|
| 261 | return true; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | void phTestsCore::prepare(int seed) |
|---|
| 265 | { |
|---|
| 266 | ready = true; |
|---|
| 267 | if (rand) |
|---|
| 268 | ::srand(seed); |
|---|
| 269 | |
|---|
| 270 | funcNum = new int[totalProb]; |
|---|
| 271 | for (int i = 0, j = 0; i < curFunc; i++) |
|---|
| 272 | { |
|---|
| 273 | for (int k = funcTab[i]->proba; k != 0; k--, j++) |
|---|
| 274 | funcNum[j] = i; |
|---|
| 275 | } |
|---|
| 276 | if (nbLoop < 0) |
|---|
| 277 | nbLoop = -1; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | int phTestsCore::chooseFunc(int last) |
|---|
| 281 | { |
|---|
| 282 | if (rand) |
|---|
| 283 | return funcNum[::rand() % totalProb]; |
|---|
| 284 | else |
|---|
| 285 | return funcNum[(last + 1) % totalProb]; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | #define launch_func(num, funcTab) (((funcTab)[(num)]->data)->*((funcTab)[(num)]->funcM)) |
|---|
| 289 | |
|---|
| 290 | int phTestsCore::loop(testConf *conf) |
|---|
| 291 | { |
|---|
| 292 | int choosenFunc = -1, res = 1; |
|---|
| 293 | |
|---|
| 294 | if (!ready) |
|---|
| 295 | return 0; |
|---|
| 296 | |
|---|
| 297 | DBG_LOG("begin loop"); |
|---|
| 298 | testsphapi.conf(conf); |
|---|
| 299 | //test nbloop == -1 for infinite loop |
|---|
| 300 | //test (res || !conf->haltonerror) for halting if func return false |
|---|
| 301 | for (int i = 0; (nbLoop == -1 || i < nbLoop) && (res || !conf->haltonerror); ++i) |
|---|
| 302 | { |
|---|
| 303 | choosenFunc = chooseFunc(choosenFunc); |
|---|
| 304 | if (funcTab[choosenFunc]->data) |
|---|
| 305 | res = launch_func(choosenFunc, funcTab)(conf); |
|---|
| 306 | else |
|---|
| 307 | res = funcTab[choosenFunc]->func(conf); |
|---|
| 308 | sleep(funcTab[choosenFunc]->time); |
|---|
| 309 | addToPath(choosenFunc, res); |
|---|
| 310 | } |
|---|
| 311 | DBG_LOG("end loop with res %i", res); |
|---|
| 312 | dumpPath(); |
|---|
| 313 | return res; |
|---|
| 314 | } |
|---|
| 315 | #include <sstream> |
|---|
| 316 | std::ostringstream funcPath; |
|---|
| 317 | |
|---|
| 318 | void phTestsCore::dumpPath() { std::cout << "Path " << funcPath.str() << std::endl;} |
|---|
| 319 | |
|---|
| 320 | void phTestsCore::addToPath(int choosenFunc, int res) { funcPath << choosenFunc << "#" << res << ";"; } |
|---|
| 321 | |
|---|