| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 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 |
// https://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 escape |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func TestHexEscape(t *testing.T) { |
| 22 |
always := func([]rune, int) bool { return true } |
| 23 |
|
| 24 |
for _, tc := range []struct { |
| 25 |
description, s, want string |
| 26 |
should func([]rune, int) bool |
| 27 |
}{ |
| 28 |
{ |
| 29 |
description: "empty string", |
| 30 |
s: "", |
| 31 |
want: "", |
| 32 |
should: always, |
| 33 |
}, |
| 34 |
{ |
| 35 |
description: "first rune", |
| 36 |
s: "hello world", |
| 37 |
want: "__0x68__ello world", |
| 38 |
should: func(_ []rune, i int) bool { return i == 0 }, |
| 39 |
}, |
| 40 |
{ |
| 41 |
description: "last rune", |
| 42 |
s: "hello world", |
| 43 |
want: "hello worl__0x64__", |
| 44 |
should: func(r []rune, i int) bool { return i == len(r)-1 }, |
| 45 |
}, |
| 46 |
{ |
| 47 |
description: "runes in middle", |
| 48 |
s: "hello world", |
| 49 |
want: "hello__0x20____0x20__world", |
| 50 |
should: func(r []rune, i int) bool { return r[i] == ' ' }, |
| 51 |
}, |
| 52 |
{ |
| 53 |
description: "unicode", |
| 54 |
s: "☺☺", |
| 55 |
should: always, |
| 56 |
want: "__0x263a____0x263a__", |
| 57 |
}, |
| 58 |
} { |
| 59 |
got := HexEscape(tc.s, tc.should) |
| 60 |
if got != tc.want { |
| 61 |
t.Errorf("%s: got escaped %q want %q", tc.description, got, tc.want) |
| 62 |
} |
| 63 |
got = HexUnescape(got) |
| 64 |
if got != tc.s { |
| 65 |
t.Errorf("%s: got unescaped %q want %q", tc.description, got, tc.s) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestHexEscapeUnescapeWeirdStrings(t *testing.T) { |
| 71 |
for name, s := range WeirdStrings { |
| 72 |
escaped := HexEscape(s, func(r []rune, i int) bool { return !IsASCIIAlphanumeric(r[i]) }) |
| 73 |
unescaped := HexUnescape(escaped) |
| 74 |
if unescaped != s { |
| 75 |
t.Errorf("%s: got unescaped %q want %q", name, unescaped, s) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestHexUnescapeOnInvalid(t *testing.T) { |
| 81 |
// Unescaping of valid escape sequences is tested in TestEscape. |
| 82 |
// This only tests invalid escape sequences, so Unescape is expected |
| 83 |
// to do nothing. |
| 84 |
for _, s := range []string{ |
| 85 |
"0x68", |
| 86 |
"_0x68_", |
| 87 |
"__0x68_", |
| 88 |
"_0x68__", |
| 89 |
"__1x68__", |
| 90 |
"__0y68__", |
| 91 |
"__0xag__", // invalid hex digit |
| 92 |
"__0x8fffffff__", // out of int32 range |
| 93 |
} { |
| 94 |
got := HexUnescape(s) |
| 95 |
if got != s { |
| 96 |
t.Errorf("%s: got %q want %q", s, got, s) |
| 97 |
} |
| 98 |
} |
| 99 |
} |