@@ -21347,69 +21347,16 @@ void test_SSLVerifyHostname(void)
21347
21347
#endif
21348
21348
}
21349
21349
21350
- static char*
21351
- _getAccountName(natsConnection *nc)
21352
- {
21353
- natsStatus s = NATS_OK;
21354
- natsSubscription *inboxSub = NULL;
21355
- natsMsg *userInfoMsg = NULL;
21356
- natsInbox *inbox = NULL;
21357
- char *accountName = NULL;
21358
-
21359
- s = natsConn_newInbox(nc, &inbox);
21360
-
21361
- // Subscribe to inbox for response
21362
- IFOK(s, natsConnection_SubscribeSync(&inboxSub, nc, inbox));
21363
- IFOK(s, natsConnection_Flush(nc));
21364
-
21365
- // Publish account request with inbox as reply subject
21366
- IFOK(s, natsConnection_PublishRequestString(nc, "$SYS.REQ.USER.INFO", inbox, ""));
21367
- IFOK(s, natsConnection_Flush(nc));
21368
-
21369
- // Wait for response
21370
- IFOK(s, natsSubscription_NextMsg(&userInfoMsg, inboxSub, 2000));
21371
-
21372
- if (s == NATS_OK && userInfoMsg != NULL)
21373
- {
21374
- // Parse JSON response and extract account information
21375
- nats_JSON *json = NULL;
21376
- nats_JSON *data = NULL;
21377
- const char *account = NULL;
21378
-
21379
- s = nats_JSONParse(&json, natsMsg_GetData(userInfoMsg), (int)natsMsg_GetDataLength(userInfoMsg));
21380
- if (s == NATS_OK)
21381
- {
21382
- s = nats_JSONGetObject(json, "data", &data);
21383
- if (s == NATS_OK)
21384
- {
21385
- s = nats_JSONGetStr(data, "account", &account);
21386
- if (s == NATS_OK && account != NULL)
21387
- {
21388
- accountName = NATS_STRDUP(account);
21389
- }
21390
- }
21391
- }
21392
-
21393
- // Cleanup
21394
- nats_JSONDestroy(json);
21395
- natsMsg_Destroy(userInfoMsg);
21396
- }
21397
-
21398
- natsSubscription_Destroy(inboxSub);
21399
- NATS_FREE(inbox);
21400
-
21401
- return accountName;
21402
- }
21403
-
21404
21350
void test_SSLVerifyDynamic(void)
21405
21351
{
21406
21352
#if defined(NATS_HAS_TLS)
21407
21353
natsStatus s;
21408
21354
natsConnection *nc = NULL;
21409
21355
natsOptions *opts = NULL;
21356
+ natsSubscription *sub = NULL;
21357
+ natsMsg *msg = NULL;
21410
21358
natsPid serverPid = NATS_INVALID_PID;
21411
21359
struct threadArg args;
21412
- char *accountName = NULL;
21413
21360
21414
21361
s = _createDefaultThreadArgsForCbTests(&args);
21415
21362
if (s == NATS_OK)
@@ -21428,6 +21375,18 @@ void test_SSLVerifyDynamic(void)
21428
21375
IFOK(s, natsOptions_SetReconnectedCB(opts, _reconnectedCb, &args));
21429
21376
IFOK(s, natsConnection_Connect(&nc, opts));
21430
21377
testCond(s != NATS_OK);
21378
+ nats_clearLastError();
21379
+
21380
+ test("Check load certs (bad args): ");
21381
+ s = natsOptions_LoadCertificatesChainDynamic(opts, "certs/client-cert.pem", "");
21382
+ if (s == NATS_INVALID_ARG)
21383
+ s = natsOptions_LoadCertificatesChainDynamic(opts, "certs/client-cert.pem", NULL);
21384
+ if (s == NATS_INVALID_ARG)
21385
+ s = natsOptions_LoadCertificatesChainDynamic(opts, "", "certs/client-key.pem");
21386
+ if (s == NATS_INVALID_ARG)
21387
+ s = natsOptions_LoadCertificatesChainDynamic(opts, NULL, "certs/client-key.pem");
21388
+ testCond(s == NATS_INVALID_ARG);
21389
+ nats_clearLastError();
21431
21390
21432
21391
test("Check that connect succeeds with dynamic cert loading: ");
21433
21392
// Create temporary certificate files for dynamic loading
@@ -21443,21 +21402,19 @@ void test_SSLVerifyDynamic(void)
21443
21402
if ((s == NATS_OK) && (system("copy certs\\client-key.pem certs\\key-dynamic.pem") != 0))
21444
21403
s = NATS_ERR;
21445
21404
#endif
21446
-
21447
21405
IFOK(s, natsOptions_LoadCertificatesChainDynamic(opts,
21448
21406
"certs/cert-dynamic.pem",
21449
21407
"certs/key-dynamic.pem"));
21450
21408
s = natsConnection_Connect(&nc, opts);
21409
+ IFOK(s, natsConnection_SubscribeSync(&sub, nc, "*"));
21451
21410
IFOK(s, natsConnection_PublishString(nc, "foo", "test"));
21452
21411
IFOK(s, natsConnection_Flush(nc));
21453
- testCond(s == NATS_OK);
21454
-
21455
- test("Check account name equals \"DEREK\": ");
21456
- accountName = _getAccountName(nc);
21457
- testCond(accountName != NULL && !strcmp(accountName, "DEREK"));
21458
- NATS_FREE(accountName);
21412
+ IFOK(s, natsSubscription_NextMsg(&msg, sub, 1000));
21413
+ testCond((s == NATS_OK) && (msg != NULL) && (strcmp(natsMsg_GetData(msg), "test") == 0));
21414
+ natsMsg_Destroy(msg);
21415
+ msg = NULL;
21459
21416
21460
- test("Check reconnects with different cert is OK : ");
21417
+ test("Change certs : ");
21461
21418
_stopServer(serverPid);
21462
21419
21463
21420
nats_Sleep(100);
@@ -21474,24 +21431,34 @@ void test_SSLVerifyDynamic(void)
21474
21431
if ((s == NATS_OK) && (system("copy certs\\server-key.pem certs\\key-dynamic.pem") != 0))
21475
21432
s = NATS_ERR;
21476
21433
#endif
21434
+ testCond(s == NATS_OK);
21477
21435
21478
21436
serverPid = _startServer("nats://127.0.0.1:4443", "-config tlsverify.conf", true);
21479
21437
CHECK_SERVER_STARTED(serverPid);
21480
21438
21439
+ test("Wait for reconnect: ");
21481
21440
natsMutex_Lock(args.m);
21482
21441
while ((s != NATS_TIMEOUT) && !(args.reconnected))
21483
21442
s = natsCondition_TimedWait(args.c, args.m, 2000);
21484
21443
natsMutex_Unlock(args.m);
21444
+ testCond(s == NATS_OK);
21485
21445
21446
+ test("Check not able to publish on foo: ");
21486
21447
IFOK(s, natsConnection_PublishString(nc, "foo", "test"));
21487
21448
IFOK(s, natsConnection_Flush(nc));
21488
- testCond(s == NATS_OK);
21449
+ IFOK(s, natsSubscription_NextMsg(&msg, sub, 250));
21450
+ testCond((s == NATS_TIMEOUT) && (msg == NULL));
21451
+ nats_clearLastError();
21489
21452
21490
- test("Check account name equals \"JOHN\": ");
21491
- accountName = _getAccountName(nc);
21492
- testCond(accountName != NULL && !strcmp(accountName, "JOHN"));
21493
- NATS_FREE(accountName);
21453
+ test("Send to right subject: ");
21454
+ s = natsConnection_PublishString(nc, "bar", "test2");
21455
+ IFOK(s, natsConnection_Flush(nc));
21456
+ IFOK(s, natsSubscription_NextMsg(&msg, sub, 1000));
21457
+ testCond((s == NATS_OK) && (msg != NULL) && (strcmp(natsMsg_GetData(msg), "test2") == 0));
21458
+ natsMsg_Destroy(msg);
21459
+ msg = NULL;
21494
21460
21461
+ natsSubscription_Destroy(sub);
21495
21462
natsConnection_Destroy(nc);
21496
21463
natsOptions_Destroy(opts);
21497
21464
0 commit comments