| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <stdarg.h> |
| 4 |
#include <stddef.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include <setjmp.h> |
| 7 |
#include <cmocka.h> |
| 8 |
|
| 9 |
#include <sys/types.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <arpa/inet.h> |
| 12 |
#include <netinet/in.h> |
| 13 |
#include <netdb.h> |
| 14 |
#include <pwd.h> |
| 15 |
#include <grp.h> |
| 16 |
#include <string.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
static void test_nwrap_passwd_group(void **state) |
| 20 |
{ |
| 21 |
struct passwd *pwd; |
| 22 |
struct group *grp; |
| 23 |
|
| 24 |
(void) state; /* unused */ |
| 25 |
|
| 26 |
pwd = getpwuid(getuid()); |
| 27 |
assert_non_null(pwd); |
| 28 |
|
| 29 |
pwd = getpwnam(pwd->pw_name); |
| 30 |
assert_non_null(pwd); |
| 31 |
|
| 32 |
grp = getgrgid(pwd->pw_gid); |
| 33 |
assert_non_null(grp); |
| 34 |
|
| 35 |
grp = getgrnam(grp->gr_name); |
| 36 |
assert_non_null(grp); |
| 37 |
} |
| 38 |
|
| 39 |
/* Test libnsl */ |
| 40 |
static void test_nwrap_hostent(void **state) |
| 41 |
{ |
| 42 |
#ifdef HAVE_NONNULL_GETHOSTENT |
| 43 |
struct hostent *he; |
| 44 |
|
| 45 |
(void) state; /* unused */ |
| 46 |
|
| 47 |
sethostent(0); |
| 48 |
|
| 49 |
he = gethostent(); |
| 50 |
assert_non_null(he); |
| 51 |
|
| 52 |
endhostent(); |
| 53 |
#else |
| 54 |
(void) state; /* unused */ |
| 55 |
|
| 56 |
sethostent(0); |
| 57 |
gethostent(); |
| 58 |
endhostent(); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
static void test_nwrap_gethostname(void **state) |
| 63 |
{ |
| 64 |
char host[256] = {0}; |
| 65 |
int rc; |
| 66 |
|
| 67 |
(void) state; /* unused */ |
| 68 |
|
| 69 |
rc = gethostname(host, sizeof(host)); |
| 70 |
assert_int_equal(rc, 0); |
| 71 |
|
| 72 |
assert_true(strlen(host) > 1); |
| 73 |
} |
| 74 |
|
| 75 |
static void test_nwrap_getaddrinfo_local(void **state) |
| 76 |
{ |
| 77 |
struct addrinfo hints; |
| 78 |
struct addrinfo *res; |
| 79 |
union { |
| 80 |
struct sockaddr *sa; |
| 81 |
struct sockaddr_in *in; |
| 82 |
} addr; |
| 83 |
int rc; |
| 84 |
|
| 85 |
(void) state; /* unused */ |
| 86 |
|
| 87 |
/* IPv4 */ |
| 88 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 89 |
hints.ai_family = AF_UNSPEC; |
| 90 |
hints.ai_socktype = SOCK_STREAM; |
| 91 |
hints.ai_flags = AI_ADDRCONFIG; |
| 92 |
|
| 93 |
rc = getaddrinfo("127.0.0.1", NULL, &hints, &res); |
| 94 |
assert_int_equal(rc, 0); |
| 95 |
|
| 96 |
assert_int_equal(res->ai_family, AF_INET); |
| 97 |
assert_int_equal(res->ai_socktype, SOCK_STREAM); |
| 98 |
|
| 99 |
#ifdef HAVE_GETADDRINFO_SETS_CANONNAME_FOR_IPADDRESSES |
| 100 |
assert_string_equal(res->ai_canonname, "127.0.0.1"); |
| 101 |
#else /* HAVE_GETADDRINFO_SETS_CANONNAME_FOR_IPADDRESSES */ |
| 102 |
assert_null(res->ai_canonname); |
| 103 |
#endif /* HAVE_GETADDRINFO_SETS_CANONNAME_FOR_IPADDRESSES */ |
| 104 |
|
| 105 |
addr.sa = res->ai_addr; |
| 106 |
|
| 107 |
assert_int_equal(ntohl(addr.in->sin_addr.s_addr), INADDR_LOOPBACK); |
| 108 |
|
| 109 |
freeaddrinfo(res); |
| 110 |
} |
| 111 |
|
| 112 |
int main(void) { |
| 113 |
int rc; |
| 114 |
|
| 115 |
const struct CMUnitTest tests[] = { |
| 116 |
cmocka_unit_test(test_nwrap_passwd_group), |
| 117 |
cmocka_unit_test(test_nwrap_hostent), |
| 118 |
cmocka_unit_test(test_nwrap_gethostname), |
| 119 |
cmocka_unit_test(test_nwrap_getaddrinfo_local), |
| 120 |
}; |
| 121 |
|
| 122 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 123 |
|
| 124 |
return rc; |
| 125 |
} |