Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
FIX: logic error in Diff()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Apr 8, 2021
1 parent 3e90691 commit 06c516f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inet/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (b Block) Diff(bs []Block) []Block {
case d.Covers(b):
// masks rest
return out
case d.base == b.base:
case d.base.Less(b.base), d.base == b.base:
// move forward
b.base = d.last.addOne()
case b.base.Less(d.base):
Expand Down
32 changes: 32 additions & 0 deletions inet/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,38 @@ func TestFindDiffCornerCases(t *testing.T) {
t.Errorf("Diff(base>last), got %v, want %v", rs, want)
}

// left overlap v4
r = mustBlock("10.0.0.5-10.0.0.15")
rs = r.Diff([]Block{mustBlock("10.0.0.3-10.0.0.10")})
want = mustBlock("10.0.0.11-10.0.0.15")
if rs[0] != want {
t.Errorf("Diff(leftOverlapV4), got %v, want %v", rs, want)
}

// right overlap v4
r = mustBlock("10.0.0.4-10.0.0.15")
rs = r.Diff([]Block{mustBlock("10.0.0.6-10.0.0.19")})
want = mustBlock("10.0.0.4-10.0.0.5")
if rs[0] != want {
t.Errorf("Diff(leftOverlapV4), got %v, want %v", rs, want)
}

// left overlap v6
r = mustBlock("2001:db8::17-2001:db8::177")
rs = r.Diff([]Block{mustBlock("2001:db8::14-2001:db8::137")})
want = mustBlock("2001:db8::138-2001:db8::177")
if rs[0] != want {
t.Errorf("Diff(leftOverlapV4), got %v, want %v", rs, want)
}

// right overlap v6
r = mustBlock("2001:db8::17-2001:db8::177")
rs = r.Diff([]Block{mustBlock("2001:db8::3f-2001:db8::fff")})
want = mustBlock("2001:db8::17-2001:db8::3e")
if rs[0] != want {
t.Errorf("Diff(leftOverlapV4), got %v, want %v", rs, want)
}

}

func TestFindDiffIANAv6(t *testing.T) {
Expand Down

0 comments on commit 06c516f

Please # to comment.