00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #include "test-harness.h"
00028
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033
00034 #include "treeconf_int.h"
00035
00036 RCSTAG("@(#)$Id: t_tc_load.c,v 1.6 2005/06/08 04:38:39 klmitch Exp $");
00037
00038 TEST_PROG(t_tc_load, "Test tc_load() function")
00039 TEST_DEP(t_tc_load, t_treeconf)
00040 TEST_ARG(t_tc_load, "<t_tc_load.conf>")
00041 TEST_ARG(t_tc_load, "<t_tc_load.res>")
00042
00049 #define RES_CHUNK 10
00050
00057 #define RES_BUFSIZE 1024
00058
00065 static const char *prog;
00066
00067
00080 static char **
00081 load_results(treeconf_ctx_t *ctx, const char *file)
00082 {
00083 char **results = 0, buf[RES_BUFSIZE], *var, *def, *val = 0, *nl;
00084 unsigned int err;
00085 int pos = 0, size = 0;
00086 FILE *fp;
00087
00088
00089 if (!(fp = fopen(file, "r"))) {
00090 perror(file);
00091 exit(1);
00092 }
00093
00094 while ((var = fgets(buf, sizeof(buf), fp))) {
00095 if (*var == '#' ||
00096 !(nl = strchr(buf, '\n')) ||
00097 ((def = strchr(var, ':')) &&
00098 !(val = strchr(def + 1, ':'))))
00099 continue;
00100
00101 *nl = '\0';
00102
00103 if (def) {
00104 *(def++) = '\0';
00105 *(val++) = '\0';
00106
00107 if (pos >= size - 1) {
00108 if (!(results = (char **)realloc(results, sizeof(char *) *
00109 (size += RES_CHUNK)))) {
00110 fprintf(stderr, "%s: Out of memory\n", prog);
00111 exit(1);
00112 }
00113 }
00114
00115
00116 if (!(results[pos] = (char *)malloc(strlen(var) + strlen(val) + 2))) {
00117 fprintf(stderr, "%s: Out of memory\n", prog);
00118 exit(1);
00119 }
00120
00121
00122 sprintf(results[pos++], "%s=%s", var, val);
00123 }
00124
00125
00126 if ((err = tc_register(ctx, var, def, 0, 0, 0, 0))) {
00127 fprintf(stderr, "%s: tc_register() failed on %s: %u\n", prog, var, err);
00128 exit(1);
00129 }
00130 }
00131
00132 fclose(fp);
00133
00134 if (!results) {
00135 fprintf(stderr, "%s: No results in list\n", file);
00136 exit(1);
00137 }
00138
00139 results[pos] = 0;
00140
00141 return results;
00142 }
00143
00150 int
00151 main(int argc, char **argv)
00152 {
00153 treeconf_ctx_t ctx = TREECONF_CTX_INIT;
00154 char *conf, *result, **results, *t;
00155 const char *val;
00156 unsigned int err = 0;
00157
00158 prog = argv[0];
00159
00160 if (argc < 3) {
00161 fprintf(stderr, "Usage: %s <conf> <res>\n", prog);
00162 exit(1);
00163 }
00164
00165 conf = argv[1];
00166 result = argv[2];
00167
00168 results = load_results(&ctx, result);
00169
00170
00171 TEST(t_tc_load, tc_load_call, "Test that tc_load() may be called",
00172 (!(err = tc_load(conf, &ctx))), FATAL(0),
00173 ("tc_load() successfully loaded configuration \"%s\"", conf),
00174 ("tc_load() call failed with error %u", err));
00175
00176 TEST_DECL(t_tc_load, tc_load_verify, "Test that tc_load() loaded expected "
00177 "values")
00178
00179 for (; *results; results++) {
00180 if (!(t = strchr(*results, '=')))
00181 continue;
00182 *(t++) = '\0';
00183
00184
00185 if ((err = tc_get(&ctx, *results, &val, 0)))
00186 FAIL(TEST_NAME(tc_load_verify), FATAL(0), "tc_get() on variable \"%s\" "
00187 "failed with error %u", *results, err);
00188 else if (strcmp(t, val))
00189 FAIL(TEST_NAME(tc_load_verify), FATAL(0), "Value mismatch on variable "
00190 "\"%s\": expected \"%s\", got \"%s\"", *results, t, val);
00191 }
00192 PASS(TEST_NAME(tc_load_verify), "tc_load() successfully loaded all "
00193 "variables");
00194
00195 return 0;
00196 }