summaryrefslogtreecommitdiff
path: root/test/unit/ApplicationRunner.c
blob: 54b48124fad0046f26d6139b3edd29d3dee83491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdio.h>
#include <stdarg.h>

#include "ApplicationRunner.h"
#include "base/File.h"
#include "base/PlatformInfo.h"
#include "analysis/AnalyzeFile.h"

const char *kDefaultTestOutputFileType = "pcm";
static const char *kApplicationRunnerOutputFolder = "out";
static const int kApplicationRunnerWaitTimeoutInMs = 1000;

CharString buildTestArgumentString(const char *arguments, ...)
{
    CharString formattedArguments;
    va_list argumentList;
    va_start(argumentList, arguments);
    formattedArguments = newCharStringWithCapacity(kCharStringLengthLong);
    vsnprintf(formattedArguments->data, formattedArguments->capacity, arguments, argumentList);
    va_end(argumentList);
    return formattedArguments;
}

CharString getTestResourceFilename(const char *resourcesPath, const char *resourceType, const char *resourceName)
{
    CharString filename = newCharString();
    snprintf(filename->data, filename->capacity, "%s%c%s%c%s",
             resourcesPath, PATH_DELIMITER, resourceType, PATH_DELIMITER, resourceName);
    return filename;
}

CharString getTestOutputFilename(const char *testName, const char *fileExtension)
{
    CharString filename = newCharString();
    char *space;
    char *spacePtr;

    snprintf(filename->data, filename->capacity, "%s%c%s.%s",
             kApplicationRunnerOutputFolder, PATH_DELIMITER, testName, fileExtension);
    spacePtr = filename->data;

    do {
        space = strchr(spacePtr + 1, ' ');

        if (space == NULL || (unsigned int)(space - filename->data) > strlen(filename->data)) {
            break;
        } else {
            *space = '-';
        }
    } while (true);

    return filename;
}

static CharString _getTestPluginResourcesPath(const char *resourcesPath)
{
    CharString pluginRoot = newCharString();
    PlatformInfo platform = newPlatformInfo();
    snprintf(pluginRoot->data, pluginRoot->capacity, "%s%cvst%c%s",
             resourcesPath, PATH_DELIMITER, PATH_DELIMITER, platform->shortName->data);
    freePlatformInfo(platform);
    return pluginRoot;
}

static CharString _getDefaultArguments(TestEnvironment testEnvironment, const char *testName, const char *outputFilename)
{
    CharString outString = newCharStringWithCapacity(kCharStringLengthLong);
    CharString logfileName = getTestOutputFilename(testName, "txt");
    CharString resourcesPath = _getTestPluginResourcesPath(testEnvironment->resourcesPath);
    snprintf(outString->data, outString->capacity,
             "--log-file \"%s\" --verbose --output \"%s\" --plugin-root \"%s\"",
             logfileName->data, outputFilename, resourcesPath->data);
    freeCharString(logfileName);
    freeCharString(resourcesPath);
    return outString;
}

static void _removeOutputFile(const char *argument)
{
    CharString outputFilename = newCharStringWithCString(argument);
    File outputFile = newFileWithPath(outputFilename);

    if (fileExists(outputFile)) {
        fileRemove(outputFile);
    }

    freeCharString(outputFilename);
    freeFile(outputFile);
}

static void _removeOutputFiles(const char *testName)
{
    // Remove all possible output files generated during testing
    CharString outputFilename;

    outputFilename = getTestOutputFilename(testName, "aif");
    _removeOutputFile(outputFilename->data);
    freeCharString(outputFilename);
    outputFilename = getTestOutputFilename(testName, "flac");
    _removeOutputFile(outputFilename->data);
    freeCharString(outputFilename);
    outputFilename = getTestOutputFilename(testName, "pcm");
    _removeOutputFile(outputFilename->data);
    freeCharString(outputFilename);
    outputFilename = getTestOutputFilename(testName, "wav");
    _removeOutputFile(outputFilename->data);
    freeCharString(outputFilename);
    outputFilename = getTestOutputFilename(testName, "txt");
    _removeOutputFile(outputFilename->data);
    freeCharString(outputFilename);
}

static const char *_getResultCodeString(const int resultCode)
{
    switch (resultCode) {
    case RETURN_CODE_SUCCESS:
        return "Success";

    case RETURN_CODE_NOT_RUN:
        return "Not run";

    case RETURN_CODE_INVALID_ARGUMENT:
        return "Invalid argument";

    case RETURN_CODE_MISSING_REQUIRED_OPTION:
        return "Missing required option";

    case RETURN_CODE_IO_ERROR:
        return "I/O error";

    case RETURN_CODE_PLUGIN_ERROR:
        return "Plugin error";

    case RETURN_CODE_INVALID_PLUGIN_CHAIN:
        return "Invalid plugin chain";

    case RETURN_CODE_UNSUPPORTED_FEATURE:
        return "Unsupported feature";

    case RETURN_CODE_INTERNAL_ERROR:
        return "Internal error";

    case RETURN_CODE_SIGNAL:
        return "Caught signal";

    default:
        return "Unknown";
    }
}

void runIntegrationTest(const TestEnvironment testEnvironment,
                        const char *testName, CharString testArguments,
                        ReturnCodes expectedResultCode, const char *outputFileType)
{
    int result = -1;
    ReturnCodes resultCode = (ReturnCodes)result;
    CharString arguments = newCharStringWithCapacity(kCharStringLengthLong);
    CharString defaultArguments;
    CharString failedAnalysisFunctionName = newCharString();
    ChannelCount failedAnalysisChannel;
    SampleCount failedAnalysisFrame;
    File outputFolder = NULL;
    CharString outputFilename = getTestOutputFilename(testName,
                                outputFileType == NULL ? kDefaultTestOutputFileType : outputFileType);

#if WINDOWS
    STARTUPINFOA startupInfo;
    PROCESS_INFORMATION  processInfo;
#endif

    // Remove files from a previous test run
    outputFolder = newFileWithPathCString(kApplicationRunnerOutputFolder);

    if (fileExists(outputFolder)) {
        _removeOutputFiles(testName);
    } else {
        fileCreate(outputFolder, kFileTypeDirectory);
    }

    // Create the command line argument
    charStringAppendCString(arguments, "\"");
    charStringAppendCString(arguments, testEnvironment->applicationPath);
    charStringAppendCString(arguments, "\"");
    charStringAppendCString(arguments, " ");
    defaultArguments = _getDefaultArguments(testEnvironment, testName, outputFilename->data);
    charStringAppend(arguments, defaultArguments);
    charStringAppendCString(arguments, " ");
    charStringAppend(arguments, testArguments);

    if (!testEnvironment->results->onlyPrintFailing) {
        printTestName(testName);
    }

#if WINDOWS
    memset(&startupInfo, 0, sizeof(startupInfo));
    memset(&processInfo, 0, sizeof(processInfo));
    startupInfo.cb = sizeof(startupInfo);
    result = CreateProcessA((LPCSTR)(testEnvironment->applicationPath), (LPSTR)(arguments->data),
                            0, 0, false, CREATE_DEFAULT_ERROR_MODE, 0, 0, &startupInfo, &processInfo);

    if (result) {
        // TODO: Check return codes for these calls
        WaitForSingleObject(processInfo.hProcess, kApplicationRunnerWaitTimeoutInMs);
        GetExitCodeProcess(processInfo.hProcess, (LPDWORD)&resultCode);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    } else {
        logCritical("Could not launch process, got error %s", stringForLastError(GetLastError()));
        return;
    }

#else
    result = system(arguments->data);
    resultCode = (ReturnCodes)WEXITSTATUS(result);
#endif

    if (resultCode == RETURN_CODE_FORK_FAILED ||
            resultCode == RETURN_CODE_SHELL_FAILED ||
            resultCode == RETURN_CODE_LAUNCH_FAILED_OTHER) {
        if (testEnvironment->results->onlyPrintFailing) {
            printTestName(testName);
        }

        printTestFail();
        logCritical("Could not launch shell, got return code %d\n\
Please check the executable path specified in the --mrswatson-path argument.",
                    resultCode);
        testEnvironment->results->numFail++;
    } else if (resultCode == expectedResultCode) {
        if (outputFileType != NULL) {
            if (analyzeFile(outputFilename->data, failedAnalysisFunctionName,
                            &failedAnalysisChannel, &failedAnalysisFrame)) {
                testEnvironment->results->numSuccess++;

                if (!testEnvironment->results->keepFiles) {
                    _removeOutputFiles(testName);
                }

                if (!testEnvironment->results->onlyPrintFailing) {
                    printTestSuccess();
                }
            } else {
                if (testEnvironment->results->onlyPrintFailing) {
                    printTestName(testName);
                }

                fprintf(stderr, "Audio analysis check for %s failed at channel %d, frame %lu. ",
                        failedAnalysisFunctionName->data, failedAnalysisChannel, failedAnalysisFrame);
                printTestFail();
                testEnvironment->results->numFail++;
            }
        } else {
            testEnvironment->results->numSuccess++;

            if (!testEnvironment->results->keepFiles) {
                _removeOutputFiles(testName);
            }

            if (!testEnvironment->results->onlyPrintFailing) {
                printTestSuccess();
            }
        }
    } else {
        if (testEnvironment->results->onlyPrintFailing) {
            printTestName(testName);
        }

        fprintf(stderr, "Expected result code %d (%s), got %d (%s). ",
                expectedResultCode, _getResultCodeString(expectedResultCode),
                resultCode, _getResultCodeString(resultCode));
        printTestFail();
        testEnvironment->results->numFail++;
    }

    freeCharString(outputFilename);
    freeCharString(arguments);
    freeCharString(defaultArguments);
    freeCharString(testArguments);
    freeCharString(failedAnalysisFunctionName);
}

void freeTestEnvironment(TestEnvironment self)
{
    if (self != NULL) {
        freeTestSuite(self->results);
        free(self);
    }
}

TestEnvironment newTestEnvironment(char *applicationPath, char *resourcesPath)
{
    TestEnvironment testEnvironment = (TestEnvironment)malloc(sizeof(TestEnvironmentMembers));
    testEnvironment->applicationPath = applicationPath;
    testEnvironment->resourcesPath = resourcesPath;
    testEnvironment->results = newTestSuite("Results", NULL, NULL);
    return testEnvironment;
}