AC_CHECK_HEADERS(ctype.h pwd.h stdlib.h string.h strings.h sys/time.h sys/mman.h)
AC_CHECK_HEADERS(syslog.h)
+AC_CHECK_HEADERS(arpa/inet.h)
AC_CHECK_HEADERS(termios.h)
AC_CHECK_FUNCS(gethostname)
AC_CHECK_FUNCS(getopt_long)
AC_CHECK_FUNCS(getpwuid)
AC_CHECK_FUNCS(gettimeofday)
+AC_CHECK_FUNCS(htonl)
AC_CHECK_FUNCS(localtime_r)
AC_CHECK_FUNCS(mkstemp)
AC_CHECK_FUNCS(realpath)
const char *get_hostname(void);
const char *tmp_string(void);
char *format_hash_as_string(const unsigned char *hash, int size);
+typedef uint32_t binary[5]; // 20 bytes: 16 for hash + 4 for size
+void format_hash_as_binary(binary result, const unsigned char *hash, int size);
int create_cachedirtag(const char *dir);
char *format(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
+char *format_hex(unsigned char *data, size_t size);
void reformat(char **ptr, const char *format, ...) ATTR_FORMAT(printf, 2, 3);
char *x_strdup(const char *s);
char *x_strndup(const char *s, size_t n);
#include <sys/time.h>
#endif
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+
#ifdef _WIN32
#include <windows.h>
#include <sys/locking.h>
return ret;
}
+// Return the hash result in binary.
+void format_hash_as_binary(binary result, const unsigned char *hash, int size)
+{
+ memcpy(result, hash, 16);
+#ifdef HAVE_HTONL
+ result[4] = htonl(size); // network byte order
+#else
+ uint32_t i = size;
+ unsigned char *bytes = (unsigned char *) result;
+ for (int j = 0; j < 4; j++) {
+ bytes[16 + j] = (i >> ((3 - j) * 8)) & 0xff; // (big endian)
+ }
+#endif
+}
+
static char const CACHEDIR_TAG[] =
"Signature: 8a477f597d28d172789f06886806bc55\n"
"# This file is a cache directory tag created by ccache.\n"
return ptr;
}
+// Construct a string representing data. Caller frees
+char *
+format_hex(unsigned char *data, size_t size)
+{
+ size_t i;
+ char *ret = x_malloc(2 * size + 1);
+ for (i = 0; i < size; i++) {
+ sprintf(&ret[i*2], "%02x", (unsigned) data[i]);
+ }
+ ret[2 * size] = '\0';
+ return ret;
+}
+
// This is like strdup() but dies if the malloc fails.
char *
x_strdup(const char *s)
}
}
+bool cct_check_data_eq(const char *file, int line, const char *expression,
+ void *expected, void *actual, size_t size)
+{
+ bool result;
+
+ if (expected && actual && memcmp(actual, expected, size) == 0) {
+ cct_check_passed(file, line, expression);
+ result = true;
+ } else {
+ char *exp_str = expected ? format_hex((unsigned char *) expected, size) : x_strdup("(null)");
+ char *act_str = actual ? format_hex((unsigned char *) actual, size) : x_strdup("(null)");
+ cct_check_failed(file, line, expression, exp_str, act_str);
+ free(exp_str);
+ free(act_str);
+ result = false;
+ }
+
+ return result;
+}
+
bool
cct_check_str_eq(const char *file, int line, const char *expression,
char *expected, char *actual,
// ============================================================================
+#define CHECK_DATA_EQ(expected, actual, size) \
+ do { \
+ if (!cct_check_data_eq(__FILE__, __LINE__, #actual, (expected), \
+ (actual), size)) { \
+ cct_test_end(); \
+ cct_suite_end(); \
+ return _test_counter; \
+ } \
+ } while (false)
+
+// ============================================================================
+
#define CHECK_STR_EQ(expected, actual) \
CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)
double expected, double actual);
bool cct_check_int_eq(const char *file, int line, const char *expression,
int64_t expected, int64_t actual);
+bool cct_check_data_eq(const char *file, int line, const char *expression,
+ void *expected, void *actual, size_t size);
bool cct_check_str_eq(const char *file, int line, const char *expression,
char *expected, char *actual,
bool free1, bool free2);
format_hash_as_string(hash, 12345));
}
+TEST(format_hash_as_binary)
+{
+ unsigned char hash[16] = {
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ };
+
+ unsigned char data[20] = {
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00"
+ };
+
+ binary buf;
+ CHECK_INT_EQ(sizeof(buf), 20);
+
+ format_hash_as_binary(buf, hash, 0);
+ CHECK_DATA_EQ(data, buf, 20);
+ hash[0] = 17;
+ hash[15] = 42;
+ format_hash_as_binary(buf, hash, 12345);
+ // data[0:16] = hash
+ data[0] = 0x11;
+ data[15] = 0x2a;
+ // 12345 = 0x3039 BE
+ data[18] = 0x30;
+ data[19] = 0x39;
+ CHECK_DATA_EQ(data, buf, 20);
+}
+
TEST(subst_env_in_string)
{
char *errmsg;
}
+TEST(format_hex)
+{
+ unsigned char none[] = "";
+ unsigned char text[4] = "foo"; // incl. NUL
+ unsigned char data[4] = "\x00\x01\x02\x03";
+
+ CHECK_STR_EQ_FREE2("", format_hex(none, 0));
+ CHECK_STR_EQ_FREE2("666f6f00", format_hex(text, sizeof(text)));
+ CHECK_STR_EQ_FREE2("00010203", format_hex(data, sizeof(data)));
+}
+
TEST_SUITE_END