~ancarda/tls-redirector

ref: e4636a57dc63f656f5b8db32dd716c84038a09be tls-redirector/util_test.go -rw-r--r-- 992 bytes
e4636a57Mark Dain 2.4: Nice looking HTML error pages 8 months ago
                                                                                
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
package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestRemoveSquareBrackets(t *testing.T) {
	assert.Equal(t, "[::1]a", removeSquareBrackets("[::1]a"))
	assert.Equal(t, "a[::1]", removeSquareBrackets("a[::1]"))
	assert.Equal(t, "::1", removeSquareBrackets("[::1]"))
}

func TestIsIPAddress_IPv4(t *testing.T) {
	assert.True(t, isIPAddress("127.0.0.1"), "IPv4 address")
	assert.True(t, isIPAddress("127.0.0.1:8080"), "IPv4 address with port")
}

func TestIsIPAddress_IPv6(t *testing.T) {
	assert.True(t, isIPAddress("::1"), "IPv6 address")
	assert.True(t, isIPAddress("[::1]"), "IPv6 address inside brackets")
	assert.True(t, isIPAddress("[::1]:8080"), "IPv6 address with port")
}

func TestIsIPAddress_InvalidIPAddresses(t *testing.T) {
	assert.False(t, isIPAddress("1.2.3.4.5"))
	assert.False(t, isIPAddress("256.1.1.1"))
}

func TestIsIPAddress_RejectOtherStrings(t *testing.T) {
	assert.False(t, isIPAddress("example.com"))
	assert.False(t, isIPAddress(""))
}