Skip to content

Commit

Permalink
Merge pull request #371 from dantol29/slombard/web-312-add-ipv6-addre…
Browse files Browse the repository at this point in the history
…ssees-to-the-multi-server-test

Slombard/web 312 add ipv6 addressees to the multi server test
  • Loading branch information
dantol29 authored May 20, 2024
2 parents bb818c1 + be27114 commit c31cbdf
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 17 deletions.
20 changes: 20 additions & 0 deletions multiIPv4+IPv6.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server {
listen 0.0.0.0:8081;
listen [::]:8081;
server_name www.development_site;
root /var/www/html;
}

server {
listen 127.0.0.1:8080;
listen [::1]:8080;
server_name www.saladbook;
root /var/www/html;
}

server {
listen 127.0.0.1:8082;
listen [::1]:8082;
server_name www.python_site.com;
root /var/www/html;
}
17 changes: 17 additions & 0 deletions multiIPv4.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {
listen 0.0.0.0:8081;
server_name www.development_site;
root /var/www/html;
}

server {
listen 127.0.0.1:8080;
server_name www.saladbook;
root /var/www/html;
}

server {
listen 127.0.0.1:8082;
server_name www.python_site.com;
root /var/www/html;
}
17 changes: 17 additions & 0 deletions multiIPv6.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {
listen [::]:8081;
server_name www.development_site;
root /var/www/html;
}

server {
listen [::1]:8080;
server_name www.saladbook;
root /var/www/html;
}

server {
listen [::1]:8082;
server_name www.python_site.com;
root /var/www/html;
}
17 changes: 17 additions & 0 deletions multi_mixed.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
server {
listen 0.0.0.0:8081;
server_name www.development_site;
root /var/www/html;
}

server {
listen [::1]:8080;
server_name www.saladbook;
root /var/www/html;
}

server {
listen [::1]:8082;
server_name www.python_site.com;
root /var/www/html;
}
30 changes: 14 additions & 16 deletions tests/multi_sockets/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ FROM ubuntu:20.04

# Install necessary packages
RUN apt-get update && \
apt-get install -y build-essential curl vim
apt-get install -y build-essential curl vim iproute2 net-tools

# Set the working directory
WORKDIR /app

# Copy the entire project into the container
# Copy the entire project into the containerenet-tools
COPY . .


# List the contents of the /app directory to verify the presence of the Makefile
RUN echo "Contents of /app:" && ls -al /app

# List the contents of the tests directory to verify the structure
RUN echo "Contents of /app/tests:" && ls -al /app/tests

# List the contents of the multi_sockets directory to verify the structure
RUN echo "Contents of /app/tests/multi_sockets:" && ls -al /app/tests/multi_sockets

# Compile the project in the root directory where the Makefile is located
RUN make re -C /app
# Compile the project
Expand All @@ -30,7 +20,15 @@ RUN make
WORKDIR /app/tests/multi_sockets

# Make the test script executable
RUN chmod +x test.sh

# Command to start the server and run tests
CMD ["./test.sh"]
RUN chmod +x testIPv4.sh
RUN chmod +x testIPv6.sh
RUN chmod +x testIPv4+IPv6.sh
RUN chmod +x test_mixed.sh

# Enable IPv6 in the container
RUN sysctl -w net.ipv6.conf.all.disable_ipv6=0 && \
sysctl -w net.ipv6.conf.default.disable_ipv6=0 && \
sysctl -w net.ipv6.conf.lo.disable_ipv6=0

# Just start the shell
CMD ["/bin/bash"]
34 changes: 34 additions & 0 deletions tests/multi_sockets/multi_socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@ We send requests to:
- `127.0.0.3:8080`: "000"
- `127.0.0.1:8081`: "404"

### Docker IPv6 Setup Guide

#### 0. Enable IPv6 in Docker

To enable IPv6 support in Docker, follow these steps:

1. **Edit Docker daemon configuration**:
Edit the `/etc/docker/daemon.json` file to include the necessary IPv6 parameters:

```json
{
"ipv6": true,
"fixed-cidr-v6": "2001:db8:1::/64",
"experimental": true,
"ip6tables": true
}
```

Step 1 on Mac needs to be done in the Desktop app > Settings > Engine

2. **Restart Docker**:
Save the configuration file and restart the Docker daemon for the changes to take effect:

```sh
sudo systemctl restart docker
```

3. **Create an IPv6 network**:
Create a Docker network that uses IPv6:

```sh
docker network create --ipv6 --subnet 2001:db8:1::/64 ip6net
```

#### 1. Build the Docker Image

From the root of `webserv`:
Expand Down
69 changes: 69 additions & 0 deletions tests/multi_sockets/testIPv4+IPv6.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Check if the webserv binary exists
if [ ! -f ./../../webserv ]; then
echo -e "${RED}Error: webserv binary not found!${NC}"
exit 1
fi

echo "Starting test script..."

# Modify /etc/hosts to add the new localhost aliases
echo "127.0.0.2 localhost2" >> /etc/hosts
echo "127.0.0.3 localhost3" >> /etc/hosts
echo "::2 localhost2" >> /etc/hosts
echo "::3 localhost3" >> /etc/hosts

# Start the server
echo "Starting the server..."
./../../webserv ./../../multiIPv4+IPv6.conf &
SERVER_PID=$!
sleep 2 # Wait for the server to start

# Check if the server started successfully
if ! ps -p $SERVER_PID > /dev/null; then
echo -e "${RED}Failed to start the server.${NC}"
exit 1
fi

# Function to store response
store_response() {
local url=$1
local expected=$2
local response=$(curl -s -o /dev/null -w "%{http_code}" $url)
if [ "$response" == "$expected" ]; then
echo -e "$url - Expected $expected - HTTP status code: $response ${GREEN}${NC}" >> results.txt
else
echo -e "$url - Expected $expected - HTTP status code: $response ${RED}${NC}" >> results.txt
fi
}

# Run curl commands and store responses
store_response "127.0.0.1:8080" "404"
store_response "0.0.0.0:8081" "404"
store_response "127.0.0.1:8082" "404"
store_response "127.0.0.2:8080" "000"
store_response "127.0.0.3:8080" "000"
store_response "127.0.0.1:8081" "404"
store_response "[::1]:8080" "404"
store_response "[::]:8081" "404"
store_response "[::1]:8082" "404"
store_response "[::2]:8080" "000"
store_response "[::3]:8080" "000"
store_response "[::1]:8081" "404"

# Stop the server
echo "Stopping the server..."
kill $SERVER_PID

# Print all responses
echo "Test results:"
cat results.txt
rm results.txt

echo "Test script completed."
2 changes: 1 addition & 1 deletion tests/multi_sockets/test.sh → tests/multi_sockets/testIPv4.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ echo "127.0.0.3 localhost3" >> /etc/hosts

# Start the server
echo "Starting the server..."
./../../webserv ./../../multi.conf &
./../../webserv ./../../multiIPv4.conf &
SERVER_PID=$!
sleep 2 # Wait for the server to start

Expand Down
92 changes: 92 additions & 0 deletions tests/multi_sockets/testIPv6.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Install net-tools if not present
if ! command -v netstat &> /dev/null; then
echo "Installing net-tools..."
apt-get update && apt-get install -y net-tools
fi

# Check if the webserv binary exists
if [ ! -f ./../../webserv ]; then
echo -e "${RED}Error: webserv binary not found!${NC}"
exit 1
fi

echo "Starting test script..."

# Modify /etc/hosts to add the new localhost aliases
echo "::2 localhost2" >> /etc/hosts
echo "::3 localhost3" >> /etc/hosts

# Start the server
echo "Starting the server..."
./../../webserv ./../../multiIPv6.conf &
SERVER_PID=$!
sleep 2 # Wait for the server to start

# Check if the server started successfully
if ! ps -p $SERVER_PID > /dev/null; then
echo -e "${RED}Failed to start the server.${NC}"
exit 1
fi

# Verify server is listening on the expected address and port
echo "Checking server listening ports..."
netstat -tuln | grep 8081 >> netstat_results.txt
netstat -tuln | grep 8080 >> netstat_results.txt
netstat -tuln | grep 8082 >> netstat_results.txt

# Check IPv6 configuration
echo "IPv6 configuration:"
ip -6 addr > ipv6_config.txt
ip -6 route >> ipv6_config.txt


# Function to store response
store_response() {
local url=$1
local expected=$2
local response=$(curl -s -o /dev/null -w "%{http_code}" $url)
if [ "$response" == "$expected" ]; then
echo -e "$url - Expected $expected - HTTP status code: $response ${GREEN}${NC}" >> results.txt
else
echo -e "$url - Expected $expected - HTTP status code: $response ${RED}${NC}" >> results.txt
fi
}

# Run curl commands and store responses
store_response "[::1]:8080" "404"
# store_response "[::]:8081" "404"
store_response "[::0]:8081" "404"
store_response "[::1]:8082" "404"
store_response "[::2]:8080" "000"
store_response "[::3]:8080" "000"
store_response "[::1]:8081" "404"

# Stop the server
echo "Stopping the server..."
kill $SERVER_PID

# Print all responses
echo "Test results:"
cat results.txt
rm results.txt

# Print netstat results
echo "Netstat results:"
cat netstat_results.txt
rm netstat_results.txt

# Print IPv6 configuration
echo "IPv6 configuration:"
cat ipv6_config.txt
rm ipv6_config.txt



echo "Test script completed."
Loading

0 comments on commit c31cbdf

Please sign in to comment.