github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/is_ip_test.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package function 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 "github.com/dolthub/go-mysql-server/sql/types" 25 ) 26 27 func TestIsIPv4(t *testing.T) { 28 f := NewIsIPv4(expression.NewGetField(0, types.LongText, "", false)) 29 testCases := []struct { 30 name string 31 row sql.Row 32 expected interface{} 33 err bool 34 }{ 35 {"null input", sql.NewRow(nil), nil, false}, 36 {"valid ipv4 address", sql.NewRow("10.0.5.10"), true, false}, 37 {"valid ipv6 address", sql.NewRow("fdfe::5a55:caff:fefa:9098"), false, false}, 38 {"malformed ipv4 address", sql.NewRow("1.10.0.5.10"), false, false}, 39 {"malformed ipv6 address", sql.NewRow("::ffffff"), false, false}, 40 {"invalid ip address", sql.NewRow("thisisnotavalidipaddress"), false, false}, 41 } 42 for _, tt := range testCases { 43 t.Run(tt.name, func(t *testing.T) { 44 t.Helper() 45 require := require.New(t) 46 ctx := sql.NewEmptyContext() 47 48 v, err := f.Eval(ctx, tt.row) 49 if tt.err { 50 require.Error(err) 51 } else { 52 require.NoError(err) 53 require.Equal(tt.expected, v) 54 } 55 }) 56 } 57 } 58 59 func TestIsIPv6(t *testing.T) { 60 f := NewIsIPv6(expression.NewGetField(0, types.LongText, "", false)) 61 testCases := []struct { 62 name string 63 row sql.Row 64 expected interface{} 65 err bool 66 }{ 67 {"null input", sql.NewRow(nil), nil, false}, 68 {"valid ipv4 address", sql.NewRow("10.0.5.10"), false, false}, 69 {"valid ipv6 address", sql.NewRow("::10.0.5.10"), true, false}, 70 {"valid ipv6 address", sql.NewRow("fdfe::5a55:caff:fefa:9098"), true, false}, 71 {"malformed ipv4 address", sql.NewRow("1.10.0.5.10"), false, false}, 72 {"malformed ipv6 address", sql.NewRow("::ffffff"), false, false}, 73 {"invalid ip address", sql.NewRow("thisisnotavalidipaddress"), false, false}, 74 } 75 for _, tt := range testCases { 76 t.Run(tt.name, func(t *testing.T) { 77 t.Helper() 78 require := require.New(t) 79 ctx := sql.NewEmptyContext() 80 81 v, err := f.Eval(ctx, tt.row) 82 if tt.err { 83 require.Error(err) 84 } else { 85 require.NoError(err) 86 require.Equal(tt.expected, v) 87 } 88 }) 89 } 90 } 91 92 func TestIsIPv4Compat(t *testing.T) { 93 f := NewIsIPv4Compat(expression.NewGetField(0, types.LongText, "", false)) 94 testCases := []struct { 95 name string 96 row sql.Row 97 expected interface{} 98 err bool 99 }{ 100 {"null input", sql.NewRow(nil), nil, false}, 101 {"valid ipv4 address", sql.NewRow([]byte{10, 0, 1, 10}), false, false}, 102 {"valid ipv4-compat ipv6 address", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 1, 10}), true, false}, 103 {"valid ipv4-mapped ipv6 address", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 10, 0, 1, 10}), false, false}, 104 {"malformed hex string", sql.NewRow([]byte{0, 0}), false, false}, 105 {"invalid ip address", sql.NewRow("thisisnotavalidipaddress"), false, false}, 106 } 107 for _, tt := range testCases { 108 t.Run(tt.name, func(t *testing.T) { 109 t.Helper() 110 require := require.New(t) 111 ctx := sql.NewEmptyContext() 112 113 v, err := f.Eval(ctx, tt.row) 114 if tt.err { 115 require.Error(err) 116 } else { 117 require.NoError(err) 118 require.Equal(tt.expected, v) 119 } 120 }) 121 } 122 } 123 124 func TestIsIPv4Mapped(t *testing.T) { 125 f := NewIsIPv4Mapped(expression.NewGetField(0, types.LongText, "", false)) 126 testCases := []struct { 127 name string 128 row sql.Row 129 expected interface{} 130 err bool 131 }{ 132 {"null input", sql.NewRow(nil), nil, false}, 133 {"valid ipv4 address", sql.NewRow([]byte{10, 0, 1, 10}), false, false}, 134 {"valid ipv4-compat ipv6 address", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 1, 10}), false, false}, 135 {"valid ipv4-mapped ipv6 address", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 10, 0, 1, 10}), true, false}, 136 {"malformed hex string", sql.NewRow([]byte{0, 0}), false, false}, 137 {"invalid ip address", sql.NewRow("thisisnotavalidipaddress"), false, false}, 138 } 139 for _, tt := range testCases { 140 t.Run(tt.name, func(t *testing.T) { 141 t.Helper() 142 require := require.New(t) 143 ctx := sql.NewEmptyContext() 144 145 v, err := f.Eval(ctx, tt.row) 146 if tt.err { 147 require.Error(err) 148 } else { 149 require.NoError(err) 150 require.Equal(tt.expected, v) 151 } 152 }) 153 } 154 }