diff --git a/js_api_module/uri/native_module_uri.cpp b/js_api_module/uri/native_module_uri.cpp index 94ef0c850049824fe17bcb8983e341c6ff1fc7c4..eae55b82bb648cc90eae28029f84a84a59e78530 100644 --- a/js_api_module/uri/native_module_uri.cpp +++ b/js_api_module/uri/native_module_uri.cpp @@ -35,7 +35,6 @@ namespace OHOS::Uri { std::string type = ""; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type.reserve(typelen + 1); type.resize(typelen); NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type.data(), typelen + 1, &typelen)); object = new (std::nothrow) Uri(type); @@ -172,13 +171,11 @@ namespace OHOS::Uri { std::string key = ""; size_t keyLen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &keyLen)); - key.reserve(keyLen + 1); key.resize(keyLen); NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], key.data(), keyLen + 1, &keyLen)); std::string value = ""; size_t valueLen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &valueLen)); - value.reserve(valueLen + 1); value.resize(valueLen); NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], value.data(), valueLen + 1, &valueLen)); std::string temp = muri->AddQueryValue(key, value); @@ -203,7 +200,6 @@ namespace OHOS::Uri { std::string segment = ""; size_t segmentLen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &segmentLen)); - segment.reserve(segmentLen + 1); segment.resize(segmentLen); NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], segment.data(), segmentLen + 1, &segmentLen)); std::string temp = muri->AddSegment(segment); @@ -233,7 +229,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get scheme size"); return nullptr; } - scheme.reserve(schemeLen + 1); + scheme.reserve(schemeLen); scheme.resize(schemeLen); if (napi_get_value_string_utf8(env, argv[0], scheme.data(), schemeLen + 1, &schemeLen) != napi_ok) { HILOG_ERROR("URI:: can not get scheme value"); @@ -266,7 +262,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get userInfo size"); return nullptr; } - userInfo.reserve(userInfoLen + 1); + userInfo.reserve(userInfoLen); userInfo.resize(userInfoLen); if (napi_get_value_string_utf8(env, argv[0], userInfo.data(), userInfoLen + 1, &userInfoLen) != napi_ok) { HILOG_ERROR("URI:: can not get userInfo value"); @@ -299,7 +295,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get pathStr size"); return nullptr; } - pathStr.reserve(pathLen + 1); + pathStr.reserve(pathLen); pathStr.resize(pathLen); if (napi_get_value_string_utf8(env, argv[0], pathStr.data(), pathLen + 1, &pathLen) != napi_ok) { HILOG_ERROR("URI:: can not get pathStr value"); @@ -332,7 +328,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get fragmentStr size"); return nullptr; } - fragmentStr.reserve(fragmentLen + 1); + fragmentStr.reserve(fragmentLen); fragmentStr.resize(fragmentLen); if (napi_get_value_string_utf8(env, argv[0], fragmentStr.data(), fragmentLen + 1, &fragmentLen) != napi_ok) { HILOG_ERROR("URI:: can not get fragmentStr value"); @@ -365,7 +361,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get queryStr size"); return nullptr; } - queryStr.reserve(queryLen + 1); + queryStr.reserve(queryLen); queryStr.resize(queryLen); if (napi_get_value_string_utf8(env, argv[0], queryStr.data(), queryLen + 1, &queryLen) != napi_ok) { HILOG_ERROR("URI:: can not get queryStr value"); @@ -398,7 +394,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get authorityStr size"); return nullptr; } - authorityStr.reserve(authorityStrLen + 1); + authorityStr.reserve(authorityStrLen); authorityStr.resize(authorityStrLen); if (napi_get_value_string_utf8(env, argv[0], authorityStr.data(), authorityStrLen + 1, &authorityStrLen) != napi_ok) { @@ -432,7 +428,7 @@ namespace OHOS::Uri { HILOG_ERROR("URI:: can not get authorityStr size"); return nullptr; } - sspStr.reserve(sspStrLen + 1); + sspStr.reserve(sspStrLen); sspStr.resize(sspStrLen); if (napi_get_value_string_utf8(env, argv[0], sspStr.data(), sspStrLen + 1, &sspStrLen) != napi_ok) { HILOG_ERROR("URI:: can not get authorityStr value"); diff --git a/js_api_module/uri/test/test_napi.cpp b/js_api_module/uri/test/test_napi.cpp index 3eb99c8e620a7d024fc5b87ca709328114815f8a..4ef6465710618ea6bed55ee5ebaffaf968434bcb 100644 --- a/js_api_module/uri/test/test_napi.cpp +++ b/js_api_module/uri/test/test_napi.cpp @@ -1201,91 +1201,4 @@ HWTEST_F(NativeEngineTest, ModuleTest010, testing::ext::TestSize.Level0) napi_get_named_property(env, instance, "ssp", &uriProperty); res = GetStringUtf8(env, uriProperty); ASSERT_STREQ(res.c_str(), "usc@www.test321.cn:996?key=1"); -} - -HWTEST_F(NativeEngineTest, ModuleTest011, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value exports = nullptr; - napi_create_object(env, &exports); - OHOS::Uri::UriInit(env, exports); - napi_value uriClass = nullptr; - napi_value constructorArgs[1] = { 0 }; - std::string input = "http://username:password@www.uritest.com:99/path/abc?query#fagment"; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]); - napi_status status = napi_get_named_property(env, exports, "Uri", &uriClass); - napi_value instance = nullptr; - status = napi_new_instance(env, uriClass, 1, constructorArgs, &instance); - napi_value tempFn = nullptr; - napi_value result = nullptr; - napi_value key = StrToNapiValue(env, "qqwwee"); - napi_value value = StrToNapiValue(env, "erfa"); - napi_value keyArgs[] = { key, value }; - napi_get_named_property(env, instance, "addQueryValue", &tempFn); - napi_call_function(env, instance, tempFn, 2, keyArgs, &result); - std::string res = GetStringUtf8(env, result); - ASSERT_STREQ(res.c_str(), "http://username:password@www.uritest.com:99/path/abc?query&qqwwee=erfa#fagment"); - napi_value segment = StrToNapiValue(env, "wqaaas"); - napi_value segargs[] = { segment }; - napi_get_named_property(env, instance, "addSegment", &tempFn); - napi_call_function(env, instance, tempFn, 1, segargs, &result); - res = GetStringUtf8(env, result); - ASSERT_STREQ(res.c_str(), "http://username:password@www.uritest.com:99/path/abc/wqaaas?query#fagment"); - value = StrToNapiValue(env, "https:"); - napi_set_named_property(env, instance, "scheme", value); - napi_value uriProperty = nullptr; - napi_get_named_property(env, instance, "scheme", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "https"); - value = StrToNapiValue(env, "usernameinfo"); - napi_set_named_property(env, instance, "userInfo", value); - uriProperty = nullptr; - napi_get_named_property(env, instance, "userInfo", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "usernameinfo"); -} - -HWTEST_F(NativeEngineTest, ModuleTest012, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value exports = nullptr; - napi_create_object(env, &exports); - OHOS::Uri::UriInit(env, exports); - napi_value uriClass = nullptr; - napi_value constructorArgs[1] = { 0 }; - std::string input = "http://username:password@www.uritest.com:99/path/abc?query#fagment"; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]); - napi_status status = napi_get_named_property(env, exports, "Uri", &uriClass); - napi_value instance = nullptr; - status = napi_new_instance(env, uriClass, 1, constructorArgs, &instance); - napi_value value = StrToNapiValue(env, "/path1/path2"); - napi_set_named_property(env, instance, "path", value); - napi_value uriProperty = nullptr; - napi_get_named_property(env, instance, "path", &uriProperty); - std::string res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "/path1/path2"); - value = StrToNapiValue(env, "qwfagm"); - napi_set_named_property(env, instance, "fragment", value); - uriProperty = nullptr; - napi_get_named_property(env, instance, "fragment", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "qwfagm"); - value = StrToNapiValue(env, "aaa=bbb"); - napi_set_named_property(env, instance, "query", value); - uriProperty = nullptr; - napi_get_named_property(env, instance, "query", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "aaa=bbb"); - value = StrToNapiValue(env, "user@www.test112.cn:189"); - napi_set_named_property(env, instance, "authority", value); - uriProperty = nullptr; - napi_get_named_property(env, instance, "authority", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "user@www.test112.cn:189"); - value = StrToNapiValue(env, "//uscffac@www.testut.cn:223?key=val"); - napi_set_named_property(env, instance, "ssp", value); - uriProperty = nullptr; - napi_get_named_property(env, instance, "ssp", &uriProperty); - res = GetStringUtf8(env, uriProperty); - ASSERT_STREQ(res.c_str(), "//uscffac@www.testut.cn:223?key=val"); } \ No newline at end of file diff --git a/js_api_module/url/js_url.cpp b/js_api_module/url/js_url.cpp index 85d127c2ec746890b800ccf19b2c182d43c876c5..215ee3d5206fa13443da9314ee31c1aaeacda4b7 100644 --- a/js_api_module/url/js_url.cpp +++ b/js_api_module/url/js_url.cpp @@ -1805,7 +1805,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get buffer size"); return nullptr; } - name.reserve(nameSize + 1); + name.reserve(nameSize); name.resize(nameSize); if (napi_get_value_string_utf8(env, buffer, name.data(), nameSize + 1, &nameSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get buffer value"); @@ -1836,7 +1836,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get buffer size"); return nullptr; } - name.reserve(nameSize + 1); + name.reserve(nameSize); name.resize(nameSize); if (napi_get_value_string_utf8(env, buffer, name.data(), nameSize + 1, &nameSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get buffer value"); @@ -1871,7 +1871,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get buffer size"); return; } - name.reserve(nameSize + 1); + name.reserve(nameSize); name.resize(nameSize); if (napi_get_value_string_utf8(env, buffer, name.data(), nameSize + 1, &nameSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get buffer value"); @@ -1884,7 +1884,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get temp size"); return; } - value.reserve(valueSize + 1); + value.reserve(valueSize); value.resize(valueSize); if (napi_get_value_string_utf8(env, temp, value.data(), valueSize + 1, &valueSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get temp value"); @@ -1902,7 +1902,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get buffer size"); return; } - name.reserve(nameSize + 1); + name.reserve(nameSize); name.resize(nameSize); if (napi_get_value_string_utf8(env, buffer, name.data(), nameSize + 1, &nameSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get buffer value"); @@ -1950,7 +1950,6 @@ namespace OHOS::Url { return nullptr; } std::string buf = ""; - buf.reserve(bufferSize + 1); buf.resize(bufferSize); if (napi_get_value_string_utf8(env, name, buf.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get name value"); @@ -1977,7 +1976,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get name size"); return; } - buffer.reserve(bufferSize + 1); + buffer.reserve(bufferSize); buffer.resize(bufferSize); if (napi_get_value_string_utf8(env, name, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get name value"); @@ -1990,7 +1989,7 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get value size"); return; } - temp.reserve(tempSize + 1); + temp.reserve(tempSize); temp.resize(tempSize); if (napi_get_value_string_utf8(env, value, temp.data(), tempSize + 1, &tempSize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get value value"); diff --git a/js_api_module/url/native_module_url.cpp b/js_api_module/url/native_module_url.cpp index ce29dec3a233b335f0c92f082e6de5b3fefc7c41..5455e277dcd7f007296b447283911f24e74ed53d 100644 --- a/js_api_module/url/native_module_url.cpp +++ b/js_api_module/url/native_module_url.cpp @@ -42,7 +42,7 @@ namespace OHOS::Url { HILOG_ERROR("UrlStructor:: can not get argv[0] size"); return; } - temp.reserve(tempSize + 1); + temp.reserve(tempSize); temp.resize(tempSize); if (napi_get_value_string_utf8(env, argv[0], temp.data(), tempSize + 1, &tempSize) != napi_ok) { HILOG_ERROR("UrlStructor:: can not get argv[0] value"); @@ -55,7 +55,7 @@ namespace OHOS::Url { HILOG_ERROR("UrlStructor:: can not get argv[1] size"); return; } - tempType.reserve(tempTypeSize + 1); + tempType.reserve(tempTypeSize); tempType.resize(tempTypeSize); if (napi_get_value_string_utf8(env, argv[1], tempType.data(), tempTypeSize + 1, &tempTypeSize) != napi_ok) { @@ -108,7 +108,7 @@ namespace OHOS::Url { HILOG_ERROR("UrlStructor:: can not get argv[0] size"); return nullptr; } - type.reserve(typeSize + 1); + type.reserve(typeSize); type.resize(typeSize); if (napi_get_value_string_utf8(env, argv[0], type.data(), typeSize + 1, &typeSize) != napi_ok) { HILOG_ERROR("UrlStructor:: can not get argv[0] value"); @@ -289,7 +289,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -315,7 +314,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -341,7 +339,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -367,7 +364,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -393,7 +389,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -422,7 +417,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -454,7 +448,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -480,7 +473,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -506,7 +498,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -532,7 +523,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -558,7 +548,6 @@ namespace OHOS::Url { HILOG_ERROR("URL:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URL:: can not get argv[0] value"); @@ -617,7 +606,6 @@ namespace OHOS::Url { } if (arraySize > 0) { std::string cstr = ""; - cstr.reserve(arraySize + 1); cstr.resize(arraySize); if (napi_get_value_string_utf8(env, napiStr, cstr.data(), arraySize + 1, &arraySize) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get name value"); @@ -822,7 +810,6 @@ namespace OHOS::Url { HILOG_ERROR("URLSearchParams:: can not get argv[0] size"); return nullptr; } - input.reserve(typelen + 1); input.resize(typelen); if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { HILOG_ERROR("URLSearchParams:: can not get argv[0] value"); diff --git a/js_api_module/url/test/test_napi.cpp b/js_api_module/url/test/test_napi.cpp index d2f4db8707319085ff958dd49bfea6132ec26121..d1d858b490d0764c4fd891f0e23f9c4fcd8dcc21 100644 --- a/js_api_module/url/test/test_napi.cpp +++ b/js_api_module/url/test/test_napi.cpp @@ -3439,223 +3439,4 @@ HWTEST_F(NativeEngineTest, testUrlParamsComprehensive001, testing::ext::TestSize DealNapiStrValue(env, params.IterByKeys(env), output); ASSERT_STREQ(output.c_str(), ""); -} - -HWTEST_F(NativeEngineTest, testUrlModule009, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value exports = nullptr; - napi_create_object(env, &exports); - OHOS::Url::Init(env, exports); - napi_value urlClass = nullptr; - napi_value constructorArgs[1] = { 0 }; - std::string input = "http://username:password@www.testut.com:99/path/path?query#fagment"; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]); - napi_get_named_property(env, exports, "Url", &urlClass); - napi_value instance = nullptr; - napi_new_instance(env, urlClass, 1, constructorArgs, &instance); - - std::string input1 = "http11:"; - napi_value newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "protocol", newValue); - napi_value urlProperty = nullptr; - napi_get_named_property(env, instance, "protocol", &urlProperty); - std::string res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "http:"); - - input1 = "/pathda/fanah"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "pathname", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "pathname", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "/pathda/fanah"); - - input1 = "168"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "port", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "port", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "168"); - - napi_value constructorArgs1[1] = { 0 }; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs1[0]); - napi_value hrefFn = nullptr; - napi_get_named_property(env, instance, "href", &hrefFn); - napi_value result1 = nullptr; - napi_call_function(env, instance, hrefFn, 1, constructorArgs1, &result1); - - urlProperty = nullptr; - napi_get_named_property(env, instance, "port", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "99"); -} - -HWTEST_F(NativeEngineTest, testUrlModule010, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value exports = nullptr; - napi_create_object(env, &exports); - OHOS::Url::Init(env, exports); - napi_value urlClass = nullptr; - napi_value constructorArgs[1] = { 0 }; - std::string input = "http://username:password@www.test123.com:99/path/path?query#fagment"; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]); - napi_get_named_property(env, exports, "Url", &urlClass); - napi_value instance = nullptr; - napi_new_instance(env, urlClass, 1, constructorArgs, &instance); - - std::string input1 = "www.example123.com"; - napi_value newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "hostname", newValue); - napi_value urlProperty = nullptr; - napi_get_named_property(env, instance, "hostname", &urlProperty); - std::string res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "www.example123.com"); - - input1 = "query1=banf"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "search", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "search", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "?query1=banf"); - - input1 = "asfee1s"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "username", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "username", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "asfee1s"); -} - -HWTEST_F(NativeEngineTest, testUrlModule011, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value exports = nullptr; - napi_create_object(env, &exports); - OHOS::Url::Init(env, exports); - napi_value urlClass = nullptr; - napi_value constructorArgs[1] = { 0 }; - std::string input = "http://username:password@www.test776.com:155/path/path?query#fagment"; - napi_create_string_utf8(env, input.c_str(), input.size(), &constructorArgs[0]); - napi_get_named_property(env, exports, "Url", &urlClass); - napi_value instance = nullptr; - napi_new_instance(env, urlClass, 1, constructorArgs, &instance); - - std::string input1 = "pass123"; - napi_value newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "password", newValue); - napi_value urlProperty = nullptr; - napi_get_named_property(env, instance, "password", &urlProperty); - std::string res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "pass123"); - - input1 = "www.test.com:117"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "host", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "host", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "www.test.com:117"); - - input1 = "fagsafd"; - newValue = nullptr; - napi_create_string_utf8(env, input1.c_str(), input1.size(), &newValue); - napi_set_named_property(env, instance, "hash", newValue); - urlProperty = nullptr; - napi_get_named_property(env, instance, "hash", &urlProperty); - res = GetStringUtf8(env, urlProperty); - ASSERT_STREQ(res.c_str(), "#fagsafd"); -} - -HWTEST_F(NativeEngineTest, testUrlFullUpdate002, testing::ext::TestSize.Level0) -{ - OHOS::Url::URL url("http://test.com/newpath?q=1#old"); - std::string output; - napi_env env = (napi_env)engine_; - - url.SetHostname("newhost11.org"); - DealNapiStrValue(env, url.GetHostname(env), output); - ASSERT_STREQ(output.c_str(), "newhost11.org"); - - url.SetPath("/new/path/resourceqq"); - DealNapiStrValue(env, url.GetPath(env), output); - ASSERT_STREQ(output.c_str(), "/new/path/resourceqq"); - - url.SetSearch("kk=val1"); - DealNapiStrValue(env, url.GetSearch(env), output); - ASSERT_STREQ(output.c_str(), "?kk=val1"); - - url.SetFragment("new-Fragment"); - DealNapiStrValue(env, url.GetFragment(env), output); - ASSERT_STREQ(output.c_str(), "#new-Fragment"); -} - -HWTEST_F(NativeEngineTest, testUrlParamsAppend002, testing::ext::TestSize.Level0) -{ - OHOS::Url::URLSearchParams params = OHOS::Url::URLSearchParams(); - napi_env env = (napi_env)engine_; - napi_value key = StrToNapiValue(env, "kk1"); - napi_value val = StrToNapiValue(env, "valrr"); - params.Append(env, key, val); - - napi_value query = StrToNapiValue(env, "kk1"); - std::string output; - DealNapiStrValue(env, params.Get(env, query), output); - ASSERT_STREQ(output.c_str(), "valrr"); -} - -HWTEST_F(NativeEngineTest, testUrlParamsDelete004, testing::ext::TestSize.Level0) -{ - OHOS::Url::URLSearchParams params = OHOS::Url::URLSearchParams(); - napi_env env = (napi_env)engine_; - napi_value key = StrToNapiValue(env, "token112"); - napi_value val = StrToNapiValue(env, "abc123"); - params.Append(env, key, val); - bool value = false; - napi_get_value_bool(env, params.IsHas(env, key), &value); - ASSERT_TRUE(value); - params.Delete(env, key); - napi_get_value_bool(env, params.IsHas(env, key), &value); - ASSERT_FALSE(value); -} - -HWTEST_F(NativeEngineTest, testUrlParamsGetAll003, testing::ext::TestSize.Level0) -{ - OHOS::Url::URLSearchParams params = OHOS::Url::URLSearchParams(); - napi_env env = (napi_env)engine_; - napi_value key = StrToNapiValue(env, "strnumber"); - - params.Append(env, key, StrToNapiValue(env, "one")); - params.Append(env, key, StrToNapiValue(env, "two")); - params.Append(env, key, StrToNapiValue(env, "three")); - - std::string output; - DealNapiStrValue(env, params.Get(env, key), output); - ASSERT_STREQ(output.c_str(), "one"); -} - -HWTEST_F(NativeEngineTest, testUrlParamsSet003, testing::ext::TestSize.Level0) -{ - OHOS::Url::URLSearchParams params = OHOS::Url::URLSearchParams(); - napi_env env = (napi_env)engine_; - napi_value key = StrToNapiValue(env, "number"); - - params.Append(env, key, StrToNapiValue(env, "100")); - params.Set(env, key, StrToNapiValue(env, "2000")); - - std::string output; - DealNapiStrValue(env, params.Get(env, key), output); - ASSERT_STREQ(output.c_str(), "2000"); } \ No newline at end of file diff --git a/js_sys_module/dfx/native_module_dfx.cpp b/js_sys_module/dfx/native_module_dfx.cpp index a9d3ada5cee5888a352d88b3fe1eba057cc38ca2..1e7ad666d113c7ed4c2861a86107d8d4537bf9c4 100644 --- a/js_sys_module/dfx/native_module_dfx.cpp +++ b/js_sys_module/dfx/native_module_dfx.cpp @@ -101,7 +101,7 @@ namespace OHOS::JsSysModule::Dfx { HILOG_ERROR("can not get argv size"); return nullptr; } - tempStr.reserve(tempStrsize + 1); + tempStr.reserve(tempStrsize); tempStr.resize(tempStrsize); if (napi_get_value_string_utf8(env, argv, tempStr.data(), tempStrsize + 1, &tempStrsize) != napi_ok) { HILOG_ERROR("can not get argv value"); diff --git a/js_util_module/util/js_uuid.cpp b/js_util_module/util/js_uuid.cpp index 41441b7c863c4d36ae5e108e009679bd9e170219..8d9c58e3056c15faf526dc957e6d930d16fe895b 100644 --- a/js_util_module/util/js_uuid.cpp +++ b/js_util_module/util/js_uuid.cpp @@ -208,7 +208,6 @@ napi_value DoParseUUID(napi_env env, napi_value src) HILOG_ERROR("DoParseUUID:: can not get src size"); return nullptr; } - buffer.reserve(bufferSize + 1); buffer.resize(bufferSize); status = napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize); if (status != napi_ok) { diff --git a/js_util_module/util/native_module_util.cpp b/js_util_module/util/native_module_util.cpp index e3db10b3d2eff124cb3b03a3a7927e4e0445e54a..4319d78ace65a44ac817e717bc345cda81d74a74 100644 --- a/js_util_module/util/native_module_util.cpp +++ b/js_util_module/util/native_module_util.cpp @@ -224,7 +224,7 @@ namespace OHOS::Util { FreeMemory(argv); return nullptr; } - format.reserve(formatsize + 1); + format.reserve(formatsize); format.resize(formatsize); if (napi_get_value_string_utf8(env, argv[0], format.data(), formatsize + 1, &formatsize) != napi_ok) { HILOG_ERROR("Printf:: can not get argv[0] value"); @@ -240,7 +240,7 @@ namespace OHOS::Util { FreeMemory(argv); return nullptr; } - valueString.reserve(valuesize + 1); + valueString.reserve(valuesize); valueString.resize(valuesize); if (napi_get_value_string_utf8(env, argv[i], valueString.data(), valuesize + 1, &valuesize) != napi_ok) { @@ -578,9 +578,7 @@ namespace OHOS::Util { HILOG_ERROR("TextEncoder:: can not get src size"); return nullptr; } - std::string buffer = ""; - buffer.reserve(bufferSize + 1); - buffer.resize(bufferSize, '\0'); + std::string buffer(bufferSize, '\0'); if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("TextEncoder:: can not get src value"); return nullptr; @@ -1602,7 +1600,7 @@ namespace OHOS::Util { return nullptr; } std::string buffer = ""; - buffer.reserve(bufferSize + 1); + buffer.reserve(bufferSize); buffer.resize(bufferSize); if (napi_get_value_string_utf8(env, argv, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("StringDecoder:: can not get argv value"); diff --git a/js_util_module/util/test/test_uuid.cpp b/js_util_module/util/test/test_uuid.cpp index 26b13f703d820ff02f3e32a4155a553cbb10bdde..8042e60b41ef790a8f05f6c4f5e7aa938479d1d3 100644 --- a/js_util_module/util/test/test_uuid.cpp +++ b/js_util_module/util/test/test_uuid.cpp @@ -1560,23 +1560,4 @@ HWTEST_F(NativeEngineTest, DoParseUUIDTest085, testing::ext::TestSize.Level0) napi_value resultBuffer = nullptr; napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); ASSERT_EQ(length, 16); -} - -/* @tc.name: DoParseUUIDTest086 - * @tc.desc: Parse a UUID from the string standard representation as described in the RFC 4122 version 4. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, DoParseUUIDTest086, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - napi_value src = nullptr; - napi_create_string_utf8(env, "84bdf796-65cc-4755-9b89-d6218d101186", NAPI_AUTO_LENGTH, &src); - napi_value arr = OHOS::Util::DoParseUUID(env, src); - napi_typedarray_type type = napi_int8_array; - size_t byteOffset = 0; - size_t length = 0; - void* resultData = nullptr; - napi_value resultBuffer = nullptr; - napi_get_typedarray_info(env, arr, &type, &length, &resultData, &resultBuffer, &byteOffset); - ASSERT_EQ(length, 16); } \ No newline at end of file diff --git a/platform/default/util_helper.cpp b/platform/default/util_helper.cpp index bc3c556982929c64fcabdedd9f2dba46881bfd76..7e5782eea8803bfc6de46f6dd41fd04a24748afc 100644 --- a/platform/default/util_helper.cpp +++ b/platform/default/util_helper.cpp @@ -82,7 +82,6 @@ void EncodeIntoChinese(napi_env env, napi_value src, std::string encoding, std:: std::string input = ""; size_t inputSize = 0; napi_get_value_string_utf8(env, src, nullptr, 0, &inputSize); // 0:buffer size - input.reserve(inputSize + 1); input.resize(inputSize); napi_get_value_string_utf8(env, src, input.data(), inputSize + 1, &inputSize); buffer = UtilPlugin::EncodeIntoChinese(input, encoding); diff --git a/platform/ohos/util_helper.cpp b/platform/ohos/util_helper.cpp index 5c44a0e7fdaa7fc04e5347a8725dcc2d3aaea07b..4e5eb7dda6088671a1f0a34356c99ebe946c934b 100644 --- a/platform/ohos/util_helper.cpp +++ b/platform/ohos/util_helper.cpp @@ -318,7 +318,6 @@ namespace Commonlibrary::Platform { HILOG_ERROR("TextEncoder:: can not get src size"); return u""; } - buffer.reserve(bufferSize + 1); buffer.resize(bufferSize); if (napi_get_value_string_utf8(env, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { HILOG_ERROR("TextEncoder:: can not get src value");