Commit fc900234 authored by Matt Joiner's avatar Matt Joiner
Browse files

Fix marshalling of krpc.ID by value

Fixes #30.
parent 221f300f
......@@ -8,10 +8,12 @@ import (
type ID [20]byte
var _ interface {
bencode.Marshaler
bencode.Unmarshaler
} = (*ID)(nil)
var (
_ interface {
bencode.Unmarshaler
} = (*ID)(nil)
_ bencode.Marshaler = ID{}
)
func IdFromString(s string) (id ID) {
if n := copy(id[:], s); n != 20 {
......@@ -20,7 +22,7 @@ func IdFromString(s string) (id ID) {
return
}
func (id *ID) MarshalBencode() ([]byte, error) {
func (id ID) MarshalBencode() ([]byte, error) {
return []byte("20:" + string(id[:])), nil
}
......
package krpc
import (
"testing"
"github.com/anacrolix/torrent/bencode"
"github.com/stretchr/testify/assert"
)
func TestMarshalID(t *testing.T) {
var id ID
copy(id[:], []byte("012345678901234567890"))
assert.Equal(t, "20:01234567890123456789", string(bencode.MustMarshal(id)))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment