|
| 1 | +// Copyright 2017 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package lex_test |
| 12 | + |
| 13 | +import ( |
| 14 | + "bytes" |
| 15 | + "fmt" |
| 16 | + "strings" |
| 17 | + "testing" |
| 18 | + "unicode/utf8" |
| 19 | + |
| 20 | + "github.com/auxten/postgresql-parser/pkg/sql/lex" |
| 21 | + "github.com/auxten/postgresql-parser/pkg/sql/parser" |
| 22 | + "github.com/auxten/postgresql-parser/pkg/sql/sessiondata" |
| 23 | +) |
| 24 | + |
| 25 | +func TestEncodeSQLBytes(t *testing.T) { |
| 26 | + testEncodeSQL(t, lex.EncodeSQLBytes, false) |
| 27 | +} |
| 28 | + |
| 29 | +func TestEncodeSQLString(t *testing.T) { |
| 30 | + testEncodeSQL(t, lex.EncodeSQLString, true) |
| 31 | +} |
| 32 | + |
| 33 | +func testEncodeSQL(t *testing.T, encode func(*bytes.Buffer, string), forceUTF8 bool) { |
| 34 | + type entry struct{ i, j int } |
| 35 | + seen := make(map[string]entry) |
| 36 | + for i := 0; i < 256; i++ { |
| 37 | + for j := 0; j < 256; j++ { |
| 38 | + bytepair := []byte{byte(i), byte(j)} |
| 39 | + if forceUTF8 && !utf8.Valid(bytepair) { |
| 40 | + continue |
| 41 | + } |
| 42 | + stmt := testEncodeString(t, bytepair, encode) |
| 43 | + if e, ok := seen[stmt]; ok { |
| 44 | + t.Fatalf("duplicate entry: %s, from %v, currently at %v, %v", stmt, e, i, j) |
| 45 | + } |
| 46 | + seen[stmt] = entry{i, j} |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestEncodeSQLStringSpecial(t *testing.T) { |
| 52 | + tests := [][]byte{ |
| 53 | + // UTF8 replacement character |
| 54 | + {0xEF, 0xBF, 0xBD}, |
| 55 | + } |
| 56 | + for _, tc := range tests { |
| 57 | + testEncodeString(t, tc, lex.EncodeSQLString) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func testEncodeString(t *testing.T, input []byte, encode func(*bytes.Buffer, string)) string { |
| 62 | + s := string(input) |
| 63 | + var buf bytes.Buffer |
| 64 | + encode(&buf, s) |
| 65 | + sql := fmt.Sprintf("SELECT %s", buf.String()) |
| 66 | + for n := 0; n < len(sql); n++ { |
| 67 | + ch := sql[n] |
| 68 | + if ch < 0x20 || ch >= 0x7F { |
| 69 | + t.Fatalf("unprintable character: %v (%v): %s %v", ch, input, sql, []byte(sql)) |
| 70 | + } |
| 71 | + } |
| 72 | + stmts, err := parser.Parse(sql) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("%s: expected success, but found %s", sql, err) |
| 75 | + } |
| 76 | + stmt := stmts.String() |
| 77 | + if sql != stmt { |
| 78 | + t.Fatalf("expected %s, but found %s", sql, stmt) |
| 79 | + } |
| 80 | + return stmt |
| 81 | +} |
| 82 | + |
| 83 | +func BenchmarkEncodeSQLString(b *testing.B) { |
| 84 | + str := strings.Repeat("foo", 10000) |
| 85 | + for i := 0; i < b.N; i++ { |
| 86 | + lex.EncodeSQLStringWithFlags(bytes.NewBuffer(nil), str, lex.EncBareStrings) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestEncodeRestrictedSQLIdent(t *testing.T) { |
| 91 | + testCases := []struct { |
| 92 | + input string |
| 93 | + output string |
| 94 | + }{ |
| 95 | + {`foo`, `foo`}, |
| 96 | + {``, `""`}, |
| 97 | + {`3`, `"3"`}, |
| 98 | + {`foo3`, `foo3`}, |
| 99 | + {`foo"`, `"foo"""`}, |
| 100 | + {`fo"o"`, `"fo""o"""`}, |
| 101 | + {`fOo`, `"fOo"`}, |
| 102 | + {`_foo`, `_foo`}, |
| 103 | + {`-foo`, `"-foo"`}, |
| 104 | + {`select`, `"select"`}, |
| 105 | + {`integer`, `"integer"`}, |
| 106 | + // N.B. These type names are examples of type names that *should* be |
| 107 | + // unrestricted (left out of the reserved keyword list) because they're not |
| 108 | + // part of the sql standard type name list. This is important for Postgres |
| 109 | + // compatibility. If you find yourself about to change this, don't - you can |
| 110 | + // convince yourself of such by looking at the output of `quote_ident` |
| 111 | + // against a Postgres instance. |
| 112 | + {`int8`, `int8`}, |
| 113 | + {`date`, `date`}, |
| 114 | + {`inet`, `inet`}, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tc := range testCases { |
| 118 | + var buf bytes.Buffer |
| 119 | + lex.EncodeRestrictedSQLIdent(&buf, tc.input, lex.EncBareStrings) |
| 120 | + out := buf.String() |
| 121 | + |
| 122 | + if out != tc.output { |
| 123 | + t.Errorf("`%s`: expected `%s`, got `%s`", tc.input, tc.output, out) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestByteArrayDecoding(t *testing.T) { |
| 129 | + const ( |
| 130 | + fmtHex = sessiondata.BytesEncodeHex |
| 131 | + fmtEsc = sessiondata.BytesEncodeEscape |
| 132 | + fmtB64 = sessiondata.BytesEncodeBase64 |
| 133 | + ) |
| 134 | + testData := []struct { |
| 135 | + in string |
| 136 | + auto bool |
| 137 | + inFmt sessiondata.BytesEncodeFormat |
| 138 | + out string |
| 139 | + err string |
| 140 | + }{ |
| 141 | + {`a`, false, fmtHex, "", "encoding/hex: odd length hex string"}, |
| 142 | + {`aa`, false, fmtHex, "\xaa", ""}, |
| 143 | + {`aA`, false, fmtHex, "\xaa", ""}, |
| 144 | + {`AA`, false, fmtHex, "\xaa", ""}, |
| 145 | + {`x0`, false, fmtHex, "", "encoding/hex: invalid byte: U+0078 'x'"}, |
| 146 | + {`a\nbcd`, false, fmtEsc, "", "invalid bytea escape sequence"}, |
| 147 | + {`a\'bcd`, false, fmtEsc, "", "invalid bytea escape sequence"}, |
| 148 | + {`a\00`, false, fmtEsc, "", "bytea encoded value ends with incomplete escape sequence"}, |
| 149 | + {`a\099`, false, fmtEsc, "", "invalid bytea escape sequence"}, |
| 150 | + {`a\400`, false, fmtEsc, "", "invalid bytea escape sequence"}, |
| 151 | + {`a\777`, false, fmtEsc, "", "invalid bytea escape sequence"}, |
| 152 | + {`a'b`, false, fmtEsc, "a'b", ""}, |
| 153 | + {`a''b`, false, fmtEsc, "a''b", ""}, |
| 154 | + {`a\\b`, false, fmtEsc, "a\\b", ""}, |
| 155 | + {`a\000b`, false, fmtEsc, "a\x00b", ""}, |
| 156 | + {"a\nb", false, fmtEsc, "a\nb", ""}, |
| 157 | + {`a`, false, fmtB64, "", "illegal base64 data at input byte 0"}, |
| 158 | + {`aa=`, false, fmtB64, "", "illegal base64 data at input byte 3"}, |
| 159 | + {`AA==`, false, fmtB64, "\x00", ""}, |
| 160 | + {`/w==`, false, fmtB64, "\xff", ""}, |
| 161 | + {`AAAA`, false, fmtB64, "\x00\x00\x00", ""}, |
| 162 | + {`a`, true, 0, "a", ""}, |
| 163 | + {`\x`, true, 0, "", ""}, |
| 164 | + {`\xx`, true, 0, "", "encoding/hex: invalid byte: U+0078 'x'"}, |
| 165 | + {`\x6162`, true, 0, "ab", ""}, |
| 166 | + {`\\x6162`, true, 0, "\\x6162", ""}, |
| 167 | + } |
| 168 | + for _, s := range testData { |
| 169 | + t.Run(fmt.Sprintf("%s:%s", s.in, s.inFmt), func(t *testing.T) { |
| 170 | + var dec []byte |
| 171 | + var err error |
| 172 | + if s.auto { |
| 173 | + dec, err = lex.DecodeRawBytesToByteArrayAuto([]byte(s.in)) |
| 174 | + } else { |
| 175 | + dec, err = lex.DecodeRawBytesToByteArray(s.in, s.inFmt) |
| 176 | + } |
| 177 | + if s.err != "" { |
| 178 | + if err == nil { |
| 179 | + t.Fatalf("expected err %q, got no error", s.err) |
| 180 | + } |
| 181 | + if s.err != err.Error() { |
| 182 | + t.Fatalf("expected err %q, got %q", s.err, err) |
| 183 | + } |
| 184 | + return |
| 185 | + } |
| 186 | + if err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + if string(dec) != s.out { |
| 190 | + t.Fatalf("expected %q, got %q", s.out, dec) |
| 191 | + } |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func TestByteArrayEncoding(t *testing.T) { |
| 197 | + testData := []struct { |
| 198 | + in string |
| 199 | + out []string |
| 200 | + }{ |
| 201 | + // The reference values were gathered from PostgreSQL. |
| 202 | + {"", []string{`\x`, ``, ``}}, |
| 203 | + {"abc", []string{`\x616263`, `abc`, `YWJj`}}, |
| 204 | + {"a\nb", []string{`\x610a62`, `a\012b`, `YQpi`}}, |
| 205 | + {`a\nb`, []string{`\x615c6e62`, `a\\nb`, `YVxuYg==`}}, |
| 206 | + {"a'b", []string{`\x612762`, `a'b`, `YSdi`}}, |
| 207 | + {"a\"b", []string{`\x612262`, `a"b`, `YSJi`}}, |
| 208 | + {"a\x00b", []string{`\x610062`, `a\000b`, `YQBi`}}, |
| 209 | + } |
| 210 | + |
| 211 | + for _, s := range testData { |
| 212 | + t.Run(s.in, func(t *testing.T) { |
| 213 | + for _, format := range []sessiondata.BytesEncodeFormat{ |
| 214 | + sessiondata.BytesEncodeHex, sessiondata.BytesEncodeEscape, sessiondata.BytesEncodeBase64} { |
| 215 | + t.Run(format.String(), func(t *testing.T) { |
| 216 | + enc := lex.EncodeByteArrayToRawBytes(s.in, format, false) |
| 217 | + |
| 218 | + expEnc := s.out[int(format)] |
| 219 | + if enc != expEnc { |
| 220 | + t.Fatalf("encoded %q, expected %q", enc, expEnc) |
| 221 | + } |
| 222 | + |
| 223 | + if format == sessiondata.BytesEncodeHex { |
| 224 | + // Check that the \x also can be skipped. |
| 225 | + enc2 := lex.EncodeByteArrayToRawBytes(s.in, format, true) |
| 226 | + if enc[2:] != enc2 { |
| 227 | + t.Fatal("can't skip prefix") |
| 228 | + } |
| 229 | + enc = enc[2:] |
| 230 | + } |
| 231 | + |
| 232 | + dec, err := lex.DecodeRawBytesToByteArray(enc, format) |
| 233 | + if err != nil { |
| 234 | + t.Fatal(err) |
| 235 | + } |
| 236 | + if string(dec) != s.in { |
| 237 | + t.Fatalf("decoded %q, expected %q", string(dec), s.in) |
| 238 | + } |
| 239 | + }) |
| 240 | + } |
| 241 | + }) |
| 242 | + } |
| 243 | +} |
0 commit comments