Skip to content

Commit 4d16e65

Browse files
committed
test(fixtures): add a test framework along with test fixtures
1 parent 3467038 commit 4d16e65

File tree

22 files changed

+375
-1
lines changed

22 files changed

+375
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
**/*.rs.bk
66

77
# Default upload directory
8-
/upload/
8+
**/upload/

fixtures/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RUST_LOG=error

fixtures/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Fixtures
2+
3+
This directory contains the [test fixtures](https://en.wikipedia.org/wiki/Test_fixture) and a simple testing framework for `rustypaste`.
4+
5+
### Running fixtures
6+
7+
1. Build the project in debug mode: `cargo build`
8+
2. Execute the runner script in this directory: `./test-fixtures.sh`
9+
10+
### Adding new fixtures
11+
12+
Create an appropriately named directory for the test fixture you want to add. e.g. `test-file-upload`
13+
14+
Each fixture directory should contain the following files:
15+
16+
```
17+
test-file-upload/
18+
├── config.toml
19+
└── test.sh
20+
```
21+
22+
- `config.toml`: Contains the `rustypaste` configuration. See [the default configuration](../config.toml).
23+
- `test.sh`: Contains the helper functions for testing. The file format is the following:
24+
25+
```sh
26+
#!/usr/bin/env bash
27+
28+
setup() {
29+
# preparation
30+
}
31+
32+
run_test() {
33+
# assertions
34+
}
35+
36+
teardown() {
37+
# cleanup
38+
}
39+
```
40+
41+
These functions are executed in the order defined above.
42+
43+
See the [test-file-upload](./test-file-upload/test.sh) fixture for an example.
44+
45+
### Debugging
46+
47+
Set the `DEBUG` environment variable to `true` while executing the runner script:
48+
49+
```sh
50+
$ DEBUG=true ./test-fixtures.sh
51+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
duplicate_content="test data"
4+
5+
setup() {
6+
echo "$duplicate_content" > file
7+
date +%s > unique_file1
8+
sleep 1
9+
date +%s > unique_file2
10+
}
11+
12+
run_test() {
13+
first_file_url=$(curl -s -F "file=@file" localhost:8000)
14+
test "$duplicate_content" = "$(cat upload/file.txt)"
15+
16+
second_file_url=$(curl -s -F "file=@file" localhost:8000)
17+
test "$first_file_url" = "$second_file_url"
18+
for url in "$first_file_url" "$second_file_url"; do
19+
test "$duplicate_content" = "$(curl -s $url)"
20+
done
21+
22+
first_file_url=$(curl -s -F "file=@unique_file1" localhost:8000)
23+
second_file_url=$(curl -s -F "file=@unique_file2" localhost:8000)
24+
test "$first_file_url" != "$second_file_url"
25+
}
26+
27+
teardown() {
28+
rm file unique_file1 unique_file2
29+
rm -r upload
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
content="test data"
4+
5+
setup() {
6+
echo "$content" > file
7+
}
8+
9+
run_test() {
10+
file_url=$(curl -s -F "file=@file" -H "expire:1s" localhost:8000)
11+
test "$content" = "$(cat upload/file.txt.*)"
12+
sleep 2s
13+
14+
result="$(curl -s $file_url)"
15+
test "file is not found or expired :(" = "$result"
16+
}
17+
18+
teardown() {
19+
rm file
20+
rm -r upload
21+
}

fixtures/test-file-upload/config.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false

fixtures/test-file-upload/test.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
content="test data"
4+
5+
setup() {
6+
echo "$content" > file
7+
}
8+
9+
run_test() {
10+
file_url=$(curl -s -F "file=@file" localhost:8000)
11+
test "$file_url" = "http://localhost:8000/file.txt"
12+
test "$content" = "$(cat upload/file.txt)"
13+
test "$content" = "$(curl -s $file_url)"
14+
}
15+
16+
teardown() {
17+
rm file
18+
rm -r upload
19+
}

fixtures/test-fixtures.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
FIXTURE_DIR=$(readlink -f "$(dirname "$0")")
4+
PROJECT_DIR="$FIXTURE_DIR/.."
5+
GREEN='\033[0;32m'
6+
RED='\033[0;31m'
7+
NC='\033[0m'
8+
9+
run_fixture() {
10+
cd "$FIXTURE_DIR/$1" || exit 1
11+
source "test.sh"
12+
NO_COLOR=1 CONFIG=config.toml "$PROJECT_DIR/target/debug/rustypaste" &
13+
SERVER_PID=$!
14+
trap 'kill -9 "$SERVER_PID" && wait "$SERVER_PID" 2> /dev/null' RETURN
15+
sleep 1
16+
setup
17+
run_test
18+
result=$?
19+
teardown
20+
return "$result"
21+
}
22+
23+
main() {
24+
find * -maxdepth 0 -type d -print0 | while IFS= read -r -d '' fixture; do
25+
run_fixture "$fixture"
26+
exit_status=$?
27+
if [ "$exit_status" -eq 0 ]; then
28+
echo -e "[${GREEN}ok${NC}] $fixture"
29+
else
30+
echo -e "[${RED}fail${NC}] $fixture"
31+
exit "$exit_status"
32+
fi
33+
done
34+
}
35+
36+
[ "$DEBUG" == 'true' ] && set -x && export RUST_LOG=debug
37+
main
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false

fixtures/test-oneshot-upload/test.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
3+
content="0nesh0t"
4+
5+
setup() {
6+
echo "$content" > file
7+
}
8+
9+
run_test() {
10+
file_url=$(curl -s -F "oneshot=@file" localhost:8000)
11+
test "$content" = $(curl -s "$file_url")
12+
test "$content" = "$(cat upload/oneshot/file.txt.*)"
13+
14+
result="$(curl -s $file_url)"
15+
test "file is not found or expired :(" = "$result"
16+
}
17+
18+
teardown() {
19+
rm file
20+
rm -r upload
21+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10kb"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false

fixtures/test-path-traversal/test.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
content="test"
4+
5+
setup() {
6+
echo "$content" > file
7+
}
8+
9+
run_test() {
10+
result=$(curl -s --path-as-is localhost:8000/.)
11+
test "file is not found or expired :(" = "$result"
12+
13+
result=$(curl -s --write-out "%{http_code}" --path-as-is localhost:8000/../test.sh)
14+
test "404" = "$result"
15+
16+
result=$(curl -s -X POST -F "file=@file;filename=../." localhost:8000)
17+
test "$content" = "$(cat upload/file.txt)"
18+
}
19+
20+
teardown() {
21+
rm file
22+
rm -r upload
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
timeout="60s"
6+
7+
[paste]
8+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
9+
default_extension = "txt"
10+
duplicate_files = false
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
url="https://raw.githubusercontent.com/orhun/rustypaste/master/img/rustypaste_logo.png"
4+
5+
setup() {
6+
:;
7+
}
8+
9+
run_test() {
10+
file_url=$(curl -s -F "remote=$url" localhost:8000)
11+
curl -s "$file_url" -o uploaded_file > /dev/null
12+
curl -s "$url" -o remote_file > /dev/null
13+
test "$(sha256sum uploaded_file | awk '{print $1}')" = "$(sha256sum remote_file | awk '{print $1}')"
14+
}
15+
16+
teardown() {
17+
rm uploaded_file remote_file
18+
rm -r upload
19+
}

fixtures/test-server-auth/config.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
auth_token="rustypasteisawesome"
6+
7+
[paste]
8+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
9+
default_extension = "txt"
10+
duplicate_files = false

fixtures/test-server-auth/test.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
auth_token="rustypasteisawesome"
4+
content="topsecret"
5+
6+
setup() {
7+
echo "$content" > file
8+
}
9+
10+
run_test() {
11+
result=$(curl -s -F "file=@file" localhost:8000)
12+
test "unauthorized" = "$result"
13+
14+
result=$(curl -s -F "file=@file" -H "Authorization: $auth_token" localhost:8000)
15+
test "unauthorized" != "$result"
16+
test "$content" = "$(cat upload/file.txt)"
17+
test "$content" = "$(curl -s $result)"
18+
}
19+
20+
teardown() {
21+
rm file
22+
rm -r upload
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10kb"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
setup() {
4+
touch emptyfile
5+
truncate -s 9KB smallfile
6+
truncate -s 10KB normalfile
7+
truncate -s 11KB bigfile
8+
}
9+
10+
run_test() {
11+
result=$(curl -s -F "file=@emptyfile" localhost:8000)
12+
test "invalid file size" = "$result"
13+
14+
result=$(curl -s -F "file=@bigfile" localhost:8000)
15+
test "upload limit exceeded" = "$result"
16+
17+
result=$(curl -s -F "file=@normalfile" localhost:8000)
18+
test "upload limit exceeded" != "$result"
19+
20+
result=$(curl -s -F "file=@smallfile" localhost:8000)
21+
test "upload limit exceeded" != "$result"
22+
}
23+
24+
teardown() {
25+
rm emptyfile smallfile normalfile bigfile
26+
rm -r upload
27+
}

fixtures/test-url-upload/config.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[server]
2+
address="127.0.0.1:8000"
3+
max_content_length="10MB"
4+
upload_path="./upload"
5+
6+
[paste]
7+
random_url = { enabled = false, type = "petname", words = 2, separator = "-" }
8+
default_extension = "txt"
9+
duplicate_files = false

0 commit comments

Comments
 (0)