]> Sergey Matveev's repositories - btrtrc.git/blobdiff - peer_protocol/protocol_test.go
Fix request/chunk confusion, missing outgoing message prefix, protocol tests; improve...
[btrtrc.git] / peer_protocol / protocol_test.go
index 7a7b8dd1ac1d109caafaa8811c6c5a86e8a68fb4..3c04aff85f23e9258a095cde464e4c15cda6bfc1 100644 (file)
@@ -10,22 +10,47 @@ func TestConstants(t *testing.T) {
                t.FailNow()
        }
 }
+
 func TestBitfieldEncode(t *testing.T) {
-       bm := make(Bitfield, 37)
-       bm[2] = true
-       bm[7] = true
-       bm[32] = true
-       s := string(bm.Encode())
+       bf := make([]bool, 37)
+       bf[2] = true
+       bf[7] = true
+       bf[32] = true
+       s := string(marshalBitfield(bf))
        const expected = "\x21\x00\x00\x00\x80"
        if s != expected {
                t.Fatalf("got %#v, expected %#v", s, expected)
        }
 }
 
+func TestBitfieldUnmarshal(t *testing.T) {
+       bf := unmarshalBitfield([]byte("\x81\x06"))
+       expected := make([]bool, 16)
+       expected[0] = true
+       expected[7] = true
+       expected[13] = true
+       expected[14] = true
+       if len(bf) != len(expected) {
+               t.FailNow()
+       }
+       for i := range expected {
+               if bf[i] != expected[i] {
+                       t.FailNow()
+               }
+       }
+}
+
 func TestHaveEncode(t *testing.T) {
-       actual := string(Have(42).Encode())
+       actualBytes, err := Message{
+               Type:  Have,
+               Index: 42,
+       }.MarshalBinary()
+       if err != nil {
+               t.Fatal(err)
+       }
+       actualString := string(actualBytes)
        expected := "\x04\x00\x00\x00\x2a"
-       if actual != expected {
-               t.Fatalf("expected %#v, got %#v", expected, actual)
+       if actualString != expected {
+               t.Fatalf("expected %#v, got %#v", expected, actualString)
        }
 }