Line data Source code
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 : #ifndef _WIN32
11 : FILE *file =
12 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r");
13 1 : assert(file);
14 :
15 : passgen_wordlist wordlist;
16 1 : passgen_wordlist_load(&wordlist, file, 3);
17 :
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 :
23 1 : passgen_wordlist_free(&wordlist);
24 : #endif
25 :
26 1 : return test_ok;
27 : }
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++) {
32 702 : for(char b = 0; b < 26; b++) {
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 : }
37 : }
38 :
39 : // null-terminate
40 1 : wordlist_data[26 * 26 * 3 - 1] = 0;
41 :
42 : #ifndef _WIN32
43 1 : FILE *file = fmemopen((void *) wordlist_data, strlen(wordlist_data), "r");
44 1 : assert(file);
45 :
46 : passgen_wordlist wordlist;
47 1 : passgen_wordlist_load(&wordlist, file, 3);
48 :
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 :
59 1 : passgen_wordlist_free(&wordlist);
60 : #endif
61 :
62 1 : free(wordlist_data);
63 :
64 1 : return test_ok;
65 : }
66 :
67 1 : test_result test_wordlist_random(void) {
68 : #ifndef _WIN32
69 : FILE *file =
70 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r");
71 1 : assert(file);
72 :
73 : passgen_wordlist wordlist;
74 1 : passgen_wordlist_load(&wordlist, file, 3);
75 :
76 : passgen_random random;
77 1 : passgen_random_open(&random, NULL);
78 :
79 4 : for(size_t i = 0; i < wordlist.count; i++) {
80 3 : const char *word = passgen_wordlist_random(&wordlist, &random);
81 :
82 : // 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++) {
85 5 : if(word == wordlist.words[j]) {
86 3 : found = true;
87 3 : break;
88 : }
89 : }
90 3 : assert(found);
91 : }
92 :
93 1 : passgen_wordlist_free(&wordlist);
94 1 : passgen_random_close(&random);
95 : #endif
96 :
97 1 : return test_ok;
98 : }
99 :
100 1 : test_result test_wordlist_random_uninit(void) {
101 : #ifndef _WIN32
102 : FILE *file =
103 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r");
104 1 : assert(file);
105 :
106 : passgen_wordlist wordlist;
107 1 : passgen_wordlist_init(&wordlist, file, 3);
108 1 : assert_eq(wordlist.parsed, false);
109 :
110 : passgen_random random;
111 1 : passgen_random_open(&random, NULL);
112 :
113 1 : const char *word = passgen_wordlist_random(&wordlist, &random);
114 1 : assert_eq(word, NULL);
115 :
116 1 : passgen_wordlist_free(&wordlist);
117 1 : passgen_random_close(&random);
118 : #endif
119 :
120 1 : return test_ok;
121 : }
|