From aa1bbd5867b0904dc49c7b683099e808e8feecf4 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Tue, 23 Apr 2024 04:50:39 +0000 Subject: [PATCH] chore(tests): add tests for CI automation testing --- .github/workflows/build_and_test.yml | 18 +++++++++ .gitignore | 1 + configure | 55 +++++++++++++++++++++++++++- t/aes.c | 33 +++++++++++++++++ t/base64.c | 33 +++++++++++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build_and_test.yml create mode 100644 t/aes.c create mode 100644 t/base64.c diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml new file mode 100644 index 0000000..4206a93 --- /dev/null +++ b/.github/workflows/build_and_test.yml @@ -0,0 +1,18 @@ +name: Build & Test + +on: + pull_request: + branches: + - '*' + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build and Test + run: | + ./configure && make && make test && export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH && make run diff --git a/.gitignore b/.gitignore index 128fee0..aa4bd88 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ Makefile lib/ objs/ +bin/ build/ conf/melon.conf diff --git a/configure b/configure index 1637310..4182e10 100755 --- a/configure +++ b/configure @@ -432,7 +432,7 @@ generate_Makefile() { echo "compile: MKDIR \$(OBJS) \$(MELONSO) \$(MELONA)" >> Makefile fi echo "clean:" >> Makefile - echo -e "\trm -fr objs lib Makefile" >> Makefile + echo -e "\trm -fr objs lib bin Makefile" >> Makefile echo "MKDIR :" >> Makefile echo -e "\ttest -d objs || mkdir objs" >> Makefile echo -e "\ttest -d lib || mkdir lib" >> Makefile @@ -462,6 +462,59 @@ generate_Makefile() { echo -e "\ttest -d $install_path/conf || cp -fr conf $install_path" >> Makefile echo -e "\ttest -d $melang_script_path/trace || cp -fr trace $melang_script_path" >> Makefile + echo "TEST_MKDIR: " >> Makefile + echo -e "\ttest -d bin || mkdir -p bin" >> Makefile + for fname in ${select_files[@]} + do + fname=$(echo $fname | sed 's/^src\/mln_//') + binname=`echo $fname | cut -d '.' -f 1` + fname="t/$fname" + if [ -f $fname ]; then + echo -n "bin/$binname : " >> Makefile + for header in `cpp -MM -MG $fname 2> /dev/null` + do + suffix=`echo $header | cut -d '.' -f 2` + if [ $suffix = 'c' ]; then + echo -n $header >> Makefile + echo -n " " >> Makefile + continue + fi + if [ $suffix != 'h' ]; then + continue + fi + test -e include/$header && echo -n "include/$header " >> Makefile + done + echo "" >> Makefile + + echo -e "\t\$(CC) -o \$@ $fname $debug -Iinclude -Llib -lmelon" >> Makefile + fi + done + echo -e "BINS = \\" >> Makefile + for fname in ${select_files[@]} + do + fname=$(echo $fname | sed 's/^src\/mln_//') + binname="bin/"`echo $fname | cut -d '.' -f 1` + fname="t/$fname" + if [ -f $fname ]; then + echo " $binname \\" >> Makefile + fi + done + echo "" >> Makefile + echo "test: TEST_MKDIR \$(BINS)" >> Makefile + echo "" >> Makefile + + echo "run:" >> Makefile + for fname in ${select_files[@]} + do + fname=$(echo $fname | sed 's/^src\/mln_//') + binname="bin/"`echo $fname | cut -d '.' -f 1` + fname="t/$fname" + if [ -f $fname ]; then + echo -e "\t./$binname && \\" >> Makefile + fi + done + echo "echo 'Done'" >> Makefile + for fname in ${select_files[@]} do objname=`basename $fname | cut -d '.' -f 1`".o" diff --git a/t/aes.c b/t/aes.c new file mode 100644 index 0000000..ae4e32e --- /dev/null +++ b/t/aes.c @@ -0,0 +1,33 @@ +#include +#include +#include "mln_string.h" +#include "mln_aes.h" + +int main(int argc, char *argv[]) +{ + mln_aes_t a; + char p[] = "1234567890123456"; + mln_string_t s; + + if (mln_aes_init(&a, (mln_u8ptr_t)"abcdefghijklmnop", M_AES_128) < 0) { + fprintf(stderr, "aes init failed\n"); + return -1; + } + + mln_string_set(&s, p); + if (mln_aes_encrypt(&a, s.data) < 0) { + fprintf(stderr, "aes encrypt failed\n"); + return -1; + } + write(STDOUT_FILENO, s.data, s.len); + write(STDOUT_FILENO, "\n", 1); + + if (mln_aes_decrypt(&a, s.data) < 0) { + fprintf(stderr, "aes decrypt failed\n"); + return -1; + } + write(STDOUT_FILENO, s.data, s.len); + write(STDOUT_FILENO, "\n", 1); + + return 0; +} diff --git a/t/base64.c b/t/base64.c new file mode 100644 index 0000000..23cf0ad --- /dev/null +++ b/t/base64.c @@ -0,0 +1,33 @@ +#include +#include +#include "mln_string.h" +#include "mln_base64.h" + +int main(int argc, char *argv[]) +{ + mln_string_t text = mln_string("Hello"); + mln_string_t tmp; + mln_u8ptr_t p1, p2; + mln_uauto_t len1, len2; + + if (mln_base64_encode(text.data, text.len, &p1, &len1) < 0) { + fprintf(stderr, "encode failed\n"); + return -1; + } + mln_string_nset(&tmp, p1, len1); + write(STDOUT_FILENO, tmp.data, tmp.len); + write(STDOUT_FILENO, "\n", 1); + + if (mln_base64_decode(p1, len1, &p2, &len2) < 0) { + fprintf(stderr, "decode failed\n"); + return -1; + } + mln_string_nset(&tmp, p2, len2); + write(STDOUT_FILENO, tmp.data, tmp.len); + write(STDOUT_FILENO, "\n", 1); + + mln_base64_free(p1); + mln_base64_free(p2); + + return 0; +}