rsyslog/tests/test_id.c
Rainer Gerhards 2b28661246 testbench: short test id and make more reliable
The RSYSLOG_DYNNAME used to make test file unique was very long,
which potentially causes issues with some test scenarios.
see also: https://github.com/rsyslog/rsyslog/pull/2945#issuecomment-417078768

Also, the dynamic port determination method we currently use
is not 100% reliable. That port number was used inside the DYNNAME
and thus was not necessarily unique. This has now changed to the
current time in microseconds plus a hash of the test file name. This
should be sufficiently unique. If still not, we can now simply
extend the test_id program (e.g. read /dev/urandom).
2018-09-01 12:32:33 +02:00

40 lines
851 B
C

#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
/* one provided by Aaaron Wiebe based on perl's hashing algorithm
* (so probably pretty generic). Not for excessively large strings!
*/
#if defined(__clang__)
#pragma GCC diagnostic ignored "-Wunknown-attributes"
#endif
static unsigned __attribute__((nonnull(1))) int
#if defined(__clang__)
__attribute__((no_sanitize("unsigned-integer-overflow")))
#endif
hash_from_string(void *k)
{
char *rkey = (char*) k;
unsigned hashval = 1;
while (*rkey)
hashval = hashval * 33 + *rkey++;
return hashval;
}
int main(int argc, char *argv[])
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
if(argc != 2) {
fprintf(stderr, "usage: test_id test-file-name\n");
exit(1);
}
printf("%06ld_%04.4x", tv.tv_usec, hash_from_string(argv[1]));
return 0;
}