GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: test/test_str.c Lines: 109 109 100.0 %
Date: 2020-12-10 21:44:00 Branches: 120 372 32.3 %

Line Branch Exec Source
1
#include <signal.h>
2
#include <stddef.h>
3
#include <stdio.h>
4
#include <arpa/inet.h>
5
#include <criterion/criterion.h>
6
7
#include "str.h"
8
#include "alloc.h"
9
10
#if defined(USE_OPENSSL) | defined(USE_BORINGSSL) | defined(USE_LIBRESSL)
11
12
#include <openssl/sha.h>
13
14
#elif defined(USE_WOLFSSL)
15
16
#pragma GCC diagnostic push
17
#pragma GCC diagnostic ignored "-Wcpp"
18
#include <wolfssl/wolfcrypt/sha.h>
19
#pragma GCC diagnostic pop
20
21
#define SHA_DIGEST_LENGTH 20
22
23
#else
24
25
#include "sha1.h"
26
#define SHA_DIGEST_LENGTH 20
27
28
#endif
29
30
8
static void setup(void) {
31
#ifdef USE_RPMALLOC
32
8
    rpmalloc_initialize();
33
#endif
34
8
}
35
36
8
static void teardown(void) {
37
#ifdef USE_RPMALLOC
38
8
    rpmalloc_finalize();
39
#endif
40
8
}
41
42
TestSuite(bin2hex, .init = setup, .fini = teardown);
43
44
4
Test(bin2hex, zero_length) {
45
2
    char *string = "";
46
2
    char *str = bin2hex((const char unsigned *) string, 0);
47
48


2
    cr_assert(str != NULL);
49


2
    cr_assert(str[0] == '\0');
50
51
2
    WSS_free((void**)&str);
52
2
}
53
54
4
Test(bin2hex, simple_string) {
55
2
    char *str;
56
2
    const char unsigned *string = (const char unsigned *)WSS_malloc(6);
57
2
    sprintf((char *)string, "%s", "12345");
58
59
2
    str = bin2hex(string, strlen((char *)string));
60
61


2
    cr_assert(str != NULL);
62


2
    cr_assert(strncmp(str, "3132333435", 10) == 0);
63
64
2
    WSS_free((void **)&str);
65
2
    WSS_free((void **) &string);
66
2
}
67
68
4
Test(bin2hex, sha1_string) {
69
2
    char *str;
70
2
    char sha1Key[SHA_DIGEST_LENGTH];
71
2
    const char unsigned *string = (const char unsigned *)WSS_malloc(6);
72
2
    sprintf((char *)string, "%s", "12345");
73
74
#if defined(USE_OPENSSL)
75
2
    SHA1((const unsigned char *)string, strlen((char *)string), (unsigned char*) sha1Key);
76
#elif defined(USE_WOLFSSL)
77
    Sha sha;
78
    wc_InitSha(&sha);
79
    wc_ShaUpdate(&sha, (const unsigned char *) string, strlen((char *)string));
80
    wc_ShaFinal(&sha, (unsigned char *) sha1Key);
81
#else
82
    SHA1Context sha;
83
    int i, b;
84
    memset(sha1Key, '\0', SHA_DIGEST_LENGTH);
85
86
    SHA1Reset(&sha);
87
    SHA1Input(&sha, (const unsigned char*) string, strlen((char *)string));
88
    if ( SHA1Result(&sha) ) {
89
        for (i = 0; i < 5; i++) {
90
            b = htonl(sha.Message_Digest[i]);
91
            memcpy(sha1Key+(4*i), (unsigned char *) &b, 4);
92
        }
93
    }
94
#endif
95
2
    str = bin2hex((const unsigned char *)sha1Key, SHA_DIGEST_LENGTH);
96
97


2
    cr_assert(str != NULL);
98


2
    cr_assert(strncmp(str, "8cb2237d0679ca88db6464eac60da96345513964", SHA_DIGEST_LENGTH));
99
100
2
    WSS_free((void **) &str);
101
2
    WSS_free((void **) &string);
102
2
}
103
104
TestSuite(strinarray, .init = setup, .fini = teardown);
105
106
4
Test(strinarray, test_zero_length) {
107
2
    int i;
108
2
    int length = 0;
109
2
    char **array = (char **) WSS_calloc(100, sizeof(char *));
110
111
202
    for (i = 0; i < 100; i++) {
112
200
        array[i] = WSS_malloc(3);
113
200
        sprintf(array[i], "%d", i);
114
    }
115
116


2
    cr_assert(strinarray("87", (const char **)array, length) == -1);
117


2
    cr_assert(strinarray("34", (const char **)array, length) == -1);
118


2
    cr_assert(strinarray("0", (const char **)array, length) == -1);
119


2
    cr_assert(strinarray("99", (const char **)array, length) == -1);
120


2
    cr_assert(strinarray("37", (const char **)array, length) == -1);
121


2
    cr_assert(strinarray("100", (const char **)array, length) == -1);
122


2
    cr_assert(strinarray("testing123", (const char **)array, length) == -1);
123


2
    cr_assert(strinarray("lol", (const char **)array, length) == -1);
124
125
204
    for (i = 0; i < 100; i++) {
126
200
        WSS_free((void **)&array[i]);
127
    }
128
2
    WSS_free((void **)&array);
129
2
}
130
131
4
Test(strinarray, test_string_in_first_half_array) {
132
2
    int i;
133
2
    int length = 100;
134
2
    char **array = (char **) WSS_calloc(length, sizeof(char *));
135
136
202
    for (i = 0; i < length; i++) {
137
200
        array[i] = WSS_malloc(3);
138
200
        sprintf(array[i], "%d", i);
139
    }
140
141


2
    cr_assert(strinarray("87", (const char **)array, length/2) == -1);
142


2
    cr_assert(strinarray("34", (const char **)array, length/2) == 0);
143


2
    cr_assert(strinarray("0", (const char **)array, length/2) == 0);
144


2
    cr_assert(strinarray("99", (const char **)array, length/2) == -1);
145


2
    cr_assert(strinarray("37", (const char **)array, length/2) == 0);
146


2
    cr_assert(strinarray("100", (const char **)array, length/2) == -1);
147


2
    cr_assert(strinarray("50", (const char **)array, length/2) == -1);
148


2
    cr_assert(strinarray("49", (const char **)array, length/2) == 0);
149


2
    cr_assert(strinarray("testing123", (const char **)array, length/2) == -1);
150


2
    cr_assert(strinarray("lol", (const char **)array, length/2) == -1);
151
152
204
    for (i = 0; i < 100; i++) {
153
200
        WSS_free((void **)&array[i]);
154
    }
155
2
    WSS_free((void **)&array);
156
2
}
157
158
4
Test(strinarray, test_string_in_array) {
159
2
    int i;
160
2
    int length = 100;
161
2
    char **array = (char **) WSS_calloc(length, sizeof(char *));
162
163
202
    for (i = 0; i < length; i++) {
164
200
        array[i] = WSS_malloc(3);
165
200
        sprintf(array[i], "%d", i);
166
    }
167
168


2
    cr_assert(strinarray("87", (const char **)array, length) == 0);
169


2
    cr_assert(strinarray("34", (const char **)array, length) == 0);
170


2
    cr_assert(strinarray("0", (const char **)array, length) == 0);
171


2
    cr_assert(strinarray("99", (const char **)array, length) == 0);
172


2
    cr_assert(strinarray("37", (const char **)array, length) == 0);
173


2
    cr_assert(strinarray("100", (const char **)array, length) == -1);
174


2
    cr_assert(strinarray("testing123", (const char **)array, length) == -1);
175


2
    cr_assert(strinarray("lol", (const char **)array, length) == -1);
176
177
204
    for (i = 0; i < length; i++) {
178
200
        WSS_free((void **)&array[i]);
179
    }
180
2
    WSS_free((void **)&array);
181
2
}
182
183
TestSuite(strload, .init = setup, .fini = teardown);
184
185
4
Test(strload, none_existing_file) {
186
2
    int n;
187
2
    char *content;
188
189
2
    n = strload("lollern.txt", &content);
190
191


2
    cr_assert(n == 0);
192


2
    cr_assert(content == NULL);
193
2
}
194
195
4
Test(strload, load_txt_file) {
196
2
    int n;
197
2
    char *content;
198
199
2
    n = strload("resources/test.txt", &content);
200
201


2
    cr_assert(n == 34);
202


2
    cr_assert(strncmp(content, "This is a file, used for testing.\n", n) == 0);
203
204
2
    WSS_free((void **)&content);
205
2
}