/builds/xfbs/passgen/src/tests/wordlist.c
Line | Count | Source |
1 | | #include "passgen/wordlist.h" |
2 | | #include "tests.h" |
3 | | #include <stdlib.h> |
4 | | #include <string.h> |
5 | | |
6 | | const char example_wordlist[] = "abacus\nbrother\nculling"; |
7 | | size_t example_wordlist_size = sizeof(example_wordlist); |
8 | | |
9 | 1 | test_result test_wordlist_load(void) { |
10 | 1 | #ifndef _WIN32 |
11 | 1 | FILE *file = |
12 | 1 | fmemopen((void *) example_wordlist, example_wordlist_size, "r"); |
13 | 1 | assert(file); |
14 | 1 | |
15 | 1 | passgen_wordlist wordlist; |
16 | 1 | passgen_wordlist_load(&wordlist, file, 3); |
17 | 1 | |
18 | 1 | assert(wordlist.count == 3); |
19 | 1 | assert(0 == strcmp(wordlist.words[0], "abacus")); |
20 | 1 | assert(0 == strcmp(wordlist.words[1], "brother")); |
21 | 1 | assert(0 == strcmp(wordlist.words[2], "culling")); |
22 | 1 | |
23 | 1 | passgen_wordlist_free(&wordlist); |
24 | 1 | #endif |
25 | 1 | |
26 | 1 | return test_ok; |
27 | 1 | } |
28 | | |
29 | 1 | test_result test_wordlist_load_long(void) { |
30 | 1 | char *wordlist_data = malloc(26 * 26 * 3 + 1); |
31 | 27 | for(size_t a = 0; a < 26; a++26 ) { |
32 | 702 | for(char b = 0; b < 26; b++676 ) { |
33 | 676 | wordlist_data[a * 26 * 3 + b * 3] = 'a' + a; |
34 | 676 | wordlist_data[a * 26 * 3 + b * 3 + 1] = 'a' + b; |
35 | 676 | wordlist_data[a * 26 * 3 + b * 3 + 2] = '\n'; |
36 | 676 | } |
37 | 26 | } |
38 | 1 | |
39 | 1 | // null-terminate |
40 | 1 | wordlist_data[26 * 26 * 3 - 1] = 0; |
41 | 1 | |
42 | 1 | #ifndef _WIN32 |
43 | 1 | FILE *file = fmemopen((void *) wordlist_data, strlen(wordlist_data), "r"); |
44 | 1 | assert(file); |
45 | 1 | |
46 | 1 | passgen_wordlist wordlist; |
47 | 1 | passgen_wordlist_load(&wordlist, file, 3); |
48 | 1 | |
49 | 1 | assert_eq(wordlist.count, 26 * 26); |
50 | 1 | assert_eq(passgen_wordlist_count(&wordlist), 26 * 26); |
51 | 1 | assert(0 == strcmp(wordlist.words[0], "aa")); |
52 | 1 | assert(0 == strcmp(wordlist.words[1], "ab")); |
53 | 1 | assert(0 == strcmp(wordlist.words[2], "ac")); |
54 | 1 | assert(0 == strcmp(wordlist.words[26], "ba")); |
55 | 1 | assert(0 == strcmp(wordlist.words[27], "bb")); |
56 | 1 | assert(0 == strcmp(wordlist.words[28], "bc")); |
57 | 1 | assert(0 == strcmp(wordlist.words[26 * 26 - 1], "zz")); |
58 | 1 | |
59 | 1 | passgen_wordlist_free(&wordlist); |
60 | 1 | #endif |
61 | 1 | |
62 | 1 | free(wordlist_data); |
63 | 1 | |
64 | 1 | return test_ok; |
65 | 1 | } |
66 | | |
67 | 1 | test_result test_wordlist_random(void) { |
68 | 1 | #ifndef _WIN32 |
69 | 1 | FILE *file = |
70 | 1 | fmemopen((void *) example_wordlist, example_wordlist_size, "r"); |
71 | 1 | assert(file); |
72 | 1 | |
73 | 1 | passgen_wordlist wordlist; |
74 | 1 | passgen_wordlist_load(&wordlist, file, 3); |
75 | 1 | |
76 | 1 | passgen_random random; |
77 | 1 | passgen_random_open(&random, NULL); |
78 | 1 | |
79 | 4 | for(size_t i = 0; i < wordlist.count; i++3 ) { |
80 | 3 | const char *word = passgen_wordlist_random(&wordlist, &random); |
81 | 3 | |
82 | 3 | // make sure the word exists in the word list |
83 | 3 | bool found = false; |
84 | 5 | for(size_t j = 0; j < wordlist.count; j++2 ) { |
85 | 5 | if(word == wordlist.words[j]) { |
86 | 3 | found = true; |
87 | 3 | break; |
88 | 3 | } |
89 | 5 | } |
90 | 3 | assert(found); |
91 | 3 | } |
92 | 1 | |
93 | 1 | passgen_wordlist_free(&wordlist); |
94 | 1 | passgen_random_close(&random); |
95 | 1 | #endif |
96 | 1 | |
97 | 1 | return test_ok; |
98 | 1 | } |
99 | | |
100 | 1 | test_result test_wordlist_random_uninit(void) { |
101 | 1 | #ifndef _WIN32 |
102 | 1 | FILE *file = |
103 | 1 | fmemopen((void *) example_wordlist, example_wordlist_size, "r"); |
104 | 1 | assert(file); |
105 | 1 | |
106 | 1 | passgen_wordlist wordlist; |
107 | 1 | passgen_wordlist_init(&wordlist, file, 3); |
108 | 1 | assert_eq(wordlist.parsed, false); |
109 | 1 | |
110 | 1 | passgen_random random; |
111 | 1 | passgen_random_open(&random, NULL); |
112 | 1 | |
113 | 1 | const char *word = passgen_wordlist_random(&wordlist, &random); |
114 | 1 | assert_eq(word, NULL); |
115 | 1 | |
116 | 1 | passgen_wordlist_free(&wordlist); |
117 | 1 | passgen_random_close(&random); |
118 | 1 | #endif |
119 | 1 | |
120 | 1 | return test_ok; |
121 | 1 | } |