multiaddr_test.go 951 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package multiaddr

import (
  "bytes"
  "testing"
  "encoding/hex"
)


func TestStringToBytes(t *testing.T) {

  testString := func(s string, h string) {
    b1, err := hex.DecodeString(h)
    if err != nil {
      t.Error("failed to decode hex", h)
    }

    b2, err := StringToBytes(s)
    if err != nil {
      t.Error("failed to convert", s)
    }

    if !bytes.Equal(b1, b2) {
      t.Error("failed to convert", s, "to", b1, "got", b2)
    }
  }

  testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
}

func TestBytesToString(t *testing.T) {

  testString := func(s1 string, h string) {
    b, err := hex.DecodeString(h)
    if err != nil {
      t.Error("failed to decode hex", h)
    }

    s2, err := BytesToString(b)
    if err != nil {
      t.Error("failed to convert", b)
    }

    if s1 == s2 {
      t.Error("failed to convert", b, "to", s1, "got", s2)
    }
  }

  testString("/ip4/127.0.0.1/udp/1234", "047f0000011104d2")
}