test_like_escape.js (2296B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const LATIN1_AE = "\xc6"; 6 const LATIN1_ae = "\xe6"; 7 8 function setup() { 9 getOpenedDatabase().createTable("t1", "x TEXT"); 10 11 var stmt = createStatement( 12 "INSERT INTO t1 (x) VALUES ('foo/bar_baz%20cheese')" 13 ); 14 stmt.execute(); 15 stmt.finalize(); 16 17 stmt = createStatement("INSERT INTO t1 (x) VALUES (?1)"); 18 // insert LATIN_ae, but search on LATIN_AE 19 stmt.bindByIndex(0, "foo%20" + LATIN1_ae + "/_bar"); 20 stmt.execute(); 21 stmt.finalize(); 22 } 23 24 function test_escape_for_like_ascii() { 25 const paramForLike = "oo/bar_baz%20chees"; 26 const escapeChar = "/"; 27 28 for (const utf8 of [false, true]) { 29 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?1 ESCAPE '/'"); 30 var paramForLikeEscaped = utf8 31 ? stmt.escapeUTF8StringForLIKE(paramForLike, escapeChar) 32 : stmt.escapeStringForLIKE(paramForLike, escapeChar); 33 // verify that we escaped / _ and % 34 Assert.equal(paramForLikeEscaped, "oo//bar/_baz/%20chees"); 35 // prepend and append with % for "contains" 36 stmt.bindByIndex(0, "%" + paramForLikeEscaped + "%"); 37 stmt.executeStep(); 38 Assert.equal("foo/bar_baz%20cheese", stmt.getString(0)); 39 stmt.finalize(); 40 } 41 } 42 43 function test_escape_for_like_non_ascii() { 44 const paramForLike = "oo%20" + LATIN1_AE + "/_ba"; 45 const escapeChar = "/"; 46 47 for (const utf8 of [false, true]) { 48 var stmt = createStatement("SELECT x FROM t1 WHERE x LIKE ?1 ESCAPE '/'"); 49 var paramForLikeEscaped = utf8 50 ? stmt.escapeUTF8StringForLIKE(paramForLike, escapeChar) 51 : stmt.escapeStringForLIKE(paramForLike, escapeChar); 52 // verify that we escaped / _ and % 53 Assert.equal(paramForLikeEscaped, "oo/%20" + LATIN1_AE + "///_ba"); 54 // prepend and append with % for "contains" 55 stmt.bindByIndex(0, "%" + paramForLikeEscaped + "%"); 56 stmt.executeStep(); 57 Assert.equal("foo%20" + LATIN1_ae + "/_bar", stmt.getString(0)); 58 stmt.finalize(); 59 } 60 } 61 62 var tests = [test_escape_for_like_ascii, test_escape_for_like_non_ascii]; 63 64 function run_test() { 65 setup(); 66 67 for (var i = 0; i < tests.length; i++) { 68 tests[i](); 69 } 70 71 cleanup(); 72 }