File: binder_test.c

package info (click to toggle)
sniproxy 0.6.1%2Bgit20240321-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: ansic: 5,594; perl: 1,673; sh: 237; makefile: 131
file content (60 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "binder.h"

static int test_binder(int);

int main() {
    int i;

    start_binder("binder_test");
    for (i = 8080; i <= 8084; i++)
        test_binder(i);

    stop_binder();

    return 0;
}

static int
test_binder(int port) {
    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
        .sin_port = htons(port),
    };

    int fd = bind_socket((struct sockaddr *)&addr, sizeof(addr));

    assert(fd > 0);

    /* Verify we obtained the expected socket address */
    struct sockaddr_storage addr_verify;
    socklen_t len = sizeof(addr_verify);
    if (getsockname(fd, (struct sockaddr *)&addr_verify, &len) < 0) {
        perror("getsockname:");
        exit(1);
    }

    assert(addr.sin_family == ((struct sockaddr_in *)&addr_verify)->sin_family);
    assert(addr.sin_addr.s_addr == ((struct sockaddr_in *)&addr_verify)->sin_addr.s_addr);
    assert(addr.sin_port == ((struct sockaddr_in *)&addr_verify)->sin_port);

    /* Verify we can listen to it */
    if (listen(fd, 5) < 0) {
        perror("listen:");
        exit(1);
    }

    /* Test error handling: */
    fd = bind_socket((struct sockaddr *)&addr, sizeof(addr));
    assert(fd == -1);

    return 0;
}