File: test_getprotobyname.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (94 lines) | stat: -rw-r--r-- 2,528 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2013 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void checkEntryByValue(const char* name, int port, const char** aliasArray) {
    struct protoent* entry;
    char** aliases;

    // Perform a protocol look up by name
    entry = getprotobyname(name);
    assert(entry != NULL);

    // Check results
    assert(strcmp(name, entry->p_name) == 0);
    assert(port == entry->p_proto);

    aliases = entry->p_aliases;
    for (int i = 0; aliases[i] != NULL; i++) {
        assert(strcmp(aliases[i], aliasArray[i]) == 0);
    }

    // Perform a protocol look up by number
    entry = getprotobynumber(port);
    assert(entry != NULL);

    // Check results
    assert(strcmp(name, entry->p_name) == 0);
    assert(port == entry->p_proto);

    aliases = entry->p_aliases;
    for (int i = 0; aliases[i] != NULL; i++) {
        assert(strcmp(aliases[i], aliasArray[i]) == 0);
    }
}

void checkEntryDatabase() {
    struct protoent* entry;

    // Don't call setprotoent() initially as getprotoent() should open the "database" if necessary.
    entry = getprotoent();
    assert(entry != NULL);
    assert(strcmp("tcp", entry->p_name) == 0);

    entry = getprotoent();
    assert(entry != NULL);
    assert(strcmp("udp", entry->p_name) == 0);

    // Check that setprotoent() correctly sets the next entry to the first entry
    setprotoent(1);

    entry = getprotoent();
    assert(entry != NULL);
    assert(strcmp("tcp", entry->p_name) == 0);

    entry = getprotoent();
    assert(entry != NULL);
    assert(strcmp("udp", entry->p_name) == 0);

    // If we do a getprotoent() that goes past the end of the 'database' check that it returns NULL.
    entry = getprotoent();
    assert(entry == NULL);
}

int main() {
    // First check getprotobyname() and getprotobynumber()
    const char* aliases[] = {"TCP"};
    checkEntryByValue("tcp", 6, aliases);

    aliases[0] = "UDP";
    checkEntryByValue("udp", 17, aliases);

    // Check that the doomsday protocol hasn't been implemented :-) ......
    assert(getprotobyname("doomsday") == NULL);

    // Now check setprotoent() and getprotoent()
    checkEntryDatabase();

    endprotoent();

    puts("success");

    return EXIT_SUCCESS;
}