1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 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 * http://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 16/* 17 * @tc.name:decodeuricomponent 18 * @tc.desc:test decodeuricomponent 19 * @tc.type: FUNC 20 * @tc.require: issueI7CTF7 21 */ 22let uri="%c2%aa%66%55%58%c2%83%c2%93%00%c2%89%c3%96%08%58%c2%b4%c3%bd%46"; 23let uri_encode=decodeURIComponent(uri); 24assert_equal(encodeURIComponent(uri_encode), "%C2%AAfUX%C2%83%C2%93%00%C2%89%C3%96%08X%C2%B4%C3%BDF"); 25uri_encode=decodeURI(uri); 26assert_equal(encodeURI(uri_encode), "%C2%AAfUX%C2%83%C2%93%00%C2%89%C3%96%08X%C2%B4%C3%BDF"); 27 28let uri1="%00"; 29let uri_encode1=decodeURIComponent(uri1); 30assert_equal(encodeURIComponent(uri_encode1), "%00"); 31uri_encode1=decodeURI(uri1); 32assert_equal(encodeURI(uri_encode1), "%00"); 33 34let errorStr = ""; 35try { 36 let invalidURI = "%c0%80"; 37 decodeURIComponent(invalidURI); 38} catch(err) { 39 errorStr = err.toString(); 40} 41assert_equal(errorStr, "URIError: DecodeURI: invalid character: %c0%80"); 42 43try { 44 let invalidURI1 = "http://example.com/?q=%"; 45 decodeURI(invalidURI1); 46} catch(err) { 47 errorStr = err.toString(); 48} 49assert_equal(errorStr, "URIError: DecodeURI: invalid character: http://example.com/?q=%"); 50 51try { 52 let invalidURI2 = "%E4%25BD%2593%25E8%2582%25B2"; 53 decodeURIComponent(invalidURI2); 54} catch(err) { 55 errorStr = err.toString(); 56} 57assert_equal(errorStr, "URIError: DecodeURI: invalid character: %E4%25BD%2593%25E8%2582%25B2"); 58 59let header = "https://ifs.tanx.com/display_ifs?i~"; 60try { 61 let invalidURI = header + "%F5%8F%BF%BF"; 62 decodeURIComponent(invalidURI); 63} catch(err) { 64 errorStr = err.toString(); 65} 66assert_equal(errorStr, "URIError: DecodeURI: invalid character: https://ifs.tanx.com/display_ifs?i~%F5%8F%BF%BF"); 67 68try { 69 let invalidURI = header + "%F4%90%BF%BF"; 70 decodeURIComponent(invalidURI); 71} catch(err) { 72 errorStr = err.toString(); 73} 74assert_equal(errorStr, "URIError: DecodeURI: invalid character: https://ifs.tanx.com/display_ifs?i~%F4%90%BF%BF"); 75 76{ 77 let validURI = header + "%F4%8F%BF%BF"; 78 decodeURIComponent(validURI); 79} 80 81var uri0="https://www.runoob.com/my t" + "est.php?name=ståle&car=saab"; 82var uri11 = uri0 + "/jfdlskafasfd"; 83var uri2 = uri0 + "/2389018203"; 84var uri3 = uri11 + "/jd2931dsafdsa"; 85var uri4 = uri3 + "/jd2931wjeiojfwre"; 86assert_equal(encodeURIComponent(uri0), 87 "https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab"); 88assert_equal(encodeURIComponent(uri11), 89 "https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab%2Fjfdlskafasfd"); 90assert_equal(encodeURIComponent(uri2), 91 "https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab%2F2389018203"); 92assert_equal(encodeURIComponent(uri3), 93 "https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab%2Fjfdlskafasfd%2Fjd2931dsafdsa"); 94assert_equal(encodeURIComponent(uri4), 95 "https%3A%2F%2Fwww.runoob.com%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab%2Fjfdlskafasfd%2Fjd2931dsafdsa%2Fj" + 96 "d2931wjeiojfwre"); 97 98assert_equal(encodeURI(uri0), 99 "https://www.runoob.com/my%20test.php?name=st%C3%A5le&car=saab"); 100assert_equal(encodeURI(uri11), 101 "https://www.runoob.com/my%20test.php?name=st%C3%A5le&car=saab/jfdlskafasfd"); 102assert_equal(encodeURI(uri2), 103 "https://www.runoob.com/my%20test.php?name=st%C3%A5le&car=saab/2389018203"); 104assert_equal(encodeURI(uri3), 105 "https://www.runoob.com/my%20test.php?name=st%C3%A5le&car=saab/jfdlskafasfd/jd2931dsafdsa"); 106assert_equal(encodeURI(uri4), 107 "https://www.runoob.com/my%20test.php?name=st%C3%A5le&car=saab/jfdlskafasfd/jd2931dsafdsa/jd2931wjeiojfwre"); 108 109assert_equal(decodeURIComponent(uri0), 110 "https://www.runoob.com/my test.php?name=ståle&car=saab"); 111assert_equal(decodeURIComponent(uri11), 112 "https://www.runoob.com/my test.php?name=ståle&car=saab/jfdlskafasfd"); 113assert_equal(decodeURIComponent(uri2), 114 "https://www.runoob.com/my test.php?name=ståle&car=saab/2389018203"); 115assert_equal(decodeURIComponent(uri3), 116 "https://www.runoob.com/my test.php?name=ståle&car=saab/jfdlskafasfd/jd2931dsafdsa"); 117assert_equal(decodeURIComponent(uri4), 118 "https://www.runoob.com/my test.php?name=ståle&car=saab/jfdlskafasfd/jd2931dsafdsa/jd2931wjeiojfwre"); 119 120{ 121 var result = true; 122 var arr = [ 123 [0x00, 0x2F], 124 [0x47, 0x60], 125 ]; 126 for (var i = 0; i < arr.length; i++) { 127 for (var j = arr[i][0]; j <= arr[i][1]; j++) { 128 try { 129 decodeURIComponent("%" + String.fromCharCode(j)+ "1"); 130 decodeURIComponent("%" + "1" + String.fromCharCode(j)); 131 decodeURIComponent("%C0%" + String.fromCharCode(j, j)); 132 decodeURIComponent("%E0%" + String.fromCharCode(j, j) + "%A0"); 133 decodeURIComponent("%E0" + "%A0%" + String.fromCharCode(j, j)); 134 decodeURIComponent("%F0%" + String.fromCharCode(j, j) + "%A0%A0"); 135 decodeURIComponent("%F0" + "%A0%" + String.fromCharCode(j, j) + "%A0"); 136 decodeURIComponent("%F0" + "%A0%A0%" + String.fromCharCode(j, j)); 137 result = false; 138 } catch (e) { 139 if ((e instanceof URIError) !== true) { 140 result = false; 141 } 142 } 143 } 144 } 145 assert_true(result); 146} 147 148let invalid_uri_list = ["%ED%A0%80", "%ED%AF%BF", "%ED%B0%80", "%ED%BF%BF", "%E0%9F%BF"] 149for (let i = 0; i < invalid_uri_list.length; ++i) { 150 try { 151 decodeURIComponent(invalid_uri_list[i]); 152 } catch (err) { 153 errorStr = err.toString(); 154 } 155 assert_equal(errorStr, "URIError: DecodeURI: invalid character: " + invalid_uri_list[i]); 156} 157 158let URIStr0 = "你好HelloمرحباأهلاओइПриветनमस्तेΓεια σαςこんにちは안녕하세요សួស្តី" 159assert_equal(encodeURI(URIStr0), 160 "%E4%BD%A0%E5%A5%BDHello%D9%85%D8%B1%D8%AD%D8%A8%D8%A7%D8%A3%D9%87%D9%84%D8%A7%E0%A4%93%E0%A4%87%D0%9F%D1%80%D0%B" + 161 "8%D0%B2%D0%B5%D1%82%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87%CE%93%CE%B5%CE%B9%CE%B1%20%CF%83%CE%B1" + 162 "%CF%82%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%EC%95%88%EB%85%95%ED%95%98%EC%84%B8%EC%9A%94%E1%9E%9F%E1%9E%" + 163 "BD%E1%9E%9F%E1%9F%92%E1%9E%8F%E1%9E%B8"); 164 165test_end();