📄 tutils.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/socktest/tutils.c,v 1.1.1.1 2001/11/05 17:49:15 tneale Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: tutils.c,v $ * Revision 1.1.1.1 2001/11/05 17:49:15 tneale * Tornado shuffle * * Revision 1.4 2001/01/19 22:24:57 paul * Update copyright. * * Revision 1.3 2000/10/27 18:06:39 paul * Do not report successes in quiet mode, only failures. * * Revision 1.2 2000/10/20 18:32:36 paul * si.sin_len is only defined for BSD * * Revision 1.1 2000/10/16 19:23:22 paul * Renamed from test_utils.c * * Revision 1.10 2000/03/17 00:14:50 meister * Update copyright message * * Revision 1.9 1999/10/06 19:31:53 qli * adjust the errno value when testing with pNA * * Revision 1.8 1999/07/30 21:29:49 paul * enhanced test_fail, test_pass * * Revision 1.6 1999/05/03 19:24:21 paul * Added test_pass() for the benefit of test_close(). * * Revision 1.5 1999/03/04 17:52:13 wes * str_* functions moved to attache/sockets/sockbug.c * * Revision 1.4 1999/02/18 04:16:20 wes * Socket Merge: new code (socket tests). * * Revision 1.3.2.5 1999/01/06 21:43:18 paul * changed the magic return-a-socket value from 1 to 17, * to avoid confusing the select test * * Revision 1.3.2.4 1998/11/20 16:19:59 paul * freestanding ttcp * * Revision 1.3.2.3 1998/11/06 23:36:55 paul * rototill to support tcp * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include "test.h"int verbose = 0;int quiet = 0;/* This assumes that errno values are contiguous, in the order * presented here. It doesn't assume that they start numbering at * zero, so it should work for pNA and Winsock. */static const char *errno_sym[] = { "EZERO","EPERM","ENOENT","ESRCH","EINTR","EIO","ENXIO","E2BIG","ENOEXEC", "EBADF","ECHILD","EDEADLK","ENOMEM","EACCES","EFAULT","ENOTBLK","EBUSY", "EEXIST","EXDEV","ENODEV","ENOTDIR","EISDIR","EINVAL","ENFILE","EMFILE", "ENOTTY","ETXTBSY","EFBIG","ENOSPC","ESPIPE","EROFS","EMLINK","EPIPE", "EDOM","ERANGE","EAGAIN/EWOULDBLOCK","EINPROGRESS","EALREADY","ENOTSOCK", "EDESTADDRREQ","EMSGSIZE","EPROTOTYPE","ENOPROTOOPT","EPROTONOSUPPORT", "ESOCKTNOSUPPORT","EOPNOTSUPP","EPFNOSUPPORT","EAFNOSUPPORT","EADDRINUSE", "EADDRNOTAVAIL","ENETDOWN","ENETUNREACH","ENETRESET","ECONNABORTED", "ECONNRESET","ENOBUFS","EISCONN","ENOTCONN","ESHUTDOWN","ETOOMANYREFS", "ETIMEDOUT","ECONNREFUSED","ELOOP","ENAMETOOLONG","EHOSTDOWN", "EHOSTUNREACH","ENOTEMPTY"};static const int n_errno_sym = sizeof(errno_sym) / sizeof(errno_sym[0]);#define errno_sym_idx(x) (x - (EPERM - 1))static int pass_count;static int fail_count;inttest(char *proto, char *name, int result, int expected, int expected_errno){ int i, fail, this_errno=errno;#ifdef TEST_PNA this_errno &= 0xFF;#endif /* socket() and accept() return socket descriptors. we can't * predict the precise value, so we 'expect' them to return 17 (the * only truly random number). This used to be 1, but I changed it * to avoid confusing the select test. */ if ((expected == 17) && (result != -1)) expected = result; if ((result != expected) || ((result == -1) && (this_errno != expected_errno))) { fail_count++; } else { pass_count++; } /* the following nonsense prints a message like * "!foo() returned -1/EINVAL (expected -1/EACCESS)" */ fail = ((result != expected) || ((result == -1) && (this_errno != expected_errno))); if (fail || !quiet) { printf("%s: ", fail ? "FAIL" : "PASS"); if (proto) printf("%s ", proto); printf("%s returned %d", name, result); if (result == -1) { i = errno_sym_idx(this_errno); if ((i >= 0) && (i < n_errno_sym)) printf("/%s", errno_sym[i]); else { printf("/%d", this_errno); } } if ((result != expected) || ((expected == -1) && (this_errno != expected_errno))) { printf(" (expected %d", expected); if (expected == -1) { i = errno_sym_idx(expected_errno); if ((i >= 0) && (i < n_errno_sym)) printf("/%s", errno_sym[i]); else { printf("/%d", expected_errno); } } printf(")"); } printf("\n"); } return result;}void test_pass(char *proto, char *test_name, char *what){ pass_count++; if (!quiet) { printf("PASS: "); if (proto) printf("%s ", proto); if (test_name) printf("%s: ", test_name); printf("%s\n", what); }}void test_fail(char *proto, char *test_name, char *what){ int i, this_errno = errno;#ifdef TEST_PNA this_errno &= 0xFF;#endif fail_count++; printf("FAIL: "); if (proto) printf("%s ", proto); if (test_name) printf("%s: ", test_name); printf("%s: ", what); i = errno_sym_idx(this_errno); if ((i >= 0) && (i < n_errno_sym)) printf("%s\n", errno_sym[i]); else { printf("%d\n", this_errno); }}/*** print data structures ***/voidpr_sockaddr_in(struct sockaddr_in *si){ if (verbose) printf(" sin_family=%d, sin_port=%d, sin_addr=%s\n", si->sin_family, ntohs(si->sin_port), inet_ntoa(si->sin_addr));}intpr_sockname(int s){ struct sockaddr_in si; int ret, namelen; memset(&si, 0, sizeof(si)); ret = getsockname(s, (struct sockaddr *) &si, &namelen); if (ret == 0) pr_sockaddr_in(&si); else test_fail(0, "pr_sockname", "getsockname"); return ret;}intpr_peername(int s){ struct sockaddr_in si; int ret, namelen; memset(&si, 0, sizeof(si)); ret = getpeername(s, (struct sockaddr *) &si, &namelen); if (ret == 0) pr_sockaddr_in(&si); else test_fail(0, "pr_peername", "getpeername"); return ret;}voidtest_init(void){ pass_count = 0; fail_count = 0;}voidtest_summarize(void){ printf("*** TESTS COMPLETE\n"); printf(" PASS: %d\n", pass_count); printf(" FAIL: %d\n", fail_count);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -