--- a/branches/ucs-3.2/ucs-3.2-0/base/univention-config-registry/lib/config.c +++ a/branches/ucs-3.2/ucs-3.2-0/base/univention-config-registry/lib/config.c @@ -48,7 +48,7 @@ #define BASECONFIG_MAX_LINE 1024 -const char *SCOPES[] = { +static const char *SCOPES[] = { "forced", "schedule", "ldap", @@ -56,7 +56,7 @@ const char *SCOPES[] = { "custom", NULL}; -const char *BASES[] = { +static const char *BASES[] = { "/etc/univention/base-forced.conf", "/etc/univention/base-schedule.conf", "/etc/univention/base-ldap.conf", @@ -87,7 +87,21 @@ char *univention_config_get_string(const char *key) { if (!strncmp(line, nvalue, len)) { - ret = strndup(line + len, strlen(line) - len - 1 ); /* no newline */ + char *value; + size_t vlen; + + value = line + len; // skip key + vlen = strlen(value); + while (vlen > 0) { + switch (value[vlen - 1]) { + case '\n': + case '\r': + value[--vlen] = '\0'; + continue; + } + break; + } + ret = strndup(value, vlen); fclose(file); goto done; } --- a/branches/ucs-3.2/ucs-3.2-0/base/univention-config-registry/tests/clib.c +++ a/branches/ucs-3.2/ucs-3.2-0/base/univention-config-registry/tests/clib.c @@ -3,31 +3,107 @@ #include #include #include +#include +#include + +static char ucr_bin[] = "/usr/sbin/univention-config-registry"; +static char ucr_name[] = "univention-config-registry"; +static char key[] = "test/clib"; +static char *LAYER[] = { + "--ldap-policy", + "--schedule", + "--force", + NULL, +}; + +int fork_exec(char **argv) { + pid_t pid; + int status; + + pid = fork(); + switch (pid) { + case -1: // error + abort(); + + case 0: // child + return execv(ucr_bin, argv); + + default: // parent + pid = wait(&status); + assert(WIFEXITED(status) && WEXITSTATUS(status) == 0); + return 0; + } +} + +int test_layer(void) { + char assign[64]; + int r, layer; + pid_t pid; + + for (layer = 0; LAYER[layer]; layer++) { + size_t size; + + size = snprintf(assign, sizeof assign, "%s=%s", key, LAYER[layer]); + assert(0 < size && size < sizeof assign); + char *argv[] = { + ucr_name, + "set", + LAYER[layer], + assign, + NULL + }; + fprintf(stderr, "set %s %s\n", LAYER[layer], assign); + fork_exec(argv); + + char *c = univention_config_get_string(key); + fprintf(stderr, "should=%s is=%s\n", LAYER[layer], c); + r = strcmp(c, LAYER[layer]); + assert(r == 0); + } + + for (layer = 0; LAYER[layer]; layer++) { + char *argv[] = { + ucr_name, + "unset", + LAYER[layer], + key, + NULL + }; + fprintf(stderr, "unset %s %s\n", LAYER[layer], key); + fork_exec(argv); + } +} int main(void) { - char key[] = "test/clib"; char value[] = "42"; int r; r = univention_config_set_string(key, value); + fprintf(stderr, "set %s=%s [%d]\n", key, value, r); assert(r == 0); char *c = univention_config_get_string(key); + fprintf(stderr, "get_str %s=%s\n", key, c); assert(c != NULL); assert(strcmp(c, value) == 0); free(c); int i = univention_config_get_int(key); + fprintf(stderr, "get_int %s=%d\n", key, i); assert(i == atoi(value)); long l = univention_config_get_long(key); + fprintf(stderr, "get_long %s=%d\n", key, l); assert(l == atol(value)); + test_layer(); + char *argv[] = { - "univention-config-registry", + ucr_name, "unset", key, NULL }; - return execv("/usr/sbin/univention-config-registry", argv); + fprintf(stderr, "unset %s\n", key); + return execv(ucr_bin, argv); }