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 #define LOG_TAG "URIUtilsTest" 16 17 #include <gtest/gtest.h> 18 #include <unistd.h> 19 20 #include "datashare_uri_utils.h" 21 #include "log_print.h" 22 23 namespace OHOS { 24 namespace DataShare { 25 using namespace testing::ext; 26 using namespace OHOS::DataShare; 27 class URIUtilsTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void){}; TearDownTestCase(void)30 static void TearDownTestCase(void){}; SetUp()31 void SetUp(){}; TearDown()32 void TearDown(){}; 33 }; 34 35 HWTEST_F(URIUtilsTest, GetUserFromUri_001, TestSize.Level0) 36 { 37 ZLOGI("GetUserFromUri_001 starts"); 38 std::string uri = "datashare:///com.acts.datasharetest"; 39 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 40 EXPECT_EQ(res, true); 41 EXPECT_EQ(user, -1); 42 ZLOGI("GetUserFromUri_001 ends"); 43 } 44 45 HWTEST_F(URIUtilsTest, GetUserFromUri_002, TestSize.Level0) 46 { 47 ZLOGI("GetUserFromUri_002 starts"); 48 std::string uri = "datashare:///com.acts.datasharetest?user=100"; 49 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 50 EXPECT_EQ(res, true); 51 EXPECT_EQ(user, 100); 52 ZLOGI("GetUserFromUri_002 ends"); 53 } 54 55 HWTEST_F(URIUtilsTest, GetUserFromUri_003, TestSize.Level0) 56 { 57 ZLOGI("GetUserFromUri_003 starts"); 58 std::string uri = "datashare:///com.acts.datasharetest?user=f"; 59 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 60 EXPECT_EQ(res, false); 61 EXPECT_EQ(user, -1); 62 ZLOGI("GetUserFromUri_003 ends"); 63 } 64 65 HWTEST_F(URIUtilsTest, GetUserFromUri_004, TestSize.Level0) 66 { 67 ZLOGI("GetUserFromUri_004 starts"); 68 std::string uri = "datashare:///com.acts.datasharetest?user=-1"; 69 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 70 EXPECT_EQ(res, false); 71 EXPECT_EQ(user, -1); 72 ZLOGI("GetUserFromUri_004 ends"); 73 } 74 75 HWTEST_F(URIUtilsTest, GetUserFromUri_005, TestSize.Level0) 76 { 77 ZLOGI("GetUserFromUri_005 starts"); 78 std::string uri = "datashare:///com.acts.datasharetest?user="; 79 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 80 EXPECT_EQ(res, true); 81 EXPECT_EQ(user, -1); 82 ZLOGI("GetUserFromUri_005 ends"); 83 } 84 85 HWTEST_F(URIUtilsTest, GetUserFromUri_006, TestSize.Level0) 86 { 87 ZLOGI("GetUserFromUri_006 starts"); 88 std::string uri = "datashare:///com.acts.datasharetest?user= "; 89 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 90 EXPECT_EQ(res, false); 91 EXPECT_EQ(user, -1); 92 ZLOGI("GetUserFromUri_006 ends"); 93 } 94 95 HWTEST_F(URIUtilsTest, GetUserFromUri_007, TestSize.Level0) 96 { 97 ZLOGI("GetUserFromUri_007 starts"); 98 std::string uri = "datashare:///com.acts.datasharetest?user=2147483648"; 99 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 100 EXPECT_EQ(res, false); 101 EXPECT_EQ(user, -1); 102 ZLOGI("GetUserFromUri_007 ends"); 103 } 104 105 HWTEST_F(URIUtilsTest, GetUserFromUri_008, TestSize.Level0) 106 { 107 ZLOGI("GetUserFromUri_008 starts"); 108 std::string uri = "datashare:///com.acts.datasharetest?user=100&user=111"; 109 auto [res, user] = DataShareURIUtils::GetUserFromUri(uri); 110 EXPECT_EQ(res, true); 111 EXPECT_EQ(user, 111); 112 ZLOGI("GetUserFromUri_008 ends"); 113 } 114 115 HWTEST_F(URIUtilsTest, GetQueryParams_001, TestSize.Level0) 116 { 117 ZLOGI("GetQueryParams_001 starts"); 118 std::string uri = "datashare:///com.acts.datasharetest?user=100&srcToken=12345"; 119 auto res = DataShareURIUtils::GetQueryParams(uri); 120 EXPECT_EQ(res.empty(), false); 121 ZLOGI("GetQueryParams_001 ends"); 122 } 123 124 HWTEST_F(URIUtilsTest, Strtoul_001, TestSize.Level0) 125 { 126 ZLOGI("Strtoul_001 starts"); 127 std::string str = ""; 128 std::pair<bool, uint32_t> res = DataShareURIUtils::Strtoul(str); 129 EXPECT_EQ(res.first, false); 130 EXPECT_EQ(res.second, 0); 131 ZLOGI("Strtoul_001 ends"); 132 } 133 134 HWTEST_F(URIUtilsTest, FormatUri_001, TestSize.Level0) 135 { 136 ZLOGI("FormatUri_001 starts"); 137 std::string uri = "datashare:///com.acts.datasharetest?user=100&srcToken=12345"; 138 std::string res = DataShareURIUtils::FormatUri(uri); 139 EXPECT_EQ(res, "datashare:///com.acts.datasharetest"); 140 ZLOGI("FormatUri_001 ends"); 141 } 142 143 HWTEST_F(URIUtilsTest, FormatUri_002, TestSize.Level0) 144 { 145 ZLOGI("FormatUri_002 starts"); 146 std::string uri = "datashare:///com.acts.datasharetest"; 147 std::string res = DataShareURIUtils::FormatUri(uri); 148 EXPECT_EQ(res, "datashare:///com.acts.datasharetest"); 149 ZLOGI("FormatUri_002 ends"); 150 } 151 } // namespace DataShare 152 }