Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion assignment-autotest
Submodule assignment-autotest deleted from 83e5df
2 changes: 1 addition & 1 deletion conf/username.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
your-github-username-here-in-conf-file
beaspin
2 changes: 1 addition & 1 deletion examples/autotest-validate/autotest-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ bool this_function_returns_false()
*/
const char *my_username()
{
return "todo-please-enter-your-username-here-in-my_username";
return "beaspin";
}
6 changes: 5 additions & 1 deletion student-test/assignment1/Test_validate_username.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ void test_validate_my_username()
* TODO: Replace the line below with your code here as described above to verify your /conf/username.txt
* config file and my_username() functions are setup properly
*/
TEST_ASSERT_TRUE_MESSAGE(false,"AESD students, please fix me!");
const char* expected_username = my_username();
char* actual_username = malloc_username_from_conf_file();
TEST_ASSERT_NOT_NULL_MESSAGE(actual_username, "Failed to read username from config file!");
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_username, actual_username,"Username mismatch!");
free(actual_username);
}
13 changes: 13 additions & 0 deletions test/assignment1/Test_assignment_validate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "unity.h"
#include <stdbool.h>
#include "examples/autotest-validate/autotest-validate.h"


/**
* Verify we can automated test code in the "examples" directory within your project
*/
void test_assignment_validate()
{
TEST_ASSERT_TRUE_MESSAGE(this_function_returns_true(),"The function should return true");
TEST_ASSERT_FALSE_MESSAGE(this_function_returns_false(),"The function should have returned false");
}
10 changes: 10 additions & 0 deletions test/assignment1/Test_hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "unity.h"
#include <stdbool.h>

void test_hello()
{
TEST_MESSAGE("Hello! Your unity setup is working!");
TEST_ASSERT_TRUE_MESSAGE(true,"This assertion passed!");
// Remove the comment on the line below to see what happens with failed assertions
// TEST_ASSERT_TRUE_MESSAGE(false,"This assertion failed (as expected)");
}
54 changes: 54 additions & 0 deletions test/assignment1/assignment-1-test-iteration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

. ./script-helpers

assignment_1_test_validation() {
filesdir=$1
numfiles=$2
writestr=$3
username=$4

./tester.sh ${numfiles} ${writestr}

if [ $? -ne 0 ]; then
add_validate_error "Expected zero success return from tester script"
fi

files_list=$(find ${filesdir} -name "${username}*.txt")
files_created=$(echo "${files_list}" | wc -l)

if [ ${files_created} -ne ${numfiles} ]; then
add_validate_error "expected ${numfiles} files created by ./tester.sh matching ${username}*.txt pattern within ${filesdir} but found ${files_created} with files list ${files_list}"
fi

./finder.sh

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if no parameters were specified"
fi

./finder.sh /tmp

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if search string was not specified"
fi

./finder.sh /non-exist-path "search"

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if a non-existent path was specified"
fi

output=$(./finder.sh ${filesdir} ${writestr})

echo ${output} | grep "number of files are ${numfiles}"

if [ $? -ne 0 ]; then
add_validate_error "Expected to find number of files listed as ${numfiles} by finder script, found ${output}"
fi

echo ${output} | grep "number of matching lines are ${numfiles}"
if [ $? -ne 0 ]; then
add_validate_error "Expected to find number of matching lines listed as ${numfiles} by finder script, found ${output}"
fi
}
41 changes: 41 additions & 0 deletions test/assignment1/assignment-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
pushd $(dirname $0)
source script-helpers

SCRIPTS_DIR=$(pwd)
SOURCE_DIR=$(realpath ${SCRIPTS_DIR}/../../../)

pushd ${SOURCE_DIR}/finder-app

./writer.sh
rc=$?
if [ $rc -ne 1 ]; then
add_validate_error "writer.sh should have exited with return value 1 if no parameters were specified"
fi

./writer.sh "$filedir"
rc=$?
if [ $rc -ne 1 ]; then
add_validate_error "writer.sh should have exited with return value 1 if write string is not specified"
fi

# Run finder-test.sh with default directory and random directory
./finder-test.sh
rc=$?
if [ $rc -ne 0 ]; then
add_validate_error "finder-test.sh execution for default directory failed with return code $rc"
fi

# generate directory name from random string
dir_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w32 | head -n 1)
./finder-test.sh 10 AELD_IS_FUN $dir_name

rc=$?
if [ $rc -ne 0 ]; then
add_validate_error "finder-test.sh execution for random directory failed with return code $rc"
fi

if [ ! -z "${validate_error}" ]; then
echo "Validation failed with error list ${validate_error}"
exit 1
fi
4 changes: 4 additions & 0 deletions test/assignment1/assignment-timeout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Specify the timeout for this assignment, in a format
# suitable for use with the timeout builtin.
# See http://man7.org/linux/man-pages/man1/timeout.1.html
assignment_timeout=5m
1 change: 1 addition & 0 deletions test/assignment1/script-helpers
45 changes: 45 additions & 0 deletions test/assignment1/username-from-conf-file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/**
* @return the name of the user from the conf file, or
* an empty string if not found. Must be freed by the caller
*/
static inline char *malloc_username_from_conf_file()
{
size_t len = 0;
// Note: this buffer will be reallocated in getline() as necessary
char *buffer = malloc(len + 1);
buffer[0] = '\0';

FILE *fp = fopen("conf/username.txt","r");
if ( fp != NULL ) {
/**
* See https://man7.org/linux/man-pages/man3/getline.3.html
*/
ssize_t bytes_read = getline(&buffer, &len, fp);

// remove whitespace from the end
while ( bytes_read > 0 && !isgraph(buffer[bytes_read-1]) ) {
buffer[bytes_read-1] = '\0';
bytes_read--;
}

// remove whitespace from the front
char *start = &buffer[0];
while ( bytes_read > 0 && !isgraph(start[0]) && (++start)[0] != '\0' ) {}
if ( start != buffer ) {
memmove(buffer, start, strlen(start)+1);
}

if ( bytes_read < 1 ) {
fprintf(stderr, "Could not find username in conf/username.txt\n");
} else {
printf("Read %s from conf/username.txt\n", buffer);
}
} else {
fprintf(stderr, "Could not open conf/username.txt for reading\n");
}
return buffer;
}
54 changes: 54 additions & 0 deletions test/assignment2/assignment-1-test-iteration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

. ./script-helpers

assignment_1_test_validation() {
filesdir=$1
numfiles=$2
writestr=$3
username=$4

./tester.sh ${numfiles} ${writestr}

if [ $? -ne 0 ]; then
add_validate_error "Expected zero success return from tester script"
fi

files_list=$(find ${filesdir} -name "${username}*.txt")
files_created=$(echo "${files_list}" | wc -l)

if [ ${files_created} -ne ${numfiles} ]; then
add_validate_error "expected ${numfiles} files created by ./tester.sh matching ${username}*.txt pattern within ${filesdir} but found ${files_created} with files list ${files_list}"
fi

./finder.sh

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if no parameters were specified"
fi

./finder.sh /tmp

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if search string was not specified"
fi

./finder.sh /non-exist-path "search"

if [ $? -ne 1 ]; then
add_validate_error "finder.sh should have exited with return value 1 if a non-existent path was specified"
fi

output=$(./finder.sh ${filesdir} ${writestr})

echo ${output} | grep "number of files are ${numfiles}"

if [ $? -ne 0 ]; then
add_validate_error "Expected to find number of files listed as ${numfiles} by finder script, found ${output}"
fi

echo ${output} | grep "number of matching lines are ${numfiles}"
if [ $? -ne 0 ]; then
add_validate_error "Expected to find number of matching lines listed as ${numfiles} by finder script, found ${output}"
fi
}
34 changes: 34 additions & 0 deletions test/assignment2/assignment-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
pushd $(dirname $0)
source script-helpers

SCRIPTS_DIR=$(pwd)
SOURCE_DIR=$(realpath ${SCRIPTS_DIR}/../../../)

pushd ${SOURCE_DIR}/finder-app

make clean
make

./writer
rc=$?
if [ $rc -ne 1 ]; then
add_validate_error "writer should have exited with return value 1 if no parameters were specified"
fi

./writer "$filedir"
rc=$?
if [ $rc -ne 1 ]; then
add_validate_error "writer should have exited with return value 1 if write string is not specified"
fi

./finder-test.sh
rc=$?
if [ $rc -ne 0 ]; then
add_validate_error "finder-test.sh execution failed with return code $rc"
fi

if [ ! -z "${validate_error}" ]; then
echo "Validation failed with error list ${validate_error}"
exit 1
fi
4 changes: 4 additions & 0 deletions test/assignment2/assignment-timeout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Specify the timeout for this assignment, in a format
# suitable for use with the timeout builtin.
# See http://man7.org/linux/man-pages/man1/timeout.1.html
assignment_timeout=5m
1 change: 1 addition & 0 deletions test/assignment2/script-helpers
101 changes: 101 additions & 0 deletions test/assignment3/Test_systemcalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "unity.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "../../../examples/systemcalls/systemcalls.h"
#include "file_write_commands.h"

/**
* This function should:
* 1) Call the do_system() function in systemcalls.c to perform the system() operation.
* 2) Obtain the value returned from return_string_validation() in file_write_commands.h within
* the assignment autotest submodule at assignment-autotest/test/assignment3/
* 3) Use unity assertion TEST_ASSERT_EQUAL_STRING_MESSAGE the two strings are equal. See
* the [unity assertion reference](https://github.com/ThrowTheSwitch/Unity/blob/master/docs/UnityAssertionsReference.md)
* 4) Test with strncmp fo string which return the full path for $HOME,
* and compare the value using TEST_ASSERT_EQUAL_INT16_MESSAGE.
* 5) Call the do_exec() function in systemcalls.c to perform fork(), execv() and wait() instead of using the system() command.
* 6) Test for a true and false message on the above commands using TEST_ASSERT_FALSE_MESSAGE(TRUE_MESSAGE).
* 7) Call the do_exec_redirect() function in systemcalls.c to perform the same operation as do_exec(),
* but redirect ouptut to standard out.
* 8) Obtain the value returned from return_string_validation() in file_write_commands.h within
* the assignment autotest submodule at assignment-autotest/test/assignment3/
* 9) Use unity assertion TEST_ASSERT_EQUAL_STRING_MESSAGE the two strings are equal.
*
**/
#define REDIRECT_FILE "testfile.txt"

void test_systemcalls()
{

printf("Running tests at %s : function %s\n",__FILE__,__func__);
TEST_ASSERT_TRUE_MESSAGE(do_system("echo this is a test > " REDIRECT_FILE ),
"do_system call should return true running echo command");
char *test_string = malloc_first_line_of_file(REDIRECT_FILE);
printf("system() echo this is a test returned %s\n", test_string);
// Testing implementation with testfile.txt output
TEST_ASSERT_EQUAL_STRING_MESSAGE("this is a test", test_string,
"Did not find \"this is a test\" in output of echo command."
" Is your system() function implemented properly?");
free((void *)test_string);

TEST_ASSERT_TRUE_MESSAGE(do_system("echo \"home is $HOME\" > " REDIRECT_FILE),
"do_system call should return true running echo command");
test_string = malloc_first_line_of_file(REDIRECT_FILE);
int test_value = strncmp(test_string, "home is /", 9);
printf("system() echo home is $HOME returned: %s\n", test_string);
// Testing implementation with testfile.txt output
TEST_ASSERT_EQUAL_INT16_MESSAGE(test_value, 0,
"The first 9 chars echoed should be \"home is /\". The last chars will include "
"the content of the $HOME variable");
test_value = strncmp(test_string, "home is $HOME", 9);
TEST_ASSERT_NOT_EQUAL_INT16_MESSAGE(test_value, 0,
"The $HOME parameter should be expanded when using system()");
free((void *)test_string);

}

void test_exec_calls()
{
printf("Running tests at %s : function %s\n",__FILE__,__func__);
TEST_ASSERT_FALSE_MESSAGE(do_exec(2, "echo", "Testing execv implementation with echo"),
"The exec() function should have returned false since echo was not specified"
" with absolute path as a command and PATH expansion is not performed.");
TEST_ASSERT_FALSE_MESSAGE(do_exec(3, "/usr/bin/test","-f","echo"),
"The exec() function should have returned false since echo was not specified"
" with absolute path in argument to the test executable.");
TEST_ASSERT_TRUE_MESSAGE(do_exec(3, "/usr/bin/test","-f","/bin/echo"),
"The function should return true since /bin/echo represents the echo command"
" and test -f verifies this is a valid file");
}

void test_exec_redirect_calls()
{
printf("Running tests at %s : function %s\n",__FILE__,__func__);
do_exec_redirect(REDIRECT_FILE, 3, "/bin/sh", "-c", "echo home is $HOME");
char *test_string = malloc_first_line_of_file(REDIRECT_FILE);
TEST_ASSERT_NOT_NULL_MESSAGE(test_string,"Nothing written to file at " REDIRECT_FILE );
if( test_string != NULL ) {
int test_value = strncmp(test_string, "home is /", 9);
printf("execv /bin/sh -c echo home is $HOME returned %s\n", test_string);
// Testing implementation with testfile.txt output
TEST_ASSERT_EQUAL_INT16_MESSAGE(test_value, 0,
"The first 9 chars echoed should be \"home is /\". The last chars will include "
"the content of the $HOME variable");
test_value = strncmp(test_string, "home is $HOME", 9);
TEST_ASSERT_NOT_EQUAL_INT16_MESSAGE(test_value, 0,
"The $HOME parameter should be expanded when using /bin/sh with do_exec()");
free(test_string);
}

do_exec_redirect(REDIRECT_FILE, 2, "/bin/echo", "home is $HOME");
test_string = malloc_first_line_of_file(REDIRECT_FILE);
TEST_ASSERT_NOT_NULL_MESSAGE(test_string,"Nothing written to file at " REDIRECT_FILE );
if( test_string != NULL ) {
printf("execv /bin/echo home is $HOME returned %s\n", test_string);
// Testing implementation with testfile.txt output
TEST_ASSERT_EQUAL_STRING_MESSAGE("home is $HOME", test_string,
"The variable $HOME should not be expanded using execv()");
free(test_string);
}
}
Loading