github.com/m3db/m3@v1.5.0/src/query/models/strconv/checker_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package strconv 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func generateUnescapedSlice() []byte { 30 bottomBound := int(' ') 31 upperBound := int('~') 32 unescaped := make([]byte, upperBound-bottomBound) 33 ignore := int('"') 34 idx := 0 35 for i := bottomBound; i <= upperBound; i++ { 36 if i != ignore { 37 unescaped[idx] = byte(i) 38 idx++ 39 } 40 } 41 42 return unescaped 43 } 44 45 func TestUnescapedSliceDoesNotNeedToEscape(t *testing.T) { 46 unescaped := generateUnescapedSlice() 47 assert.False(t, NeedToEscape(unescaped)) 48 } 49 50 func TestSliceWithQuoteNeedsToEscape(t *testing.T) { 51 unescaped := generateUnescapedSlice() 52 unescaped = append(unescaped, '"') 53 assert.True(t, NeedToEscape(unescaped)) 54 } 55 56 func TestSliceWithControlCharactersNeedsToEscape(t *testing.T) { 57 unescaped := generateUnescapedSlice() 58 lowByte := byte(int(' ') - 1) 59 unescapedWithLowByte := append(unescaped, lowByte) 60 assert.True(t, NeedToEscape(unescapedWithLowByte)) 61 62 highByte := byte(int('~') + 1) 63 unescaped = append(unescaped, highByte) 64 assert.True(t, NeedToEscape(unescaped)) 65 } 66 67 func TestIsAlphaNumeric(t *testing.T) { 68 alphaStr := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 69 assert.True(t, IsAlphaNumeric(alphaStr)) 70 71 // Ensure each rune is alpha numeric 72 for _, r := range alphaStr { 73 assert.True(t, IsRuneAlphaNumeric(r)) 74 } 75 } 76 77 func TestEachNonAlphaNumeric(t *testing.T) { 78 // NB: generate every character then remove any alphanumeric 79 charMap := make(map[int]string, 256) 80 for i := 0; i < 256; i++ { 81 charMap[i] = string(byte(i)) 82 } 83 84 alphaStr := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 85 for _, c := range alphaStr { 86 delete(charMap, int(c)) 87 } 88 89 for _, invalid := range charMap { 90 assert.False(t, IsAlphaNumeric(invalid)) 91 92 // Ensure each rune is alpha numeric 93 for _, r := range invalid { 94 assert.False(t, IsRuneAlphaNumeric(r)) 95 } 96 } 97 }