Skip to content

Commit

Permalink
test(share/p2p): fix namespace add operations in tests (#2422)
Browse files Browse the repository at this point in the history
## Overview

Fixes the test with unreliable add/sub operations. Adds AddToNamespace
for tests
  • Loading branch information
walldiss committed Jul 31, 2023
1 parent 954f748 commit 794360f
Showing 1 changed file with 108 additions and 11 deletions.
119 changes: 108 additions & 11 deletions share/getters/shrex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package getters

import (
"context"
"encoding/binary"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -100,18 +102,16 @@ func TestShrexGetter(t *testing.T) {
Height: 1,
})

// corrupt NID
nID := make([]byte, share.NamespaceSize)
copy(nID, maxNamespace)
nID[share.NamespaceSize-1]--
namespace, err := addToNamespace(maxNamespace, -1)
require.NoError(t, err)
// check for namespace to be between max and min namespace in root
require.Len(t, filterRootsByNamespace(&dah, maxNamespace), 1)
require.Len(t, filterRootsByNamespace(&dah, namespace), 1)

emptyShares, err := getter.GetSharesByNamespace(ctx, &dah, nID)
emptyShares, err := getter.GetSharesByNamespace(ctx, &dah, namespace)
require.NoError(t, err)
// no shares should be returned
require.Empty(t, emptyShares.Flatten())
require.Nil(t, emptyShares.Verify(&dah, nID))
require.Nil(t, emptyShares.Verify(&dah, namespace))
})

t.Run("ND_namespace_not_in_dah", func(t *testing.T) {
Expand All @@ -126,10 +126,8 @@ func TestShrexGetter(t *testing.T) {
Height: 1,
})

// corrupt namespace
namespace := make([]byte, share.NamespaceSize)
copy(namespace, maxNamesapce)
namespace[share.NamespaceSize-1]++
namespace, err := addToNamespace(maxNamesapce, 1)
require.NoError(t, err)
// check for namespace to be not in root
require.Len(t, filterRootsByNamespace(&dah, namespace), 0)

Expand Down Expand Up @@ -271,3 +269,102 @@ func newEDSClientServer(
require.NoError(t, err)
return client, server
}

// addToNamespace adds arbitrary int value to namespace, treating namespace as big-endian implementation of int
func addToNamespace(namespace share.Namespace, val int) (share.Namespace, error) {
if val == 0 {
return namespace, nil
}
// Convert the input integer to a byte slice and add it to result slice
result := make([]byte, len(namespace))
if val > 0 {
binary.BigEndian.PutUint64(result[len(namespace)-8:], uint64(val))
} else {
binary.BigEndian.PutUint64(result[len(namespace)-8:], uint64(-val))
}

// Perform addition byte by byte
var carry int
for i := len(namespace) - 1; i >= 0; i-- {
sum := 0
if val > 0 {
sum = int(namespace[i]) + int(result[i]) + carry
} else {
sum = int(namespace[i]) - int(result[i]) + carry
}

switch {
case sum > 255:
carry = 1
sum -= 256
case sum < 0:
carry = -1
sum += 256
default:
carry = 0
}

result[i] = uint8(sum)
}

// Handle any remaining carry
if carry != 0 {
return nil, errors.New("namespace overflow")
}

return result, nil
}

func TestAddToNamespace(t *testing.T) {
testCases := []struct {
name string
value int
input share.Namespace
expected share.Namespace
expectedError error
}{
{
name: "Positive value addition",
value: 42,
input: share.Namespace{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
expected: share.Namespace{0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2b},
expectedError: nil,
},
{
name: "Negative value addition",
value: -42,
input: share.Namespace{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
expected: share.Namespace{0x1, 0x1, 0x1, 0x1, 0x1, 0x01, 0x1, 0x1, 0x1, 0x0, 0xd7},
expectedError: nil,
},
{
name: "Overflow error",
value: 1,
input: share.Namespace{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
expected: nil,
expectedError: errors.New("namespace overflow"),
},
{
name: "Overflow error negative",
value: -1,
input: share.Namespace{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
expected: nil,
expectedError: errors.New("namespace overflow"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := addToNamespace(tc.input, tc.value)
if tc.expectedError == nil {
require.NoError(t, err)
require.Equal(t, tc.expected, result)
return
}
require.Error(t, err)
if err.Error() != tc.expectedError.Error() {
t.Errorf("Unexpected error message. Expected: %v, Got: %v", tc.expectedError, err)
}
})
}
}

0 comments on commit 794360f

Please sign in to comment.