1169523Srwatson/*- 2169523Srwatson * Copyright (c) 2007 Robert N. M. Watson 3169523Srwatson * All rights reserved. 4169523Srwatson * 5169523Srwatson * Redistribution and use in source and binary forms, with or without 6169523Srwatson * modification, are permitted provided that the following conditions 7169523Srwatson * are met: 8169523Srwatson * 1. Redistributions of source code must retain the above copyright 9169523Srwatson * notice, this list of conditions and the following disclaimer. 10169523Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11169523Srwatson * notice, this list of conditions and the following disclaimer in the 12169523Srwatson * documentation and/or other materials provided with the distribution. 13169523Srwatson * 14169523Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15169523Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16169523Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17169523Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18169523Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19169523Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20169523Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21169523Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22169523Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23169523Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24169523Srwatson * SUCH DAMAGE. 25169523Srwatson * 26169523Srwatson * $FreeBSD: releng/10.2/tools/regression/lib/libc/net/test-ether.c 169523 2007-05-13 14:03:21Z rwatson $ 27169523Srwatson */ 28169523Srwatson 29169523Srwatson#include <sys/types.h> 30169523Srwatson 31169523Srwatson#include <net/ethernet.h> 32169523Srwatson 33169523Srwatson#include <stdio.h> 34169523Srwatson#include <string.h> 35169523Srwatson 36169523Srwatsonstatic int testnum; 37169523Srwatson 38169523Srwatson#define OK() do { \ 39169523Srwatson printf("ok %d %s\n", testnum, __func__); \ 40169523Srwatson return; \ 41169523Srwatson} while (0) 42169523Srwatson 43169523Srwatson#define NOTOK(why) do { \ 44169523Srwatson printf("not ok %d %s # %s\n", testnum, __func__, why); \ 45169523Srwatson return; \ 46169523Srwatson} while (0) 47169523Srwatson 48169523Srwatson#define TODO() NOTOK("TODO") 49169523Srwatson 50169523Srwatsonstatic const char *ether_line_string = 51169523Srwatson "01:23:45:67:89:ab ether_line_hostname"; 52169523Srwatsonstatic const char *ether_line_hostname = "ether_line_hostname"; 53169523Srwatsonstatic const struct ether_addr ether_line_addr = { 54169523Srwatson { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } 55169523Srwatson}; 56169523Srwatson 57169523Srwatsonstatic void 58169523Srwatsontest_ether_line(void) 59169523Srwatson{ 60169523Srwatson struct ether_addr e; 61169523Srwatson char hostname[256]; 62169523Srwatson 63169523Srwatson testnum++; 64169523Srwatson if (ether_line(ether_line_string, &e, hostname) != 0) 65169523Srwatson NOTOK("returned error"); 66169523Srwatson if (bcmp(&e, ðer_line_addr, ETHER_ADDR_LEN) != 0) 67169523Srwatson NOTOK("bad address"); 68169523Srwatson if (strcmp(hostname, ether_line_hostname) != 0) { 69169523Srwatson printf("hostname: %s\n", hostname); 70169523Srwatson NOTOK("bad hostname"); 71169523Srwatson } 72169523Srwatson OK(); 73169523Srwatson} 74169523Srwatson 75169523Srwatsonstatic const char *ether_line_bad_1_string = "x"; 76169523Srwatson 77169523Srwatsonstatic void 78169523Srwatsontest_ether_line_bad_1(void) 79169523Srwatson{ 80169523Srwatson struct ether_addr e; 81169523Srwatson char hostname[256]; 82169523Srwatson 83169523Srwatson testnum++; 84169523Srwatson if (ether_line(ether_line_bad_1_string, &e, hostname) == 0) 85169523Srwatson NOTOK("returned success"); 86169523Srwatson OK(); 87169523Srwatson} 88169523Srwatson 89169523Srwatsonstatic const char *ether_line_bad_2_string = "x x"; 90169523Srwatson 91169523Srwatsonstatic void 92169523Srwatsontest_ether_line_bad_2(void) 93169523Srwatson{ 94169523Srwatson struct ether_addr e; 95169523Srwatson char hostname[256]; 96169523Srwatson 97169523Srwatson testnum++; 98169523Srwatson if (ether_line(ether_line_bad_2_string, &e, hostname) == 0) 99169523Srwatson NOTOK("returned success"); 100169523Srwatson OK(); 101169523Srwatson} 102169523Srwatson 103169523Srwatsonstatic const char *ether_aton_string = "01:23:45:67:89:ab"; 104169523Srwatsonstatic const struct ether_addr ether_aton_addr = { 105169523Srwatson { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } 106169523Srwatson}; 107169523Srwatson 108169523Srwatsonstatic void 109169523Srwatsontest_ether_aton_r(void) 110169523Srwatson{ 111169523Srwatson struct ether_addr e, *ep; 112169523Srwatson 113169523Srwatson testnum++; 114169523Srwatson ep = ether_aton_r(ether_aton_string, &e); 115169523Srwatson if (ep == NULL) 116169523Srwatson NOTOK("returned NULL"); 117169523Srwatson if (ep != &e) 118169523Srwatson NOTOK("returned different pointer"); 119169523Srwatson if (bcmp(&e, ðer_aton_addr, ETHER_ADDR_LEN) != 0) 120169523Srwatson NOTOK("bad address"); 121169523Srwatson OK(); 122169523Srwatson} 123169523Srwatson 124169523Srwatsonstatic const char *ether_aton_bad_string = "x"; 125169523Srwatson 126169523Srwatsonstatic void 127169523Srwatsontest_ether_aton_r_bad(void) 128169523Srwatson{ 129169523Srwatson struct ether_addr e, *ep; 130169523Srwatson 131169523Srwatson testnum++; 132169523Srwatson ep = ether_aton_r(ether_aton_bad_string, &e); 133169523Srwatson if (ep == &e) 134169523Srwatson NOTOK("returned success"); 135169523Srwatson if (ep != NULL) 136169523Srwatson NOTOK("returned different pointer"); 137169523Srwatson OK(); 138169523Srwatson} 139169523Srwatson 140169523Srwatsonstatic void 141169523Srwatsontest_ether_aton(void) 142169523Srwatson{ 143169523Srwatson struct ether_addr *ep; 144169523Srwatson 145169523Srwatson testnum++; 146169523Srwatson ep = ether_aton(ether_aton_string); 147169523Srwatson if (ep == NULL) 148169523Srwatson NOTOK("returned NULL"); 149169523Srwatson if (bcmp(ep, ðer_aton_addr, ETHER_ADDR_LEN) != 0) 150169523Srwatson NOTOK("bad address"); 151169523Srwatson OK(); 152169523Srwatson} 153169523Srwatson 154169523Srwatsonstatic void 155169523Srwatsontest_ether_aton_bad(void) 156169523Srwatson{ 157169523Srwatson struct ether_addr *ep; 158169523Srwatson 159169523Srwatson testnum++; 160169523Srwatson ep = ether_aton(ether_aton_bad_string); 161169523Srwatson if (ep != NULL) 162169523Srwatson NOTOK("returned success"); 163169523Srwatson OK(); 164169523Srwatson} 165169523Srwatson 166169523Srwatsonstatic const char *ether_ntoa_string = "01:23:45:67:89:ab"; 167169523Srwatsonstatic const struct ether_addr ether_ntoa_addr = { 168169523Srwatson { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab } 169169523Srwatson}; 170169523Srwatson 171169523Srwatsonstatic void 172169523Srwatsontest_ether_ntoa_r(void) 173169523Srwatson{ 174169523Srwatson char buf[256], *cp; 175169523Srwatson 176169523Srwatson testnum++; 177169523Srwatson cp = ether_ntoa_r(ðer_ntoa_addr, buf); 178169523Srwatson if (cp == NULL) 179169523Srwatson NOTOK("returned NULL"); 180169523Srwatson if (cp != buf) 181169523Srwatson NOTOK("returned different pointer"); 182169523Srwatson if (strcmp(cp, ether_ntoa_string) != 0) 183169523Srwatson NOTOK("bad string"); 184169523Srwatson OK(); 185169523Srwatson} 186169523Srwatson 187169523Srwatsonstatic void 188169523Srwatsontest_ether_ntoa(void) 189169523Srwatson{ 190169523Srwatson char *cp; 191169523Srwatson 192169523Srwatson testnum++; 193169523Srwatson cp = ether_ntoa(ðer_ntoa_addr); 194169523Srwatson if (cp == NULL) 195169523Srwatson NOTOK("returned NULL"); 196169523Srwatson if (strcmp(cp, ether_ntoa_string) != 0) 197169523Srwatson NOTOK("bad string"); 198169523Srwatson OK(); 199169523Srwatson} 200169523Srwatson 201169523Srwatsonstatic void 202169523Srwatsontest_ether_ntohost(void) 203169523Srwatson{ 204169523Srwatson 205169523Srwatson testnum++; 206169523Srwatson TODO(); 207169523Srwatson} 208169523Srwatson 209169523Srwatsonstatic void 210169523Srwatsontest_ether_hostton(void) 211169523Srwatson{ 212169523Srwatson 213169523Srwatson testnum++; 214169523Srwatson TODO(); 215169523Srwatson} 216169523Srwatson 217169523Srwatsonint 218169523Srwatsonmain(int argc, char *argv[]) 219169523Srwatson{ 220169523Srwatson 221169523Srwatson printf("1..11\n"); 222169523Srwatson 223169523Srwatson test_ether_line(); 224169523Srwatson test_ether_line_bad_1(); 225169523Srwatson test_ether_line_bad_2(); 226169523Srwatson test_ether_aton_r(); 227169523Srwatson test_ether_aton_r_bad(); 228169523Srwatson test_ether_aton(); 229169523Srwatson test_ether_aton_bad(); 230169523Srwatson test_ether_ntoa_r(); 231169523Srwatson test_ether_ntoa(); 232169523Srwatson test_ether_ntohost(); 233169523Srwatson test_ether_hostton(); 234169523Srwatson return (0); 235169523Srwatson} 236