Back to index

glibc  2.9
tst-network.c
Go to the documentation of this file.
00001 /* Test for inet_network.
00002    Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Andreas Jaeger <aj@suse.de>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <sys/socket.h>
00023 #include <netinet/in.h>
00024 #include <arpa/inet.h>
00025 
00026 struct
00027 {
00028   const char *network;
00029   uint32_t number;
00030 } tests [] =
00031 {
00032   {"1.0.0.0", 0x1000000},
00033   {"1.0.0", 0x10000},
00034   {"1.0", 0x100},
00035   {"1", 0x1},
00036   {"192.168.0.0", 0xC0A80000},
00037   {"0", 0},
00038   {"0x0", 0},
00039   /* Now some invalid addresses.  */
00040   {"0x", INADDR_NONE},
00041   {"141.30.225.2800", INADDR_NONE},
00042   {"141.76.1.1.1", INADDR_NONE},
00043   {"141.76.1.11.", INADDR_NONE},
00044   {"1410", INADDR_NONE},
00045   {"1.1410", INADDR_NONE},
00046   {"1.1410.", INADDR_NONE},
00047   {"1.1410", INADDR_NONE},
00048   {"141.76.1111", INADDR_NONE},
00049   {"141.76.1111.", INADDR_NONE}
00050 };
00051 
00052 
00053 int
00054 main (void)
00055 {
00056   int errors = 0;
00057   size_t i;
00058   uint32_t res;
00059 
00060   for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
00061     {
00062       printf ("Testing: %s\n", tests[i].network);
00063       res = inet_network (tests[i].network);
00064 
00065       if (res != tests[i].number)
00066        {
00067          ++errors;
00068          printf ("Test failed for inet_network (\"%s\"):\n",
00069                 tests[i].network);
00070          printf ("Expected return value %u (0x%x) but got %u (0x%x).\n",
00071                 tests[i].number, tests[i].number, res, res);
00072        }
00073 
00074     }
00075 
00076   return errors != 0;
00077 }