2011-07-01 17:58:15 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2011-07-01 17:58:15 +00:00
|
|
|
#include "testutils.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
static const char* diskNames[] = {
|
|
|
|
"sda", "sdb", "sdc", "sdd", "sde", "sdf", "sdg", "sdh", "sdi", "sdj", "sdk", "sdl", "sdm", "sdn", "sdo", "sdp", "sdq", "sdr", "sds", "sdt", "sdu", "sdv", "sdw", "sdx", "sdy", "sdz",
|
|
|
|
"sdaa", "sdab", "sdac", "sdad", "sdae", "sdaf", "sdag", "sdah", "sdai", "sdaj", "sdak", "sdal", "sdam", "sdan", "sdao", "sdap", "sdaq", "sdar", "sdas", "sdat", "sdau", "sdav", "sdaw", "sdax", "sday", "sdaz",
|
|
|
|
"sdba", "sdbb", "sdbc", "sdbd", "sdbe", "sdbf", "sdbg", "sdbh", "sdbi", "sdbj", "sdbk", "sdbl", "sdbm", "sdbn", "sdbo", "sdbp", "sdbq", "sdbr", "sdbs", "sdbt", "sdbu", "sdbv", "sdbw", "sdbx", "sdby", "sdbz",
|
|
|
|
"sdca", "sdcb", "sdcc", "sdcd", "sdce", "sdcf", "sdcg", "sdch", "sdci", "sdcj", "sdck", "sdcl", "sdcm", "sdcn", "sdco", "sdcp", "sdcq", "sdcr", "sdcs", "sdct", "sdcu", "sdcv", "sdcw", "sdcx", "sdcy", "sdcz",
|
|
|
|
"sdda", "sddb", "sddc", "sddd", "sdde", "sddf", "sddg", "sddh", "sddi", "sddj", "sddk", "sddl", "sddm", "sddn", "sddo", "sddp", "sddq", "sddr", "sdds", "sddt", "sddu", "sddv", "sddw", "sddx", "sddy", "sddz",
|
|
|
|
"sdea", "sdeb", "sdec", "sded", "sdee", "sdef", "sdeg", "sdeh", "sdei", "sdej", "sdek", "sdel", "sdem", "sden", "sdeo", "sdep", "sdeq", "sder", "sdes", "sdet", "sdeu", "sdev", "sdew", "sdex", "sdey", "sdez",
|
|
|
|
"sdfa", "sdfb", "sdfc", "sdfd", "sdfe", "sdff", "sdfg", "sdfh", "sdfi", "sdfj", "sdfk", "sdfl", "sdfm", "sdfn", "sdfo", "sdfp", "sdfq", "sdfr", "sdfs", "sdft", "sdfu", "sdfv", "sdfw", "sdfx", "sdfy", "sdfz",
|
|
|
|
"sdga", "sdgb", "sdgc", "sdgd", "sdge", "sdgf", "sdgg", "sdgh", "sdgi", "sdgj", "sdgk", "sdgl", "sdgm", "sdgn", "sdgo", "sdgp", "sdgq", "sdgr", "sdgs", "sdgt", "sdgu", "sdgv", "sdgw", "sdgx", "sdgy", "sdgz",
|
|
|
|
"sdha", "sdhb", "sdhc", "sdhd", "sdhe", "sdhf", "sdhg", "sdhh", "sdhi", "sdhj", "sdhk", "sdhl", "sdhm", "sdhn", "sdho", "sdhp", "sdhq", "sdhr", "sdhs", "sdht", "sdhu", "sdhv", "sdhw", "sdhx", "sdhy", "sdhz",
|
|
|
|
"sdia", "sdib", "sdic", "sdid", "sdie", "sdif", "sdig", "sdih", "sdii", "sdij", "sdik", "sdil", "sdim", "sdin", "sdio", "sdip", "sdiq", "sdir", "sdis", "sdit", "sdiu", "sdiv", "sdiw", "sdix", "sdiy", "sdiz"
|
|
|
|
};
|
|
|
|
|
2015-11-13 13:14:44 +00:00
|
|
|
struct testDiskName
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int idx;
|
|
|
|
int partition;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct testDiskName diskNamesPart[] = {
|
|
|
|
{"sda0", 0, 0},
|
|
|
|
{"sdb10", 1, 10},
|
|
|
|
{"sdc2147483647", 2, 2147483647},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* diskNamesInvalid[] = {
|
2017-02-15 11:14:58 +00:00
|
|
|
"sda00", "sda01", "sdb-1",
|
|
|
|
"vd2"
|
2015-11-13 13:14:44 +00:00
|
|
|
};
|
|
|
|
|
2011-07-01 17:58:15 +00:00
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testIndexToDiskName(const void *data G_GNUC_UNUSED)
|
2011-07-01 17:58:15 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-07-01 17:58:15 +00:00
|
|
|
char *diskName = NULL;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(diskNames); ++i) {
|
2011-07-01 17:58:15 +00:00
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
diskName = virIndexToDiskName(i, "sd");
|
|
|
|
|
|
|
|
if (STRNEQ(diskNames[i], diskName)) {
|
2016-05-26 15:01:51 +00:00
|
|
|
virTestDifference(stderr, diskNames[i], diskName);
|
2011-07-01 17:58:15 +00:00
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testDiskNameToIndex(const void *data G_GNUC_UNUSED)
|
2011-07-01 17:58:15 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
|
|
|
int idx;
|
2011-07-01 17:58:15 +00:00
|
|
|
char *diskName = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < 100000; ++i) {
|
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
diskName = virIndexToDiskName(i, "sd");
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
idx = virDiskNameToIndex(diskName);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
if (idx < 0 || idx != i) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nExpect [%zu]", i);
|
|
|
|
VIR_TEST_DEBUG("Actual [%d]", idx);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(diskName);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-13 13:14:44 +00:00
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testDiskNameParse(const void *data G_GNUC_UNUSED)
|
2015-11-13 13:14:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int idx;
|
|
|
|
int partition;
|
|
|
|
struct testDiskName *disk = NULL;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(diskNamesPart); ++i) {
|
2015-11-13 13:14:44 +00:00
|
|
|
disk = &diskNamesPart[i];
|
|
|
|
if (virDiskNameParse(disk->name, &idx, &partition))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (disk->idx != idx) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nExpect [%d]", disk->idx);
|
|
|
|
VIR_TEST_DEBUG("Actual [%d]", idx);
|
2015-11-13 13:14:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disk->partition != partition) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nExpect [%d]", disk->partition);
|
|
|
|
VIR_TEST_DEBUG("Actual [%d]", partition);
|
2015-11-13 13:14:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(diskNamesInvalid); ++i) {
|
2015-11-13 13:14:44 +00:00
|
|
|
if (!virDiskNameParse(diskNamesInvalid[i], &idx, &partition)) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Should Fail [%s]", diskNamesInvalid[i]);
|
2015-11-13 13:14:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-01 17:58:15 +00:00
|
|
|
struct testVersionString
|
|
|
|
{
|
|
|
|
const char *string;
|
|
|
|
bool allowMissing;
|
|
|
|
int result;
|
|
|
|
unsigned long version;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct testVersionString versions[] = {
|
|
|
|
{ "2.6.38-8-generic", false, 0, 1000000 * 2 + 1000 * 6 + 38 },
|
|
|
|
{ "3.0-1-virtual", true, 0, 1000000 * 3 + 1000 * 0 + 0 },
|
|
|
|
{ "5", true, 0, 1000000 * 5 + 1000 * 0 + 0 },
|
|
|
|
{ "4.1.0", false, 0, 1000000 * 4 + 1000 * 1 + 0 },
|
|
|
|
{ "12.345.678", false, 0, 1000000 * 12 + 1000 * 345 + 678 },
|
|
|
|
{ "1.234.5678", false, -1, 0 },
|
|
|
|
{ "1.2345.678", false, -1, 0 },
|
|
|
|
{ "12345.6.78", false, -1, 0 },
|
|
|
|
{ "123456789", true, -1, 0 },
|
|
|
|
{ "3.0-2-virtual", false, -1, 0 },
|
|
|
|
{ "no-number-here", false, -1, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testParseVersionString(const void *data G_GNUC_UNUSED)
|
2011-07-01 17:58:15 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int result;
|
|
|
|
size_t i;
|
2011-07-01 17:58:15 +00:00
|
|
|
unsigned long version;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(versions); ++i) {
|
2011-07-01 17:58:15 +00:00
|
|
|
result = virParseVersionString(versions[i].string, &version,
|
|
|
|
versions[i].allowMissing);
|
|
|
|
|
|
|
|
if (result != versions[i].result) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nVersion string [%s]", versions[i].string);
|
|
|
|
VIR_TEST_DEBUG("Expect result [%d]", versions[i].result);
|
|
|
|
VIR_TEST_DEBUG("Actual result [%d]", result);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-13 14:20:43 +00:00
|
|
|
if (result < 0)
|
2011-07-01 17:58:15 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (version != versions[i].version) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nVersion string [%s]", versions[i].string);
|
|
|
|
VIR_TEST_DEBUG("Expect version [%lu]", versions[i].version);
|
|
|
|
VIR_TEST_DEBUG("Actual version [%lu]", version);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-18 16:13:38 +00:00
|
|
|
struct testRoundData {
|
|
|
|
unsigned int input;
|
|
|
|
unsigned int output;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct testRoundData roundData[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 1, 1 },
|
|
|
|
{ 1000, 1024 },
|
|
|
|
{ 1024, 1024 },
|
|
|
|
{ 1025, 2048 },
|
|
|
|
{ UINT_MAX, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testRoundValueToPowerOfTwo(const void *data G_GNUC_UNUSED)
|
2014-11-18 16:13:38 +00:00
|
|
|
{
|
|
|
|
unsigned int result;
|
|
|
|
size_t i;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(roundData); i++) {
|
2014-11-18 16:13:38 +00:00
|
|
|
result = VIR_ROUND_UP_POWER_OF_TWO(roundData[i].input);
|
|
|
|
if (roundData[i].output != result) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("\nInput number [%u]", roundData[i].input);
|
|
|
|
VIR_TEST_DEBUG("Expected number [%u]", roundData[i].output);
|
|
|
|
VIR_TEST_DEBUG("Actual number [%u]", result);
|
2014-11-18 16:13:38 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_OVERFLOW(var, val, expect) \
|
|
|
|
tmp = val; \
|
|
|
|
if (VIR_ASSIGN_IS_OVERFLOW(var, tmp) != expect) { \
|
|
|
|
fprintf(stderr, "\noverflow check failed: " \
|
|
|
|
"var: " #var " val: " #val "\n"); \
|
|
|
|
return -1; \
|
2015-05-27 08:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-10-14 12:45:03 +00:00
|
|
|
testOverflowCheckMacro(const void *data G_GNUC_UNUSED)
|
2015-05-27 08:54:38 +00:00
|
|
|
{
|
|
|
|
long long tmp;
|
2015-06-08 08:24:08 +00:00
|
|
|
uint8_t luchar;
|
|
|
|
int8_t lchar;
|
2015-05-27 08:54:38 +00:00
|
|
|
|
|
|
|
TEST_OVERFLOW(luchar, 254, false);
|
|
|
|
TEST_OVERFLOW(luchar, 255, false);
|
|
|
|
TEST_OVERFLOW(luchar, 256, true);
|
|
|
|
TEST_OVERFLOW(luchar, 767, true);
|
|
|
|
|
|
|
|
TEST_OVERFLOW(lchar, 127, false);
|
|
|
|
TEST_OVERFLOW(lchar, -128, false);
|
|
|
|
TEST_OVERFLOW(lchar, -129, true);
|
|
|
|
TEST_OVERFLOW(lchar, 128, true);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-18 16:13:38 +00:00
|
|
|
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
2016-05-26 15:02:00 +00:00
|
|
|
virTestQuiesceLibvirtErrors(true);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST(_name) \
|
|
|
|
do { \
|
|
|
|
if (virTestRun("Util "#_name, test##_name, \
|
|
|
|
NULL) < 0) { \
|
|
|
|
result = -1; \
|
|
|
|
} \
|
2011-07-01 17:58:15 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
DO_TEST(IndexToDiskName);
|
|
|
|
DO_TEST(DiskNameToIndex);
|
2015-11-13 13:14:44 +00:00
|
|
|
DO_TEST(DiskNameParse);
|
2011-07-01 17:58:15 +00:00
|
|
|
DO_TEST(ParseVersionString);
|
2014-11-18 16:13:38 +00:00
|
|
|
DO_TEST(RoundValueToPowerOfTwo);
|
2015-05-27 08:54:38 +00:00
|
|
|
DO_TEST(OverflowCheckMacro);
|
2011-07-01 17:58:15 +00:00
|
|
|
|
|
|
|
return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|