Skip to content

Commit 4482f83

Browse files
Andrew Boiecarlescufi
authored andcommitted
tests: net: tcp: test zsock_get_context_object
Add test case to prove that this new API works. Signed-off-by: Andrew Boie <[email protected]>
1 parent 9a27ba5 commit 4482f83

File tree

1 file changed

+68
-1
lines changed
  • tests/net/socket/tcp/src

1 file changed

+68
-1
lines changed

tests/net/socket/tcp/src/main.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,73 @@ void test_v4_accept_timeout(void)
446446
k_sleep(TCP_TEARDOWN_TIMEOUT);
447447
}
448448

449+
#ifdef CONFIG_USERSPACE
450+
#define CHILD_STACK_SZ (2048 + CONFIG_TEST_EXTRA_STACKSIZE)
451+
struct k_thread child_thread;
452+
K_THREAD_STACK_DEFINE(child_stack, CHILD_STACK_SZ);
453+
ZTEST_BMEM volatile int result;
454+
455+
static void child_entry(void *p1, void *p2, void *p3)
456+
{
457+
int sock = (int)p1;
458+
459+
result = close(sock);
460+
}
461+
462+
static void spawn_child(int sock)
463+
{
464+
k_thread_create(&child_thread, child_stack,
465+
K_THREAD_STACK_SIZEOF(child_stack), child_entry,
466+
(void *)sock, NULL, NULL, 0, K_USER,
467+
K_FOREVER);
468+
}
469+
#endif
470+
471+
void test_socket_permission(void)
472+
{
473+
#ifdef CONFIG_USERSPACE
474+
int sock;
475+
struct sockaddr_in saddr;
476+
struct net_context *ctx;
477+
478+
prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, ANY_PORT,
479+
&sock, &saddr);
480+
481+
ctx = zsock_get_context_object(sock);
482+
zassert_not_null(ctx, "zsock_get_context_object() failed");
483+
484+
/* Spawn a child thread which doesn't inherit our permissions,
485+
* it will try to perform a socket operation and fail due to lack
486+
* of permissions on it.
487+
*/
488+
spawn_child(sock);
489+
k_thread_start(&child_thread);
490+
k_thread_join(&child_thread, K_FOREVER);
491+
492+
zassert_not_equal(result, 0, "child succeeded with no permission");
493+
494+
/* Now spawn the same child thread again, but this time we grant
495+
* permission on the net_context before we start it, and the
496+
* child should now succeed.
497+
*/
498+
spawn_child(sock);
499+
k_object_access_grant(ctx, &child_thread);
500+
k_thread_start(&child_thread);
501+
k_thread_join(&child_thread, K_FOREVER);
502+
503+
zassert_equal(result, 0, "child failed with permissions");
504+
#else
505+
ztest_test_skip();
506+
#endif /* CONFIG_USERSPACE */
507+
}
508+
449509
void test_main(void)
450510
{
511+
#ifdef CONFIG_USERSPACE
512+
/* ztest thread inherit permissions from main */
513+
k_thread_access_grant(k_current_get(), &child_thread, child_stack);
514+
#endif
515+
451516
ztest_test_suite(
452517
socket_tcp,
453518
ztest_user_unit_test(test_v4_send_recv),
@@ -457,7 +522,9 @@ void test_main(void)
457522
ztest_user_unit_test(test_v4_sendto_recvfrom_null_dest),
458523
ztest_user_unit_test(test_v6_sendto_recvfrom_null_dest),
459524
ztest_unit_test(test_open_close_immediately),
460-
ztest_user_unit_test(test_v4_accept_timeout));
525+
ztest_user_unit_test(test_v4_accept_timeout),
526+
ztest_user_unit_test(test_socket_permission)
527+
);
461528

462529
ztest_run_test_suite(socket_tcp);
463530
}

0 commit comments

Comments
 (0)