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 : #ifndef _WIN32 // fmemopen does not exist on windows 10 : 11 1 : test_result test_wordlist_load(void) { 12 : FILE *file = 13 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r"); 14 1 : assert(file); 15 : 16 : passgen_wordlist wordlist; 17 1 : passgen_wordlist_load(&wordlist, file, 3); 18 : 19 1 : assert(wordlist.count == 3); 20 1 : assert(0 == strcmp(wordlist.words[0], "abacus")); 21 1 : assert(0 == strcmp(wordlist.words[1], "brother")); 22 1 : assert(0 == strcmp(wordlist.words[2], "culling")); 23 : 24 1 : passgen_wordlist_free(&wordlist); 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 1 : FILE *file = fmemopen((void *) wordlist_data, strlen(wordlist_data), "r"); 43 1 : assert(file); 44 : 45 : passgen_wordlist wordlist; 46 1 : passgen_wordlist_load(&wordlist, file, 3); 47 : 48 1 : assert_eq(wordlist.count, 26 * 26); 49 1 : assert_eq(passgen_wordlist_count(&wordlist), 26 * 26); 50 1 : assert(0 == strcmp(wordlist.words[0], "aa")); 51 1 : assert(0 == strcmp(wordlist.words[1], "ab")); 52 1 : assert(0 == strcmp(wordlist.words[2], "ac")); 53 1 : assert(0 == strcmp(wordlist.words[26], "ba")); 54 1 : assert(0 == strcmp(wordlist.words[27], "bb")); 55 1 : assert(0 == strcmp(wordlist.words[28], "bc")); 56 1 : assert(0 == strcmp(wordlist.words[26 * 26 - 1], "zz")); 57 : 58 1 : passgen_wordlist_free(&wordlist); 59 : 60 1 : free(wordlist_data); 61 : 62 1 : return test_ok; 63 : } 64 : 65 1 : test_result test_wordlist_random(void) { 66 : FILE *file = 67 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r"); 68 1 : assert(file); 69 : 70 : passgen_wordlist wordlist; 71 1 : passgen_wordlist_load(&wordlist, file, 3); 72 : 73 : passgen_random random; 74 1 : passgen_random_open(&random, NULL); 75 : 76 4 : for(size_t i = 0; i < wordlist.count; i++) { 77 3 : const char *word = passgen_wordlist_random(&wordlist, &random); 78 : 79 : // make sure the word exists in the word list 80 3 : bool found = false; 81 5 : for(size_t j = 0; j < wordlist.count; j++) { 82 5 : if(word == wordlist.words[j]) { 83 3 : found = true; 84 3 : break; 85 : } 86 : } 87 3 : assert(found); 88 : } 89 : 90 1 : passgen_wordlist_free(&wordlist); 91 1 : passgen_random_close(&random); 92 : 93 1 : return test_ok; 94 : } 95 : 96 1 : test_result test_wordlist_random_uninit(void) { 97 : FILE *file = 98 1 : fmemopen((void *) example_wordlist, example_wordlist_size, "r"); 99 1 : assert(file); 100 : 101 : passgen_wordlist wordlist; 102 1 : passgen_wordlist_init(&wordlist, file, 3); 103 1 : assert_eq(wordlist.parsed, false); 104 : 105 : passgen_random random; 106 1 : passgen_random_open(&random, NULL); 107 : 108 1 : const char *word = passgen_wordlist_random(&wordlist, &random); 109 1 : assert_eq(word, NULL); 110 : 111 1 : passgen_wordlist_free(&wordlist); 112 1 : passgen_random_close(&random); 113 : 114 1 : return test_ok; 115 : } 116 : 117 : #endif