tests: add cram based unit tests
authorPetr Štetiar <ynezz@true.cz>
Mon, 12 Oct 2020 12:06:11 +0000 (14:06 +0200)
committerPetr Štetiar <ynezz@true.cz>
Tue, 27 Oct 2020 22:03:04 +0000 (23:03 +0100)
For the start with cgi-exec testing.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
tests/CMakeLists.txt
tests/cram/CMakeLists.txt [new file with mode: 0644]
tests/cram/test-cases/cgi-exec-01.txt [new file with mode: 0644]
tests/cram/test-cases/cgi-exec-02.txt [new file with mode: 0644]
tests/cram/test-cases/cgi-exec-03.txt [new file with mode: 0644]
tests/cram/test-san_cgi-exec.t [new file with mode: 0644]
tests/cram/test_cgi-exec.t [new file with mode: 0644]

index efad20642dd6a5e6db307cccc4068f3a4bfe92d5..5306b913c0d932e0908cd87f19f4a3ebdf09a3f3 100644 (file)
@@ -1,3 +1,5 @@
+ADD_SUBDIRECTORY(cram)
+
 IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
   ADD_SUBDIRECTORY(fuzz)
   ADD_SUBDIRECTORY(fuzz-multipart-parser)
diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d8b317c
--- /dev/null
@@ -0,0 +1,25 @@
+FIND_PACKAGE(PythonInterp 3 REQUIRED)
+FILE(GLOB test_cases "test_*.t")
+
+IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
+  FILE(GLOB test_cases_san "test-san_*.t")
+ENDIF()
+
+SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv")
+SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip")
+SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram")
+
+ADD_CUSTOM_COMMAND(
+       OUTPUT ${PYTHON_VENV_CRAM}
+       COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR}
+       COMMAND ${PYTHON_VENV_PIP} install cram
+)
+ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM})
+
+ADD_TEST(
+       NAME cram
+       COMMAND ${PYTHON_VENV_CRAM} ${test_cases} ${test_cases_san}
+       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "BUILD_BIN_DIR=$<TARGET_FILE_DIR:cgi-io>")
diff --git a/tests/cram/test-cases/cgi-exec-01.txt b/tests/cram/test-cases/cgi-exec-01.txt
new file mode 100644 (file)
index 0000000..0b03e8b
--- /dev/null
@@ -0,0 +1 @@
+sessionid=0&
diff --git a/tests/cram/test-cases/cgi-exec-02.txt b/tests/cram/test-cases/cgi-exec-02.txt
new file mode 100644 (file)
index 0000000..a4969b8
--- /dev/null
@@ -0,0 +1 @@
+sessionid=0&command=basename /tmp/foo&
diff --git a/tests/cram/test-cases/cgi-exec-03.txt b/tests/cram/test-cases/cgi-exec-03.txt
new file mode 100644 (file)
index 0000000..3561f10
--- /dev/null
@@ -0,0 +1 @@
+sessionid=0&command=basename /king/banik/1922&filename=output.txt&mimetype=0&
diff --git a/tests/cram/test-san_cgi-exec.t b/tests/cram/test-san_cgi-exec.t
new file mode 100644 (file)
index 0000000..e4a3356
--- /dev/null
@@ -0,0 +1,30 @@
+check that cgi-exec is producing expected results:
+
+  $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH"
+  $ ln -sf $BUILD_BIN_DIR/cgi-io-san $BUILD_BIN_DIR/cgi-exec
+
+  $ for file in $(LC_ALL=C find "$TESTDIR/test-cases" -type f | sort); do
+  >   export CONTENT_TYPE="application/x-www-form-urlencoded"; \
+  >   export CONTENT_LENGTH="$(wc -c < $file)"; \
+  >   printf "\n[-] testing: $(basename $file)\n"; \
+  >   cgi-exec < $file; \
+  > done
+  
+  [-] testing: cgi-exec-01.txt
+  Status: 400 Invalid command parameter\r (esc)
+  Content-Type: text/plain\r (esc)
+  \r (esc)
+  Invalid command parameter
+  
+  [-] testing: cgi-exec-02.txt
+  Status: 200 OK\r (esc)
+  Content-Type: application/octet-stream\r (esc)
+  \r (esc)
+  foo
+  
+  [-] testing: cgi-exec-03.txt
+  Status: 200 OK\r (esc)
+  Content-Type: 0\r (esc)
+  Content-Disposition: attachment; filename="output.txt"\r (esc)
+  \r (esc)
+  1922
diff --git a/tests/cram/test_cgi-exec.t b/tests/cram/test_cgi-exec.t
new file mode 100644 (file)
index 0000000..d5cffb4
--- /dev/null
@@ -0,0 +1,30 @@
+check that cgi-exec is producing expected results:
+
+  $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH"
+  $ ln -sf $BUILD_BIN_DIR/cgi-io $BUILD_BIN_DIR/cgi-exec
+
+  $ for file in $(LC_ALL=C find "$TESTDIR/test-cases" -type f | sort); do
+  >   export CONTENT_TYPE="application/x-www-form-urlencoded"; \
+  >   export CONTENT_LENGTH="$(wc -c < $file)"; \
+  >   printf "\n[-] testing: $(basename $file)\n"; \
+  >   valgrind --quiet --leak-check=full cgi-exec < $file; \
+  > done
+  
+  [-] testing: cgi-exec-01.txt
+  Status: 400 Invalid command parameter\r (esc)
+  Content-Type: text/plain\r (esc)
+  \r (esc)
+  Invalid command parameter
+  
+  [-] testing: cgi-exec-02.txt
+  Status: 200 OK\r (esc)
+  Content-Type: application/octet-stream\r (esc)
+  \r (esc)
+  foo
+  
+  [-] testing: cgi-exec-03.txt
+  Status: 200 OK\r (esc)
+  Content-Type: 0\r (esc)
+  Content-Disposition: attachment; filename="output.txt"\r (esc)
+  \r (esc)
+  1922