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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#include "Config.h"
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include "Bootstrap.h"
#include "Str.h"
#include "List.h"
#include "system/System.h"
#include "system/Command.h"
#include "system/Time.h"
/**
* Command.c unit tests.
*/
static void onExec(Process_T P) {
assert(P);
char buf[STRLEN];
// Child process info
printf("\tSubprocess ((pid=%d) created with cwd (%s)\n", Process_getPid(P), Process_getDir(P));
InputStream_T in = Process_getInputStream(P);
OutputStream_T out = Process_getOutputStream(P);
InputStream_T err = Process_getErrorStream(P);
printf("\tSub-Process is %s\n", Process_isRunning(P) ? "running" : "not running");
printf("\tCommunication with child:\n");
if (! InputStream_readLine(in, buf, STRLEN)) {
InputStream_readLine(err, buf, STRLEN);
printf("\tError in script: %s\n", buf);
} else {
printf("\t%s", buf);
OutputStream_print(out, "Elessar Telcontar\n");
assert(OutputStream_flush(out) > 0);
char *line = InputStream_readLine(in, buf, STRLEN);
assert(line);
printf("\t%s", line);
}
Process_free(&P);
assert(! P);
}
static void onTerminate(Process_T P) {
assert(P);
printf("\tTest terminate subprocess ((pid=%d)\n", Process_getPid(P));
assert(Process_isRunning(P));
Process_terminate(P);
printf("\tWaiting on process to terminate.. ");
printf("Process exited with status: %d\n", Process_waitFor(P));
Process_free(&P);
assert(! P);
}
static void onKill(Process_T P) {
assert(P);
printf("\tTest kill subprocess ((pid=%d)\n", Process_getPid(P));
assert(Process_isRunning(P));
Process_kill(P);
printf("\tWaiting on process to exit.. ");
printf("Process exited with status: %d\n", Process_waitFor(P));
Process_free(&P);
assert(! P);
}
int main(void) {
Bootstrap(); // Need to initialize library
printf("============> Start Command Tests\n\n");
printf("=> Test1: create/destroy\n");
{
Command_T c = Command_new("/bin/sh", "-c", "ps -aef|grep monit", NULL);
assert(c);
Command_free(&c);
assert(!c);
}
printf("=> Test1: OK\n\n");
printf("=> Test2: set and get uid/gid\n");
{
Command_T c = Command_new("/bin/sh", "-c", "ps -aef|grep monit", NULL);
assert(c);
// Check that default is 0
assert(Command_getUid(c) == 0);
assert(Command_getGid(c) == 0);
// set and test uid and gid
Command_setUid(c,42);
assert(Command_getUid(c) == 42);
Command_setGid(c,148);
assert(Command_getGid(c) == 148);
Command_free(&c);
assert(!c);
}
printf("=> Test2: OK\n\n");
printf("=> Test3: set and get working directory\n");
{
Command_T c = Command_new("/bin/sh", "-c", "ps -aef|grep monit", NULL);
assert(c);
// Check that a default working directory is NULL. I.e. current directory
assert(! Command_getDir(c));
// Check that a NULL working directory is allowed, meaning the calling process's current directory
Command_setDir(c, NULL);
// Set and get
Command_setDir(c, "/tmp/");
assert(Str_isEqual(Command_getDir(c), "/tmp")); // trailing separator is removed upon set
// Check invalid value
TRY
{
Command_setDir(c, "/hubba/bubba");
printf("Test failed\n");
exit(1);
}
ELSE
{
assert(Str_isEqual(Command_getDir(c), "/tmp"));
}
END_TRY;
Command_free(&c);
assert(!c);
}
printf("=> Test3: OK\n\n");
printf("=> Test4: set and get env\n");
{
Command_T c = Command_new("/bin/sh", "-c", "ps -aef|grep monit", NULL);
assert(c);
// Check default PATH
assert(Str_isEqual(Command_getEnv(c, "PATH"), "/bin:/usr/bin:/usr/local/bin:/opt/csw/bin:/usr/sfw/bin"));
// Set and get
Command_setEnv(c, "LANG", "C");
assert(Str_isEqual(Command_getEnv(c, "LANG"), "C"));
Command_setEnv(c, "SHELL", "/bin/bash");
assert(Str_isEqual(Command_getEnv(c, "SHELL"), "/bin/bash"));
// setEnvString
// Check default PATH
assert(Str_isEqual(Command_getEnv(c, "PATH"), "/bin:/usr/bin:/usr/local/bin:/opt/csw/bin:/usr/sfw/bin"));
// Set and get env string
Command_vSetEnv(c, "PATH=/usr/bin;SHELL=/bin/bash");
assert(Str_isEqual(Command_getEnv(c, "PATH"), "/usr/bin"));
assert(Str_isEqual(Command_getEnv(c, "SHELL"), "/bin/bash"));
// With space in string
Command_vSetEnv(c, "LANG = C");
assert(Str_isEqual(Command_getEnv(c, "LANG"), "C"));
Command_vSetEnv(c, "PATH = /usr/bin ; SHELL = /bin/bash");
assert(Str_isEqual(Command_getEnv(c, "PATH"), "/usr/bin"));
assert(Str_isEqual(Command_getEnv(c, "SHELL"), "/bin/bash"));
// Invalid String
Command_vSetEnv(c, "HELLO:WORLD");
assert(! Command_getEnv(c, "HELLO"));
assert(! Command_getEnv(c, "HELLO:WORLD"));
// Varargs
Command_vSetEnv(c, "PATH=%s; TERM=%s;", "/bin", "vterm");
assert(Str_isEqual(Command_getEnv(c, "PATH"), "/bin"));
assert(Str_isEqual(Command_getEnv(c, "TERM"), "vterm"));
Command_free(&c);
assert(!c);
}
printf("=> Test4: OK\n\n");
printf("=> Test5: set and get Command\n");
{
Command_T c = Command_new("/bin/sh", "-c", "ps -aef|grep monit", NULL);
assert(c);
List_T l = Command_getCommand(c);
assert(Str_isEqual(l->head->e, "/bin/sh"));
assert(Str_isEqual(l->head->next->e, "-c"));
assert(Str_isEqual(l->head->next->next->e, "ps -aef|grep monit"));
Command_free(&c);
assert(!c);
}
printf("=> Test5: OK\n\n");
printf("=> Test6: execute\n");
{
// Program producing error
Command_T c = Command_new("/bin/sh", "-c", "baluba;", NULL);
assert(c);
Command_setDir(c, "/");
printf("\tThis should produce an error:\n");
onExec(Command_execute(c));
Command_free(&c);
assert(!c);
// Correct program
c = Command_new("/bin/sh", "-c", "echo \"Please enter your name:\";read name;echo \"Hello $name\";", NULL);
assert(c);
onExec(Command_execute(c));
Command_free(&c);
assert(!c);
}
printf("=> Test6: OK\n\n");
printf("=> Test7: terminate and kill sub-process\n");
{
// Test terminate
Command_T c = Command_new("/bin/sh", "-c", "sleep 30;", NULL);
assert(c);
onTerminate(Command_execute(c));
Command_free(&c);
assert(!c);
// Test kill
c = Command_new("/bin/sh", "-c", "trap 1 2 15; sleep 30; ", NULL);
assert(c);
onKill(Command_execute(c));
Command_free(&c);
assert(!c);
}
printf("=> Test7: OK\n\n");
printf("============> Command Tests: OK\n\n");
return 0;
}
|