| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * be found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 // Test that SIGPIPE is not raised when using nacl::SendDatagram or | 7 // Test that SIGPIPE is not raised when using nacl::SendDatagram or |
| 8 // nacl::SendDatagramTo when the peer has been closed for various | 8 // nacl::SendDatagramTo when the peer has been closed for various |
| 9 // flavors of sockets. | 9 // flavors of sockets. |
| 10 | 10 |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 #include <errno.h> | 15 #include <errno.h> |
| 16 | 16 |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "native_client/src/include/checked_cast.h" | 19 #include "native_client/src/include/checked_cast.h" |
| 20 #include "native_client/src/include/nacl_macros.h" | 20 #include "native_client/src/include/nacl_macros.h" |
| 21 #include "native_client/src/include/nacl_string.h" | 21 #include "native_client/src/include/nacl_string.h" |
| 22 #include "native_client/src/include/portability.h" | 22 #include "native_client/src/include/portability.h" |
| 23 #include "native_client/src/include/portability_process.h" | 23 #include "native_client/src/include/portability_process.h" |
| 24 #include "native_client/src/shared/imc/nacl_imc.h" | 24 #include "native_client/src/shared/imc/nacl_imc.h" |
| 25 #include "native_client/src/shared/platform/nacl_log.h" | 25 #include "native_client/src/shared/platform/nacl_log.h" |
| 26 #include "native_client/src/shared/platform/nacl_sync.h" | 26 #include "native_client/src/shared/platform/nacl_sync.h" |
| 27 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
| 27 #include "native_client/src/shared/platform/nacl_threads.h" | 28 #include "native_client/src/shared/platform/nacl_threads.h" |
| 28 | 29 |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const bool kPlatformUsesBoundSockets = NACL_WINDOWS; | 33 const bool kPlatformUsesBoundSockets = NACL_WINDOWS; |
| 33 | 34 |
| 34 bool gSleepBeforeReceive(false); | 35 bool gSleepBeforeReceive(false); |
| 35 std::vector<int> gTestSequence; | 36 std::vector<int> gTestSequence; |
| 36 | 37 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 : cli_sock(nacl::kInvalidHandle), | 133 : cli_sock(nacl::kInvalidHandle), |
| 133 srv_sock(nacl::kInvalidHandle), | 134 srv_sock(nacl::kInvalidHandle), |
| 134 new_sock_only(nso), | 135 new_sock_only(nso), |
| 135 repetitions(reps), | 136 repetitions(reps), |
| 136 outer_rep(out_rep), | 137 outer_rep(out_rep), |
| 137 test_sequence(seqp), | 138 test_sequence(seqp), |
| 138 errors(-1), | 139 errors(-1), |
| 139 cur_test(-1) { | 140 cur_test(-1) { |
| 140 pair[0] = nacl::kInvalidHandle; | 141 pair[0] = nacl::kInvalidHandle; |
| 141 pair[1] = nacl::kInvalidHandle; | 142 pair[1] = nacl::kInvalidHandle; |
| 142 (void) NaClMutexCtor(&mu); | 143 NaClXMutexCtor(&mu); |
| 143 (void) NaClCondVarCtor(&cv); | 144 NaClXCondVarCtor(&cv); |
| 144 } | 145 } |
| 145 | 146 |
| 146 int TestState::Init() { | 147 int TestState::Init() { |
| 147 if (kPlatformUsesBoundSockets) { | 148 if (kPlatformUsesBoundSockets) { |
| 148 PickRandomSocketAddress(&cli_addr); | 149 PickRandomSocketAddress(&cli_addr); |
| 149 cli_sock = nacl::BoundSocket(&cli_addr); | 150 cli_sock = nacl::BoundSocket(&cli_addr); |
| 150 if (nacl::kInvalidHandle == cli_sock) { | 151 if (nacl::kInvalidHandle == cli_sock) { |
| 151 MyPerror("BoundSocket"); | 152 MyPerror("BoundSocket"); |
| 152 printf("ERROR: No client socket\n"); | 153 printf("ERROR: No client socket\n"); |
| 153 return 1; | 154 return 1; |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 tsp->test_sequence->end() != it; | 632 tsp->test_sequence->end() != it; |
| 632 ++it) { | 633 ++it) { |
| 633 int test = *it; | 634 int test = *it; |
| 634 | 635 |
| 635 if (test_fn[test].new_socks != tsp->new_sock_only) { | 636 if (test_fn[test].new_socks != tsp->new_sock_only) { |
| 636 printf("PeerThread: new_socks mismatch, skipping\n"); | 637 printf("PeerThread: new_socks mismatch, skipping\n"); |
| 637 continue; | 638 continue; |
| 638 } | 639 } |
| 639 | 640 |
| 640 printf("PeerThread: Locking for test %d to start\n", test); | 641 printf("PeerThread: Locking for test %d to start\n", test); |
| 641 NaClMutexLock(&tsp->mu); | 642 NaClXMutexLock(&tsp->mu); |
| 642 while (tsp->cur_test != test) { | 643 while (tsp->cur_test != test) { |
| 643 printf("PeerThread: waiting for test %d to start\n", test); | 644 printf("PeerThread: waiting for test %d to start\n", test); |
| 644 printf("tsp->cur_test %d\n", tsp->cur_test); | 645 printf("tsp->cur_test %d\n", tsp->cur_test); |
| 645 NaClCondVarWait(&tsp->cv, &tsp->mu); | 646 NaClXCondVarWait(&tsp->cv, &tsp->mu); |
| 646 } | 647 } |
| 647 NaClMutexUnlock(&tsp->mu); | 648 NaClXMutexUnlock(&tsp->mu); |
| 648 | 649 |
| 649 printf("PeerThread: START test %d, %s\n", test, test_fn[test].name); | 650 printf("PeerThread: START test %d, %s\n", test, test_fn[test].name); |
| 650 errors += test_fn[test].sender(tsp, test_fn[test].mode); | 651 errors += test_fn[test].sender(tsp, test_fn[test].mode); |
| 651 printf("PeerThread: END test %d, %s\n", test, test_fn[test].name); | 652 printf("PeerThread: END test %d, %s\n", test, test_fn[test].name); |
| 652 | 653 |
| 653 printf("PeerThread: Locking for test %d to end\n", test); | 654 printf("PeerThread: Locking for test %d to end\n", test); |
| 654 NaClMutexLock(&tsp->mu); | 655 NaClXMutexLock(&tsp->mu); |
| 655 tsp->cur_test = -1; | 656 tsp->cur_test = -1; |
| 656 NaClCondVarSignal(&tsp->cv); | 657 NaClXCondVarSignal(&tsp->cv); |
| 657 NaClMutexUnlock(&tsp->mu); | 658 NaClXMutexUnlock(&tsp->mu); |
| 658 } | 659 } |
| 659 } | 660 } |
| 660 if (-1 == tsp->outer_rep) | 661 if (-1 == tsp->outer_rep) |
| 661 printf("\n======== EXITING PEER THREAD ========\n"); | 662 printf("\n======== EXITING PEER THREAD ========\n"); |
| 662 else | 663 else |
| 663 printf("\n======== EXITING INDEPENDENT PEER THREAD" | 664 printf("\n======== EXITING INDEPENDENT PEER THREAD" |
| 664 " ========\n"); | 665 " ========\n"); |
| 665 | 666 |
| 666 printf("%sPEER THREAD EXITING, LOCKING\n", | 667 printf("%sPEER THREAD EXITING, LOCKING\n", |
| 667 (-1 == tsp->outer_rep) ? "" : "INDEPENDENT "); | 668 (-1 == tsp->outer_rep) ? "" : "INDEPENDENT "); |
| 668 (void) NaClMutexLock(&tsp->mu); | 669 NaClXMutexLock(&tsp->mu); |
| 669 tsp->errors = errors; | 670 tsp->errors = errors; |
| 670 printf("%sPEER THREAD EXITING, SIGNALING\n", | 671 printf("%sPEER THREAD EXITING, SIGNALING\n", |
| 671 (-1 == tsp->outer_rep) ? "" : "INDEPENDENT "); | 672 (-1 == tsp->outer_rep) ? "" : "INDEPENDENT "); |
| 672 fflush(NULL); | 673 fflush(NULL); |
| 673 (void) NaClCondVarSignal(&tsp->cv); | 674 NaClXCondVarSignal(&tsp->cv); |
| 674 (void) NaClMutexUnlock(&tsp->mu); | 675 NaClXMutexUnlock(&tsp->mu); |
| 675 } | 676 } |
| 676 | 677 |
| 677 | 678 |
| 678 int TestNaClSocket(int rep_count) { | 679 int TestNaClSocket(int rep_count) { |
| 679 int errors = 0; | 680 int errors = 0; |
| 680 TestState tstate(&gTestSequence, false, rep_count, -1); | 681 TestState tstate(&gTestSequence, false, rep_count, -1); |
| 681 | 682 |
| 682 errors += tstate.Init(); | 683 errors += tstate.Init(); |
| 683 if (0 != errors) return errors; | 684 if (0 != errors) return errors; |
| 684 | 685 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 705 fflush(NULL); | 706 fflush(NULL); |
| 706 #if NACL_WINDOWS | 707 #if NACL_WINDOWS |
| 707 Sleep(1000); | 708 Sleep(1000); |
| 708 #else | 709 #else |
| 709 sleep(1); | 710 sleep(1); |
| 710 #endif | 711 #endif |
| 711 } | 712 } |
| 712 | 713 |
| 713 if (!test_fn[test].new_socks) { | 714 if (!test_fn[test].new_socks) { |
| 714 printf("Locking to start test %d\n", test); | 715 printf("Locking to start test %d\n", test); |
| 715 NaClMutexLock(&tstate.mu); | 716 NaClXMutexLock(&tstate.mu); |
| 716 tstate.cur_test = test; | 717 tstate.cur_test = test; |
| 717 NaClCondVarSignal(&tstate.cv); | 718 NaClXCondVarSignal(&tstate.cv); |
| 718 NaClMutexUnlock(&tstate.mu); | 719 NaClXMutexUnlock(&tstate.mu); |
| 719 printf("Signaled test %d start\n", test); | 720 printf("Signaled test %d start\n", test); |
| 720 | 721 |
| 721 errors += test_fn[test].receiver(&tstate, test_fn[test].mode); | 722 errors += test_fn[test].receiver(&tstate, test_fn[test].mode); |
| 722 | 723 |
| 723 printf("Locking to wait for test %d end\n", test); | 724 printf("Locking to wait for test %d end\n", test); |
| 724 NaClMutexLock(&tstate.mu); | 725 NaClXMutexLock(&tstate.mu); |
| 725 while (-1 != tstate.cur_test) { | 726 while (-1 != tstate.cur_test) { |
| 726 printf("Waiting for test %d to be finished\n", test); | 727 printf("Waiting for test %d to be finished\n", test); |
| 727 printf("tstate.cur_test %d\n", tstate.cur_test); | 728 printf("tstate.cur_test %d\n", tstate.cur_test); |
| 728 NaClCondVarWait(&tstate.cv, &tstate.mu); | 729 NaClXCondVarWait(&tstate.cv, &tstate.mu); |
| 729 } | 730 } |
| 730 NaClMutexUnlock(&tstate.mu); | 731 NaClXMutexUnlock(&tstate.mu); |
| 731 } else { | 732 } else { |
| 732 printf("test %d requests independent socket/thread\n", test); | 733 printf("test %d requests independent socket/thread\n", test); |
| 733 std::vector<int> seq; | 734 std::vector<int> seq; |
| 734 seq.push_back(test); | 735 seq.push_back(test); |
| 735 TestState private_sock(&seq, true, 1, rep); | 736 TestState private_sock(&seq, true, 1, rep); |
| 736 int private_errors(private_sock.Init()); | 737 int private_errors(private_sock.Init()); |
| 737 if (0 != private_errors) { | 738 if (0 != private_errors) { |
| 738 printf("Could not create/initialize TestState.\n"); | 739 printf("Could not create/initialize TestState.\n"); |
| 739 errors += private_errors; | 740 errors += private_errors; |
| 740 continue; | 741 continue; |
| 741 } | 742 } |
| 742 struct NaClThread private_thr; | 743 struct NaClThread private_thr; |
| 743 (void) NaClThreadCtor(&private_thr, PeerThread, | 744 (void) NaClThreadCtor(&private_thr, PeerThread, |
| 744 static_cast<void *>(&private_sock), 128*1024); | 745 static_cast<void *>(&private_sock), 128*1024); |
| 745 | 746 |
| 746 printf("Locking to start test %d\n", test); | 747 printf("Locking to start test %d\n", test); |
| 747 NaClMutexLock(&private_sock.mu); | 748 NaClXMutexLock(&private_sock.mu); |
| 748 private_sock.cur_test = test; | 749 private_sock.cur_test = test; |
| 749 NaClCondVarSignal(&private_sock.cv); | 750 NaClXCondVarSignal(&private_sock.cv); |
| 750 NaClMutexUnlock(&private_sock.mu); | 751 NaClXMutexUnlock(&private_sock.mu); |
| 751 printf("Signaled test %d start\n", test); | 752 printf("Signaled test %d start\n", test); |
| 752 | 753 |
| 753 errors += test_fn[test].receiver(&private_sock, test_fn[test].mode); | 754 errors += test_fn[test].receiver(&private_sock, test_fn[test].mode); |
| 754 | 755 |
| 755 printf("Locking to wait for test %d end\n", test); | 756 printf("Locking to wait for test %d end\n", test); |
| 756 NaClMutexLock(&private_sock.mu); | 757 NaClXMutexLock(&private_sock.mu); |
| 757 while (-1 != private_sock.cur_test) { | 758 while (-1 != private_sock.cur_test) { |
| 758 printf("Waiting for test %d to be finished\n", test); | 759 printf("Waiting for test %d to be finished\n", test); |
| 759 printf("private_sock %d\n", private_sock.cur_test); | 760 printf("private_sock %d\n", private_sock.cur_test); |
| 760 NaClCondVarWait(&private_sock.cv, &private_sock.mu); | 761 NaClXCondVarWait(&private_sock.cv, &private_sock.mu); |
| 761 } | 762 } |
| 762 NaClMutexUnlock(&private_sock.mu); | 763 NaClXMutexUnlock(&private_sock.mu); |
| 763 | 764 |
| 764 fflush(NULL); | 765 fflush(NULL); |
| 765 (void) NaClMutexLock(&private_sock.mu); | 766 NaClXMutexLock(&private_sock.mu); |
| 766 while (-1 == private_sock.errors) { | 767 while (-1 == private_sock.errors) { |
| 767 (void) NaClCondVarWait(&private_sock.cv, &private_sock.mu); | 768 NaClXCondVarWait(&private_sock.cv, &private_sock.mu); |
| 768 } | 769 } |
| 769 (void) NaClMutexUnlock(&private_sock.mu); | 770 NaClXMutexUnlock(&private_sock.mu); |
| 770 errors += private_sock.errors; | 771 errors += private_sock.errors; |
| 771 if (private_sock.outer_rep != rep) { | 772 if (private_sock.outer_rep != rep) { |
| 772 printf("Threads out of sync!?!\n"); | 773 printf("Threads out of sync!?!\n"); |
| 773 abort(); | 774 abort(); |
| 774 } | 775 } |
| 775 } | 776 } |
| 776 } | 777 } |
| 777 } | 778 } |
| 778 | 779 |
| 779 | 780 |
| 780 printf("MainThread: Waiting for receiver thread to exit.\n"); | 781 printf("MainThread: Waiting for receiver thread to exit.\n"); |
| 781 fflush(NULL); | 782 fflush(NULL); |
| 782 (void) NaClMutexLock(&tstate.mu); | 783 NaClXMutexLock(&tstate.mu); |
| 783 while (-1 == tstate.errors) { | 784 while (-1 == tstate.errors) { |
| 784 (void) NaClCondVarWait(&tstate.cv, &tstate.mu); | 785 NaClXCondVarWait(&tstate.cv, &tstate.mu); |
| 785 } | 786 } |
| 786 (void) NaClMutexUnlock(&tstate.mu); | 787 NaClXMutexUnlock(&tstate.mu); |
| 787 NaClThreadDtor(&thr); | 788 NaClThreadDtor(&thr); |
| 788 errors += tstate.errors; | 789 errors += tstate.errors; |
| 789 | 790 |
| 790 // now close server side and attempt to send again. | 791 // now close server side and attempt to send again. |
| 791 printf("nacl::Close(%d)\n", tstate.srv_sock); | 792 printf("nacl::Close(%d)\n", tstate.srv_sock); |
| 792 (void) nacl::Close(tstate.srv_sock); | 793 (void) nacl::Close(tstate.srv_sock); |
| 793 tstate.srv_sock = nacl::kInvalidHandle; | 794 tstate.srv_sock = nacl::kInvalidHandle; |
| 794 printf("nacl::Close(%d)\n", tstate.pair[0]); | 795 printf("nacl::Close(%d)\n", tstate.pair[0]); |
| 795 (void) nacl::Close(tstate.pair[0]); | 796 (void) nacl::Close(tstate.pair[0]); |
| 796 tstate.pair[0] = nacl::kInvalidHandle; | 797 tstate.pair[0] = nacl::kInvalidHandle; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 894 #endif | 895 #endif |
| 895 | 896 |
| 896 errors += TestNaClSocket(rep_count); | 897 errors += TestNaClSocket(rep_count); |
| 897 | 898 |
| 898 printf("%s\n", (errors == 0) ? "PASSED" : "FAILED"); | 899 printf("%s\n", (errors == 0) ? "PASSED" : "FAILED"); |
| 899 | 900 |
| 900 NaClLogModuleFini(); | 901 NaClLogModuleFini(); |
| 901 | 902 |
| 902 return errors; | 903 return errors; |
| 903 } | 904 } |
| OLD | NEW |