Repository: syncthing/syncthing
File: lib/protocol/protocol_test.go (Go)

Code before:
	case <-time.After(time.Second):
		t.Fatal("timed out before dispatcher loop terminated")
	}
}

func TestBlocksEqual(t *testing.T) {
	blocksOne := []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}
	blocksTwo := []BlockInfo{{Hash: []byte{5, 6, 7, 8}}}
	hashOne := []byte{42, 42, 42, 42}
	hashTwo := []byte{29, 29, 29, 29}

	cases := []struct {
		b1 []BlockInfo
		h1 []byte
		b2 []BlockInfo
		h2 []byte
		eq bool
	}{
		{blocksOne, hashOne, blocksOne, hashOne, true},  // everything equal
		{blocksOne, hashOne, blocksTwo, hashTwo, false}, // nothing equal
		{blocksOne, hashOne, blocksOne, nil, true},      // blocks compared
		{blocksOne, nil, blocksOne, nil, true},          // blocks compared
		{blocksOne, nil, blocksTwo, nil, false},         // blocks compared
		{blocksOne, hashOne, blocksTwo, hashOne, true},  // hashes equal, blocks not looked at
		{blocksOne, hashOne, blocksOne, hashTwo, true},  // hashes different, blocks ompared
		{blocksOne, hashOne, blocksTwo, hashTwo, false}, // hashes different, blocks ompared
		{blocksOne, hashOne, nil, nil, false},           // blocks is different from no blocks
		{blocksOne, nil, nil, nil, false},               // blocks is different from no blocks
		{nil, hashOne, nil, nil, true},                  // nil blocks are equal, even of one side has a hash
	}

	for _, tc := range cases {
		f1 := FileInfo{Blocks: tc.b1, BlocksHash: tc.h1}
		f2 := FileInfo{Blocks: tc.b2, BlocksHash: tc.h2}

		if !f1.BlocksEqual(f1) {
			t.Error("f1 is always equal to itself", f1)
		}
		if !f2.BlocksEqual(f2) {
			t.Error("f2 is always equal to itself", f2)
		}
		if res := f1.BlocksEqual(f2); res != tc.eq {
			t.Log("f1", f1.BlocksHash, f1.Blocks)
			t.Log("f2", f2.BlocksHash, f2.Blocks)
			t.Errorf("f1.BlocksEqual(f2) == %v but should be %v", res, tc.eq)
		}
		if res := f2.BlocksEqual(f1); res != tc.eq {
			t.Log("f1", f1.BlocksHash, f1.Blocks)
			t.Log("f2", f2.BlocksHash, f2.Blocks)
			t.Errorf("f2.BlocksEqual(f1) == %v but should be %v", res, tc.eq)
		}

Review comment (suggestion):
```suggestion
		{blocksOne, hashOne, blocksOne, hashTwo, true},  // hashes different, blocks compared
		{blocksOne, hashOne, blocksTwo, hashTwo, false}, // hashes different, blocks compared
```

Code after:
	case <-time.After(time.Second):
		t.Fatal("timed out before dispatcher loop terminated")
	}
}

func TestBlocksEqual(t *testing.T) {
	blocksOne := []BlockInfo{{Hash: []byte{1, 2, 3, 4}}}
	blocksTwo := []BlockInfo{{Hash: []byte{5, 6, 7, 8}}}
	hashOne := []byte{42, 42, 42, 42}
	hashTwo := []byte{29, 29, 29, 29}

	cases := []struct {
		b1 []BlockInfo
		h1 []byte
		b2 []BlockInfo
		h2 []byte
		eq bool
	}{
		{blocksOne, hashOne, blocksOne, hashOne, true},  // everything equal
		{blocksOne, hashOne, blocksTwo, hashTwo, false}, // nothing equal
		{blocksOne, hashOne, blocksOne, nil, true},      // blocks compared
		{blocksOne, nil, blocksOne, nil, true},          // blocks compared
		{blocksOne, nil, blocksTwo, nil, false},         // blocks compared
		{blocksOne, hashOne, blocksTwo, hashOne, true},  // hashes equal, blocks not looked at
		{blocksOne, hashOne, blocksOne, hashTwo, true},  // hashes different, blocks compared
		{blocksOne, hashOne, blocksTwo, hashTwo, false}, // hashes different, blocks compared
		{blocksOne, hashOne, nil, nil, false},           // blocks is different from no blocks
		{blocksOne, nil, nil, nil, false},               // blocks is different from no blocks
		{nil, hashOne, nil, nil, true},                  // nil blocks are equal, even of one side has a hash
	}

	for _, tc := range cases {
		f1 := FileInfo{Blocks: tc.b1, BlocksHash: tc.h1}
		f2 := FileInfo{Blocks: tc.b2, BlocksHash: tc.h2}

		if !f1.BlocksEqual(f1) {
			t.Error("f1 is always equal to itself", f1)
		}
		if !f2.BlocksEqual(f2) {
			t.Error("f2 is always equal to itself", f2)
		}
		if res := f1.BlocksEqual(f2); res != tc.eq {
			t.Log("f1", f1.BlocksHash, f1.Blocks)
			t.Log("f2", f2.BlocksHash, f2.Blocks)
			t.Errorf("f1.BlocksEqual(f2) == %v but should be %v", res, tc.eq)
		}
		if res := f2.BlocksEqual(f1); res != tc.eq {
			t.Log("f1", f1.BlocksHash, f1.Blocks)
			t.Log("f2", f2.BlocksHash, f2.Blocks)
			t.Errorf("f2.BlocksEqual(f1) == %v but should be %v", res, tc.eq)
		}