Skip to content

Commit 5107613

Browse files
committed
bio: add tests
1 parent c07d863 commit 5107613

2 files changed

Lines changed: 132 additions & 1 deletion

File tree

tests/api/test_ossl_bio.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,5 +1678,130 @@ int test_wolfSSL_BIO_custom_method(void)
16781678
return EXPECT_RESULT();
16791679
}
16801680

1681+
int test_wolfSSL_BIO_set_conn_hostname(void)
1682+
{
1683+
EXPECT_DECLS;
1684+
#if defined(OPENSSL_EXTRA)
1685+
BIO* bio = NULL;
1686+
1687+
/* NULL bio should fail */
1688+
ExpectIntEQ(BIO_set_conn_hostname(NULL, (char*)"localhost"),
1689+
WOLFSSL_FAILURE);
1690+
1691+
/* Create a bare socket BIO (ip starts as NULL) */
1692+
ExpectNotNull(bio = BIO_new(BIO_s_socket()));
1693+
1694+
/* NULL hostname should fail */
1695+
ExpectIntEQ(BIO_set_conn_hostname(bio, NULL), WOLFSSL_FAILURE);
1696+
1697+
/* Set initial hostname (ip == NULL, triggers malloc) */
1698+
ExpectIntEQ(BIO_set_conn_hostname(bio, (char*)"127.0.0.1"),
1699+
WOLFSSL_SUCCESS);
1700+
1701+
/* Set same-length hostname (reuses existing buffer) */
1702+
ExpectIntEQ(BIO_set_conn_hostname(bio, (char*)"192.168.0"),
1703+
WOLFSSL_SUCCESS);
1704+
1705+
/* Set shorter hostname (triggers free + malloc) */
1706+
ExpectIntEQ(BIO_set_conn_hostname(bio, (char*)"short"), WOLFSSL_SUCCESS);
1707+
1708+
/* Set longer hostname (triggers free + malloc) */
1709+
ExpectIntEQ(BIO_set_conn_hostname(bio, (char*)"a]longer.hostname.example"),
1710+
WOLFSSL_SUCCESS);
1711+
1712+
BIO_free(bio);
1713+
#endif
1714+
return EXPECT_RESULT();
1715+
}
1716+
1717+
#if defined(OPENSSL_EXTRA)
1718+
static long pending_ctrlCb(WOLFSSL_BIO* bio, int cmd, long larg, void* parg)
1719+
{
1720+
(void)bio;
1721+
(void)larg;
1722+
(void)parg;
1723+
if (cmd == BIO_CTRL_PENDING)
1724+
return 42;
1725+
return 0;
1726+
}
1727+
#endif
1728+
1729+
int test_wolfSSL_BIO_ctrl_pending_chain(void)
1730+
{
1731+
EXPECT_DECLS;
1732+
#if defined(OPENSSL_EXTRA)
1733+
BIO* md = NULL;
1734+
BIO* b64 = NULL;
1735+
BIO* mem = NULL;
1736+
char msg[] = "hello";
1737+
1738+
/* Build chain: md -> b64 -> mem */
1739+
ExpectNotNull(md = BIO_new(BIO_f_md()));
1740+
ExpectNotNull(b64 = BIO_new(BIO_f_base64()));
1741+
ExpectNotNull(mem = BIO_new(BIO_s_mem()));
1742+
ExpectNotNull(BIO_push(md, b64));
1743+
ExpectNotNull(BIO_push(b64, mem));
1744+
1745+
/* Write directly to mem so wrSz is set */
1746+
ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), (int)sizeof(msg));
1747+
1748+
/* ctrl_pending on mem should work */
1749+
ExpectIntEQ((int)BIO_ctrl_pending(mem), (int)sizeof(msg));
1750+
1751+
/* ctrl_pending on the head of the chain (md) should skip both wrappers
1752+
* and return mem's pending count */
1753+
ExpectIntEQ((int)BIO_ctrl_pending(md), (int)sizeof(msg));
1754+
1755+
BIO_free(md);
1756+
BIO_free(b64);
1757+
BIO_free(mem);
1758+
1759+
/* Test that ctrl_pending reaches a custom ctrlCb through a wrapper.
1760+
* Chain: md -> custom(with ctrlCb) -> mem. The custom BIO's ctrlCb
1761+
* returns 42 for BIO_CTRL_PENDING, so ctrl_pending on md should
1762+
* return 42 (stopping at custom), not fall through to mem. */
1763+
{
1764+
BIO* md2 = NULL;
1765+
BIO* custom = NULL;
1766+
BIO* mem2 = NULL;
1767+
BIO_METHOD* meth = NULL;
1768+
1769+
ExpectNotNull(meth = BIO_meth_new(0, "pending_test"));
1770+
ExpectIntEQ(BIO_meth_set_ctrl(meth, pending_ctrlCb), WOLFSSL_SUCCESS);
1771+
ExpectNotNull(md2 = BIO_new(BIO_f_md()));
1772+
ExpectNotNull(custom = BIO_new(meth));
1773+
ExpectNotNull(mem2 = BIO_new(BIO_s_mem()));
1774+
ExpectNotNull(BIO_push(md2, custom));
1775+
ExpectNotNull(BIO_push(custom, mem2));
1776+
1777+
ExpectIntEQ((int)BIO_ctrl_pending(md2), 42);
1778+
1779+
BIO_free(md2);
1780+
BIO_free(custom);
1781+
BIO_free(mem2);
1782+
BIO_meth_free(meth);
1783+
}
1784+
#endif
1785+
return EXPECT_RESULT();
1786+
}
1787+
1788+
int test_wolfSSL_BIO_meth_type_large(void)
1789+
{
1790+
EXPECT_DECLS;
1791+
#if defined(OPENSSL_EXTRA)
1792+
BIO_METHOD* method = NULL;
1793+
BIO* bio = NULL;
1794+
1795+
/* Type value exceeding 255, as used by applications like HAProxy (0x666) */
1796+
ExpectNotNull(method = BIO_meth_new(0x666, "large_type_test"));
1797+
ExpectNotNull(bio = BIO_new(method));
1798+
ExpectIntEQ(BIO_method_type(bio), 0x666);
1799+
1800+
BIO_free(bio);
1801+
BIO_meth_free(method);
1802+
#endif
1803+
return EXPECT_RESULT();
1804+
}
1805+
16811806
#endif /* !NO_BIO */
16821807

tests/api/test_ossl_bio.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ int test_wolfSSL_BIO_get_len(void);
4343
int test_wolfSSL_BIO(void);
4444
int test_wolfSSL_BIO_BIO_ring_read(void);
4545
int test_wolfSSL_BIO_custom_method(void);
46+
int test_wolfSSL_BIO_set_conn_hostname(void);
47+
int test_wolfSSL_BIO_ctrl_pending_chain(void);
48+
int test_wolfSSL_BIO_meth_type_large(void);
4649

4750
#define TEST_OSSL_BIO_DECLS \
4851
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_gets), \
@@ -58,7 +61,10 @@ int test_wolfSSL_BIO_custom_method(void);
5861
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_get_len), \
5962
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO), \
6063
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_BIO_ring_read), \
61-
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_custom_method)
64+
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_custom_method), \
65+
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_set_conn_hostname), \
66+
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_ctrl_pending_chain), \
67+
TEST_DECL_GROUP("ossl_bio", test_wolfSSL_BIO_meth_type_large)
6268

6369
#define TEST_OSSL_BIO_TLS_DECLS \
6470
TEST_DECL_GROUP("ossl_bio_tls", test_wolfSSL_BIO_connect), \

0 commit comments

Comments
 (0)