|
| 1 | +#include <errno.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +static int is_blank_line(const char *line) |
| 6 | +{ |
| 7 | + while (*line != '\0') { |
| 8 | + if (*line != ' ' && *line != '\t' && *line != '\r' && *line != '\n') { |
| 9 | + return 0; |
| 10 | + } |
| 11 | + line++; |
| 12 | + } |
| 13 | + return 1; |
| 14 | +} |
| 15 | + |
| 16 | +static int check_contains(const char *text, const char *needle) |
| 17 | +{ |
| 18 | + if (strstr(text, needle) != NULL) { |
| 19 | + printf("PASS: /proc/net/arp contains %s\n", needle); |
| 20 | + return 0; |
| 21 | + } |
| 22 | + |
| 23 | + printf("FAIL: /proc/net/arp missing %s\n", needle); |
| 24 | + return 1; |
| 25 | +} |
| 26 | + |
| 27 | +static int check_arp_rows(char *text) |
| 28 | +{ |
| 29 | + int failed = 0; |
| 30 | + int saw_header = 0; |
| 31 | + int row_count = 0; |
| 32 | + |
| 33 | + for (char *line = text; line != NULL; ) { |
| 34 | + char *next = strchr(line, '\n'); |
| 35 | + if (next != NULL) { |
| 36 | + *next = '\0'; |
| 37 | + next++; |
| 38 | + } |
| 39 | + |
| 40 | + if (is_blank_line(line)) { |
| 41 | + line = next; |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (!saw_header) { |
| 46 | + saw_header = 1; |
| 47 | + line = next; |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + char ip[32]; |
| 52 | + char hw_type[32]; |
| 53 | + char flags[32]; |
| 54 | + char hw_addr[32]; |
| 55 | + char mask[32]; |
| 56 | + char device[32]; |
| 57 | + char extra[32]; |
| 58 | + int fields = sscanf( |
| 59 | + line, |
| 60 | + "%31s %31s %31s %31s %31s %31s %31s", |
| 61 | + ip, |
| 62 | + hw_type, |
| 63 | + flags, |
| 64 | + hw_addr, |
| 65 | + mask, |
| 66 | + device, |
| 67 | + extra); |
| 68 | + if (fields != 6) { |
| 69 | + printf("FAIL: malformed /proc/net/arp row: %s\n", line); |
| 70 | + failed++; |
| 71 | + line = next; |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + row_count++; |
| 76 | + if (strcmp(ip, "10.0.2.2") == 0 |
| 77 | + && strcmp(hw_addr, "52:54:00:12:34:56") == 0 |
| 78 | + && strcmp(device, "eth0") == 0) { |
| 79 | + printf("FAIL: /proc/net/arp exposes fixed QEMU gateway stub\n"); |
| 80 | + failed++; |
| 81 | + } |
| 82 | + |
| 83 | + line = next; |
| 84 | + } |
| 85 | + |
| 86 | + printf("PASS: /proc/net/arp data rows checked: %d\n", row_count); |
| 87 | + return failed; |
| 88 | +} |
| 89 | + |
| 90 | +int main(void) |
| 91 | +{ |
| 92 | + char buf[512]; |
| 93 | + FILE *fp = fopen("/proc/net/arp", "r"); |
| 94 | + if (fp == NULL) { |
| 95 | + printf("FAIL: fopen /proc/net/arp: errno=%d %s\n", errno, strerror(errno)); |
| 96 | + return 1; |
| 97 | + } |
| 98 | + |
| 99 | + size_t nread = fread(buf, 1, sizeof(buf) - 1, fp); |
| 100 | + int saved_errno = errno; |
| 101 | + if (ferror(fp)) { |
| 102 | + printf("FAIL: fread /proc/net/arp: errno=%d %s\n", saved_errno, strerror(saved_errno)); |
| 103 | + fclose(fp); |
| 104 | + return 1; |
| 105 | + } |
| 106 | + fclose(fp); |
| 107 | + |
| 108 | + buf[nread] = '\0'; |
| 109 | + printf("/proc/net/arp content:\n%s", buf); |
| 110 | + |
| 111 | + int failed = 0; |
| 112 | + failed += check_contains(buf, "IP address"); |
| 113 | + failed += check_contains(buf, "HW type"); |
| 114 | + failed += check_contains(buf, "HW address"); |
| 115 | + failed += check_contains(buf, "Device"); |
| 116 | + failed += check_arp_rows(buf); |
| 117 | + |
| 118 | + if (failed == 0) { |
| 119 | + printf("ALL TESTS PASSED\n"); |
| 120 | + return 0; |
| 121 | + } |
| 122 | + |
| 123 | + printf("SOME TESTS FAILED\n"); |
| 124 | + return 1; |
| 125 | +} |
0 commit comments