Files
i2pd/tests/test-sam-version.sh
T
Elcoid d1428cd2ae sam: fix inconsistencies in version negotiation of sam handshake
This commit fixes [issue #2570](https://github.com/PurpleI2P/i2pd/issues/2570).

The [specification of the SAM protocol](https://i2p.net/en/docs/api/samv3/)
says the following:
- "As of version 3.1 (I2P 0.9.14), the MIN and MAX parameters are optional."
- "SAM will always return the highest version possible given the MIN and MAX
  constraints"

Given this, the following handshakes do not change:
```
printf "HELLO VERSION MIN=3.1 MAX=3.3\n" | nc -q 0 127.0.0.1 7656
HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MAX=3.3\n" | nc -q 0 127.0.0.1 7656
HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=31 MAX=31\n" | nc -q 0 127.0.0.1 7656
HELLO REPLY RESULT=OK VERSION=3.1
(periods are still optional)
```

But the following ones do (the responses before and after this commit
are included):
```
printf "HELLO VERSION MAX=3.4\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=NOVERSION
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=3.0\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=OK VERSION=3.0
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=3.1\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=OK VERSION=3.1
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=2.9\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=NOVERSION
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=NOVERSION
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=3.3 MAX=3.1\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=OK VERSION=3.1
after:  HELLO REPLY RESULT=NOVERSION

printf "HELLO VERSION MIN=3.5 MAX=2.7\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=OK VERSION=2.7
after:  HELLO REPLY RESULT=NOVERSION

printf "HELLO VERSION MIN=2.7 MAX=3.5\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=NOVERSION
after:  HELLO REPLY RESULT=OK VERSION=3.3

printf "HELLO VERSION MIN=aab3vsg1df MAX=fd3gfbgf1bf\n" | nc -q 0 127.0.0.1 7656
before: HELLO REPLY RESULT=OK VERSION=3.1
after:  HELLO REPLY RESULT=NOVERSION
```

I included a script to automatically test these handshakes.
2026-09-25 10:52:05 -04:00

60 lines
964 B
Bash

#!/usr/bin/env bash
# This script tests the version negotiation in the SAM handshake.
# Inputs and expected outputs
IN=()
EXP=()
IN+=("MIN=3.1 MAX=3.3")
EXP+=("OK VERSION=3.3")
IN+=("MAX=3.3")
EXP+=("OK VERSION=3.3")
IN+=("MAX=3.4")
EXP+=("OK VERSION=3.3")
IN+=("MIN=3.0")
EXP+=("OK VERSION=3.3")
IN+=("MIN=3.1")
EXP+=("OK VERSION=3.3")
IN+=("MIN=2.9")
EXP+=("OK VERSION=3.3")
IN+=("")
EXP+=("OK VERSION=3.3")
IN+=("MIN=3.3 MAX=3.1")
EXP+=("NOVERSION")
IN+=("MIN=3.5 MAX=2.7")
EXP+=("NOVERSION")
IN+=("MIN=2.7 MAX=3.5")
EXP+=("OK VERSION=3.3")
IN+=("MIN=afddab3vsfdsg1df MAX=dsaaffdb3ggfgfbgf1bssbf")
EXP+=("NOVERSION")
IN+=("MIN=31 MAX=31")
EXP+=("OK VERSION=3.1")
for i in $(seq 0 $((${#IN[@]} - 1))); do
printf "HELLO VERSION ${IN[$i]} - "
# Observed output
OBS=$(printf "HELLO VERSION ${IN[$i]}\n" | nc -q 0 127.0.0.1 7656)
if [ "$OBS" = "HELLO REPLY RESULT=${EXP[$i]}" ]; then
printf "OK\n"
else
printf "received $OBS\n"
fi
done