github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/exchange/bitswap/message/message_test.go (about)

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