1 /*
2 * Copyright (c) 2022 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 #include <string>
17 #include <cstring>
18 #include <securec.h>
19 #include "constant.h"
20 #include "accesstoken_log.h"
21 #include "softbus_bus_center.h"
22
23 using namespace OHOS::Security::AccessToken;
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusCenterMock"};
26 static const int REG_COUNT_LIMIT = 10;
27 } // namespace
28 static int regCount_ = -1;
29
IsRegCountOK()30 bool IsRegCountOK()
31 {
32 return regCount_ >= 0 && regCount_ < REG_COUNT_LIMIT;
33 }
34
GetLocalNodeDeviceInfo(const char * pkgName,NodeBasicInfo * info)35 int32_t GetLocalNodeDeviceInfo(const char *pkgName, NodeBasicInfo *info)
36 {
37 strcpy_s(info->deviceName, sizeof(info->deviceName), "local");
38 strcpy_s(info->networkId, sizeof(info->networkId), "local");
39 info->deviceTypeId = 1;
40 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, count: %{public}d", regCount_);
41 return Constant::SUCCESS;
42 }
43
GetNodeKeyInfo(const char * pkgName,const char * networkId,NodeDeviceInfoKey key,uint8_t * info,int32_t infoLen)44 int32_t GetNodeKeyInfo(const char *pkgName, const char *networkId, NodeDeviceInfoKey key, uint8_t *info,
45 int32_t infoLen)
46 {
47 if (networkId == nullptr || networkId[0] == '\0') {
48 ACCESSTOKEN_LOG_DEBUG(LABEL, "failure, invalid networkId, pkg name: %{public}s", pkgName);
49 return Constant::FAILURE;
50 }
51
52 if (key == NodeDeviceInfoKey::NODE_KEY_UDID) {
53 std::string temp = networkId;
54 temp += ":udid-001";
55 strncpy_s(reinterpret_cast<char *>(info), infoLen, temp.c_str(), temp.length());
56 infoLen = temp.length();
57 }
58 if (key == NodeDeviceInfoKey::NODE_KEY_UUID) {
59 std::string temp = networkId;
60 temp += ":uuid-001";
61 strncpy_s(reinterpret_cast<char *>(info), infoLen, temp.c_str(), temp.length());
62 }
63 ACCESSTOKEN_LOG_DEBUG(LABEL, "success, count: %{public}d, id: %{public}s", regCount_, info);
64 return Constant::SUCCESS;
65 }
66