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
|
/*
20140429
Jan Mojzis
Public domain.
*/
#include "str.h"
#include "byte.h"
#include "fail.h"
#include "loginshell.h"
static char buf[10];
static struct vector {
const char *in;
const char *out;
} vectors[] = {
{ "", "-" },
{ "/", "-" },
{ "//", "-" },
{ "///", "-" },
{ "////", "-" },
{ "/////", "-" },
{ "/a/b/c/d/", "-" },
{ "/a///d/", "-" },
{ "//b/c//", "-" },
{ "x/", "-" },
{ "x/x/xx/xxx/", "-" },
{ "sh", "-sh" },
{ "/bin/bash", "-bash" },
{ "///////dash", "-dash" },
{ "///////-ksh", "--ksh" },
/* truncation */
{ "longlongsh", "-longlong" },
{ 0, 0 }
};
static void testok(void) {
long long i, len;
for (i = 0; vectors[i].in; ++i) {
if (!loginshell(buf, sizeof buf, vectors[i].in)) fail("loginshell failure");
len = str_len(vectors[i].out);
if (str_len(buf) != len) fail("loginshell failure");
if (!byte_isequal(buf, len, vectors[i].out)) fail("loginshell failure");
}
}
static void testbad(void) {
if (loginshell(buf, -1, "")) fail("loginshell failure");
if (loginshell(buf, 0, "")) fail("loginshell failure");
if (loginshell(buf, 1, "")) fail("loginshell failure");
if (loginshell(buf, 1, "//")) fail("loginshell failure");
}
int main(void) {
testok();
testbad();
_exit(0);
}
|