aboutsummaryrefslogtreecommitdiff
path: root/tests/server
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-10-23 12:05:49 +0200
committerDaniel Stenberg <daniel@haxx.se>2017-10-25 09:54:37 +0200
commitb9d25f9a6b3ca791385b80a6a3c3fa5ae113e1e0 (patch)
treead56d8b7a703e96e9de0ef07bacab079b7169904 /tests/server
parent016c6a6abb525ffb9431a00a4eda6b70683f838e (diff)
timediff: return timediff_t from the time diff functions
... to cater for systems with unsigned time_t variables. - Renamed the functions to curlx_timediff and Curl_timediff_us. - Added overflow protection for both of them in either direction for both 32 bit and 64 bit time_ts - Reprefixed the curlx_time functions to use Curl_* Reported-by: Peter Piekarski Fixes #2004 Closes #2005
Diffstat (limited to 'tests/server')
-rw-r--r--tests/server/Makefile.inc2
-rw-r--r--tests/server/util.c111
-rw-r--r--tests/server/util.h3
3 files changed, 108 insertions, 8 deletions
diff --git a/tests/server/Makefile.inc b/tests/server/Makefile.inc
index c3ea664b6..208aa0fc8 100644
--- a/tests/server/Makefile.inc
+++ b/tests/server/Makefile.inc
@@ -4,14 +4,12 @@ CURLX_SRCS = \
../../lib/mprintf.c \
../../lib/nonblock.c \
../../lib/strtoofft.c \
- ../../lib/timeval.c \
../../lib/warnless.c
CURLX_HDRS = \
../../lib/curlx.h \
../../lib/nonblock.h \
../../lib/strtoofft.h \
- ../../lib/timeval.h \
../../lib/warnless.h
USEFUL = \
diff --git a/tests/server/util.c b/tests/server/util.c
index 1bbd89a3c..fdbd71f0f 100644
--- a/tests/server/util.c
+++ b/tests/server/util.c
@@ -67,6 +67,8 @@ const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
#endif /* w32api < 3.6 */
#endif /* ENABLE_IPV6 && __MINGW32__*/
+static struct timeval tvnow(void);
+
/* This function returns a pointer to STATIC memory. It converts the given
* binary lump to a hex formatted string usable for output in logs or
* whatever.
@@ -100,7 +102,7 @@ void logmsg(const char *msg, ...)
char buffer[2048 + 1];
FILE *logfp;
int error;
- struct curltime tv;
+ struct timeval tv;
time_t sec;
struct tm *now;
char timebuf[20];
@@ -112,7 +114,7 @@ void logmsg(const char *msg, ...)
return;
}
- tv = curlx_tvnow();
+ tv = tvnow();
if(!known_offset) {
epoch_offset = time(NULL) - tv.tv_sec;
known_offset = 1;
@@ -213,7 +215,7 @@ int wait_ms(int timeout_ms)
#ifndef HAVE_POLL_FINE
struct timeval pending_tv;
#endif
- struct curltime initial_tv;
+ struct timeval initial_tv;
int pending_ms;
int error;
#endif
@@ -231,7 +233,7 @@ int wait_ms(int timeout_ms)
Sleep(timeout_ms);
#else
pending_ms = timeout_ms;
- initial_tv = curlx_tvnow();
+ initial_tv = tvnow();
do {
#if defined(HAVE_POLL_FINE)
r = poll(NULL, 0, pending_ms);
@@ -245,7 +247,7 @@ int wait_ms(int timeout_ms)
error = errno;
if(error && (error != EINTR))
break;
- pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv);
+ pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
if(pending_ms <= 0)
break;
} while(r == -1);
@@ -397,3 +399,102 @@ int strncasecompare(const char *first, const char *second, size_t max)
return raw_toupper(*first) == raw_toupper(*second);
}
+
+#if defined(WIN32) && !defined(MSDOS)
+
+static struct timeval tvnow(void)
+{
+ /*
+ ** GetTickCount() is available on _all_ Windows versions from W95 up
+ ** to nowadays. Returns milliseconds elapsed since last system boot,
+ ** increases monotonically and wraps once 49.7 days have elapsed.
+ **
+ ** GetTickCount64() is available on Windows version from Windows Vista
+ ** and Windows Server 2008 up to nowadays. The resolution of the
+ ** function is limited to the resolution of the system timer, which
+ ** is typically in the range of 10 milliseconds to 16 milliseconds.
+ */
+ struct timeval now;
+#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
+ ULONGLONG milliseconds = GetTickCount64();
+#else
+ DWORD milliseconds = GetTickCount();
+#endif
+ now.tv_sec = (long)(milliseconds / 1000);
+ now.tv_usec = (milliseconds % 1000) * 1000;
+ return now;
+}
+
+#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
+
+static struct timeval tvnow(void)
+{
+ /*
+ ** clock_gettime() is granted to be increased monotonically when the
+ ** monotonic clock is queried. Time starting point is unspecified, it
+ ** could be the system start-up time, the Epoch, or something else,
+ ** in any case the time starting point does not change once that the
+ ** system has started up.
+ */
+ struct timeval now;
+ struct timespec tsnow;
+ if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
+ now.tv_sec = tsnow.tv_sec;
+ now.tv_usec = tsnow.tv_nsec / 1000;
+ }
+ /*
+ ** Even when the configure process has truly detected monotonic clock
+ ** availability, it might happen that it is not actually available at
+ ** run-time. When this occurs simply fallback to other time source.
+ */
+#ifdef HAVE_GETTIMEOFDAY
+ else
+ (void)gettimeofday(&now, NULL);
+#else
+ else {
+ now.tv_sec = (long)time(NULL);
+ now.tv_usec = 0;
+ }
+#endif
+ return now;
+}
+
+#elif defined(HAVE_GETTIMEOFDAY)
+
+static struct timeval tvnow(void)
+{
+ /*
+ ** gettimeofday() is not granted to be increased monotonically, due to
+ ** clock drifting and external source time synchronization it can jump
+ ** forward or backward in time.
+ */
+ struct timeval now;
+ (void)gettimeofday(&now, NULL);
+ return now;
+}
+
+#else
+
+static struct timeval tvnow(void)
+{
+ /*
+ ** time() returns the value of time in seconds since the Epoch.
+ */
+ struct timeval now;
+ now.tv_sec = (long)time(NULL);
+ now.tv_usec = 0;
+ return now;
+}
+
+#endif
+
+long timediff(struct timeval newer, struct timeval older)
+{
+ timediff_t diff = newer.tv_sec-older.tv_sec;
+ if(diff >= (LONG_MAX/1000))
+ return LONG_MAX;
+ else if(diff <= (LONG_MIN/1000))
+ return LONG_MIN;
+ return (long)(newer.tv_sec-older.tv_sec)*1000+
+ (long)(newer.tv_usec-older.tv_usec)/1000;
+}
diff --git a/tests/server/util.h b/tests/server/util.h
index a2a56badd..7b4ec1626 100644
--- a/tests/server/util.h
+++ b/tests/server/util.h
@@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -25,6 +25,7 @@
char *data_to_hex(char *data, size_t len);
void logmsg(const char *msg, ...);
+long timediff(struct timeval newer, struct timeval older);
#define TEST_DATA_PATH "%s/data/test%ld"