1 /*
2 * Copyright (C) 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre/util/system/service_helpers.h"
18 #include "gtest/gtest.h"
19
20 namespace chre::message {
21 namespace {
22
TEST(ServiceHelpersTest,ExtractNanoappIdAndServiceId_NullServiceDescriptor)23 TEST(ServiceHelpersTest, ExtractNanoappIdAndServiceId_NullServiceDescriptor) {
24 uint64_t nanoappId;
25 uint64_t serviceId;
26 EXPECT_FALSE(extractNanoappIdAndServiceId(nullptr, nanoappId, serviceId));
27 }
28
TEST(ServiceHelpersTest,ExtractNanoappIdAndServiceId_InvalidPrefix)29 TEST(ServiceHelpersTest, ExtractNanoappIdAndServiceId_InvalidPrefix) {
30 uint64_t nanoappId;
31 uint64_t serviceId;
32 EXPECT_FALSE(
33 extractNanoappIdAndServiceId("invalid_prefix", nanoappId, serviceId));
34 }
35
TEST(ServiceHelpersTest,ExtractNanoappIdAndServiceId_MissingSeparator)36 TEST(ServiceHelpersTest, ExtractNanoappIdAndServiceId_MissingSeparator) {
37 uint64_t nanoappId;
38 uint64_t serviceId;
39 EXPECT_FALSE(extractNanoappIdAndServiceId("chre.nanoapp_0x1234567890ABCDEF",
40 nanoappId, serviceId));
41 }
42
TEST(ServiceHelpersTest,ExtractNanoappIdAndServiceId_InvalidEncodingLength)43 TEST(ServiceHelpersTest, ExtractNanoappIdAndServiceId_InvalidEncodingLength) {
44 uint64_t nanoappId;
45 uint64_t serviceId;
46 EXPECT_FALSE(extractNanoappIdAndServiceId(
47 "chre.nanoapp_0x1234567890ABCDEF.service_0x1234567890ABCDE", nanoappId,
48 serviceId));
49
50 EXPECT_FALSE(extractNanoappIdAndServiceId(
51 "chre.nanoapp_0x1234567890ABCDE.service_0x1234567890ABCDEF", nanoappId,
52 serviceId));
53
54 EXPECT_FALSE(extractNanoappIdAndServiceId("chre.nanoapp_0x0.service_0x1",
55 nanoappId, serviceId));
56
57 EXPECT_FALSE(extractNanoappIdAndServiceId("chre.nanoapp_0x.service_0x",
58 nanoappId, serviceId));
59
60 EXPECT_FALSE(extractNanoappIdAndServiceId(
61 "chre.nanoapp_0x1234567890ABCDEF.service_0x", nanoappId, serviceId));
62
63 EXPECT_FALSE(extractNanoappIdAndServiceId(
64 "chre.nanoapp_0x.service_0x1234567890ABCDEF", nanoappId, serviceId));
65 }
66
TEST(ServiceHelpersTest,ExtractNanoappIdAndServiceId_Success)67 TEST(ServiceHelpersTest, ExtractNanoappIdAndServiceId_Success) {
68 uint64_t nanoappId;
69 uint64_t serviceId;
70 EXPECT_TRUE(extractNanoappIdAndServiceId(
71 "chre.nanoapp_0x1234567890ABCDEF.service_0x1234567890ABCDEF", nanoappId,
72 serviceId));
73 EXPECT_EQ(nanoappId, 0x1234567890ABCDEF);
74 EXPECT_EQ(serviceId, 0x1234567890ABCDEF);
75
76 EXPECT_TRUE(extractNanoappIdAndServiceId(
77 "chre.nanoapp_0xDEADBEEFCAFECAFE.service_0xCAFECAFECAFECAFE", nanoappId,
78 serviceId));
79 EXPECT_EQ(nanoappId, 0xDEADBEEFCAFECAFE);
80 EXPECT_EQ(serviceId, 0xCAFECAFECAFECAFE);
81 }
82
83 } // namespace
84 } // namespace chre::message
85