Skip to content

Commit

Permalink
fix(watcher_unix): Refactor & Add test for watcher_unix (#218)
Browse files Browse the repository at this point in the history
* add test for watcher_unix

* fix watcher_unix

* add test for watcher_unix

* resolve comments

* add comment for parse fileName

* refactor watcher_unix

* add unlimited memory & TLE test

* fix macos build

* format code

* no need to resolve symlink

* add testcases

* minor fix

* minor fix (consistent)

* remove duplicate include

* add more testcases for watcher_unix

* rename RE test
  • Loading branch information
Dust1404 authored and alphagocc committed Aug 16, 2023
1 parent 3a38f31 commit 4788f9a
Show file tree
Hide file tree
Showing 23 changed files with 441 additions and 202 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/watcher_linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Watcher for Linux Test

on:
push:
branches: [ "master" ]
paths: [ "unix/**" ]
pull_request:
branches: [ "master" ]
paths: [ "unix/**" ]
workflow_dispatch:

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} unix/test

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --verbose -C ${{env.BUILD_TYPE}}

40 changes: 40 additions & 0 deletions .github/workflows/watcher_macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Watcher for MacOS Test

on:
push:
branches: [ "master" ]
paths: [ "unix/**" ]
pull_request:
branches: [ "master" ]
paths: [ "unix/**" ]
workflow_dispatch:

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: macos-13

steps:
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} unix/test

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --verbose -C ${{env.BUILD_TYPE}}

4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ if(WIN32)
add_executable(lemon ${GUI_TYPE} ${LEMON_FULL_SOURCES})
else()
if(APPLE)
add_executable(watcher_unix unix/watcher_macos.mm)
add_executable(watcher_unix unix/watcher_unix.cpp unix/watcher_macos.mm)
else()
add_executable(watcher_unix unix/watcher_linux.cpp)
add_executable(watcher_unix unix/watcher_unix.cpp unix/watcher_linux.cpp)
endif()
configure_file(unix/watcher.qrc ${CMAKE_BINARY_DIR} COPYONLY)
list(APPEND LEMON_FULL_SOURCES ${CMAKE_BINARY_DIR}/watcher.qrc)
Expand Down
34 changes: 34 additions & 0 deletions unix/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.10.0)

project(lemon_watcher_unix_test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(APPLE)
add_executable(watcher_unix ${CMAKE_CURRENT_SOURCE_DIR}/../watcher_unix.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../watcher_macos.mm)
else()
add_executable(watcher_unix ${CMAKE_CURRENT_SOURCE_DIR}/../watcher_unix.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../watcher_linux.cpp)
endif()

add_executable(hello hello.c)
add_executable(mle_static mle_static.c)
file(COPY hello.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
add_executable(tle tle.c)
add_executable(add add.c)
add_executable(re re.c)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/scripts DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

enable_testing()
add_test(NAME watcher_run_c_test COMMAND python3 scripts/run.py)
add_test(NAME watcher_run_sh_test COMMAND python3 scripts/run_sh.py)
add_test(NAME watcher_MLE_static_test COMMAND python3 scripts/mle_static.py)
add_test(NAME watcher_unlimit_memory_test COMMAND python3 scripts/unlimit.py)
add_test(NAME watcher_TLE_test COMMAND python3 scripts/tle.py)
add_test(NAME watcher_filename_with_space_test COMMAND python3 scripts/space.py)
add_test(NAME watcher_symlink_abs_test COMMAND python3 scripts/symlink_abs.py)
add_test(NAME watcher_symlink_rel_test COMMAND python3 scripts/symlink_rel.py)
add_test(NAME watcher_redirect_IO_test COMMAND python3 scripts/redirect.py)
add_test(NAME watcher_RE_test COMMAND python3 scripts/runtimeerr.py)
10 changes: 10 additions & 0 deletions unix/test/add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

int main() {
int a, b;

scanf("%d%d", &a, &b);
printf("%d\n", a + b);

return 0;
}
7 changes: 7 additions & 0 deletions unix/test/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

int main() {
printf("Hello World!\n");

return 0;
}
1 change: 1 addition & 0 deletions unix/test/hello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "Hello World!"
9 changes: 9 additions & 0 deletions unix/test/mle_static.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

int a[100000000];
// 400 MB

int main() {
puts("Hello World!");
return 0;
}
4 changes: 4 additions & 0 deletions unix/test/re.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main() {
int *x = (void *)0;
return *x;
}
9 changes: 9 additions & 0 deletions unix/test/scripts/mle_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import subprocess

cmd = "\"%s\" %s" % ("./mle_static", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "380"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
assert(stdout[0] == 48)
18 changes: 18 additions & 0 deletions unix/test/scripts/redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import subprocess
import os
import time

with open('_tmpin', 'w') as f:
f.writelines(['1 1'])

cmd = "\"%s\" %s" % ("./add", "")
p = subprocess.Popen(["./watcher_unix", cmd, "_tmpin", "_tmpout", "_tmperr", "1000", "100"], shell=False)

time.sleep(2)
p.kill()

assert(p.returncode == 0)

assert(os.path.exists('_tmpout'))
with open('_tmpout', 'r') as f:
assert(f.read() == "2\n")
10 changes: 10 additions & 0 deletions unix/test/scripts/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess

cmd = "\"%s\" %s" % ("./hello", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
10 changes: 10 additions & 0 deletions unix/test/scripts/run_sh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess

cmd = "\"%s\" %s" % ("/bin/bash", "hello.sh")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
7 changes: 7 additions & 0 deletions unix/test/scripts/runtimeerr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import subprocess
import time

cmd = "\"%s\" %s" % ("./re", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False)

assert(p.wait() == 2)
13 changes: 13 additions & 0 deletions unix/test/scripts/space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import subprocess
import shutil

shutil.copy("./hello", "./he llo")

cmd = "\"%s\" %s" % ("./he llo", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
16 changes: 16 additions & 0 deletions unix/test/scripts/symlink_abs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess
import os

if os.path.exists("hello_s_abs"):
os.remove("hello_s_abs")

os.symlink(os.path.join(os.getcwd(), "hello"), "hello_s_abs")

cmd = "\"%s\" %s" % ("./hello_s_abs", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
16 changes: 16 additions & 0 deletions unix/test/scripts/symlink_rel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess
import os

if os.path.exists("hello_s_rel"):
os.remove("hello_s_rel")

os.symlink("hello", "hello_s_rel")

cmd = "\"%s\" %s" % ("./hello_s_rel", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
10 changes: 10 additions & 0 deletions unix/test/scripts/tle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess
import time

cmd = "\"%s\" %s" % ("./tle", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "100"], shell=False)

time.sleep(5)
p.kill()

assert(p.returncode == 3)
10 changes: 10 additions & 0 deletions unix/test/scripts/unlimit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import subprocess

cmd = "\"%s\" %s" % ("./mle_static", "")
p = subprocess.Popen(["./watcher_unix", cmd, "", "", "_tmperr", "1000", "-1"], shell=False, stdout=subprocess.PIPE)

stdout, _ = p.communicate()

assert(p.wait() == 0)
out_str = stdout.decode()
assert(out_str.split('\n')[0] == 'Hello World!')
5 changes: 5 additions & 0 deletions unix/test/tle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int main() {
while (1)
;
return 0;
}
Loading

0 comments on commit 4788f9a

Please sign in to comment.