about summary refs log tree commit diff
path: root/tools/ipconverter/main_test.go
blob: 394dc2836c8101dc3f6ba6d458abd705630b967a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"math/big"
	"testing"
)

func TestIPtoInt(t *testing.T) {

	tests := map[string]*big.Int{
		"192.168.0.1": big.NewInt(3232235521),
		"10.0.0.1":    big.NewInt(167772161),
	}
	for test := range tests {
		r, err := IPtoInt(test)
		if err != nil {
			t.Errorf("failed to convert %s to an int: %s", test, err)
		}
		if r.Cmp(tests[test]) != 0 {
			t.Errorf("convert %s to int, got %d expected %d", test, r, tests[test])
		}
	}

	if _, err := IPtoInt("10"); err == nil {
		t.Error("calling IPtoInt with invalid IP did not result in error")
	}
}


func TestIPtoN(t *testing.T) {
	tests := map[string]string{
		"3232235521": "192.168.0.1",
		"167772161": "10.0.0.1",
	}
	for test := range tests {
		r, err := IPtoN(test)
		if err != nil {
			t.Errorf("failed to convert %s to an address: %s", test, err)
		}
		if r != tests[test] {
			t.Errorf("convert %s to address, got %s expected %s", test, r, tests[test])
		}
	}
}