1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
// $Id: charpool1.cc 2845 2009-09-01 09:33:31Z rafi $
// Simple tests for yapet/pwgen/charpool.*
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef HAVE_IOSTREAM
# include <iostream>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <typeinfo>
#include <charpool.h>
#include "testpaths.h"
using namespace YAPET::PWGEN;
const char t_letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char t_digits[] = "0123456789";
const char t_punct[] = ".,;:-!?'";
const char t_special[] = "_+\"*%&/()[]={}<>";
const char t_other[] = "@#\\|$~`^";
class gainaccess : public CharacterPool {
public:
gainaccess (SUBPOOLS p) throw (std::runtime_error) : CharacterPool (p) { }
const char* _getPool() {
return getPool();
}
};
int main (int, char**) {
#ifndef TESTS_VERBOSE
int stdout_redir_fd = open("/dev/null", O_WRONLY | O_APPEND);
dup2(stdout_redir_fd ,STDOUT_FILENO);
#endif
std::cout << std::endl;
try {
// The the constructors
CharacterPool tmp1 (LETTERS | DIGITS | PUNCT | SPECIAL | OTHER);
CharacterPool tmp2 = tmp1;
CharacterPool tmp3 (tmp1);
gainaccess tmp4 (LETTERS);
if ( (tmp4.getPoolLength() != strlen (t_letters) ) ||
strncmp (tmp4._getPool(), t_letters, tmp4.getPoolLength() ) != 0 ) {
std::cout << " ==> Pool LETTERS looks odd" << std::endl;
return 1;
}
gainaccess tmp5 (DIGITS);
if ( (tmp5.getPoolLength() != strlen (t_digits) ) ||
strncmp (tmp5._getPool(), t_digits, tmp5.getPoolLength() ) != 0 ) {
std::cout << " ==> Pool DIGITS looks odd" << std::endl;
return 1;
}
gainaccess tmp6 (PUNCT);
if ( (tmp6.getPoolLength() != strlen (t_punct) ) ||
strncmp (tmp6._getPool(), t_punct, tmp6.getPoolLength() ) != 0 ) {
std::cout << " ==> Pool PUNCT looks odd" << std::endl;
return 1;
}
gainaccess tmp7 (SPECIAL);
if ( (tmp7.getPoolLength() != strlen (t_special) ) ||
strncmp (tmp7._getPool(), t_special, tmp7.getPoolLength() ) != 0 ) {
std::cout << " ==> Pool SPECIAL looks odd" << std::endl;
return 1;
}
gainaccess tmp8 (OTHER);
if ( (tmp8.getPoolLength() != strlen (t_other) ) ||
strncmp (tmp8._getPool(), t_other, tmp8.getPoolLength() ) != 0 ) {
std::cout << " ==> Pool OTHER looks odd" << std::endl;
return 1;
}
std::cout << " ==> ";
CharacterPool* cp = new CharacterPool (LETTERS);
for (size_t i = 0; i < cp->getPoolLength(); i++) {
char c = (*cp) [i];
if (c == 0) {
std::cout << std::endl << " ==> Zero encountered at position " << i << std::endl;
return 1;
}
std::cout << c;
}
delete cp;
std::cout << std::endl;
std::cout << " ==> ";
cp = new CharacterPool (DIGITS);
for (size_t i = 0; i < cp->getPoolLength(); i++) {
char c = (*cp) [i];
if (c == 0) {
std::cout << std::endl << " ==> Zero encountered at position " << i << std::endl;
return 1;
}
std::cout << c;
}
delete cp;
std::cout << std::endl;
std::cout << " ==> ";
cp = new CharacterPool (PUNCT);
for (size_t i = 0; i < cp->getPoolLength(); i++) {
char c = (*cp) [i];
if (c == 0) {
std::cout << std::endl << " ==> Zero encountered at position " << i << std::endl;
return 1;
}
std::cout << c;
}
delete cp;
std::cout << std::endl;
std::cout << " ==> ";
cp = new CharacterPool (SPECIAL);
for (size_t i = 0; i < cp->getPoolLength(); i++) {
char c = (*cp) [i];
if (c == 0) {
std::cout << std::endl << " ==> Zero encountered at position " << i << std::endl;
return 1;
}
std::cout << c;
}
delete cp;
std::cout << std::endl;
std::cout << " ==> ";
cp = new CharacterPool (OTHER);
for (size_t i = 0; i < cp->getPoolLength(); i++) {
char c = (*cp) [i];
if (c == 0) {
std::cout << std::endl << " ==> Zero encountered at position " << i << std::endl;
return 1;
}
std::cout << c;
}
delete cp;
std::cout << std::endl;
} catch (std::exception& ex) {
std::cout << typeid (ex).name() << ": " << ex.what() << std::endl;
return 1;
}
return 0;
}
|