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 16 #include <fcntl.h> 17 #include <gtest/gtest.h> 18 #include <unistd.h> 19 #include "securitylabel_core.h" 20 21 namespace OHOS { 22 namespace FileManagement { 23 namespace ModuleSecurityLabel { 24 using namespace std; 25 using namespace OHOS::FileManagement::ModuleFileIO; 26 27 static const string g_filePath = "/data/test/SecurityLabelCoreTest.txt"; 28 static const string g_validFilePath = "/data/test/validFilePath"; 29 30 class SecurityLabelCoreTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) 33 { 34 int32_t fd = open(g_filePath.c_str(), O_CREAT | O_RDWR, 0644); 35 close(fd); 36 }; TearDownTestCase()37 static void TearDownTestCase() 38 { 39 rmdir(g_filePath.c_str()); 40 }; SetUp()41 void SetUp() {}; TearDown()42 void TearDown() {}; 43 }; 44 45 /** 46 * @tc.name: DoSetSecurityLabel_0001 47 * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid level. 48 * @tc.size: MEDIUM 49 * @tc.type: FUNC 50 * @tc.level Level 1 51 */ 52 HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0001, testing::ext::TestSize.Level1) 53 { 54 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0001"; 55 auto ret = DoSetSecurityLabel(g_filePath, "abc"); 56 EXPECT_FALSE(ret.IsSuccess()); 57 58 auto err = ret.GetError(); 59 EXPECT_EQ(err.GetErrNo(), 13900020); 60 61 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0001"; 62 } 63 64 /** 65 * @tc.name: DoSetSecurityLabel_0002 66 * @tc.desc: Test function of DoSetSecurityLabel() interface for invalid path. 67 * @tc.size: MEDIUM 68 * @tc.type: FUNC 69 * @tc.level Level 1 70 */ 71 HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0002, testing::ext::TestSize.Level1) 72 { 73 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0002"; 74 auto ret = DoSetSecurityLabel(g_validFilePath, "s1"); 75 EXPECT_FALSE(ret.IsSuccess()); 76 77 auto err = ret.GetError(); 78 EXPECT_EQ(err.GetErrNo(), 13900002); 79 80 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0002"; 81 } 82 83 /** 84 * @tc.name: DoSetSecurityLabel_0003 85 * @tc.desc: Test function of DoSetSecurityLabel() interface for success. 86 * @tc.size: MEDIUM 87 * @tc.type: FUNC 88 * @tc.level Level 1 89 */ 90 HWTEST_F(SecurityLabelCoreTest, DoSetSecurityLabel_0003, testing::ext::TestSize.Level1) 91 { 92 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoSetSecurityLabel_0003"; 93 auto ret = DoSetSecurityLabel(g_filePath, "s2"); 94 ASSERT_TRUE(ret.IsSuccess()); 95 96 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoSetSecurityLabel_0003"; 97 } 98 99 /** 100 * @tc.name: DoGetSecurityLabel_0001 101 * @tc.desc: Test function of DoGetSecurityLabel() interface for invalid path. 102 * @tc.size: MEDIUM 103 * @tc.type: FUNC 104 * @tc.level Level 1 105 */ 106 HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0001, testing::ext::TestSize.Level1) 107 { 108 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0001"; 109 auto ret = DoGetSecurityLabel(g_validFilePath); 110 EXPECT_TRUE(ret.IsSuccess()); 111 112 const string level = ret.GetData().value(); 113 EXPECT_EQ(level, "s3"); 114 115 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0001"; 116 } 117 118 /** 119 * @tc.name: DoGetSecurityLabel_0002 120 * @tc.desc: Test function of DoGetSecurityLabel() interface for success. 121 * @tc.size: MEDIUM 122 * @tc.type: FUNC 123 * @tc.level Level 1 124 */ 125 HWTEST_F(SecurityLabelCoreTest, DoGetSecurityLabel_0002, testing::ext::TestSize.Level1) 126 { 127 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-begin DoGetSecurityLabel_0002"; 128 auto ret = DoGetSecurityLabel(g_filePath); 129 EXPECT_TRUE(ret.IsSuccess()); 130 131 const string level = ret.GetData().value(); 132 EXPECT_EQ(level, "s2"); 133 134 GTEST_LOG_(INFO) << "SecurityLabelCoreTest-end DoGetSecurityLabel_0002"; 135 } 136 137 } // namespace ModuleSecurityLabel 138 } // namespace FileManagement 139 } // namespace OHOS