0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <math.h> /* fabs */
0039
0040 #include <atmi.h>
0041 #include <ubf.h>
0042 #include <ndebug.h>
0043 #include <test.fd.h>
0044 #include <ndrstandard.h>
0045 #include "test040.h"
0046 #include "tperror.h"
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 int test_view2json(void)
0061 {
0062 int ret = EXSUCCEED;
0063 struct MYVIEW1 v1;
0064 char *msg = NULL;
0065 size_t msg_len;
0066 char *abuf = NULL;
0067 char view[NDRX_VIEW_NAME_LEN+1];
0068 char *testbuf = "{\"MYVIEW2\":{\"tshort1\":1,\"tlong1\":2,\"tchar1\":\"A\",\""
0069 "tfloat1\":1,\"tdouble1\":21,\"tstring1\":\"ABC\",\""
0070 "tcarray1\":\"SEVMTE8AAAAAAA==\"}}";
0071
0072 char *t3buf_null = "{\"MYVIEW3\":{\"tshort1\":0,\"tshort2\":0,\"tshort3\":0}}";
0073 char *t3buf_nnull = "{\"MYVIEW3\":{}}";
0074
0075 struct MYVIEW2 *v2;
0076 struct MYVIEW3 v3;
0077
0078 init_MYVIEW1(&v1);
0079
0080 NDRX_SYSBUF_MALLOC_OUT(msg, msg_len, ret);
0081
0082 if (EXSUCCEED!=tpviewtojson((char *)&v1, "MYVIEW1", msg, msg_len,
0083 BVACCESS_NOTNULL))
0084 {
0085 NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s",
0086 tpstrerror(tperrno));
0087 EXFAIL_OUT(ret);
0088 }
0089
0090 memset(&v1, 0, sizeof(v1));
0091
0092 NDRX_LOG(log_debug, "Got json: [%s]", msg);
0093
0094 if (NULL==(abuf=tpjsontoview(view, msg)))
0095 {
0096 NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s",
0097 tpstrerror(tperrno));
0098 EXFAIL_OUT(ret);
0099 }
0100
0101
0102 if (EXSUCCEED!=validate_MYVIEW1((struct MYVIEW1 *)abuf))
0103 {
0104 NDRX_LOG(log_error, "TESTERROR: Failed to validate view recovery!");
0105 EXFAIL_OUT(ret);
0106 }
0107
0108
0109 tpfree(abuf);
0110 abuf = NULL;
0111
0112
0113 init_MYVIEW1(&v1);
0114
0115 msg[0] = EXEOS;
0116 if (EXSUCCEED!=tpviewtojson((char *)&v1, "MYVIEW1", msg, msg_len, 0))
0117 {
0118 NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s",
0119 tpstrerror(tperrno));
0120 EXFAIL_OUT(ret);
0121 }
0122
0123 NDRX_LOG(log_debug, "Got json2: [%s]", msg);
0124
0125 if (NULL==(abuf=tpjsontoview(view, msg)))
0126 {
0127 NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s",
0128 tpstrerror(tperrno));
0129 EXFAIL_OUT(ret);
0130 }
0131
0132
0133 if (EXSUCCEED!=validate_MYVIEW1((struct MYVIEW1 *)abuf))
0134 {
0135 NDRX_LOG(log_error, "TESTERROR: Failed to validate view recovery!");
0136 EXFAIL_OUT(ret);
0137 }
0138
0139 tpfree(abuf);
0140 abuf=NULL;
0141
0142
0143
0144
0145 if (NULL==(abuf=tpjsontoview(view, testbuf)))
0146 {
0147 NDRX_LOG(log_error, "TESTERROR: Failed to get view from JSON: %s",
0148 tpstrerror(tperrno));
0149 EXFAIL_OUT(ret);
0150 }
0151
0152 v2 = (struct MYVIEW2 *)abuf;
0153
0154 if (1!=v2->tshort1)
0155 {
0156 NDRX_LOG(log_error, "TESTERROR: tshort1 got %hd expected 1",
0157 v2->tshort1);
0158 EXFAIL_OUT(ret);
0159 }
0160
0161 if (2!=v2->tlong1)
0162 {
0163 NDRX_LOG(log_error, "TESTERROR: tlong1 got %hd expected 2",
0164 v2->tlong1);
0165 EXFAIL_OUT(ret);
0166 }
0167
0168 if ('A'!=v2->tchar1)
0169 {
0170 NDRX_LOG(log_error, "TESTERROR: tchar1 got %c expected A",
0171 v2->tchar1);
0172 EXFAIL_OUT(ret);
0173 }
0174
0175 if (fabs(v2->tfloat1 - 1.0f) > 0.1)
0176 {
0177 NDRX_LOG(log_error, "TESTERROR: tfloat1 got %f expected 1",
0178 v2->tfloat1);
0179 EXFAIL_OUT(ret);
0180 }
0181
0182 if ((v2->tdouble1 - 21.0f) > 0.1)
0183 {
0184 NDRX_LOG(log_error, "TESTERROR: tdouble1 got %lf expected 21",
0185 v2->tdouble1);
0186 EXFAIL_OUT(ret);
0187 }
0188
0189 if (0!=strcmp(v2->tstring1, "ABC"))
0190 {
0191 NDRX_LOG(log_error, "TESTERROR: tstring1 got [%s] expected ABC",
0192 v2->tdouble1);
0193 EXFAIL_OUT(ret);
0194 }
0195
0196 if (0!=strncmp(v2->tcarray1, "HELLO", 5))
0197 {
0198 NDRX_LOG(log_error, "TESTERROR: tstring1 got [%10s] expected "
0199 "[HELLO]",
0200 v2->tdouble1);
0201 EXFAIL_OUT(ret);
0202 }
0203
0204
0205 msg[0] = EXEOS;
0206
0207 if (EXSUCCEED!=tpviewtojson((char *)v2, "MYVIEW2", msg, msg_len, 0))
0208 {
0209 NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s",
0210 tpstrerror(tperrno));
0211 EXFAIL_OUT(ret);
0212 }
0213
0214 NDRX_LOG(log_debug, "v2 json: [%s]", msg);
0215
0216 if (0!=strcmp(msg, testbuf))
0217 {
0218 NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]",
0219 msg, testbuf);
0220 EXFAIL_OUT(ret);
0221 }
0222
0223
0224 tpfree(abuf);
0225 abuf = NULL;
0226
0227
0228 if (NULL!=(abuf=tpjsontoview(view, "HELLO WORLD")))
0229 {
0230 NDRX_LOG(log_error, "TESTERROR: Failure must occur - invalid json");
0231 EXFAIL_OUT(ret);
0232 }
0233
0234 if (TPEINVAL!=tperrno)
0235 {
0236 NDRX_LOG(log_error, "TESTERROR: invalid error on json parse: "
0237 "expected TPEINVAL, got: %s",
0238 tpstrerror(tperrno));
0239 EXFAIL_OUT(ret);
0240 }
0241
0242 if (NULL!=(abuf=tpjsontoview(view, "{\"NONEXIST\":{}}")))
0243 {
0244 NDRX_LOG(log_error, "TESTERROR: Failure must occur - invalid json");
0245 EXFAIL_OUT(ret);
0246 }
0247
0248 if (TPEINVAL!=tperrno)
0249 {
0250 NDRX_LOG(log_error, "TESTERROR: invalid error on json parse: "
0251 "expected TPEINVAL, got: %s",
0252 tpstrerror(tperrno));
0253 EXFAIL_OUT(ret);
0254 }
0255
0256
0257
0258 if (EXSUCCEED!=Bvsinit((char *)&v3, "MYVIEW3"))
0259 {
0260 NDRX_LOG(log_error, "TESTERROR: MYVIEW3 failed to init: %s",
0261 Bstrerror(Berror));
0262 EXFAIL_OUT(ret);
0263 }
0264
0265 if (EXSUCCEED!=tpviewtojson((char *)&v3, "MYVIEW3", msg, msg_len, 0))
0266 {
0267 NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s",
0268 tpstrerror(tperrno));
0269 EXFAIL_OUT(ret);
0270 }
0271
0272 NDRX_LOG(log_debug, "v3 json: [%s]", msg);
0273
0274 if (0!=strcmp(msg, t3buf_null))
0275 {
0276 NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]",
0277 msg, t3buf_null);
0278 EXFAIL_OUT(ret);
0279 }
0280
0281
0282 if (EXSUCCEED!=tpviewtojson((char *)&v3, "MYVIEW3", msg, msg_len,
0283 BVACCESS_NOTNULL))
0284 {
0285 NDRX_LOG(log_error, "TESTERROR: tpviewtojson() failed: %s",
0286 tpstrerror(tperrno));
0287 EXFAIL_OUT(ret);
0288 }
0289
0290 NDRX_LOG(log_debug, "v3 json: [%s]", msg);
0291
0292 if (0!=strcmp(msg, t3buf_nnull))
0293 {
0294 NDRX_LOG(log_error, "TESTERROR: Built json: [%s] expected [%s]",
0295 msg, t3buf_nnull);
0296 EXFAIL_OUT(ret);
0297 }
0298
0299 if (NULL==(abuf=tpjsontoview(view, msg)))
0300 {
0301 NDRX_LOG(log_error, "TESTERROR: failed to parse empty json: %s",
0302 Bstrerror(Berror));
0303 EXFAIL_OUT(ret);
0304 }
0305
0306 tpfree(abuf);
0307 abuf = NULL;
0308
0309
0310 out:
0311 if (NULL!=abuf)
0312 {
0313 tpfree(abuf);
0314 }
0315
0316 if (NULL!=msg)
0317 {
0318 NDRX_FPFREE(msg);
0319 }
0320
0321 return ret;
0322 }
0323
0324
0325
0326
0327
0328 int test_x_view2json(void)
0329 {
0330 int ret = EXSUCCEED;
0331 struct MYVIEW3 *v3 = NULL;
0332 long rsplen;
0333
0334 v3 = (struct MYVIEW3 *)tpalloc("VIEW", "MYVIEW3", 0);
0335
0336 if (NULL==v3)
0337 {
0338 NDRX_LOG(log_error, "TESTERROR: Failed to allocate MYVIEW3: %s",
0339 tpstrerror(tperrno));
0340 EXFAIL_OUT(ret);
0341 }
0342
0343 v3->tshort1 = 1;
0344 v3->tshort2 = 2;
0345 v3->tshort3 = 3;
0346
0347 if (EXSUCCEED!=tpcall("TEST40_V2JSON", (char *) v3, 0,
0348 (char **)&v3, &rsplen, 0))
0349 {
0350 NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_V2JSON");
0351 EXFAIL_OUT(ret);
0352 }
0353
0354 if (4!=v3->tshort1)
0355 {
0356 NDRX_LOG(log_error, "TESTERROR: tshort1 must be 4 but must be %hd",
0357 v3->tshort1);
0358 EXFAIL_OUT(ret);
0359 }
0360
0361 if (5!=v3->tshort2)
0362 {
0363 NDRX_LOG(log_error, "TESTERROR: tshort2 must be 5 but must be %hd",
0364 v3->tshort2);
0365 EXFAIL_OUT(ret);
0366 }
0367
0368 if (6!=v3->tshort3)
0369 {
0370 NDRX_LOG(log_error, "TESTERROR: tshort3 must be 6 but must be %hd",
0371 v3->tshort3);
0372 EXFAIL_OUT(ret);
0373 }
0374
0375 out:
0376
0377 if (NULL!=v3)
0378 {
0379 tpfree((char *)v3);
0380 }
0381
0382 return ret;
0383 }
0384
0385
0386
0387
0388
0389 int test_x_json2view(void)
0390 {
0391 int ret = EXSUCCEED;
0392 long rsplen;
0393 char *buf;
0394
0395 char *input = "{\"MYVIEW3\":{\"tshort1\":6,\"tshort2\":7,\"tshort3\":8}}";
0396 char *output = "{\"MYVIEW3\":{\"tshort1\":9,\"tshort2\":1,\"tshort3\":2}}";
0397
0398 buf = tpalloc("JSON", NULL, 1024);
0399
0400 if (NULL==buf)
0401 {
0402 NDRX_LOG(log_error, "TESTERROR: Failed to allocate MYVIEW3: %s",
0403 tpstrerror(tperrno));
0404 EXFAIL_OUT(ret);
0405 }
0406
0407 strcpy(buf, input);
0408
0409 if (EXSUCCEED!=tpcall("TEST40_JSON2V", buf, 0, &buf, &rsplen, 0))
0410 {
0411 NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_JSON2V: %s",
0412 tpstrerror(tperrno));
0413 EXFAIL_OUT(ret);
0414 }
0415
0416 if (0!=strcmp(buf, output))
0417 {
0418 NDRX_LOG(log_error, "TESTERROR: Received [%s] expected [%s]",
0419 buf, output);
0420 EXFAIL_OUT(ret);
0421 }
0422
0423 out:
0424
0425 if (NULL!=buf)
0426 {
0427 tpfree(buf);
0428 }
0429
0430 return ret;
0431 }
0432
0433
0434
0435
0436
0437 int test_x_json2view_796(void)
0438 {
0439 struct MYVIEW4 *v4;
0440 struct MYVIEW4 *v4_2=NULL;
0441 int ret = EXSUCCEED;
0442 char tmp[2048];
0443 char view[NDRX_VIEW_NAME_LEN+1];
0444
0445 v4= (struct MYVIEW4 *) tpalloc("VIEW", "MYVIEW4", sizeof(struct MYVIEW4));
0446 if (NULL==v4)
0447 {
0448 NDRX_LOG(log_error, "TESTERROR: failed to alloc VIEW buffer!");
0449 EXFAIL_OUT(ret);
0450 }
0451
0452 memset(v4, 0, sizeof(struct MYVIEW4));
0453
0454 v4->tlong1 = LONG_MAX;
0455
0456 v4->tlong2[0] = LONG_MAX;
0457 v4->tlong2[1] = LONG_MIN;
0458 v4->tlong2[2] = LONG_MIN;
0459 v4->tlong2[3] = 109832567849012365;
0460
0461 if (EXSUCCEED!=tpviewtojson((char *)v4, "MYVIEW4", tmp, sizeof(tmp), 0))
0462 {
0463 NDRX_LOG(log_error, "TESTERROR: failed to convert to JSON: %s",
0464 tpstrerror(tperrno));
0465 EXFAIL_OUT(ret);
0466 }
0467
0468 if (NULL==(v4_2= (struct MYVIEW4 *)tpjsontoview(view, tmp)))
0469 {
0470 NDRX_LOG(log_error, "TESTERROR: failed to JSON to view: %s",
0471 tpstrerror(tperrno));
0472 EXFAIL_OUT(ret);
0473 }
0474
0475 if (0!=strcmp(view, "MYVIEW4"))
0476 {
0477 NDRX_LOG(log_error, "TESTERROR: expected MYVIEW4, got [%s]",
0478 view);
0479 EXFAIL_OUT(ret);
0480 }
0481
0482 if (v4->tlong1!=v4_2->tlong1
0483 || v4->tlong2[0]!=v4_2->tlong2[0]
0484 || v4->tlong2[1]!=v4_2->tlong2[1]
0485 || v4->tlong2[2]!=v4_2->tlong2[2]
0486 || v4->tlong2[3]!=v4_2->tlong2[3]
0487 )
0488 {
0489 NDRX_LOG(log_error, "TESTERROR: values mismatch (%ld vs %ld, %ld vs %ld, "
0490 "%ld vs %ld, %ld vs %ld, %ld vs %ld)!",
0491 v4->tlong1, v4_2->tlong1,
0492 v4->tlong2[0], v4_2->tlong2[0],
0493 v4->tlong2[1], v4_2->tlong2[1],
0494 v4->tlong2[2], v4_2->tlong2[2],
0495 v4->tlong2[3], v4_2->tlong2[3]);
0496
0497 EXFAIL_OUT(ret);
0498 }
0499
0500 out:
0501 return ret;
0502 }
0503
0504
0505
0506
0507 int main(int argc, char** argv) {
0508
0509 long rsplen;
0510 int i, j;
0511 int ret=EXSUCCEED;
0512
0513 struct MYVIEW1 *v1;
0514 struct MYVIEW3 *v3;
0515
0516 if (EXSUCCEED!=tpinit(NULL))
0517 {
0518 NDRX_LOG(log_error, "TESTERROR: failed to init: %s", tpstrerror(tperrno));
0519 EXFAIL_OUT(ret);
0520 }
0521
0522 for (i=0; i<1000; i++)
0523 {
0524 if (EXSUCCEED!=test_x_json2view())
0525 {
0526 NDRX_LOG(log_error, "TESTERROR: test_x_json2view() fail!");
0527 EXFAIL_OUT(ret);
0528 }
0529 }
0530
0531 for (i=0; i<1000; i++)
0532 {
0533 if (EXSUCCEED!=test_x_view2json())
0534 {
0535 NDRX_LOG(log_error, "TESTERROR: test_x_view2json() fail!");
0536 EXFAIL_OUT(ret);
0537 }
0538 }
0539
0540 for (i=0; i<1000; i++)
0541 {
0542 if (EXSUCCEED!=test_x_json2view_796())
0543 {
0544 NDRX_LOG(log_error, "TESTERROR: test_x_json2view_796() fail!");
0545 EXFAIL_OUT(ret);
0546 }
0547 }
0548
0549
0550
0551 for (i=0; i<10000; i++)
0552 {
0553 if (EXSUCCEED!=test_view2json())
0554 {
0555 NDRX_LOG(log_error, "TESTERROR: JSON2VIEW failed!");
0556 EXFAIL_OUT(ret);
0557 }
0558 }
0559
0560
0561 for (j=0; j<1000; j++)
0562 {
0563 v1= (struct MYVIEW1 *) tpalloc("VIEW", "MYVIEW1", sizeof(struct MYVIEW1));
0564 if (NULL==v1)
0565 {
0566 NDRX_LOG(log_error, "TESTERROR: failed to alloc VIEW buffer!");
0567 EXFAIL_OUT(ret);
0568 }
0569
0570 init_MYVIEW1(v1);
0571
0572 NDRX_DUMP(log_debug, "VIEW1 request...", v1, sizeof(struct MYVIEW1));
0573
0574 if (EXSUCCEED!=tpcall("TEST40_VIEW", (char *)v1, 0, (char **)&v1, &rsplen, 0))
0575 {
0576 NDRX_LOG(log_error, "TESTERROR: failed to call TEST40_VIEW");
0577 EXFAIL_OUT(ret);
0578 }
0579
0580 v3 = (struct MYVIEW3 *)v1;
0581 if (EXSUCCEED!=validate_MYVIEW3((v3)))
0582 {
0583 NDRX_LOG(log_error, "Failed to validate V3!");
0584 EXFAIL_OUT(ret);
0585 }
0586
0587 tpfree((char *)v3);
0588 }
0589
0590 out:
0591
0592 if (ret>=0)
0593 ret=EXSUCCEED;
0594
0595 return ret;
0596 }
0597
0598