github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/inet_convert_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 TestInetAton(t *testing.T) { 28 f := NewInetAton(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"), uint32(167773450), false}, 37 // Output does not match MySQL, but it also indicates it shouldn't be used for short-form anyway: https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_inet-aton 38 {"valid short-form ipv4 address", sql.NewRow("10.5.10"), nil, false}, 39 {"valid short-form ip4 address (non-string)", sql.NewRow(10.0), nil, false}, 40 {"valid ipv6 address", sql.NewRow("::10.0.5.10"), nil, false}, 41 {"valid ipv6 address", sql.NewRow("fdfe::5a55:caff:fefa:9098"), nil, false}, 42 {"invalid ipv4 address", sql.NewRow("1.10.0.5.10"), nil, false}, 43 {"valid ipv6 address", sql.NewRow("thisisnotavalidipaddress"), nil, false}, 44 } 45 for _, tt := range testCases { 46 t.Run(tt.name, func(t *testing.T) { 47 t.Helper() 48 require := require.New(t) 49 ctx := sql.NewEmptyContext() 50 51 v, err := f.Eval(ctx, tt.row) 52 if tt.err { 53 require.Error(err) 54 } else { 55 require.NoError(err) 56 require.Equal(tt.expected, v) 57 } 58 }) 59 } 60 } 61 62 func TestInetNtoa(t *testing.T) { 63 f := NewInetNtoa(expression.NewGetField(0, types.LongText, "", false)) 64 testCases := []struct { 65 name string 66 row sql.Row 67 expected interface{} 68 err bool 69 }{ 70 {"null input", sql.NewRow(nil), nil, false}, 71 {"valid ipv4 int", sql.NewRow(uint32(167773450)), "10.0.5.10", false}, 72 {"valid ipv4 int as string", sql.NewRow("167773450"), "10.0.5.10", false}, 73 {"floating point ipv4", sql.NewRow(10.1), "0.0.0.10", false}, 74 {"valid ipv6 int", sql.NewRow("\000\000\000\000"), "0.0.0.0", false}, 75 } 76 for _, tt := range testCases { 77 t.Run(tt.name, func(t *testing.T) { 78 t.Helper() 79 require := require.New(t) 80 ctx := sql.NewEmptyContext() 81 82 v, err := f.Eval(ctx, tt.row) 83 if tt.err { 84 require.Error(err) 85 } else { 86 require.NoError(err) 87 require.Equal(tt.expected, v) 88 } 89 }) 90 } 91 } 92 93 func TestInet6Aton(t *testing.T) { 94 f := NewInet6Aton(expression.NewGetField(0, types.LongText, "", false)) 95 testCases := []struct { 96 name string 97 row sql.Row 98 expected interface{} 99 err bool 100 }{ 101 {"null input", sql.NewRow(nil), nil, false}, 102 {"valid ipv4 address", sql.NewRow("10.0.5.10"), []byte{10, 0, 5, 10}, false}, 103 {"valid ipv4-mapped ipv6 address", sql.NewRow("::10.0.5.10"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 5, 10}, false}, 104 {"valid short-form ipv4 address", sql.NewRow("10.5.10"), nil, false}, 105 {"valid ipv6 address", sql.NewRow("fdfe::5a55:caff:fefa:9098"), []byte{0xfd, 0xfe, 0, 0, 0, 0, 0, 0, 0x5a, 0x55, 0xca, 0xff, 0xfe, 0xfa, 0x90, 0x98}, false}, 106 {"invalid ipv4 address", sql.NewRow("1.10.0.5.10"), nil, false}, 107 {"valid ipv6 address", sql.NewRow("thisisnotavalidipaddress"), nil, false}, 108 } 109 for _, tt := range testCases { 110 t.Run(tt.name, func(t *testing.T) { 111 t.Helper() 112 require := require.New(t) 113 ctx := sql.NewEmptyContext() 114 115 v, err := f.Eval(ctx, tt.row) 116 if tt.err { 117 require.Error(err) 118 } else { 119 require.NoError(err) 120 require.Equal(tt.expected, v) 121 } 122 }) 123 } 124 } 125 126 func TestInet6Ntoa(t *testing.T) { 127 f := NewInet6Ntoa(expression.NewGetField(0, types.LongText, "", false)) 128 testCases := []struct { 129 name string 130 row sql.Row 131 expected interface{} 132 err bool 133 }{ 134 {"null input", sql.NewRow(nil), nil, false}, 135 {"valid ipv4 int", sql.NewRow(uint32(167773450)), nil, false}, 136 {"valid ipv4 int as string", sql.NewRow("167773450"), nil, false}, 137 {"floating point ipv4", sql.NewRow(10.1), nil, false}, 138 {"valid ipv6 int", sql.NewRow([]byte{0, 0, 0, 0}), "0.0.0.0", false}, 139 {"valid ipv6 int", sql.NewRow([]byte{0xfd, 0xfe, 0, 0, 0, 0, 0, 0, 0x5a, 0x55, 0xca, 0xff, 0xfe, 0xfa, 0x90, 0x98}), "fdfe::5a55:caff:fefa:9098", false}, 140 {"valid ipv4-mapped int", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 10, 0, 5, 10}), "::ffff:10.0.5.10", false}, 141 {"valid ipv4-compatible int", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 5, 10}), "::10.0.5.10", false}, 142 {"all zeros", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), "::", false}, 143 {"only last 4 bytes filled", sql.NewRow([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34}), "::1234", false}, 144 } 145 for _, tt := range testCases { 146 t.Run(tt.name, func(t *testing.T) { 147 t.Helper() 148 require := require.New(t) 149 ctx := sql.NewEmptyContext() 150 151 v, err := f.Eval(ctx, tt.row) 152 if tt.err { 153 require.Error(err) 154 } else { 155 require.NoError(err) 156 require.Equal(tt.expected, v) 157 } 158 }) 159 } 160 }