Coverage Report

Created: 2023-12-01 06:05

/builds/xfbs/passgen/src/tests/entropy.c
Line
Count
Source (jump to first uncovered line)
1
#include "tests.h"
2
#include <math.h>
3
#include <passgen/generate.h>
4
#include <passgen/passgen.h>
5
6
31
double entropy(const char *p) {
7
31
    // parse pattern
8
31
    passgen_pattern pattern;
9
31
    passgen_error error;
10
31
    if(0 != passgen_parse(&pattern, &error, p)) {
11
0
        return NAN;
12
0
    }
13
31
14
31
    size_t buffer_len = 256;
15
31
    uint8_t buffer[buffer_len];
16
31
    buffer[0] = 0;
17
31
    passgen_random random;
18
31
    passgen_random_open(&random, NULL);
19
31
    passgen_env env;
20
31
    passgen_env_init(&env, &random);
21
31
    double entropy;
22
31
    int length =
23
31
        passgen_generate_fill_utf8(&pattern, &env, &entropy, &buffer[0], buffer_len);
24
31
    buffer[length] = 0;
25
31
    passgen_pattern_free(&pattern);
26
31
27
31
    return entropy;
28
31
}
29
30
32
bool equals(double a, double b) {
31
32
    double epsilon = 0.00001;
32
32
    return (a - epsilon) < b && (a + epsilon) > b;
33
32
}
34
35
1
test_result test_entropy_literal(void) {
36
1
    assert(equals(0.0, 0.0));
37
1
    assert(equals(1.0, entropy("")));
38
1
    assert(equals(1.0, entropy("a")));
39
1
    assert(equals(1.0, entropy("ab")));
40
1
    assert(equals(1.0, entropy("abc")));
41
1
    assert(equals(1.0, entropy("abcdefghijklmnopqrstuvwxyz")));
42
1
43
1
    return test_ok;
44
1
}
45
46
1
test_result test_entropy_group(void) {
47
1
    assert(equals(1.0, entropy("()")));
48
1
    assert(equals(1.0, entropy("(abc)")));
49
1
    assert(equals(1.0, entropy("(())")));
50
1
    assert(equals(2.0, entropy("(abc|def)")));
51
1
    assert(equals(3.0, entropy("(abc|def|hij)")));
52
1
    assert(equals(4.0, entropy("(abc|def|hij|klm)")));
53
1
54
1
    return test_ok;
55
1
}
56
57
1
test_result test_entropy_range(void) {
58
1
    assert(equals(1.0, entropy("[a]")));
59
1
    assert(equals(2.0, entropy("[ab]")));
60
1
    assert(equals(3.0, entropy("[abc]")));
61
1
    assert(equals(3.0, entropy("[a-c]")));
62
1
    assert(equals(26.0, entropy("[a-z]")));
63
1
    assert(equals(10.0, entropy("[0-9]")));
64
1
    assert(equals(16.0, entropy("[0-9a-f]")));
65
1
    assert(equals(20.0, entropy("[0-9a-f,.:\\-]")));
66
1
67
1
    return test_ok;
68
1
}
69
70
1
test_result test_entropy_repeat(void) {
71
1
    assert(equals(3.0, entropy("[abc]{1}")));
72
1
    assert(equals(3.0 * 3.0, entropy("[abc]{2}")));
73
1
    assert(equals(3.0 * 3.0 * 3.0, entropy("[abc]{3}")));
74
1
75
1
    assert(equals(3.0, entropy("(this|that|other){1}")));
76
1
    assert(equals(3.0 * 3.0, entropy("(this|that|other){2}")));
77
1
    assert(equals(3.0 * 3.0 * 3.0, entropy("(this|that|other){3}")));
78
1
79
1
    assert(equals(1.0, entropy("a{1}")));
80
1
    assert(equals(2.0, entropy("a{1,2}")));
81
1
    assert(equals(3.0, entropy("a{1,3}")));
82
1
83
1
    assert(equals(1.0, entropy("(a){1}")));
84
1
    assert(equals(2.0, entropy("(a){1,2}")));
85
1
    assert(equals(3.0, entropy("(a){1,3}")));
86
1
87
1
    return test_ok;
88
1
}