Skip to content

Commit

Permalink
chore: add support for i386 (#52)
Browse files Browse the repository at this point in the history
This PR reuses the `install_nim` action from `nim-libp2p`to install Nim
`i386`. We aren't running the test on `i386`- as GitHub runners are
amd64, but only using Nim `i386`.

It also uses `int64` instead of `int` for `VarIntCompatible` and
`PacketNumber` to support `i386`.
  • Loading branch information
diegomrsantos authored Sep 11, 2024
1 parent 8a97eee commit ddcb31f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
51 changes: 40 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,47 @@ on:

jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
nim: [1.6.20, 2.0.8]
platform:
- os: linux
runner: ubuntu-latest
cpu: amd64
shell: bash
- os: linux
runner: ubuntu-latest
cpu: i386
shell: bash
- os: macos
runner: macos-latest
cpu: amd64
shell: bash
- os: windows
runner: windows-latest
cpu: amd64
shell: msys2 {0}
nim:
- branch: version-1-6
- branch: version-2-0

name: '${{ matrix.platform.os }}-${{ matrix.platform.cpu }} (Nim ${{ matrix.nim.branch }})'
runs-on: ${{ matrix.platform.runner }}
steps:
- uses: actions/checkout@v2
- uses: iffy/install-nim@v3
with:
version: ${{ matrix.nim }}
- name: Build
run: nimble install -y
- name: Test
run: nimble test -y
- name: Checkout
uses: actions/checkout@v2

- name: Setup Nim
uses: vacp2p/nim-libp2p/.github/actions/install_nim@master
with:
os: ${{ matrix.platform.os }}
cpu: ${{ matrix.platform.cpu }}
shell: ${{ matrix.platform.shell }}
nim_branch: ${{ matrix.nim.branch }}

- name: Install dependencies
run: nimble install -y

- name: Test
run: nimble test -y
2 changes: 1 addition & 1 deletion quic/transport/packets/packetnumber.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import std/math
import pkg/stew/endians2

type
PacketNumber* = range[0..2^62-1]
PacketNumber* = range[0'i64..2'i64^62-1]

proc toMinimalBytes*(packetnumber: PacketNumber): seq[byte] =
let bytes = packetnumber.uint64.toBytesBE
Expand Down
2 changes: 1 addition & 1 deletion quic/transport/packets/read.nim
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ proc readPacketNumber(reader: var PacketReader, datagram: openArray[byte],
padded[padded.len-bytes.len..<padded.len] = bytes
except RangeError:
doAssert false, "programmer error: assignment ranges do not match"
reader.packet.packetnumber = fromBytesBE(uint32, padded)
reader.packet.packetnumber = fromBytesBE(uint32, padded).int64

proc `payload=`(packet: var Packet, payload: seq[byte]) =
case packet.form
Expand Down
5 changes: 3 additions & 2 deletions quic/transport/packets/varints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import std/math
import pkg/stew/endians2

type
VarIntCompatible* = range[0..2^62-1]
VarIntCompatible* = range[0'i64..2'i64^62-1]

proc toVarInt*(value: VarIntCompatible): seq[byte] =
case value
Expand All @@ -22,7 +22,7 @@ proc varintlen*(varint: openArray[byte]): int =
2^(varint[0] shr 6)

proc fromVarInt*(varint: openArray[byte]): VarIntCompatible =
case varintlen(varint)
let r = case varintlen(varint)
of 1:
varint[0].uint64
of 2:
Expand All @@ -34,3 +34,4 @@ proc fromVarInt*(varint: openArray[byte]): VarIntCompatible =
else:
const mask = not(0b11'u64 shl 62)
fromBytesBE(uint64, varint) and mask
r.int64
8 changes: 4 additions & 4 deletions tests/quic/testPacketLength.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ suite "packet length":
var packet = handshakePacket()
packet.destination = destination
packet.source = source
packet.handshake.packetnumber = 0x00BBCCDD'u32
packet.handshake.packetnumber = 0x00BBCCDD'u32.int64
packet.handshake.payload = repeat(0xEE'u8, 1024)
check packet.len == 7 +
destination.len +
Expand All @@ -42,7 +42,7 @@ suite "packet length":
var packet = zeroRttPacket()
packet.destination = destination
packet.source = source
packet.rtt.packetnumber = 0x00BBCCDD'u32
packet.rtt.packetnumber = 0x00BBCCDD'u32.int64
packet.rtt.payload = repeat(0xEE'u8, 1024)
check packet.len == 7 +
destination.len +
Expand All @@ -56,7 +56,7 @@ suite "packet length":
packet.destination = destination
packet.source = source
packet.initial.token = token
packet.initial.packetnumber = 0x00BBCCDD'u32
packet.initial.packetnumber = 0x00BBCCDD'u32.int64
packet.initial.payload = repeat(0xEE'u8, 1024)
check packet.len == 7 +
destination.len +
Expand All @@ -70,7 +70,7 @@ suite "packet length":
test "knows the length of a short packet":
var packet = shortPacket()
packet.destination = destination
packet.short.packetnumber = 0x00BBCCDD'u32
packet.short.packetnumber = 0x00BBCCDD'u32.int64
packet.short.payload = repeat(0xEE'u8, 1024)
check packet.len == 1 +
destination.len +
Expand Down
2 changes: 1 addition & 1 deletion tests/quic/testPacketNumber.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ suite "packet numbers":

test "packet numbers are in the range 0 to 2^62-1":
check PacketNumber.low == 0
check PacketNumber.high == 2 ^ 62 - 1
check PacketNumber.high == 2'i64 ^ 62 - 1

test "conversion to bytes":
check 0.toMinimalBytes == @[0'u8]
Expand Down
14 changes: 7 additions & 7 deletions tests/quic/testPacketWriting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ suite "packet writing":
const packetnumber = 0xAABBCCDD'u32
const payload = repeat(0xAB'u8, 1024)
var packet = handshakePacket()
packet.handshake.packetnumber = packetnumber
packet.handshake.packetnumber = packetnumber.int64
packet.handshake.payload = payload
datagram.write(packet)
check datagram[7..8] == (sizeof(packetnumber) + payload.len).toVarInt

test "writes handshake packet number":
const packetnumber = 0xAABBCCDD'u32
var packet = handshakePacket()
packet.handshake.packetnumber = packetnumber
packet.handshake.packetnumber = packetnumber.int64
datagram.write(packet)
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[8..11] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
Expand All @@ -97,15 +97,15 @@ suite "packet writing":
const packetnumber = 0xAABBCCDD'u32
const payload = repeat(0xAB'u8, 1024)
var packet = zeroRttPacket()
packet.rtt.packetnumber = packetnumber
packet.rtt.packetnumber = packetnumber.int64
packet.rtt.payload = payload
datagram.write(packet)
check datagram[7..8] == (sizeof(packetnumber) + payload.len).toVarInt

test "writes 0-RTT packet number":
const packetnumber = 0xAABBCCDD'u32
var packet = zeroRttPacket()
packet.rtt.packetnumber = packetnumber
packet.rtt.packetnumber = packetnumber.int64
datagram.write(packet)
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[8..11] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
Expand All @@ -129,15 +129,15 @@ suite "packet writing":
const packetnumber = 0xAABBCCDD'u32
const payload = repeat(0xAB'u8, 1024)
var packet = initialPacket()
packet.initial.packetnumber = packetnumber
packet.initial.packetnumber = packetnumber.int64
packet.initial.payload = payload
datagram.write(packet)
check datagram[8..9] == (sizeof(packetnumber) + payload.len).toVarInt

test "writes initial packet number":
const packetnumber = 0xAABBCCDD'u32
var packet = initialPacket()
packet.initial.packetnumber = packetnumber
packet.initial.packetnumber = packetnumber.int64
datagram.write(packet)
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[9..12] == @[0xAA'u8, 0xBB'u8, 0xCC'u8, 0xDD'u8]
Expand Down Expand Up @@ -184,7 +184,7 @@ suite "packet writing":
test "writes packet number for short packet":
const packetnumber = 0xAABB'u16
var packet = shortPacket()
packet.short.packetnumber = packetnumber
packet.short.packetnumber = packetnumber.int64
datagram.write(packet)
check int(datagram[0] and 0b11'u8) + 1 == sizeof(packetnumber)
check datagram[1..2] == @[0xAA'u8, 0xBB'u8]
Expand Down
10 changes: 5 additions & 5 deletions tests/quic/testVarInts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ suite "variable length integer encoding":
test "encodes 30-62 bit numbers as 8 bytes":
check toVarInt(2^30) ==
@[0b11000000'u8, 0'u8, 0'u8, 0'u8, 0b01000000'u8, 0'u8, 0'u8, 0'u8]
check toVarInt(2^32) ==
check toVarInt(2'i64^32) ==
@[0b11000000'u8, 0'u8, 0'u8, 1'u8, 0'u8, 0'u8, 0'u8, 0'u8]
check toVarInt(2^56) ==
check toVarInt(2'i64^56) ==
@[0b11000001'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8]
check toVarInt(2^62-1) ==
check toVarInt(2'i64^62-1) ==
@[0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]

suite "variable length integer decoding":
Expand All @@ -53,10 +53,10 @@ suite "variable length integer decoding":
]) == 0
check fromVarInt(@[
0b11000001'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8
]) == 2^56
]) == 2'i64^56
check fromVarInt(@[
0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8
]) == 2^62-1
]) == 2'i64^62-1

suite "variable length":

Expand Down

0 comments on commit ddcb31f

Please sign in to comment.