esxutilstest.c 10.9 KB
Newer Older
1 2 3 4
#include <config.h>

#ifdef WITH_ESX

5 6 7
# include <stdio.h>
# include <string.h>
# include <unistd.h>
8

9 10 11 12
# include "internal.h"
# include "memory.h"
# include "testutils.h"
# include "util.h"
13
# include "vmx/vmx.h"
14
# include "esx/esx_util.h"
15
# include "esx/esx_vi_types.h"
16 17 18

static char *progname;

M
Matthias Bolte 已提交
19 20 21 22 23 24 25 26 27 28


static void
testQuietError(void *userData ATTRIBUTE_UNUSED,
               virErrorPtr error ATTRIBUTE_UNUSED)
{
    /* nothing */
}


29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

static const char* names[] = {
    "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"
};

static int
testIndexToDiskName(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    char *name = NULL;

    for (i = 0; i < ARRAY_CARDINALITY(names); ++i) {
        VIR_FREE(name);

52
        name = virIndexToDiskName(i, "sd");
53 54 55 56 57 58 59

        if (STRNEQ(names[i], name)) {
            virtTestDifference(stderr, names[i], name);
            VIR_FREE(name);

            return -1;
        }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    }

    VIR_FREE(name);

    return 0;
}

static int
testDiskNameToIndex(const void *data ATTRIBUTE_UNUSED)
{
    int i, k;
    char *name = NULL;

    for (i = 0; i < 100000; ++i) {
        VIR_FREE(name);

        name = virIndexToDiskName(i, "sd");
        k = virDiskNameToIndex(name);

        if (k != i) {
80
            if (virTestGetDebug() > 0) {
81 82 83
                fprintf(stderr, "\nExpect [%d]\n", i);
                fprintf(stderr, "Actual [%d]\n", k);
            }
84 85 86 87 88 89 90 91 92 93 94 95

            VIR_FREE(name);

            return -1;
        }
    }

    VIR_FREE(name);

    return 0;
}

M
Matthias Bolte 已提交
96 97 98


struct testPath {
99
    const char *datastorePath;
M
Matthias Bolte 已提交
100 101 102
    int result;
    const char *datastoreName;
    const char *directoryName;
103
    const char *directoryAndFileName;
M
Matthias Bolte 已提交
104 105 106
};

static struct testPath paths[] = {
107 108 109 110 111 112 113
    { "[datastore] directory/file", 0, "datastore", "directory",
      "directory/file" },
    { "[datastore] directory1/directory2/file", 0, "datastore",
      "directory1/directory2", "directory1/directory2/file" },
    { "[datastore] file", 0, "datastore", "file", "file" },
    { "[datastore] directory/", 0, "datastore", "directory", "directory/" },
    { "[datastore]", 0, "datastore", "", "" },
M
Matthias Bolte 已提交
114 115 116 117 118
    { "[] directory/file", -1, NULL, NULL, NULL },
    { "directory/file", -1, NULL, NULL, NULL },
};

static int
119
testParseDatastorePath(const void *data ATTRIBUTE_UNUSED)
M
Matthias Bolte 已提交
120 121 122 123
{
    int i, result = 0;
    char *datastoreName = NULL;
    char *directoryName = NULL;
124
    char *directoryAndFileName = NULL;
M
Matthias Bolte 已提交
125 126 127 128

    for (i = 0; i < ARRAY_CARDINALITY(paths); ++i) {
        VIR_FREE(datastoreName);
        VIR_FREE(directoryName);
129
        VIR_FREE(directoryAndFileName);
M
Matthias Bolte 已提交
130

131 132 133
        if (esxUtil_ParseDatastorePath
             (paths[i].datastorePath, &datastoreName, &directoryName,
              &directoryAndFileName) != paths[i].result) {
M
Matthias Bolte 已提交
134 135 136 137 138 139 140 141 142 143 144 145
            goto failure;
        }

        if (paths[i].result < 0) {
            continue;
        }

        if (STRNEQ(paths[i].datastoreName, datastoreName)) {
            virtTestDifference(stderr, paths[i].datastoreName, datastoreName);
            goto failure;
        }

146
        if (STRNEQ(paths[i].directoryName, directoryName)) {
M
Matthias Bolte 已提交
147 148 149 150
            virtTestDifference(stderr, paths[i].directoryName, directoryName);
            goto failure;
        }

151 152 153
        if (STRNEQ(paths[i].directoryAndFileName, directoryAndFileName)) {
            virtTestDifference(stderr, paths[i].directoryAndFileName,
                               directoryAndFileName);
M
Matthias Bolte 已提交
154 155 156 157 158 159 160
            goto failure;
        }
    }

  cleanup:
    VIR_FREE(datastoreName);
    VIR_FREE(directoryName);
161
    VIR_FREE(directoryAndFileName);
M
Matthias Bolte 已提交
162 163 164 165 166 167 168 169 170 171 172

    return result;

  failure:
    result = -1;

    goto cleanup;
}



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
struct testDateTime {
    const char *dateTime;
    time_t calendarTime;
};

static struct testDateTime times[] = {
    /* different timezones */
    { "2010-04-08T05:45:11-07:00", 1270730711 },
    { "2010-04-08T07:45:11-05:00", 1270730711 },
    { "2010-04-08T12:45:11+00:00", 1270730711 },
    { "2010-04-08T14:45:11+02:00", 1270730711 },
    { "2010-04-08T22:15:11+09:30", 1270730711 },
    { "2010-04-09T01:30:11+12:45", 1270730711 },

    /* optional parts */
    { "2010-04-08T12:45:11Z", 1270730711 },
    { "2010-04-08T12:45:11", 1270730711 },
    { "-2010-04-08T14:45:11+02:00", 0 },
    { "2010-04-08T14:45:11.529576+02:00", 1270730711 },

    /* borders */
    { "1970-01-01T00:00:00+00:00", 0 },
    { "2038-01-19T03:14:07+00:00", 2147483647 },

    /* random */
    { "1999-08-02T01:19:55+02:00", 933549595 },
    { "2004-03-07T23:23:55+02:00", 1078694635 },
    { "1984-10-27T14:33:45+02:00", 467728425 },
    { "1970-01-12T16:11:04+02:00", 1001464 },
    { "2014-07-20T13:35:38+02:00", 1405856138 },
    { "2032-06-24T17:04:49+02:00", 1971702289 },
};

static int
testConvertDateTimeToCalendarTime(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    esxVI_DateTime dateTime;
    time_t calendarTime;

    for (i = 0; i < ARRAY_CARDINALITY(times); ++i) {
        dateTime.value = (char *)times[i].dateTime;

        if (esxVI_DateTime_ConvertToCalendarTime(&dateTime,
                                                 &calendarTime) < 0) {
            return -1;
        }

        if (times[i].calendarTime != calendarTime) {
            return -1;
        }
    }

    return 0;
}



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
struct testDatastoreItem {
    const char *string;
    const char *escaped;
};

static struct testDatastoreItem datastoreItems[] = {
    { "normal", "normal" },
    { /* "Aä1ö2ü3ß4#5~6!7§8/9%Z" */
      "A\303\2441\303\2662\303\2743\303\2374#5~6!7\302\2478/9%Z",
      "A+w6Q-1+w7Y-2+w7w-3+w58-4+Iw-5+fg-6+IQ-7+wqc-8+JQ-2f9+JQ-25Z" },
    { /* "Z~6!7§8/9%0#1\"2'3`4&A" */ "Z~6!7\302\2478/9%0#1\"2'3`4&A",
      "Z+fg-6+IQ-7+wqc-8+JQ-2f9+JQ-250+Iw-1_2'3+YA-4+Jg-A" },
    { /* "標準語" */ "\346\250\231\346\272\226\350\252\236", "+5qiZ5rqW6Kqe" },
    { "!\"#$%&'()*+,-./0123456789:;<=>?",
      "+IQ-_+IyQl-25+Jg-'()_+Kw-,-.+JQ-2f0123456789_+Ow-_+PQ-__" },
    { "A Z[\\]^_B", "A Z+WyU-5c+XV4-_B" },
    { "A`B@{|}~DEL", "A+YA-B+QHs-_+fX4-DEL" },
    { /* "hÀÁÂÃÄÅH" */ "h\303\200\303\201\303\202\303\203\303\204\303\205H",
      "h+w4DDgcOCw4PDhMOF-H" },
    { /* "A쿀Z" */ "A\354\277\200Z", "A+7L+A-Z" },
    { /* "!쿀A" */ "!\354\277\200A", "+Iey,gA-A" },
    { "~~~", "+fn5+" },
    { "~~~A", "+fn5+-A" },
    { "K%U/H\\Z", "K+JQ-25U+JQ-2fH+JQ-5cZ" },
    { "vvv<A\"B\"C>zzz", "vvv_A_B_C_zzz" },
};

static int
testEscapeDatastoreItem(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    char *escaped = NULL;

    for (i = 0; i < ARRAY_CARDINALITY(datastoreItems); ++i) {
        VIR_FREE(escaped);

        escaped = esxUtil_EscapeDatastoreItem(datastoreItems[i].string);

        if (escaped == NULL) {
            return -1;
        }

        if (STRNEQ(datastoreItems[i].escaped, escaped)) {
            VIR_FREE(escaped);
            return -1;
        }
    }

    return 0;
}



284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
struct testWindows1252ToUTF8 {
    const char *windows1252;
    const char *utf8;
};

static struct testWindows1252ToUTF8 windows1252ToUTF8[] = {
    { "normal", "normal" },
    { /* "A€Z" */ "A\200Z", "A\342\202\254Z" },
    { /* "Aä1ö2ü3ß4#5~6!7§8/9%Z" */ "A\3441\3662\3743\3374#5~6!7\2478/9%Z",
      "A\303\2441\303\2662\303\2743\303\2374#5~6!7\302\2478/9%Z" },
    { /* "hÀÁÂÃÄÅH" */ "h\300\301\302\303\304\305H",
      "h\303\200\303\201\303\202\303\203\303\204\303\205H" },
};

static int
testConvertWindows1252ToUTF8(const void *data ATTRIBUTE_UNUSED)
{
    int i;
    char *utf8 = NULL;

    for (i = 0; i < ARRAY_CARDINALITY(windows1252ToUTF8); ++i) {
        VIR_FREE(utf8);

307 308
        utf8 = virVMXConvertToUTF8("Windows-1252",
                                   windows1252ToUTF8[i].windows1252);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

        if (utf8 == NULL) {
            return -1;
        }

        if (STRNEQ(windows1252ToUTF8[i].utf8, utf8)) {
            VIR_FREE(utf8);
            return -1;
        }
    }

    return 0;
}



325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static int
mymain(int argc, char **argv)
{
    int result = 0;

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
        return EXIT_FAILURE;
    }

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
        return EXIT_FAILURE;
    }

M
Matthias Bolte 已提交
342 343
    virSetErrorFunc(NULL, testQuietError);

344
# define DO_TEST(_name)                                                       \
M
Matthias Bolte 已提交
345 346 347 348 349 350 351 352
        do {                                                                  \
            if (virtTestRun("VMware "#_name, 1, test##_name,                  \
                            NULL) < 0) {                                      \
                result = -1;                                                  \
            }                                                                 \
        } while (0)

    DO_TEST(IndexToDiskName);
353
    DO_TEST(DiskNameToIndex);
354
    DO_TEST(ParseDatastorePath);
355
    DO_TEST(ConvertDateTimeToCalendarTime);
356
    DO_TEST(EscapeDatastoreItem);
357
    DO_TEST(ConvertWindows1252ToUTF8);
358 359 360 361 362 363 364 365 366 367 368 369 370 371

    return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)

#else

int main (void)
{
    return 77; /* means 'test skipped' for automake */
}

#endif /* WITH_ESX */