Browse Source

test_ntp: use compat headers instead of arpa/inet.h, add rule to AGENTS.md

topo_upd
Evgeny 2 months ago
parent
commit
17202beb0a
  1. 1
      AGENTS.md
  2. 29
      tests/test_ntp.c

1
AGENTS.md

@ -421,6 +421,7 @@ KEEPALIVE=21, ETCPROUTE=22, BBR=23, ETCP_DUMP=24
- Не делай функций-посредников: лучше сразу вызывать target функцию без вложенных вызовов - Не делай функций-посредников: лучше сразу вызывать target функцию без вложенных вызовов
- Нельзя ничего восстанавливать из репозитория не спрашивая - Нельзя ничего восстанавливать из репозитория не спрашивая
- Для отладки не printf а DEBUG_* - Для отладки не printf а DEBUG_*
- Запрещено прямое включение arpa/inet.h, sys/time.h, unistd.h — использовать ../lib/platform_compat.h + socket_compat.h
- Перед сборкой всегда make clean - Перед сборкой всегда make clean
- Все лишнее что менял при отладке - строго вернуть назад в состояние до вмешательства - Все лишнее что менял при отладке - строго вернуть назад в состояние до вмешательства

29
tests/test_ntp.c

@ -9,10 +9,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include <arpa/inet.h> #include "../lib/platform_compat.h"
#include <sys/time.h> #include "../lib/socket_compat.h"
#include "../lib/debug_config.h" #include "../lib/debug_config.h"
#include "../lib/u_async.h" #include "../lib/u_async.h"
#include "../src/utun_instance.h" #include "../src/utun_instance.h"
@ -55,8 +54,8 @@ static uint64_t timeval_to_ntp(struct timeval *tv) {
static void* test_server_thread(void* arg) { static void* test_server_thread(void* arg) {
(void)arg; (void)arg;
int sock = socket(AF_INET, SOCK_DGRAM, 0); socket_t sock = socket_create_udp(AF_INET);
if (sock < 0) { g_server_running = 0; return NULL; } if (sock == SOCKET_INVALID) { g_server_running = 0; return NULL; }
struct sockaddr_in addr; struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
@ -64,7 +63,7 @@ static void* test_server_thread(void* arg) {
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = 0; addr.sin_port = 0;
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { close(sock); g_server_running = 0; return NULL; } if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { socket_close_wrapper(sock); g_server_running = 0; return NULL; }
socklen_t addr_len = sizeof(addr); socklen_t addr_len = sizeof(addr);
getsockname(sock, (struct sockaddr*)&addr, &addr_len); getsockname(sock, (struct sockaddr*)&addr, &addr_len);
@ -76,7 +75,7 @@ static void* test_server_thread(void* arg) {
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(sock, &fds); FD_SET(sock, &fds);
tv.tv_sec = 1; tv.tv_usec = 0; tv.tv_sec = 1; tv.tv_usec = 0;
int r = select(sock + 1, &fds, NULL, NULL, &tv); int r = select((int)(sock + 1), &fds, NULL, NULL, &tv);
if (r <= 0) continue; if (r <= 0) continue;
struct ntp_packet request; struct ntp_packet request;
@ -89,7 +88,7 @@ static void* test_server_thread(void* arg) {
if (mode != 3) continue; // only respond to client-mode if (mode != 3) continue; // only respond to client-mode
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); utun_gettimeofday(&now, NULL);
now.tv_sec += TEST_KNOWN_OFFSET_SEC; now.tv_sec += TEST_KNOWN_OFFSET_SEC;
uint64_t fake_ntp = timeval_to_ntp(&now); uint64_t fake_ntp = timeval_to_ntp(&now);
@ -104,14 +103,22 @@ static void* test_server_thread(void* arg) {
sendto(sock, (const char*)&reply, sizeof(reply), 0, (struct sockaddr*)&client, sizeof(client)); sendto(sock, (const char*)&reply, sizeof(reply), 0, (struct sockaddr*)&client, sizeof(client));
} }
close(sock); socket_close_wrapper(sock);
return NULL; return NULL;
} }
static void platform_sleep_ms(int ms) {
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
static int wait_for_port(void) { static int wait_for_port(void) {
for (int i = 0; i < 200; i++) { for (int i = 0; i < 200; i++) {
if (g_server_port != 0) return 0; if (g_server_port != 0) return 0;
usleep(5000); platform_sleep_ms(5);
} }
return -1; return -1;
} }
@ -226,7 +233,7 @@ int main(void) {
// Check ntp_time_get_us internal consistency // Check ntp_time_get_us internal consistency
{ {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); utun_gettimeofday(&tv, NULL);
int64_t raw_us = (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; int64_t raw_us = (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec;
int64_t corrected_us = ntp_time_get_us(inst); int64_t corrected_us = ntp_time_get_us(inst);
int64_t expected_us = raw_us - inst->ntp.offset_us; int64_t expected_us = raw_us - inst->ntp.offset_us;

Loading…
Cancel
Save