[go: up one dir, main page]

Menu

[8b2b2c]: / src / testlo.c  Maximize  Restore  History

Download this file

1706 lines (1435 with data), 49.1 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
/*
* Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* $Id$
*/
/*
* This is some testcase code - it exercises the internals of liblo, so its not
* a good example to learn from, see examples/ for example code
*/
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#define snprintf _snprintf
#else
#include <unistd.h>
#endif
#ifdef WIN32
#include <process.h>
#include <direct.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "lo_types_internal.h"
#include "lo_internal.h"
#include "lo/lo.h"
#if defined(WIN32) || defined(_MSC_VER)
#define PATHDELIM "\\"
#define EXTEXE ".exe"
#define SLEEP_MS(x) Sleep(x)
#else
#define PATHDELIM "/"
#define EXTEXE ""
#define SLEEP_MS(x) usleep((x)*1000)
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#define TEST(cond) if (!(cond)) { fprintf(stderr, "FAILED " #cond \
" at %s:%d\n", __FILE__, __LINE__); \
exit(1); } \
else { printf("passed " #cond "\n"); }
#define DOING(s) printf("\n == " s "() ==\n\n")
#define HANDLER(h) printf(" <-- " h "_handler()\n")
union end_test32 {
uint32_t i;
char c[4];
};
union end_test64 {
uint64_t i;
char c[8];
};
static int done = 0;
static int bundle_count = 0;
static int pattern_count = 0;
static int reply_count = 0;
static int subtest_count = 0;
static int subtest_reply_count = 0;
static int error_okay = 0;
static int tcp_done = 0;
char testdata[5] = "ABCD";
static int jitter_count = 0;
static float jitter_total = 0.0f;
static float jitter_max = 0.0f;
static float jitter_min = 1000.0f;
uint8_t midi_data[4] = { 0xff, 0xf7, 0xAA, 0x00 };
void exitcheck(void);
void test_deserialise(void);
void test_validation(lo_address a);
void test_multicast(lo_server_thread st);
void error(int num, const char *m, const char *path);
void rep_error(int num, const char *m, const char *path);
int generic_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int foo_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int reply_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int lots_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int coerce_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int bundle_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int timestamp_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int jitter_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int pattern_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int subtest_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int subtest_reply_handler(const char *path, const char *types,
lo_arg ** argv, int argc, lo_message data,
void *user_data);
int quit_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data);
int test_varargs(lo_address a, const char *path, const char *types, ...);
int test_version();
void test_types();
void test_url();
void test_address();
void test_blob();
void test_server_thread(lo_server_thread *pst, lo_address *pa);
void test_message(lo_address a);
void test_pattern(lo_address a);
void test_subtest(lo_server_thread st);
void test_bundle(lo_server_thread st, lo_address a);
void test_nonblock();
void test_unix_sockets();
void test_tcp();
void test_tcp_nonblock();
void cleanup(lo_server_thread st, lo_address a);
int main()
{
#ifdef ENABLE_NETWORK_TESTS
lo_server_thread st;
lo_address a;
#endif
atexit(exitcheck);
test_version();
test_deserialise();
test_types();
test_url();
test_address();
test_blob();
#ifdef ENABLE_NETWORK_TESTS
test_server_thread(&st, &a);
test_validation(a);
test_multicast(st);
test_message(a);
test_pattern(a);
test_subtest(st);
test_bundle(st, a);
test_nonblock();
test_unix_sockets();
test_tcp();
test_tcp_nonblock();
cleanup(st, a);
#else
done = 1;
#endif
return 0;
}
void exitcheck(void)
{
if (!done) {
fprintf(stderr, "\ntest run not completed\n" PACKAGE_NAME
" test FAILED\n");
} else {
printf(PACKAGE_NAME " test PASSED\n");
}
}
void error(int num, const char *msg, const char *path)
{
fprintf(stderr, "liblo server error %d in %s: %s", num, path, msg);
if (!error_okay)
exit(1);
else
printf(" (expected)\n");
}
void rep_error(int num, const char *msg, const char *path)
{
if (num != 9904) {
error(num, msg, path);
}
}
int generic_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
int i;
HANDLER("generic");
printf("path: <%s>\n", path);
for (i = 0; i < argc; i++) {
printf("arg %d '%c' ", i, types[i]);
lo_arg_pp((lo_type) types[i], argv[i]);
printf("\n");
}
printf("\n");
return 1;
}
int foo_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("foo");
lo_server serv = (lo_server) user_data;
lo_address src = lo_message_get_source(data);
char *url = lo_address_get_url(src);
char *server_url = lo_server_get_url(serv);
printf("Address of us: %s\n", server_url);
printf("%s <- f:%f, i:%d\n", path, argv[0]->f, argv[1]->i);
if (lo_send_from(src, serv, LO_TT_IMMEDIATE, "/reply", "s", "a reply")
== -1) {
printf("OSC reply error %d: %s\nSending to %s\n",
lo_address_errno(src), lo_address_errstr(src), url);
exit(1);
} else {
printf("Reply sent to %s\n\n", url);
}
free(server_url);
free(url);
return 0;
}
int reply_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("reply");
lo_address src = lo_message_get_source(data);
char *url = lo_address_get_url(src);
printf("Reply received from %s\n", url);
free(url);
reply_count++;
return 0;
}
int lots_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
lo_blob b;
unsigned char *d;
HANDLER("lots");
if (strcmp(path, "/lotsofformats")) {
fprintf(stderr, "path != /lotsofformats\n");
exit(1);
}
printf("path = %s\n", path);
TEST(types[0] == 'f' && argv[0]->f == 0.12345678f);
TEST(types[1] == 'i' && argv[1]->i == 123);
TEST(types[2] == 's' && !strcmp(&argv[2]->s, "123"));
b = (lo_blob) argv[3];
d = (unsigned char *) lo_blob_dataptr(b);
TEST(types[3] == 'b' && lo_blob_datasize(b) == 5);
TEST(d[0] == 'A' && d[1] == 'B' && d[2] == 'C' && d[3] == 'D');
d = argv[4]->m;
TEST(d[0] == 0xff && d[1] == 0xf7 && d[2] == 0xaa && d[3] == 0x00);
TEST(types[5] == 'h' && argv[5]->h == 0x0123456789ABCDEFULL);
TEST(types[6] == 't' && argv[6]->t.sec == 1 &&
argv[6]->t.frac == 0x80000000);
TEST(types[7] == 'd' && argv[7]->d == 0.9999);
TEST(types[8] == 'S' && !strcmp(&argv[8]->S, "sym"));
printf("char: %d\n", argv[9]->c);
TEST(types[9] == 'c' && argv[9]->c == 'X');
TEST(types[10] == 'c' && argv[10]->c == 'Y');
TEST(types[11] == 'T');
TEST(types[12] == 'F');
TEST(types[13] == 'N');
TEST(types[14] == 'I');
printf("\n");
return 0;
}
int coerce_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("coerce");
printf("path = %s\n", path);
TEST(types[0] == 'd' && fabs(argv[0]->d - 0.1) < FLT_EPSILON);
TEST(types[1] == 'f' && fabs(argv[1]->f - 0.2) < FLT_EPSILON);
TEST(types[2] == 'h' && argv[2]->h == 123);
TEST(types[3] == 'i' && argv[3]->i == 124);
TEST(types[4] == 'S' && !strcmp(&argv[4]->S, "aaa"));
TEST(types[5] == 's' && !strcmp(&argv[5]->s, "bbb"));
printf("\n");
return 0;
}
int bundle_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("bundle");
bundle_count++;
printf("received bundle\n");
return 0;
}
int timestamp_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
int bundled = argv[0]->i;
lo_timetag ts, arg_ts;
HANDLER("timestamp");
ts = lo_message_get_timestamp(data);
arg_ts = argv[1]->t;
if (bundled) {
TEST((ts.sec == arg_ts.sec) && (ts.frac == arg_ts.frac));
} else {
TEST(ts.sec == LO_TT_IMMEDIATE.sec
&& ts.frac == LO_TT_IMMEDIATE.frac);
}
return 0;
}
int jitter_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
lo_timetag now;
float jitter;
HANDLER("jitter");
lo_timetag_now(&now);
jitter = fabs(lo_timetag_diff(now, argv[0]->t));
jitter_count++;
//printf("jitter: %f\n", jitter);
printf("%d expected: %x:%x received %x:%x\n", argv[1]->i,
argv[0]->t.sec, argv[0]->t.frac, now.sec, now.frac);
jitter_total += jitter;
if (jitter > jitter_max)
jitter_max = jitter;
if (jitter < jitter_min)
jitter_min = jitter;
return 0;
}
int pattern_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("pattern");
pattern_count++;
printf("pattern matched %s\n", (char*) user_data);
return 0;
}
int subtest_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
lo_address a;
HANDLER("subtest");
a = lo_message_get_source(data);
subtest_count++;
lo_send_from(a, lo_server_thread_get_server((lo_server_thread)user_data),
LO_TT_IMMEDIATE, "/subtest", "i", subtest_count);
return 0;
}
int subtest_reply_handler(const char *path, const char *types,
lo_arg ** argv, int argc, lo_message data,
void *user_data)
{
HANDLER("subtest_reply");
subtest_reply_count++;
return 0;
}
int quit_handler(const char *path, const char *types, lo_arg ** argv,
int argc, lo_message data, void *user_data)
{
HANDLER("quit");
done = 1;
return 0;
}
int test_version()
{
int major, minor, lt_maj, lt_min, lt_bug;
char extra[256];
char string[256];
DOING("test_version");
lo_version(string, 256,
&major, &minor, extra, 256,
&lt_maj, &lt_min, &lt_bug);
printf("liblo version string `%s'\n", string);
printf("liblo version: %d.%d%s\n", major, minor, extra);
printf("liblo libtool version: %d.%d.%d\n", lt_maj, lt_min, lt_bug);
printf("\n");
return 0;
}
int test_varargs(lo_address a, const char *path, const char *types, ...)
{
va_list ap;
lo_message m;
int error;
DOING("test_varargs");
m = lo_message_new();
va_start(ap, types);
if ((error = lo_message_add_varargs(m, types, ap)) == 0)
lo_send_message(a, path, m);
else
printf("lo_message_add_varargs returned %d\n", error);
lo_message_free(m);
return error < 0;
}
void replace_char(char *str, size_t size, const char find,
const char replace)
{
char *p = str;
while (size--) {
if (find == *p) {
*p = replace;
}
++p;
}
}
void test_deserialise()
{
char *buf, *buf2, *tmp;
const char *types = NULL, *path;
lo_arg **argv = NULL;
size_t len, size;
char data[256];
int result = 0;
lo_blob btest;
lo_timetag tt = { 0x1, 0x80000000 };
lo_blob b = NULL;
DOING("test_deserialise");
btest = lo_blob_new(sizeof(testdata), testdata);
// build a message
lo_message msg = lo_message_new();
TEST(0 == lo_message_get_argc(msg));
lo_message_add_float(msg, 0.12345678f); // 0 f
lo_message_add_int32(msg, 123); // 1 i
lo_message_add_string(msg, "123"); // 2 s
lo_message_add_blob(msg, btest); // 3 b
lo_message_add_midi(msg, midi_data); // 4 m
lo_message_add_int64(msg, 0x0123456789abcdefULL); // 5 h
lo_message_add_timetag(msg, tt); // 6 t
lo_message_add_double(msg, 0.9999); // 7 d
lo_message_add_symbol(msg, "sym"); // 8 S
lo_message_add_char(msg, 'X'); // 9 c
lo_message_add_char(msg, 'Y'); // 10 c
lo_message_add_true(msg); // 11 T
lo_message_add_false(msg); // 12 F
lo_message_add_nil(msg); // 13 N
lo_message_add_infinitum(msg); // 14 I
// test types, args
TEST(15 == lo_message_get_argc(msg));
types = lo_message_get_types(msg);
TEST(NULL != types);
argv = lo_message_get_argv(msg);
TEST(NULL != argv);
TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON);
TEST('i' == types[1] && 123 == argv[1]->i);
TEST('s' == types[2] && !strcmp(&argv[2]->s, "123"));
TEST('b' == types[3]);
b = (lo_blob) argv[3];
TEST(lo_blob_datasize(b) == sizeof(testdata));
TEST(12 == lo_blobsize(b));
TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata)));
TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4));
TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h);
TEST('t' == types[6] && 1 == argv[6]->t.sec
&& 0x80000000 == argv[6]->t.frac);
TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON);
TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym"));
TEST('c' == types[9] && 'X' == argv[9]->c);
TEST('c' == types[10] && 'Y' == argv[10]->c);
TEST('T' == types[11] && NULL == argv[11]);
TEST('F' == types[12] && NULL == argv[12]);
TEST('N' == types[13] && NULL == argv[13]);
TEST('I' == types[14] && NULL == argv[14]);
// serialise it
len = lo_message_length(msg, "/foo");
printf("serialise message_length=%d\n", (int) len);
buf = (char*) calloc(len, sizeof(char));
size = 0;
tmp = (char*) lo_message_serialise(msg, "/foo", buf, &size);
TEST(tmp == buf && size == len && 92 == len);
lo_message_free(msg);
// deserialise it
printf("deserialise\n");
path = lo_get_path(buf, len);
TEST(NULL != path && !strcmp(path, "/foo"));
msg = lo_message_deserialise(buf, size, NULL);
TEST(NULL != msg);
// repeat same test as above
TEST(15 == lo_message_get_argc(msg));
types = lo_message_get_types(msg);
TEST(NULL != types);
argv = lo_message_get_argv(msg);
TEST(NULL != argv);
TEST('f' == types[0] && fabs(argv[0]->f - 0.12345678f) < FLT_EPSILON);
TEST('i' == types[1] && 123 == argv[1]->i);
TEST('s' == types[2] && !strcmp(&argv[2]->s, "123"));
TEST('b' == types[3]);
b = (lo_blob) argv[3];
TEST(lo_blob_datasize(b) == sizeof(testdata));
TEST(12 == lo_blobsize(b));
TEST(!memcmp(lo_blob_dataptr(b), &testdata, sizeof(testdata)));
TEST('m' == types[4] && !memcmp(&argv[4]->m, midi_data, 4));
TEST('h' == types[5] && 0x0123456789abcdefULL == argv[5]->h);
TEST('t' == types[6] && 1 == argv[6]->t.sec
&& 0x80000000 == argv[6]->t.frac);
TEST('d' == types[7] && fabs(argv[7]->d - 0.9999) < FLT_EPSILON);
TEST('S' == types[8] && !strcmp(&argv[8]->s, "sym"));
TEST('c' == types[9] && 'X' == argv[9]->c);
TEST('c' == types[10] && 'Y' == argv[10]->c);
TEST('T' == types[11] && NULL == argv[11]);
TEST('F' == types[12] && NULL == argv[12]);
TEST('N' == types[13] && NULL == argv[13]);
TEST('I' == types[14] && NULL == argv[14]);
// serialise it again, compare
len = lo_message_length(msg, "/foo");
printf("serialise message_length=%d\n", (int) len);
buf2 = (char*) calloc(len, sizeof(char));
size = 0;
tmp = (char*) lo_message_serialise(msg, "/foo", buf2, &size);
TEST(tmp == buf2 && size == len && 92 == len);
TEST(!memcmp(buf, buf2, len));
lo_message_free(msg);
lo_blob_free(btest);
free(buf);
free(buf2);
// deserialise failure tests with invalid message data
msg = lo_message_deserialise(data, 0, &result); // 0 size
TEST(NULL == msg && LO_ESIZE == result);
snprintf(data, 256, "%s", "/foo"); // unterminated path string
msg = lo_message_deserialise(data, 4, &result);
TEST(NULL == msg && LO_EINVALIDPATH == result);
snprintf(data, 256, "%s", "/f_o"); // non-0 in pad area
msg = lo_message_deserialise(data, 4, &result);
TEST(NULL == msg && LO_EINVALIDPATH == result);
snprintf(data, 256, "%s", "/t__"); // types missing
replace_char(data, 4, '_', '\0');
msg = lo_message_deserialise(data, 4, &result);
TEST(NULL == msg && LO_ENOTYPE == result);
snprintf(data, 256, "%s%s", "/t__", "____"); // types empty
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 8, &result);
TEST(NULL == msg && LO_EBADTYPE == result);
snprintf(data, 256, "%s%s", "/t__", ",f_"); // short message
replace_char(data, 7, '_', '\0');
msg = lo_message_deserialise(data, 7, &result);
TEST(NULL == msg && LO_EINVALIDTYPE == result);
snprintf(data, 256, "%s%s", "/t__", "ifi_"); // types missing comma
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 8, &result);
TEST(NULL == msg && LO_EBADTYPE == result);
snprintf(data, 256, "%s%s", "/t__", ",ifi"); // types unterminated
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 8, &result);
TEST(NULL == msg && LO_EINVALIDTYPE == result);
snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 12, &result);
TEST(NULL == msg && LO_EINVALIDARG == result);
snprintf(data, 256, "%s%s", "/t__", ",ii_"); // not enough arg data again
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 15, &result);
TEST(NULL == msg && LO_EINVALIDARG == result);
snprintf(data, 256, "%s%s", "/t__", ",f__"); // too much arg data
replace_char(data, 8, '_', '\0');
msg = lo_message_deserialise(data, 16, &result);
TEST(NULL == msg && LO_ESIZE == result);
snprintf(data, 256, "%s%s", "/t__", ",bs_"); // blob longer than msg length
replace_char(data, 8, '_', '\0');
*(uint32_t *) (data + 8) = lo_htoo32((uint32_t) 99999);
msg = lo_message_deserialise(data, 256, &result);
TEST(NULL == msg && LO_EINVALIDARG == result);
}
void test_validation(lo_address a)
{
/* packet crafted to crash a lo_server when no input validation is performed */
char mem[] = { "/\0\0\0,bs\0,\x00\x0F\x42\x3F" }; // OSC: "/" ",bs" 999999
int eok = error_okay;
int sock = a->socket;
/* This code won't work with MSVC because the lo_client_sockets data structure
* is not explicitly made available to external programs. We could expose it
* in debug mode, perhaps, but let's just skip this test for now. (Can be tested
* on Windows using MingW.) */
#ifdef _MSC_VER
return;
#else
DOING("test_validation");
/* Note: lo_client_sockets is not available when liblo is compiled
* as a DLL. */
#if !defined(WIN32) && !defined(_MSC_VER)
if (sock == -1)
sock = lo_client_sockets.udp;
#endif
if (sock == -1) {
fprintf(stderr,
"Warning: Couldn't get socket in test_validation(), "
"lo_client_sockets.udp not supported on Windows, %s:%d\n",
__FILE__, __LINE__);
return;
}
error_okay = 1;
if (sendto(sock, (void*)&mem, sizeof(mem), MSG_NOSIGNAL,
a->ai->ai_addr, a->ai->ai_addrlen) == -1) {
fprintf(stderr,
"Error sending packet in test_validation(), %s:%d\n",
__FILE__, __LINE__);
}
SLEEP_MS(10);
error_okay = eok;
#endif
}
void test_multicast(lo_server_thread st)
{
lo_server ms;
lo_address ma;
DOING("test_multicast");
#ifdef ENABLE_IPV6
// Print a warning but we let it fail, prefer to actually fix IPv6
// support rather than just skip the test!
printf("WARNING: Compiled with --enable-ipv6, multicast not supported;"
"failure expected.\n");
#endif
/* test multicast server and sender */
/* message is sent from st otherwise reply doesn't work */
ms = lo_server_new_multicast("224.0.1.1", "15432", error);
ma = lo_address_new("224.0.1.1", "15432");
lo_address_set_ttl(ma, 1);
lo_server_add_method(ms, "/foo/bar", "fi", foo_handler, ms);
lo_server_add_method(ms, "/reply", "s", reply_handler, NULL);
if (lo_send_from(ma, lo_server_thread_get_server(st), LO_TT_IMMEDIATE,
"/foo/bar", "ff", 0.12345678f, 23.0f) == -1) {
printf("multicast send error %d: %s\n", lo_address_errno(ma),
lo_address_errstr(ma));
exit(1);
}
TEST(lo_server_recv(ms) == 24);
lo_server_free(ms);
lo_address_free(ma);
}
int test_received = 0;
int ok_received = 0;
int recv_times = 0;
int test_handler(const char *path, const char *types,
lo_arg **argv, int argc, lo_message m,
void *data)
{
HANDLER("test");
printf("/test, %d, %s\n", argv[0]->i, &argv[1]->s);
test_received += 1;
return 0;
}
int ok_handler(const char *path, const char *types,
lo_arg **argv, int argc, lo_message m,
void *data)
{
HANDLER("ok");
ok_received += 1;
return 0;
}
#ifdef HAVE_WIN32_THREADS
unsigned __stdcall test_tcp_thread(void *context)
#else
void *test_tcp_thread(void *context)
#endif
{
lo_server s;
printf("TCP thread started.\n");
s = lo_server_new_with_proto("9000", LO_TCP, error);
if (!s) {
printf("Aborting thread, s=%p\n", s);
#ifdef HAVE_WIN32_THREADS
return 1;
#else
return (void*)1;
#endif
}
lo_server_add_method(s, "/test", "is", test_handler, 0);
lo_server_add_method(s, "/ok", "", ok_handler, 0);
while (!(done || tcp_done)) {
printf("lo_server_recv_noblock()\n");
recv_times += 1;
if (!lo_server_recv_noblock(s, 0))
SLEEP_MS(300);
lo_server_max_msg_size(s, 1024);
}
printf("Freeing.\n");
lo_server_free(s);
printf("Done. Thread ending.\n");
return 0;
}
#define SLIP_END '\xC0' /* indicates end of packet */
void test_tcp_halfsend(int stream_type)
{
char prefixmsg[] = {0,0,0,0,
'/','t','e','s','t',0,0,0,
',','i','s',0,
0,0,0,4,
'b','l','a','h',0,0,0,0,
0,0,0,0,
'/','o','k',0,
',',0,0,0};
char slipmsg[] = {'/','t','e','s','t',0,0,0,
',','i','s',0,
0,0,0,4,
'b','l','a','h',0,0,0,0,SLIP_END,
'/','o','k',0,
',',0,0,0,SLIP_END};
char *msg;
int msglen;
struct sockaddr_in sa;
int rc;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
exit(1);
}
memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(9000);
sa.sin_family = AF_INET;
rc = connect(sock, (struct sockaddr*)&sa, sizeof(struct sockaddr_in));
if (rc) {
perror("Error connecting");
closesocket(sock);
exit(1);
}
printf("Connected, sending...\n");
switch (stream_type)
{
case 0:
printf("Testing a count-prefix stream.\n");
msg = prefixmsg;
msglen = sizeof(prefixmsg);
*(uint32_t*)msg = htonl(24);
*(uint32_t*)(msg+28) = htonl(8);
break;
case 1:
printf("Testing a SLIP stream.\n");
msg = slipmsg;
msglen = sizeof(slipmsg);
break;
default:
closesocket(sock);
return;
}
if (0)
{
printf("Sending everything in one big chunk.\n");
rc = send(sock, msg, msglen, 0);
if (rc != msglen) printf("Error sending, rc = %d\n", rc);
else printf("Sent.\n");
}
else
{
rc = send(sock, msg, 13, 0);
if (rc != 13) printf("Error sending, rc = %d\n", rc);
else printf("Sent.\n");
SLEEP_MS(1000);
rc = send(sock, msg+13, 20, 0);
if (rc != 20) printf("Error sending2, rc = %d\n", rc);
else printf("Sent2.\n");
SLEEP_MS(1000);
rc = send(sock, msg+33, msglen-33, 0);
if (rc != (msglen-33)) printf("Error sending3, rc = %d\n", rc);
else printf("Sent3.\n");
}
closesocket(sock);
}
void test_tcp_nonblock()
{
#ifdef HAVE_WIN32_THREADS
unsigned retval;
HANDLE thread;
#else
void *retval;
pthread_t thread;
#endif
DOING("test_tcp_nonblock");
tcp_done = 0;
#ifdef HAVE_WIN32_THREADS
if (!(thread=(HANDLE)_beginthreadex(NULL, 0, &test_tcp_thread, 0, 0, NULL)))
#else
if (pthread_create(&thread, 0, test_tcp_thread, 0))
#endif
{
perror("pthread_create");
exit(1);
}
SLEEP_MS(1000);
test_tcp_halfsend(0);
SLEEP_MS(1000);
test_tcp_halfsend(1);
SLEEP_MS(1000);
tcp_done = 1;
#ifdef HAVE_WIN32_THREADS
retval = WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
printf("Thread joined, retval=%u\n", retval);
#else
pthread_join(thread, &retval);
printf("Thread joined, retval=%p\n", retval);
#endif
TEST(retval == 0);
TEST(test_received == 2);
TEST(ok_received == 2);
TEST(recv_times > 10);
}
void test_types()
{
union end_test32 et32;
union end_test64 et64;
DOING("test_types");
TEST(sizeof(float) == sizeof(int32_t));
TEST(sizeof(double) == sizeof(int64_t));
et32.i = 0x23242526U;
et32.i = lo_htoo32(et32.i);
if (et32.c[0] != 0x23 || et32.c[1] != 0x24 || et32.c[2] != 0x25 ||
et32.c[3] != 0x26) {
fprintf(stderr, "failed 32bit endian conversion test\n");
fprintf(stderr, "0x23242526 -> %X\n", et32.i);
exit(1);
} else {
printf("passed 32bit endian conversion test\n");
}
et64.i = 0x232425262728292AULL;
et64.i = lo_htoo64(et64.i);
if (et64.c[0] != 0x23 || et64.c[1] != 0x24 || et64.c[2] != 0x25 ||
et64.c[3] != 0x26 || et64.c[4] != 0x27 || et64.c[5] != 0x28 ||
et64.c[6] != 0x29 || et64.c[7] != 0x2A) {
fprintf(stderr, "failed 64bit endian conversion\n");
fprintf(stderr, "0x232425262728292A -> %" PRINTF_LL "X\n",
(long long unsigned int) et64.i);
exit(1);
} else {
printf("passed 64bit endian conversion\n");
}
printf("\n");
}
void test_url()
{
int proto;
char *path, *protocol, *host, *port;
DOING("test_url");
/* OSC URL tests */
path = lo_url_get_path("osc.udp://localhost:9999/a/path/is/here");
if (strcmp(path, "/a/path/is/here")) {
printf("failed lo_url_get_path() test1\n");
printf("'%s' != '/a/path/is/here'\n", path);
exit(1);
} else {
printf("passed lo_url_get_path() test1\n");
}
free(path);
protocol =
lo_url_get_protocol("osc.udp://localhost:9999/a/path/is/here");
if (strcmp(protocol, "udp")) {
printf("failed lo_url_get_protocol() test1\n");
printf("'%s' != 'udp'\n", protocol);
exit(1);
} else {
printf("passed lo_url_get_protocol() test1\n");
}
free(protocol);
protocol =
lo_url_get_protocol("osc.tcp://localhost:9999/a/path/is/here");
if (strcmp(protocol, "tcp")) {
printf("failed lo_url_get_protocol() test2\n");
printf("'%s' != 'tcp'\n", protocol);
exit(1);
} else {
printf("passed lo_url_get_protocol() test2\n");
}
free(protocol);
protocol =
lo_url_get_protocol
("osc.udp://[::ffff:localhost]:9999/a/path/is/here");
if (strcmp(protocol, "udp")) {
printf("failed lo_url_get_protocol() test1 (IPv6)\n");
printf("'%s' != 'udp'\n", protocol);
exit(1);
} else {
printf("passed lo_url_get_protocol() test1 (IPv6)\n");
}
free(protocol);
proto =
lo_url_get_protocol_id("osc.udp://localhost:9999/a/path/is/here");
if (proto != LO_UDP) {
printf("failed lo_url_get_protocol_id() test1\n");
printf("'%d' != LO_UDP\n", proto);
exit(1);
} else {
printf("passed lo_url_get_protocol_id() test1\n");
}
proto =
lo_url_get_protocol_id("osc.tcp://localhost:9999/a/path/is/here");
if (proto != LO_TCP) {
printf("failed lo_url_get_protocol_id() test2\n");
printf("'%d' != LO_TCP\n", proto);
exit(1);
} else {
printf("passed lo_url_get_protocol_id() test2\n");
}
proto =
lo_url_get_protocol_id
("osc.invalid://localhost:9999/a/path/is/here");
if (proto != -1) {
printf("failed lo_url_get_protocol_id() test3\n");
printf("'%d' != -1\n", proto);
exit(1);
} else {
printf("passed lo_url_get_protocol_id() test3\n");
}
proto =
lo_url_get_protocol_id
("osc.udp://[::ffff:localhost]:9999/a/path/is/here");
if (proto != LO_UDP) {
printf("failed lo_url_get_protocol_id() test1 (IPv6)\n");
printf("'%d' != LO_UDP\n", proto);
exit(1);
} else {
printf("passed lo_url_get_protocol_id() test1 (IPv6)\n");
}
host =
lo_url_get_hostname
("osc.udp://foo.example.com:9999/a/path/is/here");
if (strcmp(host, "foo.example.com")) {
printf("failed lo_url_get_hostname() test1\n");
printf("'%s' != 'foo.example.com'\n", host);
exit(1);
} else {
printf("passed lo_url_get_hostname() test1\n");
}
free(host);
host =
lo_url_get_hostname
("osc.udp://[0000::::0001]:9999/a/path/is/here");
if (strcmp(host, "0000::::0001")) {
printf("failed lo_url_get_hostname() test2 (IPv6)\n");
printf("'%s' != '0000::::0001'\n", host);
exit(1);
} else {
printf("passed lo_url_get_hostname() test2 (IPv6)\n");
}
free(host);
port = lo_url_get_port("osc.udp://localhost:9999/a/path/is/here");
if (strcmp(port, "9999")) {
printf("failed lo_url_get_port() test1\n");
printf("'%s' != '9999'\n", port);
exit(1);
} else {
printf("passed lo_url_get_port() test1\n");
}
free(port);
port =
lo_url_get_port
("osc.udp://[::ffff:127.0.0.1]:9999/a/path/is/here");
if (strcmp(port, "9999")) {
printf("failed lo_url_get_port() test1 (IPv6)\n");
printf("'%s' != '9999'\n", port);
exit(1);
} else {
printf("passed lo_url_get_port() test1 (IPv6)\n");
}
free(port);
printf("\n");
}
void test_address()
{
const char *host, *port;
char *server_url;
lo_address a;
int proto;
DOING("test_address");
a = lo_address_new_from_url("osc://localhost/");
TEST(a != NULL);
lo_address_free(a);
a = lo_address_new_from_url("osc.://localhost/");
TEST(a == NULL);
a = lo_address_new_from_url("osc.tcp://foo.example.com:9999/");
host = lo_address_get_hostname(a);
if (strcmp(host, "foo.example.com")) {
printf("failed lo_address_get_hostname() test\n");
printf("'%s' != 'foo.example.com'\n", host);
exit(1);
} else {
printf("passed lo_address_get_hostname() test\n");
}
port = lo_address_get_port(a);
if (strcmp(port, "9999")) {
printf("failed lo_address_get_port() test\n");
printf("'%s' != '9999'\n", port);
exit(1);
} else {
printf("passed lo_address_get_port() test\n");
}
proto = lo_address_get_protocol(a);
if (proto != LO_TCP) {
printf("failed lo_address_get_protocol() test\n");
printf("'%d' != '%d'\n", proto, LO_TCP);
exit(1);
} else {
printf("passed lo_address_get_protocol() test\n");
}
server_url = lo_address_get_url(a);
if (strcmp(server_url, "osc.tcp://foo.example.com:9999/")) {
printf("failed lo_address_get_url() test\n");
printf("'%s' != '%s'\n", server_url,
"osc.tcp://foo.example.com:9999/");
exit(1);
} else {
printf("passed lo_address_get_url() test\n");
}
free(server_url);
lo_address_free(a);
printf("\n");
}
void test_blob()
{
lo_blob btest;
DOING("test_blob");
btest = lo_blob_new(sizeof(testdata), testdata);
/* Test blob sizes */
if (lo_blob_datasize(btest) != 5 || lo_blobsize(btest) != 12) {
printf("blob is %d (%d) bytes long, should be 5 (12)\n",
lo_blob_datasize(btest), lo_blobsize(btest));
lo_arg_pp(LO_BLOB, btest);
printf(" <- blob\n");
exit(1);
}
lo_blob_free(btest);
}
void test_server_thread(lo_server_thread *pst, lo_address *pa)
{
lo_address a;
char *server_url;
lo_server_thread st, sta, stb;
lo_blob btest;
lo_timetag tt = { 0x1, 0x80000000 };
DOING("test_server_thread");
btest = lo_blob_new(sizeof(testdata), testdata);
sta = lo_server_thread_new("7591", error);
stb = lo_server_thread_new("7591", rep_error);
if (stb) {
fprintf(stderr, "FAILED: create bad server thread object!\n");
exit(1);
}
lo_server_thread_free(sta);
/* leak check */
st = lo_server_thread_new(NULL, error);
if (!st) {
printf("Error creating server thread\n");
exit(1);
}
lo_server_thread_start(st);
SLEEP_MS(4);
lo_server_thread_stop(st);
lo_server_thread_free(st);
st = lo_server_thread_new(NULL, error);
lo_server_thread_start(st);
lo_server_thread_stop(st);
lo_server_thread_free(st);
st = lo_server_thread_new(NULL, error);
lo_server_thread_free(st);
st = lo_server_thread_new(NULL, error);
lo_server_thread_free(st);
st = lo_server_thread_new(NULL, error);
/* Server method handler tests */
server_url = lo_server_thread_get_url(st);
printf("Server URL: %s\n", server_url);
a = lo_address_new_from_url(server_url);
free(server_url);
/* add method that will match the path /foo/bar, with two numbers, coerced
* to float and int */
lo_server_thread_add_method(st, "/foo/bar", "fi", foo_handler,
lo_server_thread_get_server(st));
lo_server_thread_add_method(st, "/reply", "s", reply_handler, NULL);
lo_server_thread_add_method(st, "/lotsofformats", "fisbmhtdSccTFNI",
lots_handler, NULL);
lo_server_thread_add_method(st, "/coerce", "dfhiSs",
coerce_handler, NULL);
lo_server_thread_add_method(st, "/bundle", NULL, bundle_handler, NULL);
lo_server_thread_add_method(st, "/timestamp", NULL,
timestamp_handler, NULL);
lo_method jit =
lo_server_thread_add_method(st, "/jitter", "ti", jitter_handler, NULL);
lo_server_thread_add_method(st, "/pattern/foo", NULL,
pattern_handler, "foo");
lo_server_thread_add_method(st, "/pattern/bar", NULL,
pattern_handler, "bar");
lo_server_thread_add_method(st, "/pattern/baz", NULL,
pattern_handler, "baz");
lo_server_thread_add_method(st, "/subtest", "i", subtest_handler, st);
lo_server_thread_add_method(st, "/subtest-reply", "i",
subtest_reply_handler, NULL);
/* add method that will match any path and args */
lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
/* add method that will match the path /quit with no args */
lo_server_thread_add_method(st, "/quit", "", quit_handler, NULL);
/* check that the thread restarts */
lo_server_thread_start(st);
lo_server_thread_stop(st);
lo_server_thread_start(st);
if (lo_send(a, "/foo/bar", "ff", 0.12345678f, 23.0f) < 0) {
printf("OSC error A %d: %s\n", lo_address_errno(a),
lo_address_errstr(a));
exit(1);
}
if (lo_send(a, "/foo/bar", "ff", 0.12345678f, 23.0f) < 0) {
printf("OSC error B %d: %s\n", lo_address_errno(a),
lo_address_errstr(a));
exit(1);
}
lo_send(a, "/", "i", 242);
lo_send(a, "/pattern/", "i", 243);
#ifndef _MSC_VER /* MS compiler refuses to compile this case */
lo_send(a, "/bar", "ff", 0.12345678f, 1.0 / 0.0);
#endif
lo_send(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123,
"123", btest, midi_data, 0x0123456789abcdefULL, tt, 0.9999,
"sym", 'X', 'Y');
lo_send(a, "/coerce", "fdihsS", 0.1f, 0.2, 123, 124LL, "aaa", "bbb");
lo_send(a, "/coerce", "ffffss", 0.1f, 0.2f, 123.0, 124.0, "aaa",
"bbb");
lo_send(a, "/coerce", "ddddSS", 0.1, 0.2, 123.0, 124.0, "aaa", "bbb");
lo_send(a, "/a/b/c/d", "sfsff", "one", 0.12345678f, "three",
-0.00000023001f, 1.0);
lo_send(a, "/a/b/c/d", "b", btest);
/* Delete methods */
lo_server_thread_del_method(st, "/coerce", "dfhiSs");
TEST (lo_server_thread_del_lo_method(st, jit) == 0);
TEST (lo_server_thread_del_lo_method(st, jit) != 0);
{
lo_method m;
lo_server s = lo_server_new(NULL, error);
lo_server_del_method(s, NULL, NULL);
TEST (m = lo_server_add_method(s, NULL, NULL, generic_handler, NULL));
TEST (lo_server_del_lo_method(s, m) == 0);
TEST (lo_server_del_lo_method(s, m) != 0);
lo_server_free(s);
}
lo_blob_free(btest);
*pst = st;
*pa = a;
}
void test_message(lo_address a)
{
lo_blob btest;
lo_timetag tt = { 0x1, 0x80000000 };
lo_message m;
DOING("test_message");
btest = lo_blob_new(sizeof(testdata), testdata);
TEST(test_varargs
(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123, "123",
btest, midi_data, 0x0123456789abcdefULL, tt, 0.9999, "sym", 'X',
'Y', LO_ARGS_END) == 0);
#ifdef __GNUC__
#ifndef USE_ANSI_C
// Note: Lack of support for variable-argument macros in non-GCC compilers
// does not allow us to test for these conditions.
// too many args
TEST(test_varargs(a, "/lotsofformats", "f", 0.12345678f, 123,
"123", btest, midi_data, 0x0123456789abcdefULL, tt,
0.9999, "sym", 'X', 'Y', LO_ARGS_END) != 0);
// too many types
TEST(test_varargs
(a, "/lotsofformats", "fisbmhtdSccTFNI", 0.12345678f, 123, "123",
btest, midi_data, 0x0123456789abcdefULL, tt, 0.5,
LO_ARGS_END) != 0);
#endif
#endif
// test lo_message_add
m = lo_message_new();
TEST(lo_message_add(m, "fisbmhtdSccTFNI", 0.12345678f, 123, "123",
btest, midi_data, 0x0123456789abcdefULL, tt,
0.9999, "sym", 'X', 'Y') == 0);
lo_send_message(a, "/lotsofformats", m);
lo_message_free(m);
lo_blob_free(btest);
}
void test_pattern(lo_address a)
{
DOING("test_pattern");
lo_send(a, "/pattern/*", "s", "a");
lo_send(a, "/pattern/ba[rz]", "s", "b");
}
void test_subtest(lo_server_thread st)
{
char cmd[2048], *server_url;
int i, rc;
DOING("test_subtest");
server_url = lo_server_thread_get_url(st);
#ifdef WIN32
{
char cwd[2048];
_getcwd(cwd, 2048);
snprintf(cmd, 2048, "%s" PATHDELIM "subtest" EXTEXE, cwd);
}
printf("spawning subtest with `%s'\n", cmd);
for (i=0; i<2; i++) {
int j=0;
rc = _spawnl( _P_NOWAIT, cmd, cmd, server_url, NULL );
if (rc == -1) {
fprintf(stderr, "Cannot execute subtest command (%d)\n", i);
exit(1);
}
else while (subtest_count < 1 && j < 20) {
SLEEP_MS(100);
j++;
}
if (j >= 20) {
fprintf(stderr, "Never got a message from subtest (%d) "
"after 2 seconds.\n", i);
exit(1);
}
}
#else
sprintf(cmd, "." PATHDELIM "subtest" EXTEXE " %s", server_url);
printf("executing subtest with `%s'\n", cmd);
for (i=0; i<2; i++) {
int j=0;
rc = system(cmd);
if (rc == -1) {
fprintf(stderr, "Cannot execute subtest command (%d)\n", i);
exit(1);
}
else if (rc > 0) {
fprintf(stderr, "subtest command returned %d\n", rc);
exit(1);
}
while (subtest_count < 1 && j < 20) {
usleep(100000);
j++;
}
}
#endif
free(server_url);
i = 20*1000;
while (subtest_reply_count != 22 && --i > 0)
{
SLEEP_MS(10);
}
TEST(reply_count == 3);
TEST(pattern_count == 5);
TEST(subtest_count == 2);
TEST(subtest_reply_count == 22);
printf("\n");
}
void test_bundle(lo_server_thread st, lo_address a)
{
lo_bundle b;
lo_timetag sched;
lo_message m1, m2;
const char *p;
int i, tries;
lo_timetag t = { 10, 0xFFFFFFFC };
DOING("test_bundle");
b = lo_bundle_new(t);
m1 = lo_message_new();
lo_message_add_string(m1, "abcdefghijklmnopqrstuvwxyz");
lo_message_add_string(m1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
lo_bundle_add_message(b, "/bundle", m1);
lo_send_bundle(a, b);
/* This should be safe for multiple copies of the same message. */
lo_bundle_free_messages(b);
{
lo_timetag t = { 1, 2 };
b = lo_bundle_new(t);
}
m1 = lo_message_new();
lo_message_add_int32(m1, 23);
lo_message_add_string(m1, "23");
lo_bundle_add_message(b, "/bundle", m1);
m2 = lo_message_new();
lo_message_add_string(m2, "24");
lo_message_add_int32(m2, 24);
lo_bundle_add_message(b, "/bundle", m2);
lo_bundle_add_message(b, "/bundle", m1);
TEST(lo_bundle_count(b)==3);
TEST(lo_bundle_get_message(b,1,&p)==m2);
TEST(strcmp(p, "/bundle")==0);
TEST(lo_send_bundle(a, b) == 88);
/* Should fail to add a bundle recursively */
TEST(lo_bundle_add_bundle(b, b) != 0)
/* But we can create a nested bundle and it should free
* successfully. */
{
lo_bundle b2 = 0;
{
lo_timetag t = { 10, 0xFFFFFFFE };
b2 = lo_bundle_new(t);
}
lo_bundle_add_message(b2, "/bundle", m1);
TEST(lo_bundle_add_bundle(b2, b) == 0);
/* Test freeing out-of-order copies of messages in a bundle. */
lo_bundle_free_recursive(b2);
}
{
lo_timetag t = { 10, 0xFFFFFFFE };
b = lo_bundle_new(t);
}
m1 = lo_message_new();
lo_message_add_string(m1, "abcdefghijklmnopqrstuvwxyz");
lo_message_add_string(m1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
lo_bundle_add_message(b, "/bundle", m1);
lo_send_bundle(a, b);
lo_message_free(m1);
lo_bundle_free(b);
lo_timetag_now(&sched);
sched.sec += 5;
b = lo_bundle_new(sched);
m1 = lo_message_new();
lo_message_add_string(m1, "future");
lo_message_add_string(m1, "time");
lo_message_add_string(m1, "test");
lo_bundle_add_message(b, "/bundle", m1);
lo_send_bundle(a, b);
lo_message_free(m1);
lo_bundle_free(b);
lo_send_timestamped(a, sched, "/bundle", "s",
"lo_send_timestamped() test");
/* test bundle timestamp ends up in message struct (and doesn't end up in
unbundled messages) */
lo_timetag_now(&sched);
lo_send_timestamped(a, sched, "/timestamp", "it", 1, sched);
lo_send(a, "/timestamp", "it", 0, sched);
#define JITTER_ITS 25
/* jitter tests */
{
lo_timetag stamps[JITTER_ITS];
lo_timetag now;
for (i = 0; i < JITTER_ITS; i++) {
lo_timetag_now(&now);
stamps[i] = now;
stamps[i].sec += 1;
stamps[i].frac = rand();
lo_send_timestamped(a, stamps[i], "/jitter", "ti", stamps[i],
i);
}
}
SLEEP_MS(2000);
TEST(lo_server_thread_events_pending(st));
tries = 20;
while (lo_server_thread_events_pending(st)
&& (--tries > 0))
{
printf("pending events, wait...\n");
fflush(stdout);
SLEEP_MS(1000);
}
if (tries == 0) {
printf("server thread still has pending"
" events after 20 seconds!\n");
exit(1);
}
TEST(bundle_count == 7);
printf("\n");
printf("bundle timing jitter results:\n"
"max jitter = %fs\n"
"avg jitter = %fs\n"
"min jitter = %fs\n\n",
jitter_max, jitter_total / (float) jitter_count, jitter_min);
}
void test_nonblock()
{
lo_server s;
char *server_url;
lo_address a;
int testSizes[2] = { -1, 2048 };
int i;
DOING("test_nonblock");
s = lo_server_new(NULL, error);
server_url = lo_server_get_url(s);
lo_server_add_method(s, NULL, NULL, generic_handler, NULL);
a = lo_address_new_from_url(server_url);
for (i = 0; i < sizeof(testSizes) / sizeof(testSizes[0]); i++) {
lo_server_max_msg_size(s, testSizes[i]);
TEST(lo_server_recv_noblock(s, 0) == 0);
printf("Testing noblock API on %s\n", server_url);
lo_send(a, "/non-block-test", "f", 23.0);
int tries = 1000;
while (!lo_server_recv_noblock(s, 10) && --tries > 0)
{
}
if (tries == 0) {
printf("lo_server_recv_noblock() test failed\n");
exit(1);
}
}
free(server_url);
lo_server_free(s);
lo_address_free(a);
}
void test_unix_sockets()
{
#if !defined(WIN32) && !defined(_MSC_VER)
lo_address ua;
lo_server us;
char *addr;
DOING("test_unix_sockets");
unlink("/tmp/testlo.osc");
us = lo_server_new_with_proto("/tmp/testlo.osc", LO_UNIX, error);
ua = lo_address_new_from_url("osc.unix:///tmp/testlo.osc");
TEST(lo_server_get_protocol(us) == LO_UNIX);
TEST(lo_send(ua, "/unix", "f", 23.0) == 16);
TEST(lo_server_recv(us) == 16);
addr = lo_server_get_url(us);
TEST(!strcmp("osc.unix:////tmp/testlo.osc", addr));
free(addr);
lo_address_free(ua);
ua = lo_address_new_with_proto(LO_UNIX, NULL, "/tmp/testlo.osc");
TEST(lo_send(ua, "/unix", "f", 23.0) == 16);
TEST(lo_server_recv(us) == 16);
lo_server_free(us);
lo_address_free(ua);
#endif
}
void test_tcp()
{
/* TCP tests */
lo_address ta;
lo_server ts;
char *addr;
DOING("test_tcp");
ts = lo_server_new_with_proto(NULL, LO_TCP, error);
addr = lo_server_get_url(ts);
ta = lo_address_new_from_url(addr);
if (lo_address_errno(ta)) {
printf("err: %s\n", lo_address_errstr(ta));
exit(1);
}
TEST(lo_server_get_protocol(ts) == LO_TCP);
TEST(lo_send(ta, "/tcp", "f", 23.0) == 16);
TEST(lo_send(ta, "/tcp", "f", 23.0) == 16);
TEST(lo_server_recv(ts) == 16);
TEST(lo_server_recv(ts) == 16);
free(addr);
lo_server_free(ts);
lo_address_free(ta);
}
void cleanup(lo_server_thread st, lo_address a)
{
int tries;
DOING("cleanup");
/* exit */
lo_send(a, "/quit", NULL);
tries = 20*1000;
while (!done && (--tries > 0)) {
SLEEP_MS(1);
}
if (tries == 0)
{
printf("Took too long to quit\n");
exit(1);
}
lo_address_free(a);
lo_server_thread_free(st);
}
/* vi:set ts=8 sts=4 sw=4: */