Commit a125f91f authored by Jakub Kicinski's avatar Jakub Kicinski

selftests: tls: add helper for creating sock pairs

We have the same code 3 times, about to add a fourth copy.
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 9dbe33cf
...@@ -78,26 +78,21 @@ static void memrnd(void *s, size_t n) ...@@ -78,26 +78,21 @@ static void memrnd(void *s, size_t n)
*byte++ = rand(); *byte++ = rand();
} }
FIXTURE(tls_basic) static void ulp_sock_pair(struct __test_metadata *_metadata,
{ int *fd, int *cfd, bool *notls)
int fd, cfd;
bool notls;
};
FIXTURE_SETUP(tls_basic)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
socklen_t len; socklen_t len;
int sfd, ret; int sfd, ret;
self->notls = false; *notls = false;
len = sizeof(addr); len = sizeof(addr);
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = 0; addr.sin_port = 0;
self->fd = socket(AF_INET, SOCK_STREAM, 0); *fd = socket(AF_INET, SOCK_STREAM, 0);
sfd = socket(AF_INET, SOCK_STREAM, 0); sfd = socket(AF_INET, SOCK_STREAM, 0);
ret = bind(sfd, &addr, sizeof(addr)); ret = bind(sfd, &addr, sizeof(addr));
...@@ -108,26 +103,37 @@ FIXTURE_SETUP(tls_basic) ...@@ -108,26 +103,37 @@ FIXTURE_SETUP(tls_basic)
ret = getsockname(sfd, &addr, &len); ret = getsockname(sfd, &addr, &len);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = connect(self->fd, &addr, sizeof(addr)); ret = connect(*fd, &addr, sizeof(addr));
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
self->cfd = accept(sfd, &addr, &len); *cfd = accept(sfd, &addr, &len);
ASSERT_GE(self->cfd, 0); ASSERT_GE(*cfd, 0);
close(sfd); close(sfd);
ret = setsockopt(self->fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")); ret = setsockopt(*fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
if (ret != 0) { if (ret != 0) {
ASSERT_EQ(errno, ENOENT); ASSERT_EQ(errno, ENOENT);
self->notls = true; *notls = true;
printf("Failure setting TCP_ULP, testing without tls\n"); printf("Failure setting TCP_ULP, testing without tls\n");
return; return;
} }
ret = setsockopt(self->cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")); ret = setsockopt(*cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
} }
FIXTURE(tls_basic)
{
int fd, cfd;
bool notls;
};
FIXTURE_SETUP(tls_basic)
{
ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
}
FIXTURE_TEARDOWN(tls_basic) FIXTURE_TEARDOWN(tls_basic)
{ {
close(self->fd); close(self->fd);
...@@ -199,60 +205,21 @@ FIXTURE_VARIANT_ADD(tls, 13_sm4_ccm) ...@@ -199,60 +205,21 @@ FIXTURE_VARIANT_ADD(tls, 13_sm4_ccm)
FIXTURE_SETUP(tls) FIXTURE_SETUP(tls)
{ {
struct tls_crypto_info_keys tls12; struct tls_crypto_info_keys tls12;
struct sockaddr_in addr; int ret;
socklen_t len;
int sfd, ret;
self->notls = false;
len = sizeof(addr);
tls_crypto_info_init(variant->tls_version, variant->cipher_type, tls_crypto_info_init(variant->tls_version, variant->cipher_type,
&tls12); &tls12);
addr.sin_family = AF_INET; ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = 0;
self->fd = socket(AF_INET, SOCK_STREAM, 0); if (self->notls)
sfd = socket(AF_INET, SOCK_STREAM, 0); return;
ret = bind(sfd, &addr, sizeof(addr));
ASSERT_EQ(ret, 0);
ret = listen(sfd, 10);
ASSERT_EQ(ret, 0);
ret = getsockname(sfd, &addr, &len); ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = connect(self->fd, &addr, sizeof(addr)); ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12, tls12.len);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = setsockopt(self->fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
if (ret != 0) {
self->notls = true;
printf("Failure setting TCP_ULP, testing without tls\n");
}
if (!self->notls) {
ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12,
tls12.len);
ASSERT_EQ(ret, 0);
}
self->cfd = accept(sfd, &addr, &len);
ASSERT_GE(self->cfd, 0);
if (!self->notls) {
ret = setsockopt(self->cfd, IPPROTO_TCP, TCP_ULP, "tls",
sizeof("tls"));
ASSERT_EQ(ret, 0);
ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12,
tls12.len);
ASSERT_EQ(ret, 0);
}
close(sfd);
} }
FIXTURE_TEARDOWN(tls) FIXTURE_TEARDOWN(tls)
...@@ -1355,62 +1322,25 @@ TEST(non_established) { ...@@ -1355,62 +1322,25 @@ TEST(non_established) {
TEST(keysizes) { TEST(keysizes) {
struct tls12_crypto_info_aes_gcm_256 tls12; struct tls12_crypto_info_aes_gcm_256 tls12;
struct sockaddr_in addr; int ret, fd, cfd;
int sfd, ret, fd, cfd;
socklen_t len;
bool notls; bool notls;
notls = false;
len = sizeof(addr);
memset(&tls12, 0, sizeof(tls12)); memset(&tls12, 0, sizeof(tls12));
tls12.info.version = TLS_1_2_VERSION; tls12.info.version = TLS_1_2_VERSION;
tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256; tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
addr.sin_family = AF_INET; ulp_sock_pair(_metadata, &fd, &cfd, &notls);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = 0;
fd = socket(AF_INET, SOCK_STREAM, 0);
sfd = socket(AF_INET, SOCK_STREAM, 0);
ret = bind(sfd, &addr, sizeof(addr));
ASSERT_EQ(ret, 0);
ret = listen(sfd, 10);
ASSERT_EQ(ret, 0);
ret = getsockname(sfd, &addr, &len);
ASSERT_EQ(ret, 0);
ret = connect(fd, &addr, sizeof(addr));
ASSERT_EQ(ret, 0);
ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
if (ret != 0) {
notls = true;
printf("Failure setting TCP_ULP, testing without tls\n");
}
if (!notls) { if (!notls) {
ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12,
sizeof(tls12)); sizeof(tls12));
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
}
cfd = accept(sfd, &addr, &len);
ASSERT_GE(cfd, 0);
if (!notls) {
ret = setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls",
sizeof("tls"));
EXPECT_EQ(ret, 0);
ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12,
sizeof(tls12)); sizeof(tls12));
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
} }
close(sfd);
close(fd); close(fd);
close(cfd); close(cfd);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment