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
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <unistd.h>
0038 #include <cgreen/cgreen.h>
0039 #include <ubf.h>
0040 #include <ndrstandard.h>
0041 #include <string.h>
0042 #include "test.fd.h"
0043 #include "ndebug.h"
0044 #include "xatmi.h"
0045 #include "atmi_int.h"
0046 #include <fdatatype.h>
0047 #include <nstdutil.h>
0048 #include <typed_buf.h>
0049 #include <extest.h>
0050
0051
0052
0053
0054 exprivate void basic_setup(void)
0055 {
0056
0057 }
0058
0059 exprivate void basic_teardown(void)
0060 {
0061
0062 }
0063
0064
0065
0066
0067
0068 Ensure(test_mbuf)
0069 {
0070 int i;
0071
0072 for (i=0; i<10; i++)
0073 {
0074 ndrx_growlist_t list;
0075 UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 2048);
0076 UBFH *p_ub2 = (UBFH *)tpalloc("UBF", NULL, 1024);
0077 UBFH *p_ub3 = (UBFH *)tpalloc("UBF", NULL, 1024);
0078 UBFH *p_ub4 = (UBFH *)tpalloc("UBF", NULL, 1024);
0079
0080 UBFH *p_ub5 = NULL;
0081
0082
0083 UBFH *p_ub5_2 = NULL;
0084 UBFH *p_ub5_3 = NULL;
0085 UBFH *p_ub5_4 = NULL;
0086
0087 UBFH *p_callinfo = (UBFH *)tpalloc("UBF", NULL, 1024);
0088 UBFH *p_callinfo5 = NULL;
0089 BVIEWFLD vf;
0090 char buf[8096];
0091 long buf_len;
0092 long olen;
0093 struct UBTESTVIEW1 vdata;
0094 struct UBTESTVIEW1 *vdata2 = (struct UBTESTVIEW1 *)tpalloc("VIEW",
0095 "UBTESTVIEW1", sizeof(struct UBTESTVIEW1));
0096
0097 struct UBTESTVIEW1 *vdata21 = (struct UBTESTVIEW1 *)tpalloc("VIEW",
0098 "UBTESTVIEW1", sizeof(struct UBTESTVIEW1));
0099
0100 struct UBTESTVIEW1 *vdata5_2 = NULL;
0101 struct UBTESTVIEW1 *vdata5_21 = NULL;
0102
0103 extest_init_UBTESTVIEW1(&vdata);
0104 extest_init_UBTESTVIEW1(vdata2);
0105 extest_init_UBTESTVIEW1(vdata21);
0106
0107
0108 vdata2->tchar1='Z';
0109 vdata21->tchar1='Y';
0110
0111 assert_not_equal(p_ub, NULL);
0112 assert_not_equal(p_ub2, NULL);
0113 assert_not_equal(p_ub3, NULL);
0114 assert_not_equal(p_ub4, NULL);
0115 assert_not_equal(p_callinfo, NULL);
0116 assert_not_equal(vdata2, NULL);
0117
0118
0119
0120
0121 assert_equal(Bchg(p_callinfo, T_STRING_10_FLD, 0, "HELLO CALL INFO", 0L), EXSUCCEED);
0122 assert_equal(CBchg(p_callinfo, T_LONG_FLD, 0, "255266", 0L, BFLD_STRING), EXSUCCEED);
0123
0124 assert_equal(tpsetcallinfo((char *)p_ub, p_callinfo, 0), EXSUCCEED);
0125
0126
0127 assert_equal(Bchg(p_ub4, T_STRING_FLD, 0, "HELLO 4", 0L), EXSUCCEED);
0128 assert_equal(CBchg(p_ub4, T_SHORT_FLD, 0, "777", 0L, BFLD_STRING), EXSUCCEED);
0129
0130
0131
0132 assert_equal(Bchg(p_ub3, T_UBF_FLD, 0, (char *)p_ub4, 0L), EXSUCCEED);
0133 assert_equal(Bchg(p_ub3, T_STRING_5_FLD, 0, "This is string 5 ub3", 0L), EXSUCCEED);
0134
0135 assert_equal(Bchg(p_ub4, T_STRING_2_FLD, 0, "HELLO 4 PTR", 0L), EXSUCCEED);
0136 assert_equal(CBchg(p_ub4, T_SHORT_FLD, 0, "999", 0L, BFLD_STRING), EXSUCCEED);
0137
0138
0139 assert_equal(Bchg(p_ub3, T_PTR_2_FLD, 0, (char *)&p_ub4, 0L), EXSUCCEED);
0140 assert_equal(Bchg(p_ub3, T_PTR_3_FLD, 0, (char *)&p_ub4, 0L), EXSUCCEED);
0141 assert_equal(Bchg(p_ub2, T_PTR_FLD, 3, (char *)&p_ub3, 0L), EXSUCCEED);
0142 assert_equal(Bchg(p_ub, T_PTR_FLD, 3, (char *)&p_ub2, 0L), EXSUCCEED);
0143
0144
0145 assert_equal(CBchg(p_ub, T_SHORT_3_FLD, 3, "22", 0L, BFLD_STRING), EXSUCCEED);
0146 assert_equal(Bchg(p_ub, T_UBF_2_FLD, 3, (char *)p_ub4, 0L), EXSUCCEED);
0147
0148
0149 memset(&vf, 0, sizeof(vf));
0150 assert_equal(Bchg(p_ub, T_VIEW_FLD, 3, (char *)&vf, 0L), EXSUCCEED);
0151
0152
0153
0154 memset(&vf, 0, sizeof(vf));
0155
0156 vf.data=(char *)&vdata;
0157 vf.vflags=0;
0158 NDRX_STRCPY_SAFE(vf.vname, "UBTESTVIEW1");
0159
0160
0161 assert_equal(Bchg(p_ub, T_VIEW_3_FLD, 3, (char *)&vf, 0L), EXSUCCEED);
0162
0163
0164 assert_equal(Bchg(p_ub, T_PTR_3_FLD, 0, (char *)&vdata2, 0L), EXSUCCEED);
0165 assert_equal(Bchg(p_ub, T_PTR_3_FLD, 1, (char *)&vdata21, 0L), EXSUCCEED);
0166
0167
0168 buf_len=sizeof(buf);
0169
0170
0171
0172
0173
0174 assert_equal(ndrx_mbuf_prepare_outgoing ((char *)p_ub, 0, buf, &buf_len, 0, 0), EXSUCCEED);
0175
0176
0177 ndrx_mbuf_tlv_debug(buf, buf_len);
0178
0179 olen=0;
0180 assert_equal(ndrx_mbuf_prepare_incoming (buf, buf_len, (char **)&p_ub5, &olen, 0, 0), EXSUCCEED);
0181
0182
0183
0184
0185
0186
0187
0188 assert_equal(Bget(p_ub5, T_PTR_FLD, 0, (char *)&p_ub5_2, 0L), EXSUCCEED);
0189 assert_equal(p_ub5_2, NULL);
0190
0191 assert_equal(Bget(p_ub5, T_PTR_FLD, 1, (char *)&p_ub5_2, 0L), EXSUCCEED);
0192 assert_equal(p_ub5_2, NULL);
0193
0194 assert_equal(Bget(p_ub5, T_PTR_FLD, 2, (char *)&p_ub5_2, 0L), EXSUCCEED);
0195 assert_equal(p_ub5_2, NULL);
0196
0197 assert_equal(Bget(p_ub5, T_PTR_FLD, 3, (char *)&p_ub5_2, 0L), EXSUCCEED);
0198 assert_not_equal(p_ub5_2, NULL);
0199
0200 assert_equal(Bget(p_ub5, T_PTR_3_FLD, 0, (char *)&vdata5_2, 0L), EXSUCCEED);
0201 assert_not_equal(vdata5_2, NULL);
0202
0203 assert_equal(Bget(p_ub5, T_PTR_3_FLD, 1, (char *)&vdata5_21, 0L), EXSUCCEED);
0204 assert_not_equal(vdata5_21, NULL);
0205
0206
0207
0208 assert_equal(Bget(p_ub5_2, T_PTR_FLD, 0, (char *)&p_ub5_3, 0L), EXSUCCEED);
0209 assert_equal(p_ub5_3, NULL);
0210
0211 assert_equal(Bget(p_ub5_2, T_PTR_FLD, 1, (char *)&p_ub5_3, 0L), EXSUCCEED);
0212 assert_equal(p_ub5_3, NULL);
0213
0214 assert_equal(Bget(p_ub5_2, T_PTR_FLD, 2, (char *)&p_ub5_3, 0L), EXSUCCEED);
0215 assert_equal(p_ub5_3, NULL);
0216
0217 assert_equal(Bget(p_ub5_2, T_PTR_FLD, 4, (char *)&p_ub5_3, 0L), EXFAIL);
0218 assert_equal(p_ub5_3, NULL);
0219
0220 assert_equal(Bget(p_ub5_2, T_PTR_FLD, 3, (char *)&p_ub5_3, 0L), EXSUCCEED);
0221 assert_not_equal(p_ub5_3, NULL);
0222
0223
0224 assert_equal(Bget(p_ub5_3, T_PTR_2_FLD, 0, (char *)&p_ub5_4, 0L), EXSUCCEED);
0225 assert_not_equal(p_ub5_4, NULL);
0226
0227
0228 assert_equal(Bcmp(p_ub5_4, p_ub4), 0);
0229
0230
0231
0232 Bprint(p_ub5_3);
0233
0234 assert_equal(Bdel(p_ub3, T_PTR_2_FLD, 0), EXSUCCEED);
0235 Bprint(p_ub5_3);
0236 assert_equal(Bdel(p_ub5_3, T_PTR_2_FLD, 0), EXSUCCEED);
0237
0238 assert_equal(Bdel(p_ub3, T_PTR_3_FLD, 0), EXSUCCEED);
0239 assert_equal(Bdel(p_ub5_3, T_PTR_3_FLD, 0), EXSUCCEED);
0240
0241 assert_equal(Bcmp(p_ub5_3, p_ub3), 0);
0242
0243
0244
0245 assert_equal(Bdel(p_ub2, T_PTR_FLD, 3), EXSUCCEED);
0246 assert_equal(Bdel(p_ub5_2, T_PTR_FLD, 3), EXSUCCEED);
0247 assert_equal(Bcmp(p_ub5_2, p_ub2), 0);
0248
0249
0250
0251
0252 assert_equal(Bdel(p_ub, T_PTR_FLD, 3), EXSUCCEED);
0253 assert_equal(Bdel(p_ub5, T_PTR_FLD, 3), EXSUCCEED);
0254
0255 assert_equal(Bdel(p_ub, T_PTR_3_FLD, 1), EXSUCCEED);
0256 assert_equal(Bdel(p_ub5, T_PTR_3_FLD, 1), EXSUCCEED);
0257
0258 assert_equal(Bdel(p_ub, T_PTR_3_FLD, 0), EXSUCCEED);
0259 assert_equal(Bdel(p_ub5, T_PTR_3_FLD, 0), EXSUCCEED);
0260
0261 assert_equal(Bcmp(p_ub5, p_ub), 0);
0262
0263
0264 assert_equal(Bvcmp((char *)vdata2, "UBTESTVIEW1", (char *)vdata5_2, "UBTESTVIEW1"), 0);
0265 assert_equal(Bvcmp((char *)vdata21, "UBTESTVIEW1", (char *)vdata5_21, "UBTESTVIEW1"), 0);
0266
0267
0268 assert_equal(tpgetcallinfo((char *)p_ub5, (UBFH **)&p_callinfo5, 0), EXSUCCEED);
0269 assert_equal(Bcmp(p_callinfo, p_callinfo5), 0);
0270
0271
0272 tpfree((char *)p_ub);
0273 tpfree((char *)p_ub2);
0274 tpfree((char *)p_ub3);
0275 tpfree((char *)p_ub4);
0276 tpfree((char *)p_ub5);
0277 tpfree((char *)vdata2);
0278 tpfree((char *)vdata21);
0279
0280 tpfree((char *)p_ub5_2);
0281 tpfree((char *)p_ub5_3);
0282 tpfree((char *)p_ub5_4);
0283 tpfree((char *)p_callinfo);
0284 tpfree((char *)p_callinfo5);
0285 tpfree((char *)vdata5_2);
0286 tpfree((char *)vdata5_21);
0287
0288
0289 assert_equal(ndrx_buffer_list(&list), EXSUCCEED);
0290 assert_equal(list.maxindexused, -1);
0291 }
0292
0293 }
0294
0295 Ensure(test_mbuf_nocallinfo)
0296 {
0297 UBFH *p_ub = NULL;
0298 UBFH *p_ci = NULL;
0299 UBFH *p_ub_rcv = NULL;
0300 char buf[8096];
0301 long buf_len;
0302 long olen;
0303 ndrx_growlist_t list;
0304
0305 p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0306 assert_not_equal(p_ub, NULL);
0307 assert_equal(Bchg(p_ub, T_STRING_10_FLD, 0, "HELLO MAIN", 0L), EXSUCCEED);
0308
0309 p_ci = (UBFH *)tpalloc("UBF", NULL, 1024);
0310 assert_not_equal(p_ci, NULL);
0311 assert_equal(Bchg(p_ci, T_STRING_10_FLD, 0, "HELLO HEADER", 0L), EXSUCCEED);
0312
0313 assert_equal(tpsetcallinfo((char *)p_ub, p_ci, 0), EXSUCCEED);
0314
0315 buf_len=sizeof(buf);
0316 assert_equal(ndrx_mbuf_prepare_outgoing ((char *)p_ub, 0, buf, &buf_len, 0,
0317 NDRX_MBUF_FLAG_NOCALLINFO), EXSUCCEED);
0318
0319
0320 ndrx_mbuf_tlv_debug(buf, buf_len);
0321
0322 olen=0;
0323 assert_equal(ndrx_mbuf_prepare_incoming (buf, buf_len, (char **)&p_ub_rcv, &olen, 0, 0), EXSUCCEED);
0324
0325
0326 tpfree((char *)p_ci);
0327 p_ci=NULL;
0328 assert_equal(tpgetcallinfo((char *)p_ub_rcv, (UBFH **)&p_ci, 0), EXFAIL);
0329 assert_equal(tperrno, TPESYSTEM);
0330
0331 tpfree((char *)p_ub_rcv);
0332 tpfree((char *)p_ub);
0333
0334
0335 assert_equal(ndrx_buffer_list(&list), EXSUCCEED);
0336 assert_equal(list.maxindexused, -1);
0337
0338 }
0339
0340
0341
0342
0343 Ensure(test_mbuf_buf_full_ubf)
0344 {
0345 int ret=EXSUCCEED;
0346 UBFH *p_ub = NULL;
0347 UBFH *p_ub2 = NULL;
0348 char buf[4096];
0349 char blob[5000];
0350 long buf_len;
0351 int i;
0352 int fail1, fail2;
0353
0354 for (i=0; i<10000; i++)
0355 {
0356 p_ub = (UBFH *)tpalloc("UBF", NULL, 10000);
0357
0358 if (NULL==p_ub)
0359 {
0360 NDRX_LOG(log_error, "TESTERROR: Failed to alloc: %s", tpstrerror(tperrno));
0361 EXFAIL_OUT(ret);
0362 }
0363
0364 p_ub2 = (UBFH *)tpalloc("UBF", NULL, 10000);
0365
0366 if (NULL==p_ub2)
0367 {
0368 NDRX_LOG(log_error, "TESTERROR: Failed to alloc 2: %s", tpstrerror(tperrno));
0369 EXFAIL_OUT(ret);
0370 }
0371
0372 if (EXSUCCEED!=Bchg(p_ub, T_CARRAY_2_FLD, 0, blob, i+1))
0373 {
0374 NDRX_LOG(log_error, "TESTERROR: Failed to add blob: %s", Bstrerror(Berror));
0375 EXFAIL_OUT(ret);
0376 }
0377
0378 if (EXSUCCEED!=Bchg(p_ub2, T_CARRAY_2_FLD, 0, blob, i+1))
0379 {
0380 NDRX_LOG(log_error, "TESTERROR: Failed to add blob: %s", Bstrerror(Berror));
0381 EXFAIL_OUT(ret);
0382 }
0383
0384
0385 if (EXSUCCEED!=Bchg(p_ub, T_PTR_FLD, 0, (char *)&p_ub2, 0))
0386 {
0387 NDRX_LOG(log_error, "TESTERROR: Failed to add blob PTR: %s",
0388 Bstrerror(Berror));
0389 EXFAIL_OUT(ret);
0390 }
0391
0392 fail1=EXFALSE;
0393
0394
0395 buf_len=sizeof(buf);
0396 ret=ndrx_mbuf_prepare_outgoing ((char *)p_ub, 0, buf, &buf_len, 0, 0);
0397
0398 if (EXFAIL==ret)
0399 {
0400
0401 if (tperrno==TPEINVAL && i> 1000)
0402 {
0403 ret=EXSUCCEED;
0404 fail1=EXTRUE;
0405 }
0406 else
0407 {
0408 NDRX_LOG(log_error, "Failed to prep outgoing loop %d: %s",
0409 i, tpstrerror(tperrno));
0410 EXFAIL_OUT(ret);
0411 }
0412 }
0413
0414
0415 fail2=EXFALSE;
0416 buf_len=sizeof(buf);
0417 ret=ndrx_mbuf_prepare_outgoing ((char *)p_ub2, 0, buf, &buf_len, 0, 0);
0418
0419 if (EXFAIL==ret)
0420 {
0421
0422 if (tperrno==TPEINVAL && i> 1000)
0423 {
0424 ret=EXSUCCEED;
0425 fail2=EXTRUE;
0426 }
0427 else
0428 {
0429 NDRX_LOG(log_error, "Failed to prep outgoing loop %d: %s",
0430 i, tpstrerror(tperrno));
0431 EXFAIL_OUT(ret);
0432 }
0433 }
0434
0435 if (!fail1 && fail2)
0436 {
0437 NDRX_LOG(log_error, "TESTERROR: Expected fail1 && !fail2");
0438 EXFAIL_OUT(ret);
0439 }
0440
0441 if (fail1 && fail2)
0442 {
0443 break;
0444 }
0445
0446 tpfree((char *)p_ub);
0447 tpfree((char *)p_ub2);
0448 p_ub = NULL;
0449 p_ub2 = NULL;
0450 }
0451
0452 out:
0453
0454 if (NULL!=p_ub)
0455 {
0456 tpfree((char *)p_ub);
0457 }
0458
0459 if (NULL!=p_ub2)
0460 {
0461 tpfree((char *)p_ub2);
0462 }
0463
0464 assert_equal(ret, EXSUCCEED);
0465 assert_equal(fail1, EXTRUE);
0466 assert_equal(fail2, EXTRUE);
0467
0468 }
0469
0470
0471
0472
0473
0474 Ensure(test_mbuf_buf_full_any)
0475 {
0476 int ret=EXSUCCEED;
0477 char buf[4096];
0478 long buf_len;
0479 int j, i, k;
0480 char *p_buf=NULL;
0481 char *btypes[] = {"STRING", "CARRAY", "JSON"};
0482 int btypes_ok[3];
0483
0484 memset(btypes_ok, 0, sizeof(btypes_ok));
0485
0486 for (i=0; i<N_DIM(btypes); i++)
0487 {
0488 for (j=0; j<10000; j++)
0489 {
0490 p_buf = tpalloc(btypes[i], NULL, 10000);
0491
0492 if (NULL==p_buf)
0493 {
0494 NDRX_LOG(log_error, "TESTERROR: Failed to alloc %s: %s",
0495 btypes[i], tpstrerror(tperrno));
0496 EXFAIL_OUT(ret);
0497 }
0498
0499 memset(p_buf, 0, sizeof(j));
0500
0501 for (k=0; k<j; k++)
0502 {
0503 p_buf[k]=' ';
0504 }
0505
0506 buf_len=sizeof(buf);
0507 ret=ndrx_mbuf_prepare_outgoing (p_buf, j, buf, &buf_len, 0, 0);
0508
0509 if (EXFAIL==ret)
0510 {
0511
0512 if (tperrno==TPEINVAL)
0513 {
0514 ret=EXSUCCEED;
0515 btypes_ok[i]=EXTRUE;
0516 }
0517 else
0518 {
0519 NDRX_LOG(log_error, "Failed to prep outgoing loop %d: %s",
0520 j, tpstrerror(tperrno));
0521 EXFAIL_OUT(ret);
0522 }
0523 }
0524
0525 tpfree(p_buf);
0526 p_buf=NULL;
0527
0528 }
0529 }
0530
0531 out:
0532
0533 if (NULL!=p_buf)
0534 {
0535 tpfree(p_buf);
0536 }
0537 assert_equal(ret, EXSUCCEED);
0538 assert_equal(btypes_ok[0], EXTRUE);
0539 assert_equal(btypes_ok[1], EXTRUE);
0540 assert_equal(btypes_ok[2], EXTRUE);
0541
0542 }
0543
0544
0545
0546
0547 Ensure(test_mbuf_buf_carray_ilen)
0548 {
0549 int ret=EXSUCCEED;
0550 char buf[4096];
0551 long buf_len;
0552 char *p_buf=NULL;
0553
0554 p_buf = tpalloc("CARRAY", NULL, 50000);
0555
0556 if (NULL==p_buf)
0557 {
0558 NDRX_LOG(log_error, "TESTERROR: Failed to alloc CARRAY: %s", tpstrerror(tperrno));
0559 EXFAIL_OUT(ret);
0560 }
0561
0562 buf_len=sizeof(buf);
0563 ret=ndrx_mbuf_prepare_outgoing (p_buf, 1000, buf, &buf_len, 0, 0);
0564
0565 assert_equal(ret, EXSUCCEED);
0566
0567 assert_equal(!! (buf_len < 1100), EXTRUE);
0568
0569 out:
0570
0571 if (NULL!=p_buf)
0572 {
0573 tpfree(p_buf);
0574 }
0575 }
0576
0577
0578
0579
0580
0581 Ensure(test_mbuf_reuse)
0582 {
0583 char buf[8096];
0584 long buf_len;
0585 char *carray = tpalloc("CARRAY", NULL, 4096);
0586 UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 8186);
0587 ndrx_growlist_t list;
0588
0589 assert_not_equal(carray, NULL);
0590 assert_not_equal(p_ub, NULL);
0591
0592
0593 assert_equal(Bchg(p_ub, T_PTR_FLD, 0, (char *)&carray, 0L), EXSUCCEED);
0594 assert_equal(Bchg(p_ub, T_PTR_2_FLD, 0, (char *)&carray, 0L), EXSUCCEED);
0595
0596 buf_len = sizeof(buf);
0597 assert_equal(ndrx_mbuf_prepare_outgoing ((char *)p_ub, 0, buf, &buf_len, 0, 0), EXSUCCEED);
0598
0599
0600 assert_equal(ndrx_mbuf_prepare_outgoing ((char *)p_ub, 0, buf, &buf_len, 0, 0), EXSUCCEED);
0601
0602
0603 assert_equal(!!(buf_len < 4096+1024), EXTRUE);
0604 assert_equal(!!(buf_len > 4096), EXTRUE);
0605
0606 tpfree(carray);
0607 tpfree((char *)p_ub);
0608
0609
0610 assert_equal(ndrx_buffer_list(&list), EXSUCCEED);
0611 assert_equal(list.maxindexused, -1);
0612
0613 }
0614
0615
0616
0617
0618
0619 TestSuite *atmiunit0_mbuf(void)
0620 {
0621 TestSuite *suite = create_test_suite();
0622
0623 add_test(suite, test_mbuf);
0624 add_test(suite, test_mbuf_reuse);
0625 add_test(suite, test_mbuf_nocallinfo);
0626 add_test(suite, test_mbuf_buf_full_ubf);
0627 add_test(suite, test_mbuf_buf_full_any);
0628 add_test(suite, test_mbuf_buf_carray_ilen);
0629
0630 return suite;
0631 }
0632
0633