File: igraph_read_graph_graphml_invalid_inputs.c

package info (click to toggle)
igraph 1.0.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,436 kB
  • sloc: ansic: 155,759; cpp: 32,544; xml: 2,960; python: 411; makefile: 168; javascript: 20; sh: 9
file content (81 lines) | stat: -rw-r--r-- 2,583 bytes parent folder | download | duplicates (2)
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
/*
   igraph library.
   Copyright (C) 2021  The igraph development team

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#include <igraph.h>
#include <stdio.h>

#include "../unit/test_utilities.h"

int test_file(const char* fname, igraph_bool_t should_parse) {
    FILE *ifile;
    igraph_t g;
    igraph_error_t retval;

    ifile = fopen(fname, "r");
    if (ifile == 0) {
        printf("Cannot open input file: %s\n", fname);
        return 1;
    }

    retval = igraph_read_graph_graphml(&g, ifile, 0);
    if (retval == IGRAPH_SUCCESS) {
        /* destroy the graph if we managed to parse it */
        igraph_destroy(&g);
    }

    fclose(ifile);

    if (!should_parse && retval == IGRAPH_SUCCESS) {
        /* input was accepted but it should not have been, this is a bug */
        return 2;
    } else {
        return 0;
    }
}

#undef RUN_TEST
#define RUN_TEST(fname, should_parse) {   \
    index++;                              \
    if (test_file(fname, should_parse)) { \
        return index;                     \
    }                                     \
    VERIFY_FINALLY_STACK();               \
}

int main(void) {
    int index = 0;

    /* We do not care about errors; all we care about is that the library
     * should not segfault, should not accept invalid input and should not
     * print anything to stdout or stderr while parsing, apart from warnings */
    igraph_set_error_handler(igraph_error_handler_ignore);
    igraph_set_warning_handler(igraph_warning_handler_ignore);

    RUN_TEST("invalid1.graphml", /* should_parse = */ 0);
    RUN_TEST("invalid2.graphml", /* should_parse = */ 1);
    RUN_TEST("invalid3.graphml", /* should_parse = */ 0);
    RUN_TEST("invalid4.graphml", /* should_parse = */ 0);
    RUN_TEST("invalid5.graphml", /* should_parse = */ 0);
    RUN_TEST("invalid6.graphml", /* should_parse = */ 0);

    VERIFY_FINALLY_STACK();

    return 0;
}