static int testFirewallSingleGroup(const void *opaque) { virBuffer cmdbuf = VIR_BUFFER_INITIALIZER; virFirewallPtr fw = NULL; int ret = -1; const char *actual = NULL; const char *expected = IPTABLES_PATH " -A INPUT --source-host 192.168.122.1 --jump ACCEPT\n" IPTABLES_PATH " -A INPUT --source-host '!192.168.122.1' --jump REJECT\n"; const struct testFirewallData *data = opaque; fwDisabled = data->fwDisabled; if (virFirewallSetBackend(data->tryBackend) < 0) goto cleanup; if (data->expectBackend == VIR_FIREWALL_BACKEND_DIRECT) virCommandSetDryRun(&cmdbuf, NULL, NULL); else fwBuf = &cmdbuf; fw = virFirewallNew(); virFirewallStartTransaction(fw, 0); virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4, "-A", "INPUT", "--source-host", "192.168.122.1", "--jump", "ACCEPT", NULL); virFirewallAddRule(fw, VIR_FIREWALL_LAYER_IPV4, "-A", "INPUT", "--source-host", "!192.168.122.1", "--jump", "REJECT", NULL); if (virFirewallApply(fw) < 0) goto cleanup; if (virBufferError(&cmdbuf)) goto cleanup; actual = virBufferCurrentContent(&cmdbuf); if (STRNEQ_NULLABLE(expected, actual)) { fprintf(stderr, "Unexected command execution\n"); virTestDifference(stderr, expected, actual); goto cleanup; } ret = 0; cleanup: virBufferFreeAndReset(&cmdbuf); fwBuf = NULL; virCommandSetDryRun(NULL, NULL, NULL); virFirewallFree(fw); return ret; }
static int checkoutput(const char *testname, char *prefix) { int ret = -1; char *expectname = NULL; char *expectlog = NULL; char *actualname = NULL; char *actuallog = NULL; if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir, testname) < 0) goto cleanup; if (virAsprintf(&actualname, "%s/commandhelper.log", abs_builddir) < 0) goto cleanup; if (virFileReadAll(expectname, 1024*64, &expectlog) < 0) { fprintf(stderr, "cannot read %s\n", expectname); goto cleanup; } if (virFileReadAll(actualname, 1024*64, &actuallog) < 0) { fprintf(stderr, "cannot read %s\n", actualname); goto cleanup; } if (prefix) { char *tmp = NULL; if (virAsprintf(&tmp, "%s%s", prefix, expectlog) < 0) goto cleanup; VIR_FREE(expectlog); expectlog = tmp; } if (STRNEQ(expectlog, actuallog)) { virTestDifference(stderr, expectlog, actuallog); goto cleanup; } ret = 0; cleanup: if (actualname) unlink(actualname); VIR_FREE(actuallog); VIR_FREE(actualname); VIR_FREE(expectlog); VIR_FREE(expectname); return ret; }
static int testCompareXMLtoXMLFiles(const char *xml) { char *xmlData = NULL; char *actual = NULL; char *pathResult = NULL; int ret = -1; virVBoxSnapshotConfMachinePtr machine = NULL; if (VIR_STRDUP(pathResult, abs_builddir "/vboxsnapshotxmldata/testResult.vbox") < 0) return -1; if (virFileMakePath(abs_builddir "/vboxsnapshotxmldata") < 0) goto cleanup; if (virTestLoadFile(xml, &xmlData) < 0) goto cleanup; if (!(machine = virVBoxSnapshotConfLoadVboxFile(xml, (char*)""))) goto cleanup; if (virVBoxSnapshotConfSaveVboxFile(machine, pathResult) < 0) goto cleanup; if (virTestLoadFile(pathResult, &actual) < 0) goto cleanup; if (!(actual = testFilterXML(actual))) goto cleanup; if (!(xmlData = testFilterXML(xmlData))) goto cleanup; if (STRNEQ(actual, xmlData)) { virTestDifference(stderr, xmlData, actual); goto cleanup; } ret = 0; cleanup: unlink(pathResult); rmdir(abs_builddir "/vboxsnapshotxmldata"); VIR_FREE(xmlData); VIR_FREE(actual); virVBoxSnapshotConfMachineFree(machine); VIR_FREE(pathResult); return ret; }
static int testJSONFromString(const void *data) { const struct testInfo *info = data; virJSONValuePtr json; const char *expectstr = info->expect ? info->expect : info->doc; char *formatted = NULL; int ret = -1; json = virJSONValueFromString(info->doc); if (!json) { if (info->pass) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); goto cleanup; } else { VIR_TEST_DEBUG("Fail to parse %s\n", info->doc); ret = 0; goto cleanup; } } if (!info->pass) { VIR_TEST_VERBOSE("Should not have parsed %s\n", info->doc); goto cleanup; } VIR_TEST_DEBUG("Parsed %s\n", info->doc); if (!(formatted = virJSONValueToString(json, false))) { VIR_TEST_VERBOSE("Failed to format json data\n"); goto cleanup; } if (STRNEQ(expectstr, formatted)) { virTestDifference(stderr, expectstr, formatted); goto cleanup; } ret = 0; cleanup: VIR_FREE(formatted); virJSONValueFree(json); return ret; }
static int testVirNetDevBandwidthSet(const void *data) { int ret = -1; const struct testSetStruct *info = data; const char *iface = info->iface; virNetDevBandwidthPtr band = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; char *actual_cmd = NULL; PARSE(info->band, band); if (!iface) iface = "eth0"; virCommandSetDryRun(&buf, NULL, NULL); if (virNetDevBandwidthSet(iface, band, info->hierarchical_class) < 0) goto cleanup; if (!(actual_cmd = virBufferContentAndReset(&buf))) { int err = virBufferError(&buf); if (err) { fprintf(stderr, "buffer's in error state: %d", err); goto cleanup; } /* This is interesting, no command has been executed. * Maybe that's expected, actually. */ } if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) { virTestDifference(stderr, NULLSTR(info->exp_cmd), NULLSTR(actual_cmd)); goto cleanup; } ret = 0; cleanup: virCommandSetDryRun(NULL, NULL, NULL); virNetDevBandwidthFree(band); virBufferFreeAndReset(&buf); VIR_FREE(actual_cmd); return ret; }
static int testJSONAddRemove(const void *data) { const struct testInfo *info = data; virJSONValuePtr json; virJSONValuePtr name = NULL; char *result = NULL; int ret = -1; json = virJSONValueFromString(info->doc); if (!json) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->name); ret = -1; goto cleanup; } switch (virJSONValueObjectRemoveKey(json, "name", &name)) { case 1: if (!info->pass) { VIR_TEST_VERBOSE("should not remove from non-object %s\n", info->name); goto cleanup; } break; case -1: if (!info->pass) ret = 0; else VIR_TEST_VERBOSE("Fail to recognize non-object %s\n", info->name); goto cleanup; default: VIR_TEST_VERBOSE("unexpected result when removing from %s\n", info->name); goto cleanup; } if (STRNEQ_NULLABLE(virJSONValueGetString(name), "sample")) { VIR_TEST_VERBOSE("unexpected value after removing name: %s\n", NULLSTR(virJSONValueGetString(name))); goto cleanup; } if (virJSONValueObjectRemoveKey(json, "name", NULL)) { VIR_TEST_VERBOSE("%s", "unexpected success when removing missing key\n"); goto cleanup; } if (virJSONValueObjectAppendString(json, "newname", "foo") < 0) { VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n"); goto cleanup; } if (!(result = virJSONValueToString(json, false))) { VIR_TEST_VERBOSE("%s", "failed to stringize result\n"); goto cleanup; } if (STRNEQ(info->expect, result)) { virTestDifference(stderr, info->expect, result); goto cleanup; } ret = 0; cleanup: virJSONValueFree(json); virJSONValueFree(name); VIR_FREE(result); return ret; }
static int testJSONCopy(const void *data) { const struct testInfo *info = data; virJSONValuePtr json = NULL; virJSONValuePtr jsonCopy = NULL; char *result = NULL; char *resultCopy = NULL; int ret = -1; json = virJSONValueFromString(info->doc); if (!json) { VIR_TEST_VERBOSE("Failed to parse %s\n", info->doc); goto cleanup; } jsonCopy = virJSONValueCopy(json); if (!jsonCopy) { VIR_TEST_VERBOSE("Failed to copy JSON data\n"); goto cleanup; } result = virJSONValueToString(json, false); if (!result) { VIR_TEST_VERBOSE("Failed to format original JSON data\n"); goto cleanup; } resultCopy = virJSONValueToString(json, false); if (!resultCopy) { VIR_TEST_VERBOSE("Failed to format copied JSON data\n"); goto cleanup; } if (STRNEQ(result, resultCopy)) { if (virTestGetVerbose()) virTestDifference(stderr, result, resultCopy); goto cleanup; } VIR_FREE(result); VIR_FREE(resultCopy); result = virJSONValueToString(json, true); if (!result) { VIR_TEST_VERBOSE("Failed to format original JSON data\n"); goto cleanup; } resultCopy = virJSONValueToString(json, true); if (!resultCopy) { VIR_TEST_VERBOSE("Failed to format copied JSON data\n"); goto cleanup; } if (STRNEQ(result, resultCopy)) { if (virTestGetVerbose()) virTestDifference(stderr, result, resultCopy); goto cleanup; } ret = 0; cleanup: VIR_FREE(result); VIR_FREE(resultCopy); virJSONValueFree(json); virJSONValueFree(jsonCopy); return ret; }