forked from perlin-network/noise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
id: fix String() and add tests for edge cases while unmarshaling byte…
…s into ids
- Loading branch information
1 parent
cd7c313
commit 6c30632
Showing
3 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package noise_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/perlin-network/noise" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"net" | ||
"strconv" | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
func TestID_String(t *testing.T) { | ||
t.Parallel() | ||
|
||
f := func(publicKey noise.PublicKey, host net.IP, port uint16) bool { | ||
h := host.String() // Make-shift 'normalizeIP(net.IP)'. | ||
if h == "<nil>" { | ||
h = "" | ||
} | ||
|
||
id := noise.NewID(publicKey, host, port) | ||
|
||
if !assert.Equal(t, | ||
fmt.Sprintf( | ||
`{"public_key": "%s", "address": "%s"}`, | ||
publicKey, net.JoinHostPort(h, strconv.FormatUint(uint64(port), 10)), | ||
), | ||
id.String(), | ||
) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
assert.NoError(t, quick.Check(f, nil)) | ||
} | ||
|
||
func TestUnmarshalID(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := noise.UnmarshalID(nil) | ||
assert.EqualError(t, err, io.ErrUnexpectedEOF.Error()) | ||
|
||
_, err = noise.UnmarshalID(append(noise.ZeroPublicKey[:], 1)) | ||
assert.EqualError(t, err, io.ErrUnexpectedEOF.Error()) | ||
|
||
_, err = noise.UnmarshalID(append(noise.ZeroPublicKey[:], append(net.IPv6loopback, 1)...)) | ||
assert.EqualError(t, err, io.ErrUnexpectedEOF.Error()) | ||
|
||
_, err = noise.UnmarshalID(append(noise.ZeroPublicKey[:], append(net.IPv6loopback, 1, 2)...)) | ||
assert.NoError(t, err) | ||
} |