aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-cslul/unittest/test_build.c
blob: cc6320ef9c84778d7f3735d3d811e7338283a04b (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

/*

  Unit tests of build.c

  Copyright © 2021-2024 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "../build.c"
#include "unittest.h"
#include "alltests.h"
#include "testcommon.h"

#include <stdlib.h>


static struct CSlulConfig *cfg;
static enum CSlulErrorCode expected_error;
static int error_reported;

static void msghandler(const struct CSlulState *state)
{
    if (state->errorcode == expected_error) {
        error_reported = 1;
    } else {
        char s[200];
        sprintf(s, "Unexpected error %d was reported at %d.%d.",
                (int)state->errorcode,
                state->locs[0].line, state->locs[0].column);
        tsoftfail(s);
        if (!quiet) {
            tprintf("- error description: %-40s\n",
                    cslul_lookup_message(state->errorcode));
        }
    }
}

#define DUMMYFILE ((CSlulFile)111)
#define OUTPUTFILE ((CSlulFile)222)

static const char *expected_name;
static size_t expected_nmemb;
static CSlulFile ret_fopen;
static int ret_ferror;
static size_t ret_fread;
static int fclose_called, remove_called;
static const char *mocked_data;

static CSlulFile mocked_fopen(const char *name, const char *mode)
{
    tsoftassert(!strcmp(expected_name, name));
    tsoftassert(!strcmp("rb", mode));
    return ret_fopen;
}

static CSlulFile mocked_createexec(const char *filename)
{
    tsoftassert(filename != NULL);
    return OUTPUTFILE;
}

static int mocked_fclose(CSlulFile file)
{
    tsoftassert(file == DUMMYFILE || file == OUTPUTFILE);
    fclose_called = 1;
    return 0;
}

static int mocked_ferror(CSlulFile file)
{
    tsoftassert(file == DUMMYFILE);
    return ret_ferror;
}

static int mocked_remove(const char *filename)
{
    tsoftassert(filename != NULL);
    remove_called = 1;
    return 0;
}

static size_t mocked_fread(void *buffer, size_t size, size_t nmemb,
                           CSlulFile file)
{
    size_t num_read;
    tassert(buffer != NULL);
    tsoftassert(size == 1);
    tsoftassert(nmemb == expected_nmemb);
    tsoftassert(file == DUMMYFILE);

    num_read = ret_fread;
    if (num_read > nmemb) { /* handle large buffers */
        num_read = nmemb;
    }
    ret_fread -= num_read;
    if (mocked_data) {
        memcpy(buffer, mocked_data, num_read);
        mocked_data += num_read;
    }
    return num_read;
}

static size_t mocked_fwrite(const void *buffer, size_t size, size_t nmemb, CSlulFile file)
{
    tsoftassert(buffer != NULL);
    tsoftassert(size == 1);
    tsoftassert(nmemb >= 1);
    tsoftassert(file == OUTPUTFILE);
    return nmemb;
}

static struct CSlul *create_ctx(void)
{
    static const struct CSlulInitParams prm = {
        sizeof(struct CSlulInitParams), '#', /* using # as dirsep */
        NULL, NULL, NULL, /* malloc, free, realloc */
        mocked_fopen, mocked_createexec, mocked_fclose, mocked_ferror,
        mocked_remove, mocked_fwrite, mocked_fread,
        NULL, NULL /* mkdir, dropprivs */
    };
    struct CSlul *ctx;

    cfg = tcfg(cslul_config_create(&prm));
    tassert(cfg != NULL);
    cslul_config_set_message_handler(cfg, msghandler);
    cslul_config_add_arch(cfg, "aarch64-linux-gnu");
    cfg->skip_implicit_slulrt_dep = 1; /* We're not testing the runtime */

    ctx = tctx(cslul_create(cfg));
    tassert(ctx != NULL);
    /* We're not testing the runtime. Skip the check for SlulApp.main */
    ctx->has_app_main = 1;
    return ctx;
}

static void before_test(void)
{
    error_reported = 0;
    fclose_called = 0;
    remove_called = 0;
}

static void test_fopen_failure(void)
{
    struct CSlul *ctx = create_ctx();
    ret_fopen = NULL;
    expected_error = CSLUL_E_OPENFILE;

    expected_name = "main.slul";
    tassert(!cslul_build(ctx, NULL));
    tassert(error_reported);
    tassert(!fclose_called);
    tassert(!remove_called);

    expected_name = "some_dir#main.slul";
    before_test();
    tassert(!cslul_build(ctx, "some_dir"));
    tassert(error_reported);
    tassert(!fclose_called);
    tassert(!remove_called);
    cslul_free(ctx);
    cslul_config_free(cfg);
}

static void test_fread_failure(void)
{
    struct CSlul *ctx = create_ctx();
    expected_name = "main.slul";
    ret_fopen = DUMMYFILE;
    expected_nmemb = PARSEBUFFER;
    ret_fread = 0;
    ret_ferror = 1;
    expected_error = CSLUL_E_READFILE;
    mocked_data = NULL;

    tassert(!cslul_build(ctx, NULL));
    tassert(error_reported);
    tassert(fclose_called);
    tassert(!remove_called);
    cslul_free(ctx);
    cslul_config_free(cfg);
}

static void test_deletion_on_error(void)
{
    static const char source[] =
        "\\slul 0.0.0\n"
        "\\name test\n"
        "\n"
        "type type\n";
    struct CSlul *ctx = create_ctx();
    expected_name = "main.slul";
    ret_fopen = DUMMYFILE;
    expected_nmemb = PARSEBUFFER;
    ret_fread = sizeof(source)-1;
    ret_ferror = 0;
    expected_error = CSLUL_E_IDENTIFIEREXPECTED;
    mocked_data = source;

    tassert(!cslul_build(ctx, NULL));
    tassert(error_reported);
    tassert(fclose_called);
    tassert(remove_called);
    cslul_free(ctx);
    cslul_config_free(cfg);
}

static int check_long_source(const char *modheader, size_t mhsize,
                             const char *otherfunc, size_t ofsize)
{
    char *data;
    int ret;
    struct CSlul *ctx = create_ctx();
    expected_name = "main.slul";
    ret_fopen = DUMMYFILE;
    expected_nmemb = PARSEBUFFER;
    ret_fread = PARSEBUFFER + ofsize - 2;
    ret_ferror = 0;

    /* Create a buffer which has a "func" keyword spanning over two buffers */
    data = tmalloc(ret_fread);
    memcpy(data, modheader, mhsize);
    memset(&data[mhsize], '\n', PARSEBUFFER-mhsize-2);
    memcpy(&data[PARSEBUFFER-2], otherfunc, ofsize);
    mocked_data = data;

    error_reported = 0;
    fclose_called = 0;
    ret = cslul_build(ctx, NULL);
    tassert(fclose_called);
    tassert(!remove_called);
    tfree(data);
    cslul_free(ctx);
    cslul_config_free(cfg);
    return ret;
}

/** Test of module of application type with source longer than BUFFERSIZE */
static void test_long_app(void)
{
    static const char modheader[] =
        "\\slul 0.0.0\n"
        "\\name test\n"
        "\n"
        "func f() -> int\n"
        "{\n"
        "    return 1\n"
        "}\n"
        "\n";
    static const char otherfunc[] =
        "func g()\n"
        "{\n"
        "}\n"
        "\n";
    int build_ret = check_long_source(modheader, sizeof(modheader)-1,
                                      otherfunc, sizeof(otherfunc)-1);
    tsoftassert(build_ret);
}

/** Test of module of library type with source longer than BUFFERSIZE */
static void test_long_library(void)
{
    static const char modheader[] =
        "\\slul 0.0.0\n"
        "\\name test\n"
        "\\type library\n"
        "\n"
        "func f() -> int\n"
        "\n";
    static const char otherfunc[] =
        "func g()\n"
        "\n";
    int build_ret;
    expected_error = CSLUL_E_MISSINGSOURCE;
    build_ret = check_long_source(modheader, sizeof(modheader)-1,
                                  otherfunc, sizeof(otherfunc)-1);
    tsoftassert(!build_ret);
    tsoftassert(error_reported);
}

static void test_add_sources(void)
{
    struct CSlul *ctx = create_ctx();
    struct CSlulSrcIter srciter;

    cslul_ll_start_phase(ctx, CSLUL_P_MODULEHEADER);
    srciter.filename = NULL;
    tsoftassert(!cslul_ll_implsource_iter(ctx, &srciter));

    /* Add first file */
    ctx->case_insens = 1;
    tsoftassert(build_add_source(ctx, 11, 6, "a.slul") == CSLUL_E_NOERROR);
    tassert(ctx->first_srcfile != NULL);
    tsoftassert(ctx->last_srcfile == ctx->first_srcfile);
    tsoftassert(ctx->first_srcfile->next == NULL);
    tsoftassert(ctx->first_srcfile->node.hashcode == 11);

    srciter.filename = NULL;
    tsoftassert(cslul_ll_implsource_iter(ctx, &srciter));
    tassert(srciter.namelen == 6);
    tassert(srciter.filename != NULL);
    tsoftassert(!strncmp("a.slul", srciter.filename, 6));
    tsoftassert(!cslul_ll_implsource_iter(ctx, &srciter));

    /* Add second file */
    tsoftassert(build_add_source(ctx, 22, 6, "b.slul") == CSLUL_E_NOERROR);
    tassert(ctx->first_srcfile != NULL);
    tassert(ctx->last_srcfile != NULL);
    tsoftassert(ctx->last_srcfile != ctx->first_srcfile);
    tsoftassert(ctx->first_srcfile->next == ctx->last_srcfile);
    tsoftassert(ctx->first_srcfile->node.hashcode == 11);
    tsoftassert(ctx->last_srcfile->node.hashcode == 22);

    /* Add third file */
    tsoftassert(build_add_source(ctx, 33, 6, "c.slul") == CSLUL_E_NOERROR);
    tassert(ctx->first_srcfile != NULL);
    tassert(ctx->last_srcfile != NULL);
    tsoftassert(ctx->last_srcfile != ctx->first_srcfile);
    tsoftassert(ctx->first_srcfile->node.hashcode == 11);
    tassert(ctx->first_srcfile->next != NULL);
    tsoftassert(ctx->first_srcfile->next->node.hashcode == 22);
    tassert(ctx->first_srcfile->next->next == ctx->last_srcfile);
    tsoftassert(ctx->last_srcfile->node.hashcode == 33);

    srciter.filename = NULL;
    tsoftassert(cslul_ll_implsource_iter(ctx, &srciter));
    tassert(srciter.namelen == 6);
    tassert(srciter.filename != NULL);
    tsoftassert(!strncmp("a.slul", srciter.filename, 6));
    tsoftassert(cslul_ll_implsource_iter(ctx, &srciter));
    tassert(srciter.namelen == 6);
    tassert(srciter.filename != NULL);
    tsoftassert(!strncmp("b.slul", srciter.filename, 6));
    tsoftassert(cslul_ll_implsource_iter(ctx, &srciter));
    tassert(srciter.namelen == 6);
    tassert(srciter.filename != NULL);
    tsoftassert(!strncmp("c.slul", srciter.filename, 6));
    tsoftassert(!cslul_ll_implsource_iter(ctx, &srciter));
    cslul_free(ctx);
    cslul_config_free(cfg);
}

const TestFunctionInfo tests_build[] = {
    TEST_BEFORE(before_test)
    TEST_INFO(test_fopen_failure)
    TEST_INFO(test_fread_failure)
    TEST_INFO(test_deletion_on_error)
    TEST_INFO(test_long_app)
    TEST_INFO(test_long_library)
    TEST_INFO(test_add_sources)
    TEST_END
};