Skip to content

Commit

Permalink
chore: renename offset + add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
agparadiso committed Jan 10, 2025
1 parent 74e909c commit 21f5354
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pkg/workflows/wasm/host/wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ func createRandomGet(cfg *ModuleConfig) func(caller *wasmtime.Caller, buf, bufLe
}

func getSlot(events []byte, i int32) ([]byte, error) {
outOffset := i * eventsLen
offset := i * eventsLen

if outOffset+eventsLen > int32(len(events)) {
if offset+eventsLen > int32(len(events)) {
return nil, fmt.Errorf("slot %d out of bounds", i)
}

slot := events[outOffset : outOffset+eventsLen]
slot := events[offset : offset+eventsLen]
return slot, nil
}
24 changes: 15 additions & 9 deletions pkg/workflows/wasm/host/wasip1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ func TestGetSlot(t *testing.T) {
250, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
}

expectedSlots := [][]byte{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{250, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63},
}
t.Run("getSlot works correctly", func(t *testing.T) {
expectedSlots := [][]byte{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{250, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63},
}

for i := int32(0); i < 2; i++ {
slot, err := getSlot(events, i)
assert.NoError(t, err)
assert.Equal(t, expectedSlots[i], slot)
}
for i := int32(0); i < 2; i++ {
slot, err := getSlot(events, i)
assert.NoError(t, err)
assert.Equal(t, expectedSlots[i], slot)
}
})
t.Run("check for out of bound slot", func(t *testing.T) {
_, err := getSlot(events, 400)
assert.Error(t, err)
})
}

0 comments on commit 21f5354

Please sign in to comment.