Debian Codegraph @

nsprpub/pr/tests/abstract.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include <stdio.h>
7
8 #if defined(LINUX)
9
10 # include <string.h>
11 # include "nspr.h"
12
13 static const char abstractSocketName[] = "\0testsocket";
14
15 static void ClientThread(void* aArg) {
16 PRFileDesc* socket;
17 PRNetAddr addr;
18 PRUint8 buf[1024];
19 PRInt32 len;
20 PRInt32 total;
21
22 addr.local.family = PR_AF_LOCAL;
23 memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));
24
25 socket = PR_OpenTCPSocket(addr.raw.family);
26 if (!socket) {
27 fprintf(stderr, "PR_OpenTCPSokcet failed\n");
28 exit(1);
29 }
30
31 if (PR_Connect(socket, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
32 fprintf(stderr, "PR_Connect failed\n");
33 exit(1);
34 }
35
36 total = 0;
37 while (total < sizeof(buf)) {
38 len = PR_Recv(socket, buf + total, sizeof(buf) - total, 0,
39 PR_INTERVAL_NO_TIMEOUT);
40 if (len < 1) {
41 fprintf(stderr, "PR_Recv failed\n");
42 exit(1);
43 }
44 total += len;
45 }
46
47 total = 0;
48 while (total < sizeof(buf)) {
49 len = PR_Send(socket, buf + total, sizeof(buf) - total, 0,
50 PR_INTERVAL_NO_TIMEOUT);
51 if (len < 1) {
52 fprintf(stderr, "PR_Send failed\n");
53 exit(1);
54 }
55 total += len;
56 }
57
58 if (PR_Close(socket) == PR_FAILURE) {
59 fprintf(stderr, "PR_Close failed\n");
60 exit(1);
61 }
62 }
63
64 int main() {
65 PRFileDesc* socket;
66 PRFileDesc* acceptSocket;
67 PRThread* thread;
68 PRNetAddr addr;
69 PRUint8 buf[1024];
70 PRInt32 len;
71 PRInt32 total;
72
73 addr.local.family = PR_AF_LOCAL;
74 memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));
75
76 socket = PR_OpenTCPSocket(addr.raw.family);
77 if (!socket) {
78 fprintf(stderr, "PR_OpenTCPSocket failed\n");
79 exit(1);
80 }
81 if (PR_Bind(socket, &addr) == PR_FAILURE) {
82 fprintf(stderr, "PR_Bind failed\n");
83 exit(1);
84 }
85
86 if (PR_Listen(socket, 5) == PR_FAILURE) {
87 fprintf(stderr, "PR_Listen failed\n");
88 exit(1);
89 }
90
91 thread = PR_CreateThread(PR_USER_THREAD, ClientThread, 0, PR_PRIORITY_NORMAL,
92 PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
93 if (!thread) {
94 fprintf(stderr, "PR_CreateThread failed");
95 exit(1);
96 }
97
98 acceptSocket = PR_Accept(socket, NULL, PR_INTERVAL_NO_TIMEOUT);
99 if (!acceptSocket) {
100 fprintf(stderr, "PR_Accept failed\n");
101 exit(1);
102 }
103
104 memset(buf, 'A', sizeof(buf));
105
106 total = 0;
107 while (total < sizeof(buf)) {
108 len = PR_Send(acceptSocket, buf + total, sizeof(buf) - total, 0,
109 PR_INTERVAL_NO_TIMEOUT);
110 if (len < 1) {
111 fprintf(stderr, "PR_Send failed\n");
112 exit(1);
113 }
114 total += len;
115 }
116
117 total = 0;
118 while (total < sizeof(buf)) {
119 len = PR_Recv(acceptSocket, buf + total, sizeof(buf) - total, 0,
120 PR_INTERVAL_NO_TIMEOUT);
121 if (len < 1) {
122 fprintf(stderr, "PR_Recv failed\n");
123 exit(1);
124 }
125 total += len;
126 }
127
128 if (PR_Close(acceptSocket) == PR_FAILURE) {
129 fprintf(stderr, "PR_Close failed\n");
130 exit(1);
131 }
132
133 if (PR_JoinThread(thread) == PR_FAILURE) {
134 fprintf(stderr, "PR_JoinThread failed\n");
135 exit(1);
136 }
137
138 if (PR_Close(socket) == PR_FAILURE) {
139 fprintf(stderr, "PR_Close failed\n");
140 exit(1);
141 }
142 printf("PASS\n");
143 return 0;
144 }
145
146 #else
147 int main() {
148 printf("PASS\n");
149 return 0;
150 }
151 #endif