2019-04-23 14:02:30 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Harness for Loading Test Configurations
|
|
|
|
***********************************************************************************************************************************/
|
2019-09-06 13:48:28 -04:00
|
|
|
#include <string.h>
|
2019-04-23 14:02:30 -04:00
|
|
|
|
2019-09-06 13:48:28 -04:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/crypto/hash.h"
|
|
|
|
#include "common/io/bufferRead.h"
|
|
|
|
#include "common/io/filter/filter.intern.h"
|
2019-04-23 14:02:30 -04:00
|
|
|
#include "common/type/json.h"
|
|
|
|
#include "info/info.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
2019-09-06 13:48:28 -04:00
|
|
|
#include "common/harnessDebug.h"
|
|
|
|
#include "common/harnessInfo.h"
|
|
|
|
|
2019-04-23 14:02:30 -04:00
|
|
|
/***********************************************************************************************************************************
|
2019-09-06 13:48:28 -04:00
|
|
|
Add header and checksum to an info file
|
2019-04-23 14:02:30 -04:00
|
|
|
|
2019-09-06 13:48:28 -04:00
|
|
|
This prevents churn in headers and checksums in the unit tests. We purposefully do not use the checksum macros from the info module
|
|
|
|
here as a cross-check of that code.
|
2019-04-23 14:02:30 -04:00
|
|
|
***********************************************************************************************************************************/
|
2019-09-06 13:48:28 -04:00
|
|
|
typedef struct HarnessInfoChecksumData
|
|
|
|
{
|
|
|
|
MemContext *memContext; // Mem context to use for storing data in this structure
|
|
|
|
String *sectionLast; // The last section seen during load
|
|
|
|
IoFilter *checksum; // Checksum calculated from the file
|
|
|
|
} HarnessInfoChecksumData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
harnessInfoChecksumCallback(void *callbackData, const String *section, const String *key, const String *value)
|
|
|
|
{
|
|
|
|
HarnessInfoChecksumData *data = (HarnessInfoChecksumData *)callbackData;
|
|
|
|
|
|
|
|
// Calculate checksum
|
|
|
|
if (data->sectionLast == NULL || !strEq(section, data->sectionLast))
|
|
|
|
{
|
|
|
|
if (data->sectionLast != NULL)
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTRDEF("},"));
|
|
|
|
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTRDEF("\""));
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTR(section));
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTRDEF("\":{"));
|
|
|
|
|
|
|
|
MEM_CONTEXT_BEGIN(data->memContext)
|
|
|
|
{
|
|
|
|
data->sectionLast = strDup(section);
|
|
|
|
}
|
|
|
|
MEM_CONTEXT_END();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTRDEF(","));
|
|
|
|
|
2019-12-13 21:33:13 -05:00
|
|
|
ioFilterProcessIn(data->checksum, BUFSTR(jsonFromStr(key)));
|
|
|
|
ioFilterProcessIn(data->checksum, BUFSTRDEF(":"));
|
2019-09-06 13:48:28 -04:00
|
|
|
ioFilterProcessIn(data->checksum, BUFSTR(value));
|
|
|
|
}
|
|
|
|
|
2019-04-23 14:02:30 -04:00
|
|
|
Buffer *
|
|
|
|
harnessInfoChecksum(const String *info)
|
|
|
|
{
|
|
|
|
FUNCTION_HARNESS_BEGIN();
|
|
|
|
FUNCTION_HARNESS_PARAM(STRING, info);
|
|
|
|
FUNCTION_HARNESS_END();
|
|
|
|
|
|
|
|
Buffer *result = NULL;
|
|
|
|
|
|
|
|
MEM_CONTEXT_TEMP_BEGIN()
|
|
|
|
{
|
2019-09-06 13:48:28 -04:00
|
|
|
// Initialize callback data
|
|
|
|
HarnessInfoChecksumData data =
|
|
|
|
{
|
|
|
|
.memContext = MEM_CONTEXT_TEMP(),
|
|
|
|
.checksum = cryptoHashNew(HASH_TYPE_SHA1_STR),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create buffer with space for data, header, and checksum
|
|
|
|
result = bufNew(strSize(info) + 256);
|
|
|
|
|
|
|
|
bufCat(result, BUFSTRDEF("[backrest]\nbackrest-format="));
|
|
|
|
bufCat(result, BUFSTR(jsonFromUInt(REPOSITORY_FORMAT)));
|
|
|
|
bufCat(result, BUFSTRDEF("\nbackrest-version="));
|
|
|
|
bufCat(result, BUFSTR(jsonFromStr(STRDEF(PROJECT_VERSION))));
|
|
|
|
bufCat(result, BUFSTRDEF("\n\n"));
|
|
|
|
bufCat(result, BUFSTR(info));
|
|
|
|
|
|
|
|
// Generate checksum by loading ini file
|
|
|
|
ioFilterProcessIn(data.checksum, BUFSTRDEF("{"));
|
|
|
|
iniLoad(ioBufferReadNew(result), harnessInfoChecksumCallback, &data);
|
|
|
|
ioFilterProcessIn(data.checksum, BUFSTRDEF("}}"));
|
|
|
|
|
|
|
|
// Append checksum to buffer
|
|
|
|
bufCat(result, BUFSTRDEF("\n[backrest]\nbackrest-checksum="));
|
2019-10-11 13:03:52 -04:00
|
|
|
bufCat(result, BUFSTR(jsonFromVar(ioFilterResult(data.checksum))));
|
2019-09-06 13:48:28 -04:00
|
|
|
bufCat(result, BUFSTRDEF("\n"));
|
|
|
|
|
2020-01-17 13:29:49 -07:00
|
|
|
bufMove(result, memContextPrior());
|
2019-04-23 14:02:30 -04:00
|
|
|
}
|
|
|
|
MEM_CONTEXT_TEMP_END();
|
|
|
|
|
|
|
|
FUNCTION_HARNESS_RESULT(BUFFER, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer *
|
|
|
|
harnessInfoChecksumZ(const char *info)
|
|
|
|
{
|
|
|
|
FUNCTION_HARNESS_BEGIN();
|
|
|
|
FUNCTION_HARNESS_PARAM(STRINGZ, info);
|
|
|
|
FUNCTION_HARNESS_END();
|
|
|
|
|
2019-09-06 13:48:28 -04:00
|
|
|
FUNCTION_HARNESS_RESULT(BUFFER, harnessInfoChecksum(STR(info)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test callback that logs the results to a string
|
|
|
|
***********************************************************************************************************************************/
|
|
|
|
void
|
|
|
|
harnessInfoLoadNewCallback(void *callbackData, const String *section, const String *key, const String *value)
|
|
|
|
{
|
|
|
|
if (callbackData != NULL)
|
|
|
|
strCatFmt((String *)callbackData, "[%s] %s=%s\n", strPtr(section), strPtr(key), strPtr(value));
|
2019-04-23 14:02:30 -04:00
|
|
|
}
|