aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/net/af_unix/unix_connect.c
blob: d799fd8f5c7c239d465eced037bfa816c1782496 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: GPL-2.0

#define _GNU_SOURCE
#include <sched.h>

#include <stddef.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/un.h>

#include "../../kselftest_harness.h"

FIXTURE(unix_connect)
{
	int server, client;
	int family;
};

FIXTURE_VARIANT(unix_connect)
{
	int type;
	char sun_path[8];
	int len;
	int flags;
	int err;
};

FIXTURE_VARIANT_ADD(unix_connect, stream_pathname)
{
	.type = SOCK_STREAM,
	.sun_path = "test",
	.len = 4 + 1,
	.flags = 0,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, stream_abstract)
{
	.type = SOCK_STREAM,
	.sun_path = "\0test",
	.len = 5,
	.flags = 0,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, stream_pathname_netns)
{
	.type = SOCK_STREAM,
	.sun_path = "test",
	.len = 4 + 1,
	.flags = CLONE_NEWNET,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, stream_abstract_netns)
{
	.type = SOCK_STREAM,
	.sun_path = "\0test",
	.len = 5,
	.flags = CLONE_NEWNET,
	.err = ECONNREFUSED,
};

FIXTURE_VARIANT_ADD(unix_connect, dgram_pathname)
{
	.type = SOCK_DGRAM,
	.sun_path = "test",
	.len = 4 + 1,
	.flags = 0,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, dgram_abstract)
{
	.type = SOCK_DGRAM,
	.sun_path = "\0test",
	.len = 5,
	.flags = 0,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, dgram_pathname_netns)
{
	.type = SOCK_DGRAM,
	.sun_path = "test",
	.len = 4 + 1,
	.flags = CLONE_NEWNET,
	.err = 0,
};

FIXTURE_VARIANT_ADD(unix_connect, dgram_abstract_netns)
{
	.type = SOCK_DGRAM,
	.sun_path = "\0test",
	.len = 5,
	.flags = CLONE_NEWNET,
	.err = ECONNREFUSED,
};

FIXTURE_SETUP(unix_connect)
{
	self->family = AF_UNIX;
}

FIXTURE_TEARDOWN(unix_connect)
{
	close(self->server);
	close(self->client);

	if (variant->sun_path[0])
		remove("test");
}

TEST_F(unix_connect, test)
{
	socklen_t addrlen;
	struct sockaddr_un addr = {
		.sun_family = self->family,
	};
	int err;

	self->server = socket(self->family, variant->type, 0);
	ASSERT_NE(-1, self->server);

	addrlen = offsetof(struct sockaddr_un, sun_path) + variant->len;
	memcpy(&addr.sun_path, variant->sun_path, variant->len);

	err = bind(self->server, (struct sockaddr *)&addr, addrlen);
	ASSERT_EQ(0, err);

	if (variant->type == SOCK_STREAM) {
		err = listen(self->server, 32);
		ASSERT_EQ(0, err);
	}

	err = unshare(variant->flags);
	ASSERT_EQ(0, err);

	self->client = socket(self->family, variant->type, 0);
	ASSERT_LT(0, self->client);

	err = connect(self->client, (struct sockaddr *)&addr, addrlen);
	ASSERT_EQ(variant->err, err == -1 ? errno : 0);
}

TEST_HARNESS_MAIN