Skip to content

Commit

Permalink
feat: bencoder encoder implementation and testing and documenting
Browse files Browse the repository at this point in the history
Signed-off-by: nabil salah <[email protected]>
  • Loading branch information
Nabil-Salah committed Aug 15, 2024
1 parent 947bb0c commit 2f241a1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 68 deletions.
38 changes: 19 additions & 19 deletions pkg/bencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func readInteger(idx *int, str *string, terminator string) (int64, error) {
out := ""
lim := strings.Index((*str)[*idx:], terminator)
if lim == -1 {
return -1, fmt.Errorf("%s","error of terminator wasn't found")
return -1, fmt.Errorf("%s", "error of terminator wasn't found")
}
lim = lim + (*idx)
out = (*str)[*idx:lim]
*idx = lim
*idx = lim
integer, err := strconv.ParseInt(out, 10, 64)
if err != nil {
return -1, err
Expand All @@ -25,7 +25,7 @@ func readInteger(idx *int, str *string, terminator string) (int64, error) {

func readBulkString(idx *int, str *string) (interface{}, error) {
out := ""
length, err := readInteger(idx, str,":")
length, err := readInteger(idx, str, ":")
if err != nil {
return "", err
}
Expand All @@ -35,25 +35,24 @@ func readBulkString(idx *int, str *string) (interface{}, error) {
(*idx)++
lim := (*idx) + int(length)
if lim > len(*str) {
return "", fmt.Errorf("%s","error of string lenght isn't enough")
return "", fmt.Errorf("%s", "error of string lenght isn't enough")
}
out = (*str)[*idx:lim]
if out == "-1" {
return "", nil
}
*idx = lim -1
*idx = lim - 1
return out, nil
}


func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
var out = make(map[interface{}]interface{})
var prev interface{}
for i := 0; ; i++ {
var val interface{}
if (*str)[*idx] == 'i' {
(*idx)++
integer, err := readInteger(idx, str,"e")
integer, err := readInteger(idx, str, "e")
if err != nil {
return nil, err
}
Expand All @@ -62,7 +61,7 @@ func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
Expand All @@ -76,7 +75,7 @@ func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
Expand All @@ -91,11 +90,11 @@ func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}

if (*str)[*idx] == 'd' {
(*idx)++
respMap, err := readMap(idx, str)
Expand All @@ -107,7 +106,7 @@ func readMap(idx *int, str *string) (map[interface{}]interface{}, error) {
if i%2 == 1 {
out[prev] = val
}

prev = val
continue
}
Expand All @@ -128,7 +127,7 @@ func Decoder(str string, start ...*int) (interface{}, error) {
for ; idx < len(str); idx++ {
if str[idx] == 'i' {
idx++
integer, err := readInteger(&idx, &str,"e")
integer, err := readInteger(&idx, &str, "e")
if err != nil {
return nil, err
}
Expand All @@ -152,7 +151,7 @@ func Decoder(str string, start ...*int) (interface{}, error) {
out = append(out, array)
continue
}

if str[idx] == 'd' {
idx++
respMap, err := readMap(&idx, &str)
Expand All @@ -175,16 +174,17 @@ func Decoder(str string, start ...*int) (interface{}, error) {
return out, nil
}

func Encoder(benco interface{} ) ([]byte, error) {
// Encoder reads an interface, returning an byte array of the bencoded interface
func Encoder(benco interface{}) ([]byte, error) {

var encoded []byte

switch ty := benco.(type) {
case int, int64:
encoded = append(encoded, 'i')
encoded = append(encoded, []byte(strconv.FormatInt(reflect.ValueOf(ty).Int(), 10))...)
encoded = append(encoded, 'e')

case string:
encoded = append(encoded, []byte(strconv.Itoa(len(ty)))...)
encoded = append(encoded, ':')
Expand Down Expand Up @@ -216,10 +216,10 @@ func Encoder(benco interface{} ) ([]byte, error) {
encoded = append(encoded, encodedValue...)
}
encoded = append(encoded, 'e')

default:
return nil, fmt.Errorf("unsupported data type")
}

return encoded, nil
}
}
97 changes: 48 additions & 49 deletions pkg/bencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,75 @@ import (
"github.com/stretchr/testify/assert"
)


func TestDecoder(t *testing.T) {
tests := []struct {
name string
input string
expected interface{}
name string
input string
expected interface{}
expectedError bool
}{
{
name: "Simple string",
input: "4:spam",
expected: "spam",
name: "Simple string",
input: "4:spam",
expected: "spam",
expectedError: false,
},
{
name: "Integer",
input: "i123e",
expected: int64(123),
name: "Integer",
input: "i123e",
expected: int64(123),
expectedError: false,
},
{
name: "List of strings",
input: "l4:spam4:foooe",
expected: []interface{}{"spam", "fooo"},
name: "List of strings",
input: "l4:spam4:foooe",
expected: []interface{}{"spam", "fooo"},
expectedError: false,
},
{
name: "Dictionary with strings",
input: "d3:bar3:moo4:spam4:foooe",
expected: map[interface {}]interface {}(
map[interface {}]interface {}{
"bar":"moo", "spam":"fooo",
name: "Dictionary with strings",
input: "d3:bar3:moo4:spam4:foooe",
expected: map[interface{}]interface{}(
map[interface{}]interface{}{
"bar": "moo", "spam": "fooo",
},
),
expectedError: false,
},
{
name: "Invalid bencoded string",
input: "invalid",
expected: nil,
name: "Invalid bencoded string",
input: "invalid",
expected: nil,
expectedError: true,
},
{
name: "Dictionary with strings and integers",
input: "d3:bar3:moo4:spami123ee",
expected: map[interface {}]interface {}(
map[interface {}]interface {}{
"bar":"moo", "spam":int64(123),
name: "Dictionary with strings and integers",
input: "d3:bar3:moo4:spami123ee",
expected: map[interface{}]interface{}(
map[interface{}]interface{}{
"bar": "moo", "spam": int64(123),
},
),
expectedError: false,
},
{
name: "Dictionary with strings and arrays",
input: "d3:bar3:moo4:spaml4:spam4:foooee",
expected: map[interface {}]interface {}(
map[interface {}]interface {}{
"bar":"moo",
"spam":[]interface {}{"spam", "fooo"},
name: "Dictionary with strings and arrays",
input: "d3:bar3:moo4:spaml4:spam4:foooee",
expected: map[interface{}]interface{}(
map[interface{}]interface{}{
"bar": "moo",
"spam": []interface{}{"spam", "fooo"},
},
),
expectedError: false,
},
{
name: "Dictionary with strings and dictionaries",
input: "d3:bar3:moo4:spamd3:bar3:moo4:spam4:foooee",
expected: map[interface {}]interface {}(
map[interface {}]interface {}{
"bar":"moo",
"spam":map[interface {}]interface {}{"bar":"moo", "spam":"fooo"},
name: "Dictionary with strings and dictionaries",
input: "d3:bar3:moo4:spamd3:bar3:moo4:spam4:foooee",
expected: map[interface{}]interface{}(
map[interface{}]interface{}{
"bar": "moo",
"spam": map[interface{}]interface{}{"bar": "moo", "spam": "fooo"},
},
),
expectedError: false,
Expand All @@ -101,21 +100,21 @@ func TestEncoder(t *testing.T) {
expectedError bool
}{
{
name: "Simple string",
input: "spam",
expected: "4:spam",
name: "Simple string",
input: "spam",
expected: "4:spam",
expectedError: false,
},
{
name: "Integer",
input: int64(123),
expected: "i123e",
name: "Integer",
input: int64(123),
expected: "i123e",
expectedError: false,
},
{
name: "List of strings",
input: []interface{}{"spam", "fooo"},
expected: "l4:spam4:foooe",
name: "List of strings",
input: []interface{}{"spam", "fooo"},
expected: "l4:spam4:foooe",
expectedError: false,
},
{
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestEncoder(t *testing.T) {
{
name: "Dictionary with strings and arrays",
input: map[interface{}]interface{}{
"bar": "moo",
"bar": "moo",
"spam": []interface{}{"spam", "fooo"},
},
expected: "d3:bar3:moo4:spaml4:spam4:foooee",
Expand Down Expand Up @@ -173,4 +172,4 @@ func TestEncoder(t *testing.T) {
}
})
}
}
}

0 comments on commit 2f241a1

Please sign in to comment.