test_end();
}
+static void test_var_expand_generate(void)
+{
+ const char *error;
+ string_t *str = t_str_new(64);
+
+ test_begin("var_expand(generate)");
+
+ test_assert(var_expand(str, "%{generate:guid}", NULL, &error) == 0);
+ test_assert(strstr(str_c(str), my_hostname) != NULL);
+
+ str_truncate(str, 0);
+ test_assert(var_expand(str, "%{generate:guid128}", NULL, &error) == 0);
+ test_assert(str_len(str) == 32 && strspn(str_c(str), "0123456789abcdef") == 32);
+
+ str_truncate(str, 0);
+ test_assert(var_expand(str, "%{generate:uuid}", NULL, &error) == 0);
+ test_assert(str_len(str) == 36 && strspn(str_c(str), "0123456789abcdef-") == 36);
+
+ str_truncate(str, 0);
+ test_assert(var_expand(str, "%{generate:uuid:record}", NULL, &error) == 0);
+ test_assert(str_len(str) == 36 && strspn(str_c(str), "0123456789abcdef-") == 36);
+
+ str_truncate(str, 0);
+ test_assert(var_expand(str, "%{generate:uuid:compact}", NULL, &error) == 0);
+ test_assert(str_len(str) == 32 && strspn(str_c(str), "0123456789abcdef") == 32);
+
+ str_truncate(str, 0);
+ test_assert(var_expand(str, "%{generate:uuid:microsoft}", NULL, &error) == 0);
+ test_assert(str_len(str) == 38 && str_c(str)[0] == '{' &&
+ strspn(str_c(str)+1, "0123456789abcdef-") == 36 &&
+ str_c(str)[37] == '}');
+
+ test_end();
+}
+
int main(void)
{
void (*const tests[])(void) = {
test_var_expand_parameter_sorted,
test_var_expand_perc,
test_var_expand_set_copy,
+ test_var_expand_generate,
NULL
};
#include "lib.h"
#include "array.h"
#include "cpu-count.h"
+#include "guid.h"
#include "hostpid.h"
#include "str.h"
#include "time-util.h"
return 0;
}
+static int var_expand_generate(const char *key, const char **value_r,
+ void *context ATTR_UNUSED, const char **error_r)
+{
+ guid_128_t guid;
+
+ if (strcmp(key, "guid") == 0) {
+ *value_r = guid_generate();
+ return 0;
+ }
+ if (strcmp(key, "guid128") == 0) {
+ guid_128_generate(guid);
+ *value_r = guid_128_to_string(guid);
+ return 0;
+ }
+ if (str_begins(key, "uuid", &key)) {
+ guid_128_uuid4_generate(guid);
+ if (key[0] == '\0' || strcmp(key, ":record") == 0)
+ *value_r = guid_128_to_uuid_string(guid, FORMAT_RECORD);
+ else if (strcmp(key, ":compact") == 0)
+ *value_r = guid_128_to_uuid_string(guid, FORMAT_COMPACT);
+ else if (strcmp(key, ":microsoft") == 0)
+ *value_r = guid_128_to_uuid_string(guid, FORMAT_MICROSOFT);
+ else
+ ERROR_UNSUPPORTED_KEY(key);
+ return 0;
+ }
+ ERROR_UNSUPPORTED_KEY(key);
+}
+
static const struct var_expand_provider internal_providers[] = {
{ .key = "process", .func = var_expand_process },
{ .key = "system", .func = var_expand_system },
{ .key = "event", .func = var_expand_event },
{ .key = "date", .func = var_expand_date },
{ .key = "time", .func = var_expand_time },
+ { .key = "generate", .func = var_expand_generate },
VAR_EXPAND_TABLE_END
};