LCOV - code coverage report
Current view: top level - source4/lib/socket - testsuite.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 91 92 98.9 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local testing of socket routines.
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2005
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "lib/socket/socket.h"
      24             : #include "lib/events/events.h"
      25             : #include "system/network.h"
      26             : #include "lib/socket/netif.h"
      27             : #include "torture/torture.h"
      28             : #include "torture/local/proto.h"
      29             : #include "param/param.h"
      30             : #include "libcli/resolve/resolve.h"
      31             : 
      32             : /**
      33             :   basic testing of udp routines
      34             : */
      35           1 : static bool test_udp(struct torture_context *tctx)
      36             : {
      37           1 :         struct socket_context *sock1, *sock2;
      38           1 :         NTSTATUS status;
      39           1 :         struct socket_address *srv_addr, *from_addr, *localhost;
      40           1 :         size_t size = 100 + (random() % 100);
      41           1 :         DATA_BLOB blob, blob2;
      42           1 :         size_t sent, nread;
      43           1 :         TALLOC_CTX *mem_ctx = tctx;
      44           1 :         struct interface *ifaces;
      45             : 
      46           1 :         load_interface_list(tctx, tctx->lp_ctx, &ifaces);
      47             : 
      48           1 :         status = socket_create(mem_ctx, "ip", SOCKET_TYPE_DGRAM, &sock1, 0);
      49           1 :         torture_assert_ntstatus_ok(tctx, status, "creating DGRAM IP socket 1");
      50             : 
      51           1 :         status = socket_create(mem_ctx, "ip", SOCKET_TYPE_DGRAM, &sock2, 0);
      52           1 :         torture_assert_ntstatus_ok(tctx, status, "creating DGRAM IP socket 1");
      53             : 
      54           1 :         localhost = socket_address_from_strings(sock1, sock1->backend_name, 
      55             :                                                 iface_list_best_ip(ifaces, "127.0.0.1"), 0);
      56             : 
      57           1 :         torture_assert(tctx, localhost, "Localhost not found");
      58             : 
      59           1 :         status = socket_listen(sock1, localhost, 0, 0);
      60           1 :         torture_assert_ntstatus_ok(tctx, status, "listen on socket 1");
      61             : 
      62           1 :         srv_addr = socket_get_my_addr(sock1, mem_ctx);
      63           1 :         torture_assert(tctx, srv_addr != NULL && 
      64             :                        strcmp(srv_addr->addr, iface_list_best_ip(ifaces, "127.0.0.1")) == 0,
      65             :                                    talloc_asprintf(tctx, 
      66             :                 "Expected server address of %s but got %s",
      67             :                       iface_list_best_ip(ifaces, "127.0.0.1"), srv_addr ? srv_addr->addr : NULL));
      68             : 
      69           1 :         torture_comment(tctx, "server port is %d\n", srv_addr->port);
      70             : 
      71           1 :         blob  = data_blob_talloc(mem_ctx, NULL, size);
      72           1 :         blob2 = data_blob_talloc(mem_ctx, NULL, size);
      73           1 :         generate_random_buffer(blob.data, blob.length);
      74             : 
      75           1 :         sent = size;
      76           1 :         status = socket_sendto(sock2, &blob, &sent, srv_addr);
      77           1 :         torture_assert_ntstatus_ok(tctx, status, "sendto() on socket 2");
      78             : 
      79           1 :         status = socket_recvfrom(sock1, blob2.data, size, &nread, 
      80             :                                  sock1, &from_addr);
      81           1 :         torture_assert_ntstatus_ok(tctx, status, "recvfrom() on socket 1");
      82             : 
      83           1 :         torture_assert_str_equal(tctx, from_addr->addr, srv_addr->addr, 
      84             :                                                          "different address");
      85             : 
      86           1 :         torture_assert_int_equal(tctx, nread, size, "Unexpected recvfrom size");
      87             : 
      88           1 :         torture_assert_mem_equal(tctx, blob2.data, blob.data, size,
      89             :                 "Bad data in recvfrom");
      90             : 
      91           1 :         generate_random_buffer(blob.data, blob.length);
      92           1 :         status = socket_sendto(sock1, &blob, &sent, from_addr);
      93           1 :         torture_assert_ntstatus_ok(tctx, status, "sendto() on socket 1");
      94             : 
      95           1 :         status = socket_recvfrom(sock2, blob2.data, size, &nread, 
      96             :                                  sock2, &from_addr);
      97           1 :         torture_assert_ntstatus_ok(tctx, status, "recvfrom() on socket 2");
      98           1 :         torture_assert_str_equal(tctx, from_addr->addr, srv_addr->addr, 
      99             :                                                          "Unexpected recvfrom addr");
     100             :         
     101           1 :         torture_assert_int_equal(tctx, nread, size, "Unexpected recvfrom size");
     102             : 
     103           1 :         torture_assert_int_equal(tctx, from_addr->port, srv_addr->port, 
     104             :                                    "Unexpected recvfrom port");
     105             : 
     106           1 :         torture_assert_mem_equal(tctx, blob2.data, blob.data, size,
     107             :                 "Bad data in recvfrom");
     108             : 
     109           1 :         talloc_free(sock1);
     110           1 :         talloc_free(sock2);
     111           1 :         return true;
     112             : }
     113             : 
     114             : /*
     115             :   basic testing of tcp routines
     116             : */
     117           1 : static bool test_tcp(struct torture_context *tctx)
     118             : {
     119           1 :         struct socket_context *sock1, *sock2, *sock3;
     120           1 :         NTSTATUS status;
     121           1 :         struct socket_address *srv_addr, *from_addr, *localhost;
     122           1 :         size_t size = 100 + (random() % 100);
     123           1 :         DATA_BLOB blob, blob2;
     124           1 :         size_t sent, nread;
     125           1 :         TALLOC_CTX *mem_ctx = tctx;
     126           1 :         struct tevent_context *ev = tctx->ev;
     127           1 :         struct interface *ifaces;
     128             : 
     129           1 :         status = socket_create(mem_ctx, "ip", SOCKET_TYPE_STREAM, &sock1, 0);
     130           1 :         torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1");
     131             : 
     132           1 :         status = socket_create(mem_ctx, "ip", SOCKET_TYPE_STREAM, &sock2, 0);
     133           1 :         torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1");
     134             : 
     135           1 :         load_interface_list(tctx, tctx->lp_ctx, &ifaces);
     136           1 :         localhost = socket_address_from_strings(sock1, sock1->backend_name, 
     137             :                                                 iface_list_best_ip(ifaces, "127.0.0.1"), 0);
     138           1 :         torture_assert(tctx, localhost, "Localhost not found");
     139             : 
     140           1 :         status = socket_listen(sock1, localhost, 0, 0);
     141           1 :         torture_assert_ntstatus_ok(tctx, status, "listen on socket 1");
     142             : 
     143           1 :         srv_addr = socket_get_my_addr(sock1, mem_ctx);
     144           1 :         torture_assert(tctx, srv_addr && srv_addr->addr, 
     145             :                                    "Unexpected socket_get_my_addr NULL\n");
     146             : 
     147           1 :         torture_assert_str_equal(tctx, srv_addr->addr, iface_list_best_ip(ifaces, "127.0.0.1"),
     148             :                         "Unexpected server address");
     149             : 
     150           1 :         torture_comment(tctx, "server port is %d\n", srv_addr->port);
     151             : 
     152           1 :         status = socket_connect_ev(sock2, NULL, srv_addr, 0, ev);
     153           1 :         torture_assert_ntstatus_ok(tctx, status, "connect() on socket 2");
     154             : 
     155           1 :         status = socket_accept(sock1, &sock3);
     156           1 :         torture_assert_ntstatus_ok(tctx, status, "accept() on socket 1");
     157           1 :         talloc_steal(mem_ctx, sock3);
     158           1 :         talloc_free(sock1);
     159             : 
     160           1 :         blob  = data_blob_talloc(mem_ctx, NULL, size);
     161           1 :         blob2 = data_blob_talloc(mem_ctx, NULL, size);
     162           1 :         generate_random_buffer(blob.data, blob.length);
     163             : 
     164           1 :         sent = size;
     165           1 :         status = socket_send(sock2, &blob, &sent);
     166           1 :         torture_assert_ntstatus_ok(tctx, status, "send() on socket 2");
     167             : 
     168           1 :         status = socket_recv(sock3, blob2.data, size, &nread);
     169           1 :         torture_assert_ntstatus_ok(tctx, status, "recv() on socket 3");
     170             : 
     171           1 :         from_addr = socket_get_peer_addr(sock3, mem_ctx);
     172             : 
     173           1 :         torture_assert(tctx, from_addr && from_addr->addr, 
     174             :                 "Unexpected recvfrom addr NULL");
     175             : 
     176           1 :         torture_assert_str_equal(tctx, from_addr->addr, srv_addr->addr, 
     177             :                                                          "Unexpected recvfrom addr");
     178             : 
     179           1 :         torture_assert_int_equal(tctx, nread, size, "Unexpected recvfrom size");
     180             : 
     181           1 :         torture_assert_mem_equal(tctx, blob2.data, blob.data, size, 
     182             :                                    "Bad data in recv");
     183           0 :         return true;
     184             : }
     185             : 
     186        2354 : struct torture_suite *torture_local_socket(TALLOC_CTX *mem_ctx)
     187             : {
     188        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "socket");
     189             : 
     190        2354 :         torture_suite_add_simple_test(suite, "udp", test_udp);
     191        2354 :         torture_suite_add_simple_test(suite, "tcp", test_tcp);
     192             : 
     193        2354 :         return suite;
     194             : }

Generated by: LCOV version 1.14