Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(912)

Side by Side Diff: native_client_sdk/src/tests/nacl_io_socket_test/socket_test.cc

Issue 310903002: nacl_io: only copy up to the size of the sockaddr_in*, not the given len. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: stuff Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/socket/socket_node.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <arpa/inet.h> 5 #include <arpa/inet.h>
6 #include <errno.h> 6 #include <errno.h>
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <netinet/in.h> 8 #include <netinet/in.h>
9 #include <pthread.h> 9 #include <pthread.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 IP4ToSockAddr(LOCAL_HOST, PORT1, &addr); 610 IP4ToSockAddr(LOCAL_HOST, PORT1, &addr);
611 addrlen = sizeof(addr); 611 addrlen = sizeof(addr);
612 ASSERT_EQ(0, ki_connect(client_sock, (sockaddr*)&addr, addrlen)) 612 ASSERT_EQ(0, ki_connect(client_sock, (sockaddr*)&addr, addrlen))
613 << "Failed with " << errno << ": " << strerror(errno); 613 << "Failed with " << errno << ": " << strerror(errno);
614 614
615 ASSERT_EQ(greeting_len, ki_send(client_sock, client_greeting, 615 ASSERT_EQ(greeting_len, ki_send(client_sock, client_greeting,
616 greeting_len, 0)); 616 greeting_len, 0));
617 617
618 // Pass in addrlen that is larger than our actual address to make 618 // Pass in addrlen that is larger than our actual address to make
619 // sure that it is correctly set back to sizeof(sockaddr_in) 619 // sure that it is correctly set back to sizeof(sockaddr_in)
620 addrlen = sizeof(addr) + 10; 620 sockaddr_in client_addr[2];
621 int new_socket = ki_accept(server_sock, (sockaddr*)&addr, &addrlen); 621 sockaddr_in cmp_addr;
622 memset(&client_addr[0], 0, sizeof(client_addr[0]));
623 memset(&client_addr[1], 0xab, sizeof(client_addr[1]));
624 memset(&cmp_addr, 0xab, sizeof(cmp_addr));
625 addrlen = sizeof(client_addr[0]) + 10;
626 int new_socket = ki_accept(server_sock, (sockaddr*)&client_addr[0],
627 &addrlen);
622 ASSERT_GT(new_socket, -1) 628 ASSERT_GT(new_socket, -1)
623 << "accept failed with " << errno << ": " << strerror(errno); 629 << "accept failed with " << errno << ": " << strerror(errno);
630 ASSERT_EQ(addrlen, sizeof(sockaddr_in));
631 // Check that client_addr[1] and cmp_addr are the same (not overwritten).
632 ASSERT_EQ(0, memcmp(&client_addr[1], &cmp_addr, sizeof(cmp_addr)));
633 ASSERT_EQ(0xabab, client_addr[1].sin_port);
624 634
625 // Verify addr and addrlen were set correctly 635 // Verify addr and addrlen were set correctly
626 ASSERT_EQ(addrlen, sizeof(sockaddr_in)); 636 ASSERT_EQ(addrlen, sizeof(sockaddr_in));
627 sockaddr_in client_addr; 637 ASSERT_EQ(0, ki_getsockname(client_sock, (sockaddr*)&client_addr[1],
628 ASSERT_EQ(0, ki_getsockname(client_sock, (sockaddr*)&client_addr, &addrlen)); 638 &addrlen));
629 ASSERT_EQ(client_addr.sin_family, addr.sin_family); 639 ASSERT_EQ(client_addr[1].sin_family, client_addr[0].sin_family);
630 ASSERT_EQ(client_addr.sin_port, addr.sin_port); 640 ASSERT_EQ(client_addr[1].sin_port, client_addr[0].sin_port);
631 ASSERT_EQ(client_addr.sin_addr.s_addr, addr.sin_addr.s_addr); 641 ASSERT_EQ(client_addr[1].sin_addr.s_addr, client_addr[0].sin_addr.s_addr);
642
643 // Try a call where the supplied len is smaller than the expected length.
644 // The API should only write up to that amount, but should return the
645 // expected length.
646 sockaddr_in client_addr2;
647 memset(&client_addr2, 0, sizeof(client_addr2));
648 socklen_t truncated_len = sizeof(client_addr2.sin_family);
649 ASSERT_GT(sizeof(sockaddr_in), truncated_len);
650 ASSERT_EQ(0, ki_getsockname(client_sock, (sockaddr*)&client_addr2,
651 &truncated_len));
652 ASSERT_EQ(sizeof(sockaddr_in), truncated_len);
653 ASSERT_EQ(client_addr2.sin_family, client_addr[0].sin_family);
654 ASSERT_EQ(client_addr2.sin_port, 0);
655 ASSERT_EQ(client_addr2.sin_addr.s_addr, 0);
Sam Clegg 2014/06/04 00:45:28 This seems like it should be its own test really..
632 656
633 // Recv greeting from client and send reply 657 // Recv greeting from client and send reply
634 char inbuf[512]; 658 char inbuf[512];
635 ASSERT_EQ(greeting_len, ki_recv(new_socket, inbuf, sizeof(inbuf), 0)); 659 ASSERT_EQ(greeting_len, ki_recv(new_socket, inbuf, sizeof(inbuf), 0));
636 inbuf[greeting_len] = 0; 660 inbuf[greeting_len] = 0;
637 ASSERT_STREQ(inbuf, client_greeting); 661 ASSERT_STREQ(inbuf, client_greeting);
638 ASSERT_EQ(reply_len, ki_send(new_socket, server_reply, reply_len, 0)); 662 ASSERT_EQ(reply_len, ki_send(new_socket, server_reply, reply_len, 0));
639 663
640 // Recv reply on client socket 664 // Recv reply on client socket
641 ASSERT_EQ(reply_len, ki_recv(client_sock, inbuf, sizeof(inbuf), 0)); 665 ASSERT_EQ(reply_len, ki_recv(client_sock, inbuf, sizeof(inbuf), 0));
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 while (remainder > 0) { 830 while (remainder > 0) {
807 int rtn = ki_recv(new_sock, buffer, remainder, 0); 831 int rtn = ki_recv(new_sock, buffer, remainder, 0);
808 ASSERT_GT(rtn, 0); 832 ASSERT_GT(rtn, 0);
809 remainder -= rtn; 833 remainder -= rtn;
810 } 834 }
811 835
812 ASSERT_EQ(0, ki_close(new_sock)); 836 ASSERT_EQ(0, ki_close(new_sock));
813 } 837 }
814 838
815 #endif // PROVIDES_SOCKET_API 839 #endif // PROVIDES_SOCKET_API
OLDNEW
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/socket/socket_node.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698