X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ntsimulator%2Fntsim-ng%2Futils%2Frand_utils.c;h=06d746c722f15e7330926f3dc9d9440cf54927b8;hb=3bbf9d8a3c81afebcffb2b926cef219336dd53d6;hp=28222e698e0f33a010260a5c7e707ca3e8891db5;hpb=f1d5c9198acde7a7ce296490087cad37e008f688;p=sim%2Fo1-interface.git diff --git a/ntsimulator/ntsim-ng/utils/rand_utils.c b/ntsimulator/ntsim-ng/utils/rand_utils.c index 28222e6..06d746c 100644 --- a/ntsimulator/ntsim-ng/utils/rand_utils.c +++ b/ntsimulator/ntsim-ng/utils/rand_utils.c @@ -69,7 +69,7 @@ void rand_init(void) { FILE* urandom = fopen("/dev/urandom", "r"); if(urandom == 0) { - log_error("failed to open /dev/urandom"); + log_error("failed to open /dev/urandom\n"); } else { fread(&seed, sizeof(int), 1, urandom); @@ -79,9 +79,9 @@ void rand_init(void) { srand(seed); srandom(seed); - log_message(2, "rand_init() was called and seed was initialized to %u\n", seed); + log_add_verbose(2, "rand_init() was called and seed was initialized to %u\n", seed); if(RAND_MAX < 65536) { - log_error("RAND_MAX is too low: %d", RAND_MAX); + log_error("RAND_MAX is too low: %d\n", RAND_MAX); } } @@ -89,10 +89,10 @@ void rand_init_fixed(unsigned int seed) { srand(seed); srandom(seed); - log_message(2, "rand_init_fixed() was called and seed was initialized to %u\n", seed); + log_add_verbose(2, "rand_init_fixed() was called and seed was initialized to %u\n", seed); if(RAND_MAX < 65536) { - log_error("RAND_MAX is too low: %d", RAND_MAX); + log_error("RAND_MAX is too low: %d\n", RAND_MAX); } } @@ -106,7 +106,7 @@ char *rand_get_populate_value(const struct lys_type *type) { asprintf(&full_type, "%s:%s", type->der->module->name, type->der->name); if(full_type == 0) { - log_error("bad malloc"); + log_error("bad malloc\n"); return 0; } @@ -166,7 +166,7 @@ char *rand_get_populate_value(const struct lys_type *type) { } struct lys_ident *ident = rand_identity(type->info.ident.ref[0]); if(ident == 0) { - log_error("rand_identity failed"); + log_error("rand_identity failed\n"); return 0; } @@ -213,7 +213,7 @@ char *rand_get_populate_value(const struct lys_type *type) { if(expression) { char *ret = rand_regex(expression); if(ret == 0) { - log_error("rand_regex failed"); + log_error("rand_regex failed\n"); free(expression); return 0; } @@ -221,7 +221,7 @@ char *rand_get_populate_value(const struct lys_type *type) { while(strlen(ret) < min_length) { char *add = rand_regex(expression); if(add == 0) { - log_error("rand_regex failed"); + log_error("rand_regex failed\n"); free(expression); free(ret); return 0; @@ -236,7 +236,7 @@ char *rand_get_populate_value(const struct lys_type *type) { free(expression); if(ret == 0) { - log_error("rand_regex failed"); + log_error("rand_regex failed\n"); return 0; } @@ -344,7 +344,7 @@ char *rand_get_populate_value(const struct lys_type *type) { case LY_TYPE_BITS: ret = (char*)malloc(1); if(ret == 0) { - log_error("malloc failed"); + log_error("malloc failed\n"); return 0; } ret[0] = 0; @@ -355,7 +355,7 @@ char *rand_get_populate_value(const struct lys_type *type) { bool first = (ret == 0); ret = (char*)realloc(ret, sizeof(char) * (strlen(ret) + 1 + strlen(val) + 1)); if(ret == 0) { - log_error("malloc failed"); + log_error("malloc failed\n"); return 0; } @@ -391,7 +391,7 @@ char *rand_get_populate_value(const struct lys_type *type) { uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t) * length); if(!data) { - log_error("bad malloc"); + log_error("bad malloc\n"); return 0; } @@ -408,7 +408,7 @@ char *rand_get_populate_value(const struct lys_type *type) { case LY_TYPE_UNION: case LY_TYPE_INST: asprintf(&ret, "{late_resolve_%s}", type->der->name); - log_error("needed: %s", ret); + log_error("needed: %s\n", ret); assert(0); return ret; break; @@ -416,7 +416,7 @@ char *rand_get_populate_value(const struct lys_type *type) { case LY_TYPE_UNKNOWN: default: asprintf(&ret, "{unimplemented_%s}", type->der->name); - log_error("can't generate random for: %s", type->der->name); + log_error("can't generate random for: %s\n", type->der->name); assert(0); return ret; break; @@ -472,27 +472,44 @@ char *rand_regex(const char *regexp) { char *cmd = 0; static int run_time = 0; + char *regexp64 = b64_encode((const unsigned char*)regexp, strlen(regexp)); + if(regexp64 == 0) { + log_error("b64_encode failed\n"); + return 0; + } + if(framework_arguments.no_rand) { run_time++; - asprintf(&cmd, "regxstring %d '%s'", run_time, regexp); + asprintf(&cmd, "regxstring %d '%s'", run_time, regexp64); } else { - asprintf(&cmd, "regxstring '%s'", regexp); + asprintf(&cmd, "regxstring '%s'", regexp64); } + free(regexp64); - FILE* pipe = popen(cmd, "r"); - free(cmd); - - if (!pipe) { - log_error("popen() failed"); + if(cmd == 0) { + log_error("asprintf failed\n"); return 0; } - fgets(buffer, sizeof(buffer), pipe); - pclose(pipe); - + char last_char = ' '; + while(last_char == ' ') { + FILE* pipe = popen(cmd, "r"); + if (!pipe) { + log_error("popen() failed\n"); + free(cmd); + return 0; + } + + fgets(buffer, sizeof(buffer), pipe); + pclose(pipe); + + buffer[strlen(buffer) - 1] = 0; //remove trailing \n + last_char = buffer[strlen(buffer) - 1]; + } + char *ret = strdup(buffer); - ret[strlen(ret) - 1] = 0; //remove trailing \n + free(cmd); return ret; } @@ -517,8 +534,8 @@ static char *rand_string(int min_length, int max_length) { length = min_length; } else { - if((framework_config.debug_max_string_size) && (framework_config.debug_max_string_size < max_length)) { - max_length = framework_config.debug_max_string_size; + if((framework_config.datastore_generate.debug_max_string_size) && (framework_config.datastore_generate.debug_max_string_size < max_length)) { + max_length = framework_config.datastore_generate.debug_max_string_size; } length = min_length + rand_uint16() % (max_length - min_length); @@ -526,7 +543,7 @@ static char *rand_string(int min_length, int max_length) { char *ret = (char *)malloc(length + 1); if(!ret) { - log_error("bad malloc"); + log_error("bad malloc\n"); return 0; } @@ -647,7 +664,7 @@ static rand_range_t rand_range(const char *range, const LY_DATA_TYPE type) { //remove spaces char *rrange = (char*)malloc(sizeof(char) * (strlen(range) + 1)); if (!rrange) { - log_error("bad malloc"); + log_error("bad malloc\n"); return ret; } @@ -869,7 +886,7 @@ static char *rand_date_and_time(void) { char *ret = (char *)malloc(21); if(!ret) { - log_error("bad malloc"); + log_error("bad malloc\n"); return 0; } strftime(ret, 21, "%Y-%m-%dT%H:%M:%SZ", <);