Skip to content

Commit 3394daa

Browse files
Add unit test for ChannelPutData
1 parent 8e2e3c5 commit 3394daa

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

src/internal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10777,6 +10777,12 @@ int wolfSSH_TestDoChannelRequest(WOLFSSH* ssh, byte* buf, word32 len,
1077710777
{
1077810778
return DoChannelRequest(ssh, buf, len, idx);
1077910779
}
10780+
10781+
int wolfSSH_TestChannelPutData(WOLFSSH_CHANNEL* channel, byte* data,
10782+
word32 dataSz)
10783+
{
10784+
return ChannelPutData(channel, data, dataSz);
10785+
}
1078010786
#endif
1078110787

1078210788

tests/unit.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,75 @@ static int test_DoUserAuthBanner(void)
718718
return result;
719719
}
720720

721+
static int test_ChannelPutData(void)
722+
{
723+
WOLFSSH_CTX* ctx = NULL;
724+
WOLFSSH* ssh = NULL;
725+
WOLFSSH_CHANNEL* channel = NULL;
726+
byte data[110];
727+
int result = 0;
728+
int ret;
729+
730+
WMEMSET(data, 0xAB, sizeof(data));
731+
732+
ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
733+
if (ctx == NULL)
734+
return -400;
735+
ssh = wolfSSH_new(ctx);
736+
if (ssh == NULL) {
737+
wolfSSH_CTX_free(ctx);
738+
return -401;
739+
}
740+
741+
/* Window of 100 bytes, matching the input buffer size. */
742+
channel = ChannelNew(ssh, ID_CHANTYPE_SESSION, 100, 100);
743+
if (channel == NULL) {
744+
wolfSSH_free(ssh);
745+
wolfSSH_CTX_free(ctx);
746+
return -402;
747+
}
748+
749+
/* NULL channel */
750+
ret = wolfSSH_TestChannelPutData(NULL, data, 10);
751+
if (ret != WS_BAD_ARGUMENT) {
752+
result = -403;
753+
goto done;
754+
}
755+
756+
/* NULL data */
757+
ret = wolfSSH_TestChannelPutData(channel, NULL, 10);
758+
if (ret != WS_BAD_ARGUMENT) {
759+
result = -404;
760+
goto done;
761+
}
762+
763+
/* dataSz exceeds windowSz: 101 > 100 */
764+
ret = wolfSSH_TestChannelPutData(channel, data, 101);
765+
if (ret != WS_FATAL_ERROR) {
766+
result = -405;
767+
goto done;
768+
}
769+
770+
/* Valid write consuming half the window */
771+
ret = wolfSSH_TestChannelPutData(channel, data, 50);
772+
if (ret != WS_SUCCESS) {
773+
result = -406;
774+
goto done;
775+
}
776+
777+
/* Remaining windowSz is 50; sending 51 must be rejected */
778+
ret = wolfSSH_TestChannelPutData(channel, data, 51);
779+
if (ret != WS_FATAL_ERROR) {
780+
result = -407;
781+
goto done;
782+
}
783+
784+
done:
785+
ChannelDelete(channel, ctx->heap);
786+
wolfSSH_free(ssh);
787+
wolfSSH_CTX_free(ctx);
788+
return result;
789+
}
721790

722791
/* Verify DoChannelRequest sends CHANNEL_SUCCESS for known types and
723792
* CHANNEL_FAILURE for unrecognized ones (RFC 4254 Section 5.4).
@@ -1138,6 +1207,9 @@ int wolfSSH_UnitTest(int argc, char** argv)
11381207
(unitResult == 0 ? "SUCCESS" : "FAILED"));
11391208
testResult = testResult || unitResult;
11401209
#endif
1210+
unitResult = test_ChannelPutData();
1211+
printf("ChannelPutData: %s\n", (unitResult == 0 ? "SUCCESS" : "FAILED"));
1212+
testResult = testResult || unitResult;
11411213
#endif
11421214

11431215
#ifdef WOLFSSH_KEYGEN

wolfssh/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ enum WS_MessageIdLimits {
13371337
word32 len, word32* idx);
13381338
WOLFSSH_API int wolfSSH_TestDoKexDhInit(WOLFSSH* ssh, byte* buf,
13391339
word32 len, word32* idx);
1340+
WOLFSSH_API int wolfSSH_TestChannelPutData(WOLFSSH_CHANNEL*, byte*, word32);
13401341
#ifndef WOLFSSH_NO_DH_GEX_SHA256
13411342
WOLFSSH_API int wolfSSH_TestDoKexDhGexRequest(WOLFSSH* ssh, byte* buf,
13421343
word32 len, word32* idx);

0 commit comments

Comments
 (0)