Skip to content

Commit

Permalink
tests: add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Jan 4, 2025
1 parent d2faf17 commit 98253bd
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test-e2e/filesystem/read/cat/with-network-client-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-client \
cat run.sh

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
15 changes: 15 additions & 0 deletions test-e2e/filesystem/read/cat/with-network-server-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-server \
cat run.sh

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
7 changes: 7 additions & 0 deletions test-e2e/filesystem/read/go/before.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -e

dir=$(dirname "$0") # Get directory of the script (possibly a symlink)

go build -o .tmp/read $dir/read.go
13 changes: 13 additions & 0 deletions test-e2e/filesystem/read/go/no-explicit-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run .tmp/read

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
41 changes: 41 additions & 0 deletions test-e2e/filesystem/read/go/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"io"
"os"
"strings"
)

func readResolvConf() (string, error) {
// Open the file.
f, err := os.Open("/etc/resolv.conf")
if err != nil {
if os.IsNotExist(err) { // Check specifically for file not found
return "", fmt.Errorf("Error: /etc/resolv.conf not found")
}
return "", fmt.Errorf("Error opening /etc/resolv.conf: %w", err) // Wrap the error
}
defer f.Close() // Ensure the file is closed even if an error occurs later

// Read the file contents.
contents, err := io.ReadAll(f)
if err != nil {
return "", fmt.Errorf("Error reading /etc/resolv.conf: %w", err) // Wrap the error
}
return string(contents), nil
}

func main() {
contents, err := readResolvConf()
if err != nil {
fmt.Fprintln(os.Stderr, err) // Print errors to stderr
os.Exit(1)
}

if strings.Contains(contents, "nameserver") {
os.Exit(0)
} else {
os.Exit(1)
}
}
15 changes: 15 additions & 0 deletions test-e2e/filesystem/read/go/with-network-client-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-client \
.tmp/read

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
15 changes: 15 additions & 0 deletions test-e2e/filesystem/read/go/with-network-server-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-server \
.tmp/read

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
13 changes: 13 additions & 0 deletions test-e2e/filesystem/read/go/with-permissions-no-implicits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run --allow-file-system-read --no-implicit-allow .tmp/read

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
7 changes: 7 additions & 0 deletions test-e2e/filesystem/read/go/with-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run --allow-file-system-read .tmp/read
18 changes: 18 additions & 0 deletions test-e2e/filesystem/read/go/without-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-process-management \
--allow-memory-management \
--allow-process-synchronization \
--allow-misc \
.tmp/read

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
14 changes: 14 additions & 0 deletions test-e2e/filesystem/read/python/no-explicit-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run python3 $script_path/read.py

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
24 changes: 24 additions & 0 deletions test-e2e/filesystem/read/python/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def read_run_sh():
"""Reads the contents of of /etc/resolve.conf and returns them as a string.
Returns None if the file doesn't exist or if an error occurs.
Prints an error message to stderr if the file can't be read.
"""

try:
with open("/etc/resolv.conf", "r") as f:
return f.read()
except FileNotFoundError:
print("Error: not found.", file=sys.stderr) # sys needed
return None
except Exception as e: # Broad except to catch all other file errors
print(f"Error reading: {e}", file=sys.stderr)
return None

if __name__ == "__main__":
import sys # Added import statement for sys module

contents = read_run_sh()
if contents and "nameserver" in contents:
sys.exit(0) # Exit with 0 if "nameserver" is found
else:
sys.exit(1) # Exit with 1 if "nameserver" is not found or an error occurred
16 changes: 16 additions & 0 deletions test-e2e/filesystem/read/python/with-network-client-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run \
--allow-network-client \
python3 $script_path/read.py

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
16 changes: 16 additions & 0 deletions test-e2e/filesystem/read/python/with-network-server-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run \
--allow-network-server \
python3 $script_path/read.py

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
14 changes: 14 additions & 0 deletions test-e2e/filesystem/read/python/with-permissions-no-implicits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run --allow-file-system-read --no-implicit-allow python3 $script_path/read.py

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
8 changes: 8 additions & 0 deletions test-e2e/filesystem/read/python/with-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run --allow-file-system-read python3 $script_path/read.py
19 changes: 19 additions & 0 deletions test-e2e/filesystem/read/python/without-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"
declare -r script_path="$( dirname -- "${BASH_SOURCE[0]}"; )"; # Get the directory name

$main_path run \
--allow-process-management \
--allow-memory-management \
--allow-process-synchronization \
--allow-misc \
python3 $script_path/read.py

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
15 changes: 15 additions & 0 deletions test-e2e/filesystem/write/cp/with-network-client-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-client \
cp run.sh .tmp/run.sh

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1
15 changes: 15 additions & 0 deletions test-e2e/filesystem/write/cp/with-network-server-permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -uo pipefail

declare -r main_path="$1"

$main_path run \
--allow-network-server \
cp run.sh .tmp/run.sh

if [[ $? -ne 0 ]]; then
exit 0
fi

exit 1

0 comments on commit 98253bd

Please sign in to comment.