github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/exchange/bitswap/message/message_test.go (about) 1 package message 2 3 import ( 4 "bytes" 5 "testing" 6 7 blocks "github.com/jbenet/go-ipfs/blocks" 8 pb "github.com/jbenet/go-ipfs/exchange/bitswap/message/internal/pb" 9 peer "github.com/jbenet/go-ipfs/peer" 10 u "github.com/jbenet/go-ipfs/util" 11 ) 12 13 func TestAppendWanted(t *testing.T) { 14 const str = "foo" 15 m := New() 16 m.AddWanted(u.Key(str)) 17 18 if !contains(m.ToProto().GetWantlist(), str) { 19 t.Fail() 20 } 21 } 22 23 func TestNewMessageFromProto(t *testing.T) { 24 const str = "a_key" 25 protoMessage := new(pb.Message) 26 protoMessage.Wantlist = []string{string(str)} 27 if !contains(protoMessage.Wantlist, str) { 28 t.Fail() 29 } 30 m := newMessageFromProto(*protoMessage) 31 if !contains(m.ToProto().GetWantlist(), str) { 32 t.Fail() 33 } 34 } 35 36 func TestAppendBlock(t *testing.T) { 37 38 strs := make([]string, 2) 39 strs = append(strs, "Celeritas") 40 strs = append(strs, "Incendia") 41 42 m := New() 43 for _, str := range strs { 44 block := blocks.NewBlock([]byte(str)) 45 m.AddBlock(*block) 46 } 47 48 // assert strings are in proto message 49 for _, blockbytes := range m.ToProto().GetBlocks() { 50 s := bytes.NewBuffer(blockbytes).String() 51 if !contains(strs, s) { 52 t.Fail() 53 } 54 } 55 } 56 57 func TestWantlist(t *testing.T) { 58 keystrs := []string{"foo", "bar", "baz", "bat"} 59 m := New() 60 for _, s := range keystrs { 61 m.AddWanted(u.Key(s)) 62 } 63 exported := m.Wantlist() 64 65 for _, k := range exported { 66 present := false 67 for _, s := range keystrs { 68 69 if s == string(k) { 70 present = true 71 } 72 } 73 if !present { 74 t.Logf("%v isn't in original list", string(k)) 75 t.Fail() 76 } 77 } 78 } 79 80 func TestCopyProtoByValue(t *testing.T) { 81 const str = "foo" 82 m := New() 83 protoBeforeAppend := m.ToProto() 84 m.AddWanted(u.Key(str)) 85 if contains(protoBeforeAppend.GetWantlist(), str) { 86 t.Fail() 87 } 88 } 89 90 func TestToNetMethodSetsPeer(t *testing.T) { 91 m := New() 92 p := peer.WithIDString("X") 93 netmsg, err := m.ToNet(p) 94 if err != nil { 95 t.Fatal(err) 96 } 97 if !(netmsg.Peer().Key() == p.Key()) { 98 t.Fatal("Peer key is different") 99 } 100 } 101 102 func TestToNetFromNetPreservesWantList(t *testing.T) { 103 original := New() 104 original.AddWanted(u.Key("M")) 105 original.AddWanted(u.Key("B")) 106 original.AddWanted(u.Key("D")) 107 original.AddWanted(u.Key("T")) 108 original.AddWanted(u.Key("F")) 109 110 p := peer.WithIDString("X") 111 netmsg, err := original.ToNet(p) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 copied, err := FromNet(netmsg) 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 keys := make(map[u.Key]bool) 122 for _, k := range copied.Wantlist() { 123 keys[k] = true 124 } 125 126 for _, k := range original.Wantlist() { 127 if _, ok := keys[k]; !ok { 128 t.Fatalf("Key Missing: \"%v\"", k) 129 } 130 } 131 } 132 133 func TestToAndFromNetMessage(t *testing.T) { 134 135 original := New() 136 original.AddBlock(*blocks.NewBlock([]byte("W"))) 137 original.AddBlock(*blocks.NewBlock([]byte("E"))) 138 original.AddBlock(*blocks.NewBlock([]byte("F"))) 139 original.AddBlock(*blocks.NewBlock([]byte("M"))) 140 141 p := peer.WithIDString("X") 142 netmsg, err := original.ToNet(p) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 m2, err := FromNet(netmsg) 148 if err != nil { 149 t.Fatal(err) 150 } 151 152 keys := make(map[u.Key]bool) 153 for _, b := range m2.Blocks() { 154 keys[b.Key()] = true 155 } 156 157 for _, b := range original.Blocks() { 158 if _, ok := keys[b.Key()]; !ok { 159 t.Fail() 160 } 161 } 162 } 163 164 func contains(s []string, x string) bool { 165 for _, a := range s { 166 if a == x { 167 return true 168 } 169 } 170 return false 171 } 172 173 func TestDuplicates(t *testing.T) { 174 b := blocks.NewBlock([]byte("foo")) 175 msg := New() 176 177 msg.AddWanted(b.Key()) 178 msg.AddWanted(b.Key()) 179 if len(msg.Wantlist()) != 1 { 180 t.Fatal("Duplicate in BitSwapMessage") 181 } 182 183 msg.AddBlock(*b) 184 msg.AddBlock(*b) 185 if len(msg.Blocks()) != 1 { 186 t.Fatal("Duplicate in BitSwapMessage") 187 } 188 }