summaryrefslogtreecommitdiff
path: root/testing/afl_toxsave.c
blob: 9f93074b6727fc99418fcdf9d98a9d511ec0b3d4 (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
#include <stdio.h>
#include <stdlib.h>

#include "../toxcore/tox.h"

int main(int argc, char **argv)
{
    if (argc != 2) {
        return -1;
    }

    // determine file size
    FILE *fileptr = fopen(argv[1], "rb");
    fseek(fileptr, 0, SEEK_END);
    long filelen = ftell(fileptr);
    rewind(fileptr);

    // read file into buffer
    uint8_t *buffer = (uint8_t *)malloc(filelen * sizeof(uint8_t));
    size_t bytes_read = fread(buffer, filelen, 1, fileptr);

    if (bytes_read != filelen) {
        free(buffer);
        return -1;
    }

    fclose(fileptr);

    Tox_Err_Options_New error_options;

    struct Tox_Options *tox_options = tox_options_new(&error_options);

    if (error_options != TOX_ERR_OPTIONS_NEW_OK) {
        free(buffer);
        return -1;
    }

    // pass test data to Tox
    tox_options_set_savedata_data(tox_options, buffer, filelen);
    tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);

    Tox_Err_New error_new;
    Tox *tox = tox_new(tox_options, &error_new);
    tox_options_free(tox_options);

    if (!tox || error_new != TOX_ERR_NEW_OK) {
        free(buffer);
        return -1;
    }

    tox_kill(tox);
    free(buffer);

    return 0;
}