-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test the IP address and subnet generation
Test the IP address and subnet generation to avoid breaking the existing generation when updating or refactoring it. Given that the subnet generation uses some binary operations, it is hard to spot potential errors. For this usecase the function generating the subnet was externalized to be accessible from the test code instead of an inlined function.
- Loading branch information
Showing
5 changed files
with
64 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ffbs/etcd-tools/ffbs" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateAddressesAndRanges(t *testing.T) { | ||
// Using https://freifunk-bs.de/map/#!/de/map/f4068dcf9fe1 as an example | ||
id := uint64(30) | ||
node := ffbs.NodeInfo{ID: &id} | ||
generateNodeAddressesAndRanges(&node) | ||
assert.Equal(t, "10.0.120.1", *node.Address4) | ||
assert.Equal(t, "10.0.120.0/22", *node.Range4) | ||
assert.Equal(t, "2001:bf7:381:1e::1", *node.Address6) | ||
assert.Equal(t, "2001:bf7:381:1e::/64", *node.Range6) | ||
} | ||
|
||
func TestGenerateHighestAddressesAndRanges(t *testing.T) { | ||
// IPv4 base range is /8. Range of every subnet is /22 => 22-8 = 14 bits of freedom | ||
id := uint64(16383) | ||
node := ffbs.NodeInfo{ID: &id} | ||
generateNodeAddressesAndRanges(&node) | ||
assert.Equal(t, "10.255.252.1", *node.Address4) | ||
assert.Equal(t, "10.255.252.0/22", *node.Range4) | ||
assert.Equal(t, "2001:bf7:381:3fff::1", *node.Address6) | ||
assert.Equal(t, "2001:bf7:381:3fff::/64", *node.Range6) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters