| 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(int level, 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 | //this->acceptCall(cid, 0, PH_STREAM_AUDIO); |
|---|
| 160 | break; |
|---|
| 161 | |
|---|
| 162 | case phINCALLREADY: |
|---|
| 163 | DBG_LOG("INCALLREADY cid=%d\n", cid); |
|---|
| 164 | this->acceptCall(cid, 0, PH_STREAM_AUDIO); |
|---|
| 165 | break; |
|---|
| 166 | |
|---|
| 167 | case phCALLCLOSED: |
|---|
| 168 | DBG_LOG("CALLCLOSED cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 169 | break; |
|---|
| 170 | |
|---|
| 171 | case phCALLCLOSEDandSTOPRING: |
|---|
| 172 | DBG_LOG("CALLCLOSEDandSTOPRING cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 173 | break; |
|---|
| 174 | |
|---|
| 175 | case phCALLERROR: |
|---|
| 176 | DBG_LOG("CALLERROR cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 177 | _conf->cid = -1; |
|---|
| 178 | break; |
|---|
| 179 | |
|---|
| 180 | case phDTMF: |
|---|
| 181 | DBG_LOG("DTMF cid=%d digit=%c\n", cid, info->dtmfDigit); |
|---|
| 182 | break; |
|---|
| 183 | |
|---|
| 184 | case phXFERREQ: |
|---|
| 185 | DBG_LOG("XFERREQ line=%d cid=%d newcid=%d to=%s", info->vlid, cid, info->newcid, info->remoteUri); |
|---|
| 186 | break; |
|---|
| 187 | |
|---|
| 188 | case phXFERPROGRESS: |
|---|
| 189 | DBG_LOG("XFERPROGRESS cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 190 | break; |
|---|
| 191 | |
|---|
| 192 | case phXFEROK: |
|---|
| 193 | DBG_LOG("XFEROK cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 194 | break; |
|---|
| 195 | |
|---|
| 196 | case phXFERFAIL: |
|---|
| 197 | DBG_LOG("XFERFAIL cid=%d status=%d\n", cid, info->errorCode); |
|---|
| 198 | break; |
|---|
| 199 | |
|---|
| 200 | case phCALLREPLACED: |
|---|
| 201 | DBG_LOG("CALLREPLACED cid=%d newcid=%d uri=%s streams=%x, callinfo=%s\n", cid, info->newcid, |
|---|
| 202 | info->remoteUri, info->streams, nz(info->callinfo)); |
|---|
| 203 | break; |
|---|
| 204 | |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | myphapi *testsphapip; |
|---|
| 211 | |
|---|
| 212 | void prepare_phapi() |
|---|
| 213 | { |
|---|
| 214 | testsphapip = new myphapi; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | void clean_phapi() |
|---|
| 218 | { |
|---|
| 219 | delete testsphapip; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | struct testsFunc |
|---|
| 223 | { |
|---|
| 224 | testsFunc(testFunc func, int proba, int time): func(func), funcM(NULL), proba(proba), time(time), data(NULL) {}; |
|---|
| 225 | testsFunc(phtestFunc func, int proba, int time, phTestsCore *data): func(NULL), funcM(func), proba(proba), time(time), data(data) {}; |
|---|
| 226 | int proba; |
|---|
| 227 | int time; |
|---|
| 228 | testFunc func; |
|---|
| 229 | phtestFunc funcM; |
|---|
| 230 | phTestsCore *data; |
|---|
| 231 | }; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | 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) |
|---|
| 235 | { |
|---|
| 236 | funcTab = new testsFunc*[nbFunc]; |
|---|
| 237 | memset(funcTab, 0, nbFunc); |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | phTestsCore::~phTestsCore() |
|---|
| 242 | { |
|---|
| 243 | |
|---|
| 244 | for (int i = 0; i < nbFunc && funcTab[i]; ++i) |
|---|
| 245 | delete funcTab[i]; |
|---|
| 246 | delete [] funcTab; |
|---|
| 247 | delete [] funcNum; |
|---|
| 248 | delete [] name; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | const char* phTestsCore::getName() const |
|---|
| 252 | { |
|---|
| 253 | return name; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | bool phTestsCore::addFunc(testFunc func, int proba, int time) |
|---|
| 257 | { |
|---|
| 258 | if (curFunc == nbFunc) |
|---|
| 259 | return false; |
|---|
| 260 | |
|---|
| 261 | funcTab[curFunc++] = new testsFunc(func, proba, time); |
|---|
| 262 | totalProb += proba; |
|---|
| 263 | |
|---|
| 264 | return true; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | bool phTestsCore::addFunc(phtestFunc func, int proba, int time, phTestsCore *data) |
|---|
| 268 | { |
|---|
| 269 | if (curFunc == nbFunc) |
|---|
| 270 | return false; |
|---|
| 271 | |
|---|
| 272 | funcTab[curFunc++] = new testsFunc(func, proba, time, data); |
|---|
| 273 | totalProb += proba; |
|---|
| 274 | |
|---|
| 275 | return true; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | void phTestsCore::prepare(int seed) |
|---|
| 279 | { |
|---|
| 280 | ready = true; |
|---|
| 281 | if (rand) |
|---|
| 282 | ::srand(seed); |
|---|
| 283 | |
|---|
| 284 | funcNum = new int[totalProb]; |
|---|
| 285 | for (int i = 0, j = 0; i < curFunc; i++) |
|---|
| 286 | { |
|---|
| 287 | for (int k = funcTab[i]->proba; k != 0; k--, j++) |
|---|
| 288 | funcNum[j] = i; |
|---|
| 289 | } |
|---|
| 290 | if (nbLoop < 0) |
|---|
| 291 | nbLoop = -1; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | int phTestsCore::chooseFunc(int last) |
|---|
| 295 | { |
|---|
| 296 | if (rand) |
|---|
| 297 | return funcNum[::rand() % totalProb]; |
|---|
| 298 | else |
|---|
| 299 | return funcNum[(last + 1) % totalProb]; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | #define launch_func(num, funcTab) (((funcTab)[(num)]->data)->*((funcTab)[(num)]->funcM)) |
|---|
| 303 | |
|---|
| 304 | int phTestsCore::loop(testConf *conf) |
|---|
| 305 | { |
|---|
| 306 | int choosenFunc = -1, res = 1; |
|---|
| 307 | |
|---|
| 308 | if (!ready) |
|---|
| 309 | return 0; |
|---|
| 310 | |
|---|
| 311 | DBG_LOG("begin loop"); |
|---|
| 312 | testsphapip->conf(conf); |
|---|
| 313 | //test nbloop == -1 for infinite loop |
|---|
| 314 | //test (res || !conf->haltonerror) for halting if func return false |
|---|
| 315 | for (int i = 0; (nbLoop == -1 || i < nbLoop) && (res || !conf->haltonerror); ++i) |
|---|
| 316 | { |
|---|
| 317 | choosenFunc = chooseFunc(choosenFunc); |
|---|
| 318 | if (funcTab[choosenFunc]->data) |
|---|
| 319 | res = launch_func(choosenFunc, funcTab)(conf); |
|---|
| 320 | else |
|---|
| 321 | res = funcTab[choosenFunc]->func(conf); |
|---|
| 322 | sleep(funcTab[choosenFunc]->time); |
|---|
| 323 | addToPath(choosenFunc, res); |
|---|
| 324 | } |
|---|
| 325 | DBG_LOG("end loop with res %i", res); |
|---|
| 326 | dumpPath(); |
|---|
| 327 | return res; |
|---|
| 328 | } |
|---|
| 329 | #if 0 |
|---|
| 330 | #include <sstream> |
|---|
| 331 | std::ostringstream funcPath; |
|---|
| 332 | |
|---|
| 333 | void phTestsCore::dumpPath() { std::cout << "Path " << funcPath.str() << std::endl;} |
|---|
| 334 | |
|---|
| 335 | void phTestsCore::addToPath(int choosenFunc, int res) { funcPath << choosenFunc << "#" << res << ";"; } |
|---|
| 336 | |
|---|
| 337 | #else |
|---|
| 338 | void phTestsCore::dumpPath() { } |
|---|
| 339 | |
|---|
| 340 | void phTestsCore::addToPath(int choosenFunc, int res) { } |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | #endif |
|---|