#include "../src/socket_name.h" #include #include static int g_failures = 0; static void check_condition(const char *name, int condition) { if (condition) { printf("PASS: %s\n", name); } else { printf("FAIL: %s\n", name); g_failures++; } } int main(void) { char a[128]; char b[128]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); check_condition("socket_name_random(a) succeeds", socket_name_random(a, sizeof(a)) == 0); check_condition("socket_name_random(b) succeeds", socket_name_random(b, sizeof(b)) == 0); check_condition("name a starts with nsigner_", strncmp(a, "nsigner_", 8) == 0); check_condition("name b starts with nsigner_", strncmp(b, "nsigner_", 8) == 0); { const char *suffix = a + 8; const char *underscore = strchr(suffix, '_'); check_condition("name a contains second underscore", underscore != NULL && underscore > suffix && underscore[1] != '\0'); } { const char *suffix = b + 8; const char *underscore = strchr(suffix, '_'); check_condition("name b contains second underscore", underscore != NULL && underscore > suffix && underscore[1] != '\0'); } check_condition("two generated names are typically different", strcmp(a, b) != 0); if (g_failures == 0) { printf("ALL TESTS PASSED\n"); return 0; } printf("TESTS FAILED: %d\n", g_failures); return 1; }