1 /*
2  * Copyright (c) 2020 Friedt Professional Engineering Services, Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "_main.h"
8 
ZTEST_USER_F(net_socketpair,test_expected_failures)9 ZTEST_USER_F(net_socketpair, test_expected_failures)
10 {
11 	int res;
12 
13 	/* Use invalid values in fields starting from left to right */
14 
15 	res = socketpair(AF_INET, SOCK_STREAM, 0, fixture->sv);
16 	if (res != -1) {
17 		for (int i = 0; i < 2; ++i) {
18 			zassert_ok(close(fixture->sv[i]));
19 			fixture->sv[i] = -1;
20 		}
21 	}
22 	zassert_equal(res, -1, "socketpair should fail with bad address family");
23 	zassert_equal(errno, EAFNOSUPPORT,
24 				  "errno should be EAFNOSUPPORT with bad address family");
25 
26 	res = socketpair(AF_UNIX, 42, 0, fixture->sv);
27 	if (res != -1) {
28 		for (int i = 0; i < 2; ++i) {
29 			zassert_ok(close(fixture->sv[i]));
30 			fixture->sv[i] = -1;
31 		}
32 	}
33 	zassert_equal(res, -1,
34 				  "socketpair should fail with unsupported socket type");
35 	zassert_equal(errno, EPROTOTYPE,
36 				  "errno should be EPROTOTYPE with bad socket type");
37 
38 	res = socketpair(AF_UNIX, SOCK_STREAM, IPPROTO_TLS_1_0, fixture->sv);
39 	if (res != -1) {
40 		for (int i = 0; i < 2; ++i) {
41 			zassert_ok(close(fixture->sv[i]));
42 			fixture->sv[i] = -1;
43 		}
44 	}
45 	zassert_equal(res, -1,
46 				  "socketpair should fail with unsupported protocol");
47 	zassert_equal(errno, EPROTONOSUPPORT,
48 				  "errno should be EPROTONOSUPPORT with bad protocol");
49 
50 	/* This is not a POSIX requirement, but should be safe */
51 	res = socketpair(AF_UNIX, SOCK_STREAM, 0, NULL);
52 	if (res != -1) {
53 		for (int i = 0; i < 2; ++i) {
54 			zassert_ok(close(fixture->sv[i]));
55 			fixture->sv[i] = -1;
56 		}
57 	}
58 	zassert_equal(res, -1,
59 				  "socketpair should fail with invalid socket vector");
60 	zassert_equal(errno, EFAULT,
61 				  "errno should be EFAULT with bad socket vector");
62 }
63