File: json.c

package info (click to toggle)
dnssec-trigger 0.17%2Brepack-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,956 kB
  • sloc: ansic: 18,699; sh: 976; makefile: 495; xml: 444; objc: 421; cpp: 18
file content (126 lines) | stat: -rw-r--r-- 4,551 bytes parent folder | download | duplicates (4)
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
#include "../config.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../riggerd/fwd_zones.h"
#include "../riggerd/connection_list.h"
#include "../riggerd/string_list.h"
#include "../riggerd/string_buffer.h"
#include "../vendor/ccan/json/json.h"

char *json = 
"{"
"\n    \"connections\": ["
"\n        {"
"\n            \"default\": false,"
"\n            \"servers\": ["
"\n                \"10.2.0.4\","
"\n                \"10.9.2.4\","
"\n                \"10.2.0.6\""
"\n            ],"
"\n            \"type\": \"wifi\","
"\n            \"zones\": ["
"\n                \"example.com\""
"\n            ]"
"\n        },"
"\n        {"
"\n            \"default\": true,"
"\n            \"servers\": ["
"\n                \"10.60.0.16\","
"\n                \"10.67.5.56\""
"\n            ],"
"\n            \"type\": \"other\","
"\n            \"zones\": ["
"\n                \"brno.example.com\","
"\n                \"prague.example.com\","
"\n                \"finance.prague.example.com\","
"\n                \"laboratory.prague.example.com\","
"\n                \"lab2.prague.example.com\""
"\n            ]"
"\n        },"
"\n        {"
"\n            \"default\": false,"
"\n            \"servers\": ["
"\n                \"10.148.8.37\","
"\n                \"10.145.245.24\""
"\n            ],"
"\n            \"type\": \"vpn\","
"\n            \"zones\": ["
"\n                \"example.com\""
"\n            ]"
"\n        }"
"\n    ]"
"\n}\n";

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}

static void load_json_into_list_and_test_length(void **state) {
    struct nm_connection_list l = yield_connections_from_json(json);
    assert_true(nm_connection_list_length(&l) == 3);
    nm_connection_list_clear(&l);
    (void) state; /* unused */
}

static void filter_connection_list_and_test_length0(void **state) {
    struct nm_connection_list l = yield_connections_from_json(json);
    struct nm_connection_list l2 = nm_connection_list_filter(&l, 0);
    assert_true(nm_connection_list_length(&l2) == 3);
    nm_connection_list_clear(&l2);
    nm_connection_list_clear(&l);
    (void) state; /* unused */
}

static void filter_connection_list_and_test_length1(void **state) {
    struct nm_connection_list l = yield_connections_from_json(json);
    struct nm_connection_list l2 = nm_connection_list_filter(&l, 1, &nm_connection_filter_type_vpn);
    assert_true(nm_connection_list_length(&l2) == 1);
    nm_connection_list_clear(&l2);
    nm_connection_list_clear(&l);
    (void) state; /* unused */
}

int main() {
    // printf("Test json parser:\n%s\n", json);
    // struct nm_connection_list l = yield_connections_from_json(json);
    // nm_connection_list_dbg_print(&l);
    // printf("Length is: %zu\n", nm_connection_list_length(&l));
    // struct nm_connection_list l2 = nm_connection_list_filter(&l, 0);
    // printf("Length is: %zu\n", nm_connection_list_length(&l2));
    // struct nm_connection_list l3 = nm_connection_list_filter(&l, 1, &nm_connection_filter_type_vpn);
    // printf("Length is: %zu\n", nm_connection_list_length(&l3));
    // struct nm_connection_list l4 = nm_connection_list_filter(&l, 1, &nm_connection_filter_default);
    // printf("Length is: %zu\n", nm_connection_list_length(&l4));
    // struct nm_connection_list l5 = nm_connection_list_filter(&l, 2, &nm_connection_filter_type_other, &nm_connection_filter_default);
    // printf("Length is: %zu\n", nm_connection_list_length(&l5));
    // struct nm_connection_list l6 = nm_connection_list_filter(&l, 2, &nm_connection_filter_type_vpn, &nm_connection_filter_default);
    // printf("Length is: %zu\n", nm_connection_list_length(&l6));

    // {
    // struct string_buffer buffer = nm_connection_list_sprint_servers(&l);
    // printf("Servers: %s\n", buffer.string);
    // free(buffer.string);
    // }

    // char *buffer = calloc_or_die(1000);
    // string_list_sprint(&l.first->self->servers, buffer, 1000);
    // printf("%s\n", buffer);
    // free(buffer);
    // nm_connection_list_clear(&l);

    const struct CMUnitTest tests[] = {
        cmocka_unit_test(null_test_success),
        cmocka_unit_test(load_json_into_list_and_test_length),
        cmocka_unit_test(filter_connection_list_and_test_length0),
        cmocka_unit_test(filter_connection_list_and_test_length1),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}