1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2021 Hengqi Chen */
3
4 #include <test_progs.h>
5 #include <sys/un.h>
6 #include "test_skc_to_unix_sock.skel.h"
7
8 static const char *sock_path = "@skc_to_unix_sock";
9
test_skc_to_unix_sock(void)10 void test_skc_to_unix_sock(void)
11 {
12 struct test_skc_to_unix_sock *skel;
13 struct sockaddr_un sockaddr;
14 int err, sockfd = 0;
15
16 skel = test_skc_to_unix_sock__open();
17 if (!ASSERT_OK_PTR(skel, "could not open BPF object"))
18 return;
19
20 skel->rodata->my_pid = getpid();
21
22 err = test_skc_to_unix_sock__load(skel);
23 if (!ASSERT_OK(err, "could not load BPF object"))
24 goto cleanup;
25
26 err = test_skc_to_unix_sock__attach(skel);
27 if (!ASSERT_OK(err, "could not attach BPF object"))
28 goto cleanup;
29
30 /* trigger unix_listen */
31 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
32 if (!ASSERT_GT(sockfd, 0, "socket failed"))
33 goto cleanup;
34
35 memset(&sockaddr, 0, sizeof(sockaddr));
36 sockaddr.sun_family = AF_UNIX;
37 strncpy(sockaddr.sun_path, sock_path, strlen(sock_path));
38 sockaddr.sun_path[0] = '\0';
39
40 err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
41 if (!ASSERT_OK(err, "bind failed"))
42 goto cleanup;
43
44 err = listen(sockfd, 1);
45 if (!ASSERT_OK(err, "listen failed"))
46 goto cleanup;
47
48 ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed");
49
50 cleanup:
51 if (sockfd)
52 close(sockfd);
53 test_skc_to_unix_sock__destroy(skel);
54 }
55