abstract.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <stdio.h>
  6. #if defined(LINUX)
  7. #include <string.h>
  8. #include "nspr.h"
  9. static const char abstractSocketName[] = "\0testsocket";
  10. static void
  11. ClientThread(void* aArg)
  12. {
  13. PRFileDesc* socket;
  14. PRNetAddr addr;
  15. PRUint8 buf[1024];
  16. PRInt32 len;
  17. PRInt32 total;
  18. addr.local.family = PR_AF_LOCAL;
  19. memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));
  20. socket = PR_OpenTCPSocket(addr.raw.family);
  21. if (!socket) {
  22. fprintf(stderr, "PR_OpenTCPSokcet failed\n");
  23. exit(1);
  24. }
  25. if (PR_Connect(socket, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
  26. fprintf(stderr, "PR_Connect failed\n");
  27. exit(1);
  28. }
  29. total = 0;
  30. while (total < sizeof(buf)) {
  31. len = PR_Recv(socket, buf + total, sizeof(buf) - total, 0,
  32. PR_INTERVAL_NO_TIMEOUT);
  33. if (len < 1) {
  34. fprintf(stderr, "PR_Recv failed\n");
  35. exit(1);
  36. }
  37. total += len;
  38. }
  39. total = 0;
  40. while (total < sizeof(buf)) {
  41. len = PR_Send(socket, buf + total, sizeof(buf) - total, 0,
  42. PR_INTERVAL_NO_TIMEOUT);
  43. if (len < 1) {
  44. fprintf(stderr, "PR_Send failed\n");
  45. exit(1);
  46. }
  47. total += len;
  48. }
  49. if (PR_Close(socket) == PR_FAILURE) {
  50. fprintf(stderr, "PR_Close failed\n");
  51. exit(1);
  52. }
  53. }
  54. int
  55. main()
  56. {
  57. PRFileDesc* socket;
  58. PRFileDesc* acceptSocket;
  59. PRThread* thread;
  60. PRNetAddr addr;
  61. PRUint8 buf[1024];
  62. PRInt32 len;
  63. PRInt32 total;
  64. addr.local.family = PR_AF_LOCAL;
  65. memcpy(addr.local.path, abstractSocketName, sizeof(abstractSocketName));
  66. socket = PR_OpenTCPSocket(addr.raw.family);
  67. if (!socket) {
  68. fprintf(stderr, "PR_OpenTCPSocket failed\n");
  69. exit(1);
  70. }
  71. if (PR_Bind(socket, &addr) == PR_FAILURE) {
  72. fprintf(stderr, "PR_Bind failed\n");
  73. exit(1);
  74. }
  75. if (PR_Listen(socket, 5) == PR_FAILURE) {
  76. fprintf(stderr, "PR_Listen failed\n");
  77. exit(1);
  78. }
  79. thread = PR_CreateThread(PR_USER_THREAD, ClientThread, 0, PR_PRIORITY_NORMAL,
  80. PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  81. if (!thread) {
  82. fprintf(stderr, "PR_CreateThread failed");
  83. exit(1);
  84. }
  85. acceptSocket = PR_Accept(socket, NULL, PR_INTERVAL_NO_TIMEOUT);
  86. if (!acceptSocket) {
  87. fprintf(stderr, "PR_Accept failed\n");
  88. exit(1);
  89. }
  90. memset(buf, 'A', sizeof(buf));
  91. total = 0;
  92. while (total < sizeof(buf)) {
  93. len = PR_Send(acceptSocket, buf + total, sizeof(buf) - total, 0,
  94. PR_INTERVAL_NO_TIMEOUT);
  95. if (len < 1) {
  96. fprintf(stderr, "PR_Send failed\n");
  97. exit(1);
  98. }
  99. total += len;
  100. }
  101. total = 0;
  102. while (total < sizeof(buf)) {
  103. len = PR_Recv(acceptSocket, buf + total, sizeof(buf) - total, 0,
  104. PR_INTERVAL_NO_TIMEOUT);
  105. if (len < 1) {
  106. fprintf(stderr, "PR_Recv failed\n");
  107. exit(1);
  108. }
  109. total += len;
  110. }
  111. if (PR_Close(acceptSocket) == PR_FAILURE) {
  112. fprintf(stderr, "PR_Close failed\n");
  113. exit(1);
  114. }
  115. if (PR_JoinThread(thread) == PR_FAILURE) {
  116. fprintf(stderr, "PR_JoinThread failed\n");
  117. exit(1);
  118. }
  119. if (PR_Close(socket) == PR_FAILURE) {
  120. fprintf(stderr, "PR_Close failed\n");
  121. exit(1);
  122. }
  123. printf("PASS\n");
  124. return 0;
  125. }
  126. #else
  127. int
  128. main()
  129. {
  130. printf("PASS\n");
  131. return 0;
  132. }
  133. #endif