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
|
/*
* pam_abl - a PAM module and program for automatic blacklisting of hosts and users
*
* Copyright (C) 2005-2012
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "test.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void testSplit(const char *cmd, int nofParts, const char **parts) {
char *command = strdup(cmd);
int nof = splitCommand(command, NULL, NULL);
if (nof != nofParts) {
printf(" The number of parts for \"%s\" is not %d.\n", command, nofParts);
free(command);
return;
}
if (nofParts > 0) {
char **resultParts = malloc(sizeof(char*)*nofParts);
int p = 0;
//just fill in something random, that will crash if accessed
for (; p<nofParts; ++p)
resultParts[p] = (char*)(0x1);
nof = splitCommand(command, resultParts, NULL);
if (nof != nofParts) {
printf(" While actually parsing: The number of parts for \"%s\" is not %d.\n", command, nofParts);
free(command);
free(resultParts);
return;
}
int i = 0;
for (i = 0; i < nofParts; ++i) {
if (strcmp(parts[i], resultParts[i]) != 0) {
printf(" While comparing parts: \"%s\" != \"%s\"\n", resultParts[i], parts[i]);
free(command);
free(resultParts);
return;
}
}
free(resultParts);
}
free(command);
return;
}
static void testSplitExpectError(const char *cmd) {
char *command = strdup(cmd);
int nof = splitCommand(command, NULL, NULL);
if (nof >= 0)
printf(" Expected an error while splitting \"%s\".\n", command);
free(command);
}
static void testCommandNormal() {
const char *expected[] = {
"ping",
"-c",
"10",
"127.0.0.1"
};
testSplit("[ping][-c][10][127.0.0.1]", 4, expected);
testSplit("[ping] [-c] [10] [127.0.0.1]", 4, expected);
}
static void testIgnoreNonEnclosed() {
const char *expected[] = {
"command",
"arg1",
"arg2",
"arg3"
};
testSplit("command[command]arg[arg1]arg[arg2]arg[arg3]argie", 4, expected);
testSplit(" [command] [arg1] [arg2] [arg3] ", 4, expected);
testSplit("lol[command] sdfsdfsdf eerer sder [arg1]sdfsdfasd[arg2]_sdfaewr+adfasdf sdd [arg3] sdfsdfwer", 4, expected);
}
static void testEmpty() {
const char *expected[] = {
"",
"arg1",
"",
""
};
testSplit("[][arg1][][]", 4, expected);
testSplit("command[]arg[arg1]arg[]arg[]argie", 4, expected);
}
static void testWithEscapeChars() {
const char *expected[] = {
"command",
"arg1",
"arg\\2",
"arg3"
};
testSplit("\\n \\b [command] [arg\\1] \\c [arg\\\\2] [arg\\3] ", 4, expected);
}
static void testEscapeBracket() {
const char *expected[] = {
"command[]",
"[]arg1",
"[arg2]",
"][arg\\3]["
};
testSplit("[command\\[\\]] [\\[\\]arg1] [\\[arg2\\]] [\\]\\[arg\\\\3\\]\\[]", 4, expected);
}
static void testMultipleOpen() {
testSplitExpectError("[[command] [arg]");
testSplitExpectError("[[command]] [arg]");
testSplitExpectError("[command] [[arg]");
testSplitExpectError("[command] [arg] [lol");
testSplitExpectError("[command] [arg] [");
}
static void testNoClosing() {
testSplitExpectError("[command [arg]");
testSplitExpectError("[command] [arg");
testSplitExpectError("[command] [arg [arg]");
}
static void testEmptyBrackets() {
const char *expected[] = {
"",
"arg",
"",
"arg2"
};
testSplit("[] [arg] [] [arg2]", 4, expected);
}
static void testNoCommand() {
testSplit("blaat blaat \\[ \\] this should all be ignored", 0, NULL);
testSplit("", 0, NULL);
}
void testConfig() {
printf("Config test start.\n");
printf(" Starting testCommandNormal.\n");
testCommandNormal();
printf(" Starting testIgnoreNonEnclosed.\n");
testIgnoreNonEnclosed();
printf(" Starting testEmpty.\n");
testEmpty();
printf(" Starting testWithEscapeChars.\n");
testWithEscapeChars();
printf(" Starting testEscapeBracket.\n");
testEscapeBracket();
printf(" Starting testMultipleOpen.\n");
testMultipleOpen();
printf(" Starting testNoClosing.\n");
testNoClosing();
printf(" Starting testEmptyBrackets.\n");
testEmptyBrackets();
printf(" Starting testNoCommand.\n");
testNoCommand();
printf("Config test end.\n");
}
|