]> The Tcpdump Group git mirrors - libpcap/blob - testprogs/activatetest.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / testprogs / activatetest.c
1 #include <stdio.h>
2 #include <pcap.h>
3
4 #define CAPTURE_DEVICE "nosuchdevice"
5
6 int main(void)
7 {
8 /*
9 * When trying to use libpcap on a device that does not exist, the
10 * expected behaviour is that pcap_create() does not return an error,
11 * and pcap_activate() does return an error, and the error code
12 * specifically tells that the interface does not exist. tcpdump
13 * depends on this semantics to accept integer indices instead of
14 * device names. This test provides means to verify the actual
15 * behaviour, which is specific to each libpcap module.
16 */
17 char errbuf[PCAP_ERRBUF_SIZE];
18 printf("Trying to use capture device \"%s\"...\n", CAPTURE_DEVICE);
19 pcap_t *p = pcap_create(CAPTURE_DEVICE, errbuf);
20 if (! p) {
21 fprintf(stderr,
22 "FAIL: Unexpected error from pcap_create() (%s).\n",
23 errbuf);
24 return 1;
25 }
26 int ret = 1, err = pcap_activate(p);
27 switch (err) {
28 case 0:
29 fprintf(stderr, "FAIL: No error from pcap_activate().\n");
30 break;
31 case PCAP_ERROR:
32 fprintf(stderr, "FAIL: Generic error from pcap_activate().\n");
33 break;
34 case PCAP_ERROR_PERM_DENIED:
35 fprintf(stderr, "FAIL: Permission denied from pcap_activate(), "
36 "retry with higher privileges.\n");
37 break;
38 case PCAP_ERROR_NO_SUCH_DEVICE:
39 printf("PASS: Correct specific error from pcap_activate().\n");
40 ret = 0;
41 break;
42 default:
43 fprintf(stderr,
44 "FAIL: Unexpected error %d from pcap_activate().\n",
45 err);
46 }
47 pcap_close(p);
48 return ret;
49 }