1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <sys/socket.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "test.h"
21
22 /**
23 * @tc.name : socketpair_0100
24 * @tc.desc : Use the socketpair function to create a pair of unnamed, interconnected sockets.
25 * @tc.level : Level 0
26 */
socketpair_0100(void)27 void socketpair_0100(void)
28 {
29 int fb[2];
30 char buf[128] = {0};
31 char *str = "mgn info is hello world";
32 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, fb);
33 if (result == -1) {
34 t_error("%s socketpair get result error is -1\n", __func__);
35 }
36 int size = write(fb[0], str, strlen(str));
37 read(fb[1], buf, size);
38 if (strcmp(buf, str) != 0) {
39 t_error("%s socketpair get buf is %s are not %s\n", __func__, buf, str);
40 }
41 }
42
43 /**
44 * @tc.name : socketpair_0200
45 * @tc.desc : Pass in AF_INET when testing the socketpair function call
46 * @tc.level : Level 2
47 */
socketpair_0200(void)48 void socketpair_0200(void)
49 {
50 int fb[2];
51 int result = socketpair(AF_INET, SOCK_STREAM, 0, fb);
52 if (result != -1) {
53 t_error("%s socketpair get result error is %d not -1\n", __func__, result);
54 }
55 }
56
57 /**
58 * @tc.name : socketpair_0300
59 * @tc.desc : Test that the length of the array passed two
60 * @tc.level : Level 1
61 */
socketpair_0300(void)62 void socketpair_0300(void)
63 {
64 int fb[3];
65 char buf[128] = {0};
66 char *str = "mgn info is hello world";
67 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, fb);
68 if (result == -1) {
69 t_error("%s socketpair get result error is %d not -1\n", __func__, result);
70 }
71 int size = write(fb[0], str, strlen(str));
72 read(fb[1], buf, size);
73 if (strcmp(buf, str) != 0) {
74 t_error("%s socketpair get buf is %s are not %s\n", __func__, buf, str);
75 }
76 }
77
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 socketpair_0100();
81 socketpair_0200();
82 socketpair_0300();
83 return t_status;
84 }