selftests: tls: add getsockopt test
authorSabrina Dubroca <sd@queasysnail.net>
Fri, 25 Aug 2023 21:35:07 +0000 (23:35 +0200)
committerJakub Kicinski <kuba@kernel.org>
Mon, 28 Aug 2023 00:17:40 +0000 (17:17 -0700)
The kernel accepts fetching either just the version and cipher type,
or exactly the per-cipher struct. Also check that getsockopt returns
what we just passed to the kernel.

Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Link: https://lore.kernel.org/r/81a007ca13de9a74f4af45635d06682cdb385a54.1692977948.git.sd@queasysnail.net
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/net/tls.c

index 95bef2be48cd7c03949e7ecbf66c15321b9ade5f..0da6952a047a0b72fc3cd7fe936b7b247aa6e87f 100644 (file)
@@ -30,6 +30,7 @@ static int fips_enabled;
 
 struct tls_crypto_info_keys {
        union {
+               struct tls_crypto_info crypto_info;
                struct tls12_crypto_info_aes_gcm_128 aes128;
                struct tls12_crypto_info_chacha20_poly1305 chacha20;
                struct tls12_crypto_info_sm4_gcm sm4gcm;
@@ -1496,6 +1497,40 @@ TEST_F(tls, shutdown_reuse)
        EXPECT_EQ(errno, EISCONN);
 }
 
+TEST_F(tls, getsockopt)
+{
+       struct tls_crypto_info_keys expect, get;
+       socklen_t len;
+
+       /* get only the version/cipher */
+       len = sizeof(struct tls_crypto_info);
+       memrnd(&get, sizeof(get));
+       EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
+       EXPECT_EQ(len, sizeof(struct tls_crypto_info));
+       EXPECT_EQ(get.crypto_info.version, variant->tls_version);
+       EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
+
+       /* get the full crypto_info */
+       tls_crypto_info_init(variant->tls_version, variant->cipher_type, &expect);
+       len = expect.len;
+       memrnd(&get, sizeof(get));
+       EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
+       EXPECT_EQ(len, expect.len);
+       EXPECT_EQ(get.crypto_info.version, variant->tls_version);
+       EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
+       EXPECT_EQ(memcmp(&get, &expect, expect.len), 0);
+
+       /* short get should fail */
+       len = sizeof(struct tls_crypto_info) - 1;
+       EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
+       EXPECT_EQ(errno, EINVAL);
+
+       /* partial get of the cipher data should fail */
+       len = expect.len - 1;
+       EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
+       EXPECT_EQ(errno, EINVAL);
+}
+
 FIXTURE(tls_err)
 {
        int fd, cfd;