forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
158 lines (142 loc) · 4.09 KB
/
encode_test.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package lexbase_test
import (
"bytes"
"fmt"
"strings"
"testing"
"unicode/utf8"
"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
)
func TestEncodeRestrictedSQLIdent(t *testing.T) {
testCases := []struct {
input string
output string
}{
{`foo`, `foo`},
{``, `""`},
{`3`, `"3"`},
{`foo3`, `foo3`},
{`foo"`, `"foo"""`},
{`fo"o"`, `"fo""o"""`},
{`fOo`, `"fOo"`},
{`_foo`, `_foo`},
{`-foo`, `"-foo"`},
{`select`, `"select"`},
{`integer`, `"integer"`},
// N.B. These type names are examples of type names that *should* be
// unrestricted (left out of the reserved keyword list) because they're not
// part of the sql standard type name list. This is important for Postgres
// compatibility. If you find yourself about to change this, don't - you can
// convince yourself of such by looking at the output of `quote_ident`
// against a Postgres instance.
{`int8`, `int8`},
{`date`, `date`},
{`inet`, `inet`},
}
for _, tc := range testCases {
var buf bytes.Buffer
lexbase.EncodeRestrictedSQLIdent(&buf, tc.input, lexbase.EncBareStrings)
out := buf.String()
if out != tc.output {
t.Errorf("`%s`: expected `%s`, got `%s`", tc.input, tc.output, out)
}
}
}
func TestEncodeSQLBytes(t *testing.T) {
testEncodeSQL(t, lexbase.EncodeSQLBytes, false)
}
func TestEncodeSQLString(t *testing.T) {
testEncodeSQL(t, lexbase.EncodeSQLString, true)
}
func testEncodeSQL(t *testing.T, encode func(*bytes.Buffer, string), forceUTF8 bool) {
type entry struct{ i, j int }
seen := make(map[string]entry)
for i := 0; i < 256; i++ {
for j := 0; j < 256; j++ {
bytepair := []byte{byte(i), byte(j)}
if forceUTF8 && !utf8.Valid(bytepair) {
continue
}
stmt := testEncodeString(t, bytepair, encode)
if e, ok := seen[stmt]; ok {
t.Fatalf("duplicate entry: %s, from %v, currently at %v, %v", stmt, e, i, j)
}
seen[stmt] = entry{i, j}
}
}
}
func TestEncodeSQLStringSpecial(t *testing.T) {
tests := [][]byte{
// UTF8 replacement character
{0xEF, 0xBF, 0xBD},
}
for _, tc := range tests {
testEncodeString(t, tc, lexbase.EncodeSQLString)
}
}
func testEncodeString(t *testing.T, input []byte, encode func(*bytes.Buffer, string)) string {
s := string(input)
var buf bytes.Buffer
encode(&buf, s)
sql := fmt.Sprintf("SELECT %s", buf.String())
for n := 0; n < len(sql); n++ {
ch := sql[n]
if ch < 0x20 || ch >= 0x7F {
t.Fatalf("unprintable character: %v (%v): %s %v", ch, input, sql, []byte(sql))
}
}
stmts, err := parser.Parse(sql)
if err != nil {
t.Fatalf("%s: expected success, but found %s", sql, err)
}
stmt := stmts.String()
if sql != stmt {
t.Fatalf("expected %s, but found %s", sql, stmt)
}
return stmt
}
func TestEncodeSQLStringWithNoDoubleEscapeQuotes(t *testing.T) {
testCases := []struct {
input string
output string
}{
// (GH issue #107518)
{`\"`, `e'\"'`},
{`{"a": "b\u0099c"}`, `e'{"a": "b\\u0099c"}'`},
{`{\"a\": \"b\u0099c\"}`, `e'{\"a\": \"b\\u0099c\"}'`},
}
for _, tc := range testCases {
var buf bytes.Buffer
lexbase.EncodeSQLStringWithFlags(&buf, tc.input, lexbase.EncNoDoubleEscapeQuotes)
out := buf.String()
if out != tc.output {
t.Errorf("`%s`: expected `%s`, got `%s`", tc.input, tc.output, out)
}
}
}
func BenchmarkEncodeSQLString(b *testing.B) {
str := strings.Repeat("foo", 10000)
for i := 0; i < b.N; i++ {
lexbase.EncodeSQLStringWithFlags(bytes.NewBuffer(nil), str, lexbase.EncBareStrings)
}
}
func BenchmarkEncodeNonASCIISQLString(b *testing.B) {
builder := strings.Builder{}
for r := rune(0); r < 10000; r++ {
builder.WriteRune(r)
}
str := builder.String()
for i := 0; i < b.N; i++ {
lexbase.EncodeSQLStringWithFlags(bytes.NewBuffer(nil), str, lexbase.EncBareStrings)
}
}