From 98ed9f3360bb5cbfb02d2cb82ee3167fce437772 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 16 Nov 2012 09:52:21 +0200 Subject: ubi-tests: rename common.[ch] to helpers.[ch] ... to avoid confusion when the local common.h shadows the top-level common.h. Signed-off-by: Artem Bityutskiy --- tests/ubi-tests/Makefile | 2 +- tests/ubi-tests/common.c | 365 ------------------------------------------ tests/ubi-tests/common.h | 113 ------------- tests/ubi-tests/helpers.c | 365 ++++++++++++++++++++++++++++++++++++++++++ tests/ubi-tests/helpers.h | 113 +++++++++++++ tests/ubi-tests/integ.c | 2 +- tests/ubi-tests/io_basic.c | 2 +- tests/ubi-tests/io_paral.c | 2 +- tests/ubi-tests/io_read.c | 2 +- tests/ubi-tests/io_update.c | 2 +- tests/ubi-tests/mkvol_bad.c | 2 +- tests/ubi-tests/mkvol_basic.c | 2 +- tests/ubi-tests/mkvol_paral.c | 2 +- tests/ubi-tests/rsvol.c | 2 +- tests/ubi-tests/volrefcnt.c | 2 +- 15 files changed, 489 insertions(+), 489 deletions(-) delete mode 100644 tests/ubi-tests/common.c delete mode 100644 tests/ubi-tests/common.h create mode 100644 tests/ubi-tests/helpers.c create mode 100644 tests/ubi-tests/helpers.h (limited to 'tests') diff --git a/tests/ubi-tests/Makefile b/tests/ubi-tests/Makefile index 2c47a9f..ace0120 100644 --- a/tests/ubi-tests/Makefile +++ b/tests/ubi-tests/Makefile @@ -17,7 +17,7 @@ libubi.a: $(LIBUBI_PATH)/libubi.c $(LIBUBI_HEADER_PATH)/libubi.h $(LIBUBI_PATH $(CC) $(CFLAGS) -I $(LIBUBI_PATH) -I../../include -DUDEV_SETTLE_HACK -c $(LIBUBI_PATH)/libubi.c -o libubi.o ar cr libubi.a libubi.o -$(TARGETS): $(addprefix $(BUILDDIR)/, common.o) libubi.a +$(TARGETS): $(addprefix $(BUILDDIR)/, helpers.o) libubi.a clean:: rm -f libubi.a diff --git a/tests/ubi-tests/common.c b/tests/ubi-tests/common.c deleted file mode 100644 index a64ea75..0000000 --- a/tests/ubi-tests/common.c +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright (c) International Business Machines Corp., 2006 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * Author: Artem B. Bityutskiy - * - * The stuff which is common for many tests. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "libubi.h" -#include "common.h" - -/** - * __initial_check - check that common prerequisites which are required to run - * tests. - * - * @test test name - * @argc count of command-line arguments - * @argv command-line arguments - * - * This function returns %0 if all is fine and test may be run and %-1 if not. - */ -int __initial_check(const char *test, int argc, char * const argv[]) -{ - libubi_t libubi; - struct ubi_dev_info dev_info; - - /* - * All tests require UBI character device name as the first parameter, - * check this. - */ - if (argc < 2) { - __errmsg(test, __func__, __LINE__, - "UBI character device node is not specified"); - return -1; - } - - libubi = libubi_open(); - if (libubi == NULL) { - __failed(test, __func__, __LINE__, "libubi_open"); - return -1; - } - - if (ubi_get_dev_info(libubi, argv[1], &dev_info)) { - __failed(test, __func__, __LINE__, "ubi_get_dev_info"); - goto close; - } - - if (dev_info.avail_lebs < MIN_AVAIL_EBS) { - __errmsg(test, __func__, __LINE__, - "insufficient available eraseblocks %d on UBI " - "device, required %d", - dev_info.avail_lebs, MIN_AVAIL_EBS); - goto close; - } - - if (dev_info.vol_count != 0) { - __errmsg(test, __func__, __LINE__, - "device %s is not empty", argv[1]); - goto close; - } - - libubi_close(libubi); - return 0; - -close: - libubi_close(libubi); - return -1; -} - -/** - * __errmsg - print a message to stderr. - * - * @test test name - * @func function name - * @line line number - * @fmt format string - */ -void __errmsg(const char *test, const char *func, int line, - const char *fmt, ...) -{ - va_list args; - - fprintf(stderr, "[%s] %s():%d: ", test, func, line); - va_start(args, fmt); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); - va_end(args); -} - -/** - * __failed - print function fail message. - * - * @test test name - * @func calling function name - * @line line number - * @failed failed function name - */ -void __failed(const char *test, const char *func, int line, - const char *failed) -{ - fprintf(stderr, "[%s] %s():%d: function %s() failed with error %d (%s)\n", - test, func, line, failed, errno, strerror(errno)); -} - -/** - * __check_volume - check volume information. - * - * @libubi libubi descriptor - * @dev_info UBI device description - * @test test name - * @func function name - * @line line number - * @vol_id ID of existing volume to check - * @req volume creation request to compare with - * - * This function checks if a volume created using @req request has exactly the - * requested characteristics. Returns 0 in case of success and %-1 in case of - * error. - */ -int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info, - const char *test, const char *func, int line, int vol_id, - const struct ubi_mkvol_request *req) -{ - int ret; - struct ubi_vol_info vol_info; - int leb_size; - long long rsvd_bytes; - - ret = ubi_get_vol_info1(libubi, dev_info->dev_num, vol_id, &vol_info); - if (ret) { - __failed(test, func, line, "ubi_get_vol_info"); - return -1; - } - - if (req->alignment != vol_info.alignment) { - __errmsg(test, func, line, - "bad alignment: requested %d, got %d", - req->alignment, vol_info.alignment); - return -1; - } - if (req->vol_type != vol_info.type) { - __errmsg(test, func, line, "bad type: requested %d, got %d", - req->vol_type, vol_info.type); - return -1; - } - if (strlen(req->name) != strlen(vol_info.name) || - strcmp(req->name, vol_info.name) != 0) { - __errmsg(test, func, line, - "bad name: requested \"%s\", got \"%s\"", - req->name, vol_info.name); - return -1; - } - if (vol_info.corrupted) { - __errmsg(test, func, line, "corrupted new volume"); - return -1; - } - - leb_size = dev_info->leb_size - (dev_info->leb_size % req->alignment); - if (leb_size != vol_info.leb_size) { - __errmsg(test, func, line, - "bad usable LEB size %d, should be %d", - vol_info.leb_size, leb_size); - return -1; - } - - rsvd_bytes = req->bytes; - if (rsvd_bytes % leb_size) - rsvd_bytes += leb_size - (rsvd_bytes % leb_size); - - if (rsvd_bytes != vol_info.rsvd_bytes) { - __errmsg(test, func, line, - "bad reserved bytes %lld, should be %lld", - vol_info.rsvd_bytes, rsvd_bytes); - return -1; - } - - return 0; -} - -/** - * __check_vol_patt - check that volume contains certain data - * - * @libubi libubi descriptor - * @test test name - * @func function name - * @line line number - * @node volume character device node - * @byte data pattern to check - * - * This function returns %0 if the volume contains only @byte bytes, and %-1 if - * not. - */ -int __check_vol_patt(libubi_t libubi, const char *test, const char *func, - int line, const char *node, uint8_t byte) -{ - int ret, fd; - long long bytes = 0; - struct ubi_vol_info vol_info; - unsigned char buf[512]; - - fd = open(node, O_RDONLY); - if (fd == -1) { - __failed(test, func, line, "open"); - __errmsg(test, func, line, "cannot open \"%s\"\n", node); - return -1; - } - - ret = ubi_get_vol_info(libubi, node, &vol_info); - if (ret) { - __failed(test, func, line, "ubi_get_vol_info"); - goto close; - } - - while (bytes < vol_info.data_bytes) { - int i; - - memset(buf, ~byte, 512); - ret = read(fd, buf, 512); - if (ret == -1) { - __failed(test, func, line, "read"); - __errmsg(test, func, line, "bytes = %lld, ret = %d", - bytes, ret); - goto close; - } - - if (ret == 0 && bytes + ret < vol_info.data_bytes) { - __errmsg(test, func, line, - "EOF, but read only %lld bytes of %lld", - bytes + ret, vol_info.data_bytes); - goto close; - } - - for (i = 0; i < ret; i++) - if (buf[i] != byte) { - __errmsg(test, func, line, - "byte at %lld is not %#x but %#x", - bytes + i, byte, (int)buf[i]); - goto close; - } - - bytes += ret; - } - - close(fd); - return 0; - -close: - close(fd); - return -1; -} - -/** - * __update_vol_patt - update volume using a certain byte pattern - * - * @libubi libubi descriptor - * @dev_info UBI device description - * @test test name - * @func function name - * @line line number - * @node volume character device node - * @byte data pattern to check - * - * This function returns %0 in case of success, and %-1 if in case of failure. - */ -int __update_vol_patt(libubi_t libubi, const char *test, const char *func, - int line, const char *node, long long bytes, uint8_t byte) -{ - int ret, fd; - long long written = 0; - unsigned char buf[512]; - - fd = open(node, O_RDWR); - if (fd == -1) { - __failed(test, func, line, "open"); - __errmsg(test, func, line, "cannot open \"%s\"\n", node); - return -1; - } - - if (ubi_update_start(libubi, fd, bytes)) { - __failed(test, func, line, "ubi_update_start"); - __errmsg(test, func, line, "bytes = %lld", bytes); - goto close; - } - - memset(buf, byte, 512); - - while (written != bytes) { - ret = write(fd, buf, 512); - if (ret == -1) { - __failed(test, func, line, "write"); - __errmsg(test, func, line, "written = %lld, ret = %d", - written, ret); - goto close; - } - written += ret; - - if (written > bytes) { - __errmsg(test, func, line, "update length %lld bytes, " - "but %lld bytes are already written", - bytes, written); - goto close; - } - } - - close(fd); - return 0; - -close: - close(fd); - return -1; -} - -/** - * seed_random_generator - randomly seed the standard pseudo-random generator. - * - * This helper function seeds the standard libc pseudo-random generator with a - * more or less random value to make sure the 'rand()' call does not return the - * same sequence every time UBI utilities run. Returns the random seed in case - * of success and a %-1 in case of error. - */ -int seed_random_generator(void) -{ - struct timeval tv; - struct timezone tz; - int seed; - - /* - * Just assume that a combination of the PID + current time is a - * reasonably random number. - */ - if (gettimeofday(&tv, &tz)) - return -1; - - seed = (unsigned int)tv.tv_sec; - seed += (unsigned int)tv.tv_usec; - seed *= getpid(); - seed %= INT_MAX; - srand(seed); - return seed; -} diff --git a/tests/ubi-tests/common.h b/tests/ubi-tests/common.h deleted file mode 100644 index 88e963f..0000000 --- a/tests/ubi-tests/common.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (c) International Business Machines Corp., 2006 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * Author: Artem B. Bityutskiy - * - * The stuff which is common for many tests. - */ - -#ifndef __COMMON_H__ -#define __COMMON_H__ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define UBI_VOLUME_PATTERN "/dev/ubi%d_%d" -#define MIN_AVAIL_EBS 5 -#define PAGE_SIZE 4096 - -#define min(a, b) ((a) < (b) ? (a) : (b)) - -/* Normal messages */ -#define normsg(fmt, ...) do { \ - printf(TESTNAME ": " fmt "\n", ##__VA_ARGS__); \ -} while(0) - -#define errmsg(fmt, ...) ({ \ - __errmsg(TESTNAME, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__); \ - -1; \ -}) - -#define failed(name) ({ \ - __failed(TESTNAME, __FUNCTION__, __LINE__, name); \ - -1; \ -}) - -#define initial_check(argc, argv) \ - __initial_check(TESTNAME, argc, argv) - -#define check_volume(vol_id, req) \ - __check_volume(libubi, &dev_info, TESTNAME, __FUNCTION__, \ - __LINE__, vol_id, req) - -#define check_vol_patt(node, byte) \ - __check_vol_patt(libubi, TESTNAME, __FUNCTION__, __LINE__, node, byte) - -#define update_vol_patt(node, bytes, byte) \ - __update_vol_patt(libubi, TESTNAME, __FUNCTION__, __LINE__, \ - node, bytes, byte) - -#define check_failed(ret, error, func, fmt, ...) ({ \ - int __ret; \ - \ - if (!ret) { \ - errmsg("%s() returned success but should have failed", func); \ - errmsg(fmt, ##__VA_ARGS__); \ - __ret = -1; \ - } \ - if (errno != (error)) { \ - errmsg("%s failed with error %d (%s), expected %d (%s)", \ - func, errno, strerror(errno), error, strerror(error)); \ - errmsg(fmt, ##__VA_ARGS__); \ - __ret = -1; \ - } \ - __ret = 0; \ -}) - -/* Alignments to test, @s is eraseblock size */ -#define ALIGNMENTS(s) \ - {3, 5, 27, 666, 512, 1024, 2048, (s)/2-3, (s)/2-2, (s)/2-1, (s)/2+1, \ - (s)/2+2, (s)/2+3, (s)/3-3, (s)/3-2, (s)/3-1, (s)/3+1, (s)/3+2, \ - (s)/3+3, (s)/4-3, (s)/4-2, (s)/4-1, (s)/4+1, (s)/4+2, (s)/4+3, \ - (s)/5-3, (s)/5-2, (s)/5-1, (s)/5+1, (s)/5+2, (s)/5+3, (s)-17, (s)-9, \ - (s)-8, (s)-6, (s)-4, (s)-1, (s)}; - -extern int seed_random_generator(void); - -extern void __errmsg(const char *test, const char *func, int line, - const char *fmt, ...); -extern void __failed(const char *test, const char *func, int line, - const char *failed); -extern int __initial_check(const char *test, int argc, char * const argv[]); -extern int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info, - const char *test, const char *func, int line, - int vol_id, const struct ubi_mkvol_request *req); -extern int __check_vol_patt(libubi_t libubi, const char *test, const char *func, - int line, const char *node, uint8_t byte); -extern int __update_vol_patt(libubi_t libubi, const char *test, const char *func, - int line, const char *node, long long bytes, - uint8_t byte); - -#ifdef __cplusplus -} -#endif - -#endif /* !__COMMON_H__ */ diff --git a/tests/ubi-tests/helpers.c b/tests/ubi-tests/helpers.c new file mode 100644 index 0000000..dec9d23 --- /dev/null +++ b/tests/ubi-tests/helpers.c @@ -0,0 +1,365 @@ +/* + * Copyright (c) International Business Machines Corp., 2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Artem B. Bityutskiy + * + * The stuff which is common for many tests. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libubi.h" +#include "helpers.h" + +/** + * __initial_check - check that common prerequisites which are required to run + * tests. + * + * @test test name + * @argc count of command-line arguments + * @argv command-line arguments + * + * This function returns %0 if all is fine and test may be run and %-1 if not. + */ +int __initial_check(const char *test, int argc, char * const argv[]) +{ + libubi_t libubi; + struct ubi_dev_info dev_info; + + /* + * All tests require UBI character device name as the first parameter, + * check this. + */ + if (argc < 2) { + __errmsg(test, __func__, __LINE__, + "UBI character device node is not specified"); + return -1; + } + + libubi = libubi_open(); + if (libubi == NULL) { + __failed(test, __func__, __LINE__, "libubi_open"); + return -1; + } + + if (ubi_get_dev_info(libubi, argv[1], &dev_info)) { + __failed(test, __func__, __LINE__, "ubi_get_dev_info"); + goto close; + } + + if (dev_info.avail_lebs < MIN_AVAIL_EBS) { + __errmsg(test, __func__, __LINE__, + "insufficient available eraseblocks %d on UBI " + "device, required %d", + dev_info.avail_lebs, MIN_AVAIL_EBS); + goto close; + } + + if (dev_info.vol_count != 0) { + __errmsg(test, __func__, __LINE__, + "device %s is not empty", argv[1]); + goto close; + } + + libubi_close(libubi); + return 0; + +close: + libubi_close(libubi); + return -1; +} + +/** + * __errmsg - print a message to stderr. + * + * @test test name + * @func function name + * @line line number + * @fmt format string + */ +void __errmsg(const char *test, const char *func, int line, + const char *fmt, ...) +{ + va_list args; + + fprintf(stderr, "[%s] %s():%d: ", test, func, line); + va_start(args, fmt); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + va_end(args); +} + +/** + * __failed - print function fail message. + * + * @test test name + * @func calling function name + * @line line number + * @failed failed function name + */ +void __failed(const char *test, const char *func, int line, + const char *failed) +{ + fprintf(stderr, "[%s] %s():%d: function %s() failed with error %d (%s)\n", + test, func, line, failed, errno, strerror(errno)); +} + +/** + * __check_volume - check volume information. + * + * @libubi libubi descriptor + * @dev_info UBI device description + * @test test name + * @func function name + * @line line number + * @vol_id ID of existing volume to check + * @req volume creation request to compare with + * + * This function checks if a volume created using @req request has exactly the + * requested characteristics. Returns 0 in case of success and %-1 in case of + * error. + */ +int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info, + const char *test, const char *func, int line, int vol_id, + const struct ubi_mkvol_request *req) +{ + int ret; + struct ubi_vol_info vol_info; + int leb_size; + long long rsvd_bytes; + + ret = ubi_get_vol_info1(libubi, dev_info->dev_num, vol_id, &vol_info); + if (ret) { + __failed(test, func, line, "ubi_get_vol_info"); + return -1; + } + + if (req->alignment != vol_info.alignment) { + __errmsg(test, func, line, + "bad alignment: requested %d, got %d", + req->alignment, vol_info.alignment); + return -1; + } + if (req->vol_type != vol_info.type) { + __errmsg(test, func, line, "bad type: requested %d, got %d", + req->vol_type, vol_info.type); + return -1; + } + if (strlen(req->name) != strlen(vol_info.name) || + strcmp(req->name, vol_info.name) != 0) { + __errmsg(test, func, line, + "bad name: requested \"%s\", got \"%s\"", + req->name, vol_info.name); + return -1; + } + if (vol_info.corrupted) { + __errmsg(test, func, line, "corrupted new volume"); + return -1; + } + + leb_size = dev_info->leb_size - (dev_info->leb_size % req->alignment); + if (leb_size != vol_info.leb_size) { + __errmsg(test, func, line, + "bad usable LEB size %d, should be %d", + vol_info.leb_size, leb_size); + return -1; + } + + rsvd_bytes = req->bytes; + if (rsvd_bytes % leb_size) + rsvd_bytes += leb_size - (rsvd_bytes % leb_size); + + if (rsvd_bytes != vol_info.rsvd_bytes) { + __errmsg(test, func, line, + "bad reserved bytes %lld, should be %lld", + vol_info.rsvd_bytes, rsvd_bytes); + return -1; + } + + return 0; +} + +/** + * __check_vol_patt - check that volume contains certain data + * + * @libubi libubi descriptor + * @test test name + * @func function name + * @line line number + * @node volume character device node + * @byte data pattern to check + * + * This function returns %0 if the volume contains only @byte bytes, and %-1 if + * not. + */ +int __check_vol_patt(libubi_t libubi, const char *test, const char *func, + int line, const char *node, uint8_t byte) +{ + int ret, fd; + long long bytes = 0; + struct ubi_vol_info vol_info; + unsigned char buf[512]; + + fd = open(node, O_RDONLY); + if (fd == -1) { + __failed(test, func, line, "open"); + __errmsg(test, func, line, "cannot open \"%s\"\n", node); + return -1; + } + + ret = ubi_get_vol_info(libubi, node, &vol_info); + if (ret) { + __failed(test, func, line, "ubi_get_vol_info"); + goto close; + } + + while (bytes < vol_info.data_bytes) { + int i; + + memset(buf, ~byte, 512); + ret = read(fd, buf, 512); + if (ret == -1) { + __failed(test, func, line, "read"); + __errmsg(test, func, line, "bytes = %lld, ret = %d", + bytes, ret); + goto close; + } + + if (ret == 0 && bytes + ret < vol_info.data_bytes) { + __errmsg(test, func, line, + "EOF, but read only %lld bytes of %lld", + bytes + ret, vol_info.data_bytes); + goto close; + } + + for (i = 0; i < ret; i++) + if (buf[i] != byte) { + __errmsg(test, func, line, + "byte at %lld is not %#x but %#x", + bytes + i, byte, (int)buf[i]); + goto close; + } + + bytes += ret; + } + + close(fd); + return 0; + +close: + close(fd); + return -1; +} + +/** + * __update_vol_patt - update volume using a certain byte pattern + * + * @libubi libubi descriptor + * @dev_info UBI device description + * @test test name + * @func function name + * @line line number + * @node volume character device node + * @byte data pattern to check + * + * This function returns %0 in case of success, and %-1 if in case of failure. + */ +int __update_vol_patt(libubi_t libubi, const char *test, const char *func, + int line, const char *node, long long bytes, uint8_t byte) +{ + int ret, fd; + long long written = 0; + unsigned char buf[512]; + + fd = open(node, O_RDWR); + if (fd == -1) { + __failed(test, func, line, "open"); + __errmsg(test, func, line, "cannot open \"%s\"\n", node); + return -1; + } + + if (ubi_update_start(libubi, fd, bytes)) { + __failed(test, func, line, "ubi_update_start"); + __errmsg(test, func, line, "bytes = %lld", bytes); + goto close; + } + + memset(buf, byte, 512); + + while (written != bytes) { + ret = write(fd, buf, 512); + if (ret == -1) { + __failed(test, func, line, "write"); + __errmsg(test, func, line, "written = %lld, ret = %d", + written, ret); + goto close; + } + written += ret; + + if (written > bytes) { + __errmsg(test, func, line, "update length %lld bytes, " + "but %lld bytes are already written", + bytes, written); + goto close; + } + } + + close(fd); + return 0; + +close: + close(fd); + return -1; +} + +/** + * seed_random_generator - randomly seed the standard pseudo-random generator. + * + * This helper function seeds the standard libc pseudo-random generator with a + * more or less random value to make sure the 'rand()' call does not return the + * same sequence every time UBI utilities run. Returns the random seed in case + * of success and a %-1 in case of error. + */ +int seed_random_generator(void) +{ + struct timeval tv; + struct timezone tz; + int seed; + + /* + * Just assume that a combination of the PID + current time is a + * reasonably random number. + */ + if (gettimeofday(&tv, &tz)) + return -1; + + seed = (unsigned int)tv.tv_sec; + seed += (unsigned int)tv.tv_usec; + seed *= getpid(); + seed %= INT_MAX; + srand(seed); + return seed; +} diff --git a/tests/ubi-tests/helpers.h b/tests/ubi-tests/helpers.h new file mode 100644 index 0000000..ae9d030 --- /dev/null +++ b/tests/ubi-tests/helpers.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) International Business Machines Corp., 2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Artem B. Bityutskiy + * + * The stuff which is common for many tests. + */ + +#ifndef __HELPERS_H__ +#define __HELPERS_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define UBI_VOLUME_PATTERN "/dev/ubi%d_%d" +#define MIN_AVAIL_EBS 5 +#define PAGE_SIZE 4096 + +#define min(a, b) ((a) < (b) ? (a) : (b)) + +/* Normal messages */ +#define normsg(fmt, ...) do { \ + printf(TESTNAME ": " fmt "\n", ##__VA_ARGS__); \ +} while(0) + +#define errmsg(fmt, ...) ({ \ + __errmsg(TESTNAME, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__); \ + -1; \ +}) + +#define failed(name) ({ \ + __failed(TESTNAME, __FUNCTION__, __LINE__, name); \ + -1; \ +}) + +#define initial_check(argc, argv) \ + __initial_check(TESTNAME, argc, argv) + +#define check_volume(vol_id, req) \ + __check_volume(libubi, &dev_info, TESTNAME, __FUNCTION__, \ + __LINE__, vol_id, req) + +#define check_vol_patt(node, byte) \ + __check_vol_patt(libubi, TESTNAME, __FUNCTION__, __LINE__, node, byte) + +#define update_vol_patt(node, bytes, byte) \ + __update_vol_patt(libubi, TESTNAME, __FUNCTION__, __LINE__, \ + node, bytes, byte) + +#define check_failed(ret, error, func, fmt, ...) ({ \ + int __ret; \ + \ + if (!ret) { \ + errmsg("%s() returned success but should have failed", func); \ + errmsg(fmt, ##__VA_ARGS__); \ + __ret = -1; \ + } \ + if (errno != (error)) { \ + errmsg("%s failed with error %d (%s), expected %d (%s)", \ + func, errno, strerror(errno), error, strerror(error)); \ + errmsg(fmt, ##__VA_ARGS__); \ + __ret = -1; \ + } \ + __ret = 0; \ +}) + +/* Alignments to test, @s is eraseblock size */ +#define ALIGNMENTS(s) \ + {3, 5, 27, 666, 512, 1024, 2048, (s)/2-3, (s)/2-2, (s)/2-1, (s)/2+1, \ + (s)/2+2, (s)/2+3, (s)/3-3, (s)/3-2, (s)/3-1, (s)/3+1, (s)/3+2, \ + (s)/3+3, (s)/4-3, (s)/4-2, (s)/4-1, (s)/4+1, (s)/4+2, (s)/4+3, \ + (s)/5-3, (s)/5-2, (s)/5-1, (s)/5+1, (s)/5+2, (s)/5+3, (s)-17, (s)-9, \ + (s)-8, (s)-6, (s)-4, (s)-1, (s)}; + +extern int seed_random_generator(void); + +extern void __errmsg(const char *test, const char *func, int line, + const char *fmt, ...); +extern void __failed(const char *test, const char *func, int line, + const char *failed); +extern int __initial_check(const char *test, int argc, char * const argv[]); +extern int __check_volume(libubi_t libubi, struct ubi_dev_info *dev_info, + const char *test, const char *func, int line, + int vol_id, const struct ubi_mkvol_request *req); +extern int __check_vol_patt(libubi_t libubi, const char *test, const char *func, + int line, const char *node, uint8_t byte); +extern int __update_vol_patt(libubi_t libubi, const char *test, const char *func, + int line, const char *node, long long bytes, + uint8_t byte); + +#ifdef __cplusplus +} +#endif + +#endif /* !__HELPERS_H__ */ diff --git a/tests/ubi-tests/integ.c b/tests/ubi-tests/integ.c index e48f533..7ef3cf2 100644 --- a/tests/ubi-tests/integ.c +++ b/tests/ubi-tests/integ.c @@ -14,7 +14,7 @@ #include #include "libubi.h" -#include "common.h" +#include "helpers.h" struct erase_block_info; struct volume_info; diff --git a/tests/ubi-tests/io_basic.c b/tests/ubi-tests/io_basic.c index 566514c..8adb8e1 100644 --- a/tests/ubi-tests/io_basic.c +++ b/tests/ubi-tests/io_basic.c @@ -30,7 +30,7 @@ #include #include "libubi.h" #define TESTNAME "io_basic" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/io_paral.c b/tests/ubi-tests/io_paral.c index 615c1dd..4764255 100644 --- a/tests/ubi-tests/io_paral.c +++ b/tests/ubi-tests/io_paral.c @@ -32,7 +32,7 @@ #include #include "libubi.h" #define TESTNAME "io_paral" -#include "common.h" +#include "helpers.h" #define THREADS_NUM 4 #define ITERATIONS (1024 * 1) diff --git a/tests/ubi-tests/io_read.c b/tests/ubi-tests/io_read.c index fb8018a..27d4d58 100644 --- a/tests/ubi-tests/io_read.c +++ b/tests/ubi-tests/io_read.c @@ -30,7 +30,7 @@ #include #include "libubi.h" #define TESTNAME "io_basic" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/io_update.c b/tests/ubi-tests/io_update.c index ec109e0..27ece52 100644 --- a/tests/ubi-tests/io_update.c +++ b/tests/ubi-tests/io_update.c @@ -32,7 +32,7 @@ #include #include #define TESTNAME "io_update" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/mkvol_bad.c b/tests/ubi-tests/mkvol_bad.c index e28df49..2292df6 100644 --- a/tests/ubi-tests/mkvol_bad.c +++ b/tests/ubi-tests/mkvol_bad.c @@ -26,7 +26,7 @@ #include #include "libubi.h" #define TESTNAME "mkvol_bad" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/mkvol_basic.c b/tests/ubi-tests/mkvol_basic.c index 3381ac4..1a02c47 100644 --- a/tests/ubi-tests/mkvol_basic.c +++ b/tests/ubi-tests/mkvol_basic.c @@ -25,7 +25,7 @@ #include #include "libubi.h" #define TESTNAME "mkvol_basic" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/mkvol_paral.c b/tests/ubi-tests/mkvol_paral.c index 8c6e02f..4a6ae46 100644 --- a/tests/ubi-tests/mkvol_paral.c +++ b/tests/ubi-tests/mkvol_paral.c @@ -26,7 +26,7 @@ #include #include "libubi.h" #define TESTNAME "mkvol_paral" -#include "common.h" +#include "helpers.h" #define THREADS_NUM 4 #define ITERATIONS 500 diff --git a/tests/ubi-tests/rsvol.c b/tests/ubi-tests/rsvol.c index 917d6ba..50d6429 100644 --- a/tests/ubi-tests/rsvol.c +++ b/tests/ubi-tests/rsvol.c @@ -30,7 +30,7 @@ #include #include "libubi.h" #define TESTNAME "rsvol" -#include "common.h" +#include "helpers.h" static libubi_t libubi; static struct ubi_dev_info dev_info; diff --git a/tests/ubi-tests/volrefcnt.c b/tests/ubi-tests/volrefcnt.c index 9b5a53c..1700ab7 100644 --- a/tests/ubi-tests/volrefcnt.c +++ b/tests/ubi-tests/volrefcnt.c @@ -30,7 +30,7 @@ #include #include "libubi.h" #define TESTNAME "rmvol" -#include "common.h" +#include "helpers.h" #define SYSFS_FILE "/sys/class/ubi/ubi%d_%d/usable_eb_size" -- cgit v1.2.3