summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/tests/abstract.c
blob: 6be5610c04128ea17213a75e526b53984f04ebce (plain)
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
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>

#if defined(LINUX)

#include <string.h>
#include "nspr.h"

static const char abstractSocketName[] = "\0testsocket";

static void
ClientThread(void* aArg)
{
  PRFileDesc* socket;
  PRNetAddr addr;
  PRUint8 buf[1024];
  PRInt32 len;
  PRInt32 total;

  addr.local.family = PR_AF_LOCAL;
  memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));

  socket = PR_OpenTCPSocket(addr.raw.family);
  if (!socket) {
    fprintf(stderr, "PR_OpenTCPSokcet failed\n");
    exit(1);
  }

  if (PR_Connect(socket, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
    fprintf(stderr, "PR_Connect failed\n");
    exit(1);
  }

  total = 0;
  while (total < sizeof(buf)) {
    len = PR_Recv(socket, buf + total, sizeof(buf) - total, 0,
                  PR_INTERVAL_NO_TIMEOUT);
    if (len < 1) {
      fprintf(stderr, "PR_Recv failed\n");
      exit(1);
    }
    total += len;
  }

  total = 0;
  while (total < sizeof(buf)) {
    len = PR_Send(socket, buf + total, sizeof(buf) - total, 0,
                  PR_INTERVAL_NO_TIMEOUT);
    if (len < 1) {
      fprintf(stderr, "PR_Send failed\n");
      exit(1);
    }
    total += len;
  }

  if (PR_Close(socket) == PR_FAILURE) {
    fprintf(stderr, "PR_Close failed\n");
    exit(1);
  }
}

int
main()
{
  PRFileDesc* socket;
  PRFileDesc* acceptSocket;
  PRThread* thread;
  PRNetAddr addr;
  PRUint8 buf[1024];
  PRInt32 len;
  PRInt32 total;

  addr.local.family = PR_AF_LOCAL;
  memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));

  socket = PR_OpenTCPSocket(addr.raw.family);
  if (!socket) {
    fprintf(stderr, "PR_OpenTCPSocket failed\n");
    exit(1);
  }
  if (PR_Bind(socket, &addr) == PR_FAILURE) {
    fprintf(stderr, "PR_Bind failed\n");
    exit(1);
  }

  if (PR_Listen(socket, 5) == PR_FAILURE) {
    fprintf(stderr, "PR_Listen failed\n");
    exit(1);
  }

  thread = PR_CreateThread(PR_USER_THREAD, ClientThread, 0, PR_PRIORITY_NORMAL,
                           PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  if (!thread) {
    fprintf(stderr, "PR_CreateThread failed");
    exit(1);
  }

  acceptSocket  = PR_Accept(socket, NULL, PR_INTERVAL_NO_TIMEOUT);
  if (!acceptSocket) {
    fprintf(stderr, "PR_Accept failed\n");
    exit(1);
  }

  memset(buf, 'A', sizeof(buf));

  total = 0;
  while (total < sizeof(buf)) {
    len = PR_Send(acceptSocket, buf + total, sizeof(buf) - total, 0,
                  PR_INTERVAL_NO_TIMEOUT);
    if (len < 1) {
      fprintf(stderr, "PR_Send failed\n");
      exit(1);
    }
    total += len;
  }

  total = 0;
  while (total < sizeof(buf)) {
    len = PR_Recv(acceptSocket, buf + total, sizeof(buf) - total, 0,
                  PR_INTERVAL_NO_TIMEOUT);
    if (len < 1) {
      fprintf(stderr, "PR_Recv failed\n");
      exit(1);
    }
    total += len;
  }

  if (PR_Close(acceptSocket) == PR_FAILURE) {
    fprintf(stderr, "PR_Close failed\n");
    exit(1);
  }

  if (PR_JoinThread(thread) == PR_FAILURE) {
    fprintf(stderr, "PR_JoinThread failed\n");
    exit(1);
  }

  if (PR_Close(socket) == PR_FAILURE) {
    fprintf(stderr, "PR_Close failed\n");
    exit(1);
  }
  printf("PASS\n");
  return 0;
}

#else
int
main()
{
  prinf("PASS\n");
  return 0;
}
#endif