diff --git a/BUILD.gn b/BUILD.gn index 5d8d98054f3787ec53faa10b701467f0b058b71e..12f7bad48f79d3a1ee7e78701c28dce490dafc6b 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# Copyright (C) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -14,9 +14,7 @@ import("//build/ohos.gni") config("pubilc") { - include_dirs = [ - "include", - ] + include_dirs = [ "include" ] } sources_platform_common = [ @@ -24,7 +22,8 @@ sources_platform_common = [ "./src/endian_internal.c", ] -ohos_executable("syscap_tool") { +ohos_executable("syscap_tool_bin") { + output_name = "syscap_tool" install_enable = true include_dirs = [ "src" ] public_configs = [ ":pubilc" ] @@ -37,9 +36,32 @@ ohos_executable("syscap_tool") { sources = [ "./src/main.c" ] sources += sources_platform_common - deps = [ - "//third_party/bounds_checking_function:libsec_static", - ] + deps = [ "//third_party/bounds_checking_function:libsec_static" ] + + if (defined(ohos_lite)) { + deps += [ "//build/lite/config/component/cJSON:cjson_static" ] + } else { + deps += [ "//third_party/cJSON:cjson_static" ] + } + + subsystem_name = "developtools" + part_name = "syscap_codec" +} + +ohos_executable("syscap_tool_test") { + install_enable = true + include_dirs = [ "src" ] + public_configs = [ ":pubilc" ] + defines = [] + + if (is_mingw) { + defines += [ "_POSIX_" ] + } + + sources = [ "./test/syscap_tool_test.c" ] + sources += sources_platform_common + + deps = [ "//third_party/bounds_checking_function:libsec_static" ] if (defined(ohos_lite)) { deps += [ "//build/lite/config/component/cJSON:cjson_static" ] @@ -72,4 +94,4 @@ ohos_shared_library("syscap_tool_shared") { group("syscap_codec") { deps = [ ":syscap_tool_shared" ] -} \ No newline at end of file +} diff --git a/include/syscap_tool.h b/include/syscap_tool.h index a1b0917611d564cdd6c0506fb8a1047afdc24624..6421836a102a95a43a50cf5d1efb425f77982d52 100755 --- a/include/syscap_tool.h +++ b/include/syscap_tool.h @@ -1,31 +1,16 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef _SYSCAP_TOOL_H diff --git a/src/endian_internal.c b/src/endian_internal.c index 5af5a9df28eb2004efe43953093f3ba4134188b8..ec040c78c995da66d262ac173f4231b296a969b0 100755 --- a/src/endian_internal.c +++ b/src/endian_internal.c @@ -1,31 +1,16 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "endian_internal.h" @@ -35,6 +20,8 @@ #define B_L_SWAP32(A) ((((uint32_t)(A) & 0xff000000) >> 24) | (((uint32_t)(A) & 0x00ff0000) >> 8) | \ (((uint32_t)(A) & 0x0000ff00) << 8) | (((uint32_t)(A) & 0x000000ff) << 24)) +int32_t CheckEndian(void); + int32_t CheckEndian() { union { diff --git a/src/endian_internal.h b/src/endian_internal.h index dd9728523c5d1c90bf49bd55b1b52ed31c731426..8a2c2019aca3cdc77bf9bdcfe03f1a284270f1f0 100755 --- a/src/endian_internal.h +++ b/src/endian_internal.h @@ -1,31 +1,16 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #ifndef _ENDIAN_INTERNAL_H diff --git a/src/main.c b/src/main.c index bd902cefc2744b1a636962c283e51f85b7a08b10..de885a02ab2f45db7bae18d11bf2c855b8742911 100755 --- a/src/main.c +++ b/src/main.c @@ -1,31 +1,16 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "syscap_tool.h" @@ -112,7 +97,7 @@ int main(int argc, char **argv) } if (ret != 0) { - printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); + printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); printf("input file(%s) prase failed\n", inputfile); } diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 723c16f6492d0d56683586c254938aa991120f89..37ddb48076884d40f1f3c599f3c6a8c3648ff35a 100755 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -1,47 +1,32 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * http://www.apache.org/licenses/LICENSE-2.0 * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -#include "syscap_tool.h" -#include "endian_internal.h" -#include "cJSON.h" #include #include #include #include -#include -#include #include #include #include #include +#include +#include #include +#include "endian_internal.h" +#include "cJSON.h" +#include "syscap_tool.h" typedef struct ProductCompatibilityIDHead { uint16_t apiVersion : 15; @@ -62,7 +47,7 @@ typedef struct RequiredProductCompatibilityIDHead { do { \ printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ printf(__VA_ARGS__); \ - } while (0); + } while (0) static void FreeContextBuffer(char *contextBuffer) { @@ -100,11 +85,11 @@ static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t * if (ret != 1) { PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno); FreeContextBuffer(contextBuffer); - fclose(fp); + (void)fclose(fp); return -1; } contextBuffer[statBuf.st_size] = '\0'; - fclose(fp); + (void)fclose(fp); *contextBufPtr = contextBuffer; *bufferLen = statBuf.st_size + 1; @@ -144,11 +129,11 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char ret = fwrite(convertedBuffer, bufferLen, 1, fp); if (ret != 1) { PRINT_ERR("can`t write file(%s),errno = %d\n", fileFullPath, errno); - fclose(fp); + (void)fclose(fp); return -1; } - fclose(fp); + (void)fclose(fp); return 0; } diff --git a/test/syscap_tool_test.c b/test/syscap_tool_test.c new file mode 100755 index 0000000000000000000000000000000000000000..de617e6f20799aeb3bca1129249357db37e8184c --- /dev/null +++ b/test/syscap_tool_test.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "syscap_tool.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern char *optarg; + +char *g_testFilePcid = "{\n \"api_version\": 0,\n \"manufacturer_id\": 0,\n \"product\": \"rk3568\",\n" \ + " \"syscap\": {\n \"os\": [\n \"SystemCapability.Account.AppAccount\",\n" \ + " \"SystemCapability.Account.OsAccount\"\n ]\n },\n" \ + " \"system_type\": \"standard\"\n}"; +char *g_testFileRpcid = "{\n \"api_version\": 7,\n \"syscap\": [\n \"SystemCapability.hiviewdfx.hilog\",\n" \ + " \"SystemCapability.communication.softbus\",\n \"SystemCapability.hisicon.flashlight\"\n ]\n}"; + +int32_t PrepareTestFile(char *fileName, char *fileContext) +{ + int32_t ret; + FILE *fp = fopen(fileName, "wb"); + if (fp == NULL) { + return -1; + } + size_t len = fwrite(fileContext, 1, strlen(fileContext), fp); + if (len != strlen(fileContext)) { + (void)fclose(fp); + return -1; + } + ret = fclose(fp); + if (ret != 0) { + return -1; + } + return 0; +} + +int main(int argc, char **argv) +{ + int32_t status; + + char *pcidFileName = "SystemCapability.json"; + char *rpcidFileName = "rpcid.json"; + printf("###start syscap tool test###\n"); + + (void)unlink(pcidFileName); + (void)unlink(rpcidFileName); + (void)unlink("./rk3568.sc"); + (void)unlink("./rpcid.sc"); + (void)unlink("./rk3568.json"); + + pid_t pid = fork(); + if (pid == 0) { + printf("prepare test file...\n"); + int32_t passCnt = 0; + int32_t ret = PrepareTestFile(pcidFileName, g_testFilePcid); + if (ret != 0) { + printf(" error: prepare pcid file failed\n"); + exit(passCnt); + } + ret = PrepareTestFile(rpcidFileName, g_testFileRpcid); + if (ret != 0) { + printf(" error: prepare rpcid file failed\n"); + exit(passCnt); + } + printf("1.test rpcid.json encode to rpcid.sc\n"); + ret = RPCIDEncode(rpcidFileName, "./"); + if (ret != 0) { + printf(" error: rpcid.json encode failed\n"); + exit(passCnt); + } + passCnt++; + printf("pass\n"); + printf("2.test rpcid.sc decode to rpcid.json\n"); + ret = RPCIDDecode("rpcid.sc", "./"); + if (ret != 0) { + printf(" error: rpcid.sc decode failed\n"); + exit(passCnt); + } + passCnt++; + printf("pass\n"); + printf("3.test pcid.json encode to pcid.sc\n"); + ret = PCIDEncode(pcidFileName, "./"); + if (ret != 0) { + printf(" error: pcid.json encode failed\n"); + exit(passCnt); + } + passCnt++; + printf("pass\n"); + printf("4.test pcid.sc decode to pcid.json\n"); + ret = PCIDDecode("./rk3568.sc", "./"); + if (ret != 0) { + printf(" error: pcid.sc decode failed\n"); + exit(passCnt); + } + passCnt++; + printf("pass\n"); + exit(passCnt); + } + (void)wait(&status); + printf("summary: total 4 passed %d\n", WEXITSTATUS(status)); + (void)unlink(pcidFileName); + (void)unlink(rpcidFileName); + (void)unlink("./rk3568.sc"); + (void)unlink("./rpcid.sc"); + (void)unlink("./rk3568.json"); + + return 0; +} \ No newline at end of file