1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <testUtil.h>
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <cutils/log.h>
#define ALEN(a) (sizeof(a) / sizeof(a [0])) // Array length
typedef unsigned int bool_t;
#define true (0 == 0)
#define false (!true)
#define MAXSTR 200
static const char *logCatTag;
static const unsigned int uSecsPerSec = 1000000;
static const unsigned int nSecsPerSec = 1000000000;
// struct timespec to double
double ts2double(const struct timespec *val)
{
double rv;
rv = val->tv_sec;
rv += (double) val->tv_nsec / nSecsPerSec;
return rv;
}
// struct timeval to double
double tv2double(const struct timeval *val)
{
double rv;
rv = val->tv_sec;
rv += (double) val->tv_usec / uSecsPerSec;
return rv;
}
// double to struct timespec
struct timespec double2ts(double amt)
{
struct timespec rv;
rv.tv_sec = floor(amt);
rv.tv_nsec = (amt - rv.tv_sec) * nSecsPerSec;
// TODO: Handle cases where amt is negative
while ((unsigned) rv.tv_nsec >= nSecsPerSec) {
rv.tv_nsec -= nSecsPerSec;
rv.tv_sec++;
}
return rv;
}
// double to struct timeval
struct timeval double2tv(double amt)
{
struct timeval rv;
rv.tv_sec = floor(amt);
rv.tv_usec = (amt - rv.tv_sec) * uSecsPerSec;
// TODO: Handle cases where amt is negative
while ((unsigned) rv.tv_usec >= uSecsPerSec) {
rv.tv_usec -= uSecsPerSec;
rv.tv_sec++;
}
return rv;
}
// Delta (difference) between two struct timespec.
// It is expected that the time given by the structure pointed to by
// second, is later than the time pointed to by first.
struct timespec tsDelta(const struct timespec *first,
const struct timespec *second)
{
struct timespec rv;
assert(first != NULL);
assert(second != NULL);
assert(first->tv_nsec >= 0 && first->tv_nsec < nSecsPerSec);
assert(second->tv_nsec >= 0 && second->tv_nsec < nSecsPerSec);
rv.tv_sec = second->tv_sec - first->tv_sec;
if (second->tv_nsec >= first->tv_nsec) {
rv.tv_nsec = second->tv_nsec - first->tv_nsec;
} else {
rv.tv_nsec = (second->tv_nsec + nSecsPerSec) - first->tv_nsec;
rv.tv_sec--;
}
return rv;
}
// Delta (difference) between two struct timeval.
// It is expected that the time given by the structure pointed to by
// second, is later than the time pointed to by first.
struct timeval tvDelta(const struct timeval *first,
const struct timeval *second)
{
struct timeval rv;
assert(first != NULL);
assert(second != NULL);
assert(first->tv_usec >= 0 && first->tv_usec < uSecsPerSec);
assert(second->tv_usec >= 0 && second->tv_usec < uSecsPerSec);
rv.tv_sec = second->tv_sec - first->tv_sec;
if (second->tv_usec >= first->tv_usec) {
rv.tv_usec = second->tv_usec - first->tv_usec;
} else {
rv.tv_usec = (second->tv_usec + uSecsPerSec) - first->tv_usec;
rv.tv_sec--;
}
return rv;
}
void testPrint(FILE *stream, const char *fmt, ...)
{
char line[MAXSTR];
va_list args;
va_start(args, fmt);
vsnprintf(line, sizeof(line), fmt, args);
if (stream == stderr) {
ALOG(LOG_ERROR, logCatTag, "%s", line);
} else {
ALOG(LOG_INFO, logCatTag, "%s", line);
}
vfprintf(stream, fmt, args);
fputc('\n', stream);
}
// Set tag used while logging to the logcat error interface
void testSetLogCatTag(const char *tag)
{
logCatTag = tag;
}
// Obtain pointer to current log to logcat error interface tag
const char * testGetLogCatTag(void)
{
return logCatTag;
}
/*
* Random
*
* Returns a pseudo random number in the range [0:2^32-1].
*
* Precondition: srand48() called to set the seed of
* the pseudo random number generator.
*/
uint32_t testRand(void)
{
uint32_t val;
// Use lrand48() to obtain 31 bits worth
// of randomness.
val = lrand48();
// Make an additional lrand48() call and merge
// the randomness into the most significant bits.
val ^= lrand48() << 1;
return val;
}
/*
* Random Modulus
*
* Pseudo randomly returns unsigned integer in the range [0, mod).
*
* Precondition: srand48() called to set the seed of
* the pseudo random number generator.
*/
uint32_t testRandMod(uint32_t mod)
{
// Obtain the random value
// Use lrand48() when it would produce a sufficient
// number of random bits, otherwise use testRand().
const uint32_t lrand48maxVal = ((uint32_t) 1 << 31) - 1;
uint32_t val = (mod <= lrand48maxVal) ? (uint32_t) lrand48() : testRand();
/*
* The contents of individual bytes tend to be less than random
* across different seeds. For example, srand48(x) and
* srand48(x + n * 4) cause lrand48() to return the same sequence of
* least significant bits. For small mod values this can produce
* noticably non-random sequnces. For mod values of less than 2
* bytes, will use the randomness from all the bytes.
*/
if (mod <= 0x10000) {
val = (val & 0xffff) ^ (val >> 16);
// If mod less than a byte, can further combine down to
// a single byte.
if (mod <= 0x100) {
val = (val & 0xff) ^ (val >> 8);
}
}
return val % mod;
}
/*
* Random Boolean
*
* Pseudo randomly returns 0 (false) or 1 (true).
*
* Precondition: srand48() called to set the seed of
* the pseudo random number generator.
*/
int testRandBool(void)
{
return (testRandMod(2));
}
/*
* Random Fraction
*
* Pseudo randomly return a value in the range [0.0, 1.0).
*
* Precondition: srand48() called to set the seed of
* the pseudo random number generator.
*/
double testRandFract(void)
{
return drand48();
}
// Delays for the number of seconds specified by amt or a greater amount.
// The amt variable is of type float and thus non-integer amounts
// of time can be specified. This function automatically handles cases
// where nanosleep(2) returns early due to reception of a signal.
void testDelay(float amt)
{
struct timespec start, current, delta;
struct timespec remaining;
// Get the time at which we started
clock_gettime(CLOCK_MONOTONIC, &start);
do {
// Get current time
clock_gettime(CLOCK_MONOTONIC, ¤t);
// How much time is left
delta = tsDelta(&start, ¤t);
if (ts2double(&delta) > amt) { break; }
// Request to sleep for the remaining time
remaining = double2ts(amt - ts2double(&delta));
(void) nanosleep(&remaining, NULL);
} while (true);
}
// Delay spins for the number of seconds specified by amt or a greater
// amount. The amt variable is of type float and thus non-integer amounts
// of time can be specified. Differs from testDelay() in that
// testDelaySpin() performs a spin loop, instead of using nanosleep().
void testDelaySpin(float amt)
{
struct timespec start, current, delta;
// Get the time at which we started
clock_gettime(CLOCK_MONOTONIC, &start);
do {
// Get current time
clock_gettime(CLOCK_MONOTONIC, ¤t);
// How much time is left
delta = tsDelta(&start, ¤t);
if (ts2double(&delta) > amt) { break; }
} while (true);
}
/*
* Hex Dump
*
* Displays in hex the contents of the memory starting at the location
* pointed to by buf, for the number of bytes given by size.
* Each line of output is indented by a number of spaces that
* can be set by calling xDumpSetIndent(). It is also possible
* to offset the displayed address by an amount set by calling
* xDumpSetOffset.
*/
static uint8_t xDumpIndent;
static uint64_t xDumpOffset;
void
testXDump(const void *buf, size_t size)
{
const unsigned int bytesPerLine = 16;
int rv;
char line[MAXSTR];
const unsigned char *ptr = buf, *start = buf;
size_t num = size;
char *linep = line;
while (num) {
if (((ptr - start) % bytesPerLine) == 0) {
if (linep != line) {
testPrintE("%s", line);
}
linep = line;
rv = snprintf(linep, ALEN(line) - (linep - line),
"%*s%06llx:", xDumpIndent, "",
(long long) (ptr - start) + xDumpOffset);
linep += rv;
}
// Check that there is at least room for 4
// more characters. The 4 characters being
// a space, 2 hex digits and the terminating
// '\0'.
assert((ALEN(line) - 4) >= (linep - line));
rv = snprintf(linep, ALEN(line) - (linep - line),
" %02x", *ptr++);
linep += rv;
num--;
}
if (linep != line) {
testPrintE("%s", line);
}
}
// Set an indent of spaces for each line of hex dump output
void
testXDumpSetIndent(uint8_t indent)
{
xDumpIndent = indent;
}
// Obtain the current hex dump indent amount
uint8_t
testXDumpGetIndent(void)
{
return xDumpIndent;
}
// Set the hex dump address offset amount
void
testXDumpSetOffset(uint64_t offset)
{
xDumpOffset = offset;
}
// Get the current hex dump address offset amount
uint64_t
testXDumpGetOffset(void)
{
return xDumpOffset;
}
/*
* Execute Command
*
* Executes the command pointed to by cmd. Output from the
* executed command is captured and sent to LogCat Info. Once
* the command has finished execution, it's exit status is captured
* and checked for an exit status of zero. Any other exit status
* causes diagnostic information to be printed and an immediate
* testcase failure.
*/
void testExecCmd(const char *cmd)
{
FILE *fp;
int rv;
int status;
char str[MAXSTR];
// Display command to be executed
testPrintI("cmd: %s", cmd);
// Execute the command
fflush(stdout);
if ((fp = popen(cmd, "r")) == NULL) {
testPrintE("execCmd popen failed, errno: %i", errno);
exit(100);
}
// Obtain and display each line of output from the executed command
while (fgets(str, sizeof(str), fp) != NULL) {
if ((strlen(str) > 1) && (str[strlen(str) - 1] == '\n')) {
str[strlen(str) - 1] = '\0';
}
testPrintI(" out: %s", str);
}
// Obtain and check return status of executed command.
// Fail on non-zero exit status
status = pclose(fp);
if (!(WIFEXITED(status) && (WEXITSTATUS(status) == 0))) {
testPrintE("Unexpected command failure");
testPrintE(" status: %#x", status);
if (WIFEXITED(status)) {
testPrintE("WEXITSTATUS: %i", WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
testPrintE("WTERMSIG: %i", WTERMSIG(status));
}
exit(101);
}
}
|