Skip to content

Instantly share code, notes, and snippets.

@astex
Created February 7, 2016 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astex/12e0805c9e0ef86eedc1 to your computer and use it in GitHub Desktop.
Save astex/12e0805c9e0ef86eedc1 to your computer and use it in GitHub Desktop.
Test client for gfclient.c/gfserver.c
#include <stdio.h> // fprintf, stdout
#include <stdlib.h> // malloc
#include <getopt.h> // getopt_long, option, required_argument
#include <string.h> // strcmp
#include "gfclient.h"
#define USAGE \
"usage:\n" \
" webclient [options]\n" \
"options:\n" \
" -h [host] Server address (Default: 127.0.0.1)\n" \
" -p [port] Server port (Default: 8888)\n" \
" -? Show this help message\n"
static struct option gLongOptions[] = {
{"host", required_argument, NULL, 's'},
{"port", required_argument, NULL, 'p'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
/* A client configuration. */
struct config {
char *host;
unsigned int port;
};
/* An individual test case. */
struct test {
struct config* cnf; // Client configuration
char *path; // Download path
char *status; // Expected status
gfcrequest_t *gfr;
};
static void usage() {
fprintf(stdout, "%s", USAGE);
}
/* Populate command line arguments into a configuration. */
static struct config *init(int argc, char **argv) {
struct config *cnf = malloc(sizeof(struct config));
int option_char = 0;
cnf->host = "127.0.0.1";
cnf->port = 8888;
while ((option_char = getopt_long(argc, argv, "h:p:?", gLongOptions, NULL)) != -1) {
switch (option_char) {
case 'h':
cnf->host = optarg;
break;
case 'p':
cnf->port = atoi(optarg);
break;
case '?':
usage();
exit(0);
break;
default:
usage();
exit(1);
}
}
return cnf;
}
/* Free memory from a client configuration. */
static void cleanconfig(struct config *cnf) {
free(cnf);
}
/* Create a test case. */
static struct test *createtest(struct config *cnf, char *path, char* status) {
struct test *tst = malloc(sizeof(struct test));
tst->cnf = cnf;
tst->path = path;
tst->status = status;
return tst;
}
/* Makes a client for a test case. */
static void maketestrequest(struct test *tst) {
tst->gfr = gfc_create();
gfc_set_server(tst->gfr, tst->cnf->host);
gfc_set_port(tst->gfr, tst->cnf->port);
gfc_set_path(tst->gfr, tst->path);
}
/* Prints a message when a condition is false. */
static int is(int cond, char *msg) {
if (!cond)
printf("[Assertion Failed] %s.\n", msg);
return cond;
}
/* Run a test case. */
static void runtest(struct test *tst) {
int r;
printf("Running test case to fetch %s with status %s.\n", tst->path, tst->status);
maketestrequest(tst);
r = gfc_perform(tst->gfr);
if (
is(r >= 0, "gfc_perform() errored out") &&
is(!strcmp(gfc_strstatus(gfc_get_status(tst->gfr)), tst->status),
"Status is incorrect") &&
is(gfc_get_bytesreceived(tst->gfr) == gfc_get_filelen(tst->gfr),
"Bytes received is not the same as file length")
)
printf("[Success]\n");
printf("-----\n");
}
/* Free memory from a test case. */
static void cleantest(struct test *tst) {
free(tst);
}
int main(int argc, char **argv) {
// Command line options
struct config *cnf = init(argc, argv);
struct test *tests[12] = {
createtest(cnf, "/courses/ud923/filecorpus/1kb-sample-file-0.png", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/1kb-sample-file-1.html", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/1kb-sample-file-2.png", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/1kb-sample-file-3.jpg", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/1kb-sample-file-4.png", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/moranabovejacksonlake.jpg", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/paraglider.jpg", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/road.jpg", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/yellowstone.jpg", "OK"),
createtest(cnf, "/courses/ud923/filecorpus/empty.txt", "OK"),
createtest(cnf, "/fakefile.notreal", "FILE_NOT_FOUND"),
createtest(cnf, "/courses/ud923/filecorpus", "ERROR")};
for (int i = 0; i < 12; i++)
runtest(tests[i]);
for (int i = 0; i < 12; i++)
cleantest(tests[i]);
cleanconfig(cnf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment