2019-05-28 20:54:13 -05:00
|
|
|
// 28 may 2019
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
struct errorCase {
|
|
|
|
const char *name;
|
2019-06-02 00:36:53 -05:00
|
|
|
const char *file;
|
|
|
|
long line;
|
2019-05-28 20:54:13 -05:00
|
|
|
bool caught;
|
|
|
|
char *prefixGot;
|
|
|
|
bool internalGot;
|
|
|
|
char *msgGot;
|
|
|
|
const char *msgWant;
|
|
|
|
struct errorCase *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct errorCase *current = NULL;
|
2019-05-31 09:38:47 -05:00
|
|
|
static char *caseError = NULL;
|
|
|
|
static char caseErrorMemoryExhausted[] = "memory exhausted";
|
|
|
|
static char caseErrorEncodingError[] = "encoding error while handling other case error";
|
|
|
|
|
|
|
|
#define sharedbitsPrefix priv
|
|
|
|
#define sharedbitsStatic static
|
2019-05-31 21:07:51 -05:00
|
|
|
#ifdef _WIN32
|
2019-05-31 21:34:34 -05:00
|
|
|
// While we do only need strncpy(), our privInternalError() calls vsnprintf(), so include that too.
|
|
|
|
#include "../../sharedbits/strsafe_impl.h"
|
2019-05-31 09:38:47 -05:00
|
|
|
static void privInternalError(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap, ap2;
|
2019-05-31 10:17:11 -05:00
|
|
|
int n;
|
2019-05-31 09:38:47 -05:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
n = privVsnprintf(NULL, 0, fmt, ap2);
|
|
|
|
va_end(ap2);
|
|
|
|
if (n < 0) {
|
|
|
|
caseError = caseErrorEncodingError;
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
caseError = (char *) malloc((n + 1) * sizeof (char));
|
|
|
|
if (caseError == NULL) {
|
|
|
|
caseError = caseErrorMemoryExhausted;
|
|
|
|
va_end(ap);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-31 10:17:11 -05:00
|
|
|
privVsnprintf(caseError, n + 1, fmt, ap);
|
2019-05-31 09:38:47 -05:00
|
|
|
va_end(ap);
|
|
|
|
}
|
2019-05-31 21:07:51 -05:00
|
|
|
#else
|
2019-05-31 21:34:34 -05:00
|
|
|
#include "../../sharedbits/strsafe_strncpy_impl.h"
|
2019-05-31 21:07:51 -05:00
|
|
|
#endif
|
|
|
|
#undef sharedbitsStatic
|
|
|
|
#undef sharedbitsPrefix
|
2019-05-28 20:54:13 -05:00
|
|
|
|
|
|
|
static void catalogProgrammerError(const char *prefix, const char *msg, const char *suffix, bool internal)
|
|
|
|
{
|
2019-05-31 01:58:57 -05:00
|
|
|
size_t n;
|
|
|
|
|
2019-05-28 20:54:13 -05:00
|
|
|
current->caught = true;
|
|
|
|
if (strstr(prefix, "programmer error") == NULL) {
|
2019-05-31 01:58:57 -05:00
|
|
|
n = strlen(prefix);
|
|
|
|
current->prefixGot = (char *) malloc((n + 1) * sizeof (char));
|
2019-05-28 20:54:13 -05:00
|
|
|
if (current->prefixGot == NULL) {
|
2019-05-31 09:38:47 -05:00
|
|
|
caseError = caseErrorMemoryExhausted;
|
2019-05-28 20:54:13 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-05-31 09:38:47 -05:00
|
|
|
privStrncpy(current->prefixGot, prefix, n + 1);
|
|
|
|
if (caseError != NULL)
|
|
|
|
return;
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
current->internalGot = internal;
|
|
|
|
if (strstr(msg, current->msgWant) == NULL) {
|
2019-05-31 01:58:57 -05:00
|
|
|
n = strlen(msg);
|
|
|
|
current->msgGot = (char *) malloc((n + 1) * sizeof (char));
|
2019-05-28 20:54:13 -05:00
|
|
|
if (current->msgGot == NULL) {
|
2019-05-31 09:38:47 -05:00
|
|
|
caseError = caseErrorMemoryExhausted;
|
2019-05-28 20:54:13 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-05-31 09:38:47 -05:00
|
|
|
privStrncpy(current->msgGot, msg, n + 1);
|
|
|
|
if (caseError != NULL)
|
|
|
|
return;
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct errorCase *newCase(void)
|
|
|
|
{
|
|
|
|
struct errorCase *p;
|
|
|
|
|
|
|
|
p = (struct errorCase *) malloc(sizeof (struct errorCase));
|
|
|
|
if (p == NULL) {
|
2019-05-31 09:38:47 -05:00
|
|
|
caseError = caseErrorMemoryExhausted;
|
2019-05-28 20:54:13 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(p, 0, sizeof (struct errorCase));
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeCases(struct errorCase *first)
|
|
|
|
{
|
|
|
|
struct errorCase *p, *next;
|
|
|
|
|
|
|
|
p = first;
|
|
|
|
while (p != NULL) {
|
|
|
|
if (p->prefixGot != NULL)
|
|
|
|
free(p->prefixGot);
|
|
|
|
if (p->msgGot != NULL)
|
|
|
|
free(p->msgGot);
|
|
|
|
next = p->next;
|
|
|
|
free(p);
|
|
|
|
p = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportCases(testingT *t, struct errorCase *p)
|
|
|
|
{
|
|
|
|
while (p != NULL) {
|
2019-06-02 00:36:53 -05:00
|
|
|
testingTLogfFull(t, p->file, p->line, "*** %s", p->name);
|
2019-05-28 20:54:13 -05:00
|
|
|
if (!p->caught) {
|
2019-06-02 00:36:53 -05:00
|
|
|
testingTErrorfFull(t, p->file, p->line, "%s did not throw a programmer error; should have", p->name);
|
2019-05-28 20:54:13 -05:00
|
|
|
p = p->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p->prefixGot != NULL)
|
2019-06-02 00:36:53 -05:00
|
|
|
testingTErrorfFull(t, p->file, p->line, "%s prefix string doesn't contain \"programmer error\": %s", p->name, p->prefixGot);
|
2019-05-28 20:54:13 -05:00
|
|
|
if (p->internalGot)
|
2019-06-02 00:36:53 -05:00
|
|
|
testingTErrorfFull(t, p->file, p->line, "%s error is marked internal; should not have been", p->name);
|
2019-05-28 20:54:13 -05:00
|
|
|
if (p->msgGot != NULL)
|
2019-06-02 00:59:08 -05:00
|
|
|
testingTErrorfFull(t, p->file, p->line, "%s message doesn't contain expected substring:" diff("%s"),
|
2019-06-02 00:51:40 -05:00
|
|
|
p->name, p->msgGot, p->msgWant);
|
2019-05-28 20:54:13 -05:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define allcallsCase(f, ...) { \
|
|
|
|
current = newCase(); \
|
2019-05-31 09:38:47 -05:00
|
|
|
if (caseError != NULL) \
|
2019-05-28 20:54:13 -05:00
|
|
|
return first; \
|
|
|
|
current->name = #f "()"; \
|
2019-06-02 00:36:53 -05:00
|
|
|
current->file = __FILE__; \
|
|
|
|
current->line = __LINE__; \
|
2019-05-28 20:54:13 -05:00
|
|
|
current->msgWant = "attempt to call " #f "() " allcallsMsgSuffix; \
|
2019-05-28 21:53:40 -05:00
|
|
|
f(__VA_ARGS__); \
|
2019-05-28 20:54:13 -05:00
|
|
|
if (first == NULL) \
|
|
|
|
first = current; \
|
|
|
|
if (last != NULL) \
|
|
|
|
last->next = current; \
|
|
|
|
last = current; \
|
2019-05-31 09:38:47 -05:00
|
|
|
if (caseError != NULL) \
|
2019-05-28 20:54:13 -05:00
|
|
|
return first; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct errorCase *runCasesBeforeInit(void)
|
|
|
|
{
|
|
|
|
struct errorCase *first = NULL;
|
|
|
|
struct errorCase *last = NULL;
|
|
|
|
|
|
|
|
#define allcallsMsgSuffix "before uiInit()"
|
|
|
|
allcallsCase(uiQueueMain, NULL, NULL);
|
|
|
|
#include "allcalls.h"
|
|
|
|
#undef allcallsMsgSuffix
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
testingTestInSet(beforeTests, FunctionsFailBeforeInit)
|
|
|
|
{
|
|
|
|
struct errorCase *cases;
|
|
|
|
|
2019-05-31 09:38:47 -05:00
|
|
|
caseError = NULL;
|
2019-05-28 20:54:13 -05:00
|
|
|
uiprivTestHookReportProgrammerError(catalogProgrammerError);
|
|
|
|
cases = runCasesBeforeInit();
|
|
|
|
uiprivTestHookReportProgrammerError(NULL);
|
2019-05-31 09:38:47 -05:00
|
|
|
if (caseError != NULL) {
|
2019-05-28 20:54:13 -05:00
|
|
|
freeCases(cases);
|
2019-05-31 09:38:47 -05:00
|
|
|
testingTErrorf(t, "%s running tests", caseError);
|
|
|
|
if (caseError != caseErrorMemoryExhausted && caseError != caseErrorEncodingError)
|
|
|
|
free(caseError);
|
|
|
|
caseError = NULL;
|
2019-05-31 10:17:11 -05:00
|
|
|
testingTFailNow(t);
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
reportCases(t, cases);
|
|
|
|
freeCases(cases);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct errorCase *runCasesWrongThread(void)
|
|
|
|
{
|
|
|
|
struct errorCase *first = NULL;
|
|
|
|
struct errorCase *last = NULL;
|
|
|
|
|
|
|
|
#define allcallsMsgSuffix "on a thread other than the GUI thread"
|
|
|
|
#include "allcalls.h"
|
|
|
|
#undef allcallsMsgSuffix
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wrongThreadThreadProc(void *data)
|
|
|
|
{
|
|
|
|
struct errorCase **pCases = (struct errorCase **) data;
|
|
|
|
|
|
|
|
uiprivTestHookReportProgrammerError(catalogProgrammerError);
|
|
|
|
*pCases = runCasesWrongThread();
|
|
|
|
uiprivTestHookReportProgrammerError(NULL);
|
2019-05-31 09:38:47 -05:00
|
|
|
// do this now in case memory was exhausted and something gets allocated before we return to the main thread
|
|
|
|
if (caseError != NULL) {
|
2019-05-28 20:54:13 -05:00
|
|
|
freeCases(*pCases);
|
|
|
|
*pCases = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testingTest(FunctionsFailOnWrongThread)
|
|
|
|
{
|
|
|
|
struct errorCase *cases;
|
|
|
|
threadThread *thread;
|
|
|
|
threadSysError err;
|
|
|
|
|
2019-05-31 09:38:47 -05:00
|
|
|
caseError = NULL;
|
2019-05-28 20:54:13 -05:00
|
|
|
err = threadNewThread(wrongThreadThreadProc, &cases, &thread);
|
|
|
|
if (err != 0)
|
|
|
|
testingTFatalf(t, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
|
|
|
err = threadThreadWaitAndFree(thread);
|
|
|
|
if (err != 0)
|
|
|
|
testingTFatalf(t, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
2019-05-31 09:38:47 -05:00
|
|
|
if (caseError != NULL) {
|
|
|
|
freeCases(cases);
|
|
|
|
testingTErrorf(t, "%s running tests", caseError);
|
|
|
|
if (caseError != caseErrorMemoryExhausted && caseError != caseErrorEncodingError)
|
|
|
|
free(caseError);
|
|
|
|
caseError = NULL;
|
2019-05-31 10:17:11 -05:00
|
|
|
testingTFailNow(t);
|
2019-05-31 09:38:47 -05:00
|
|
|
}
|
2019-05-28 20:54:13 -05:00
|
|
|
reportCases(t, cases);
|
|
|
|
freeCases(cases);
|
|
|
|
}
|