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 "store_unittest.h"
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <vector>
21 #include "applypatch/block_set.h"
22 #include "applypatch/store.h"
23 #include "log/log.h"
24 #include "utils.h"
25
26 using namespace testing::ext;
27 using namespace UpdaterUt;
28 using namespace Updater;
29 using namespace std;
30
31 namespace UpdaterUt {
SetUp()32 void StoreUnitTest::SetUp()
33 {
34 cout << "SetUpTestCase" << endl;
35 }
36
TearDown()37 void StoreUnitTest::TearDown()
38 {
39 cout << "TearDownTestCase" << endl;
40 }
41
42 HWTEST_F(StoreUnitTest, store_test_001, TestSize.Level1)
43 {
44 std::string storePath = "/data/updater/ut_test";
45 Store::CreateNewSpace(storePath, true);
46 std::vector<uint8_t> buffer(4096, 0);
47 std::string filename1 = "test_file1";
48 std::string filename2 = "test_file2";
49 Store::WriteDataToStore(storePath, filename1, buffer, 4096);
50 Store::WriteDataToStore(storePath, filename2, buffer, 4096);
51 Store::LoadDataFromStore(storePath, filename1, buffer);
52 Store::DoFreeSpace(storePath);
53 Store::WriteDataToStore(storePath, filename1, buffer, 4096);
54 Store::WriteDataToStore(storePath, filename2, buffer, 4096);
55 Store::FreeStore(storePath, filename2);
56 EXPECT_EQ(Store::CreateNewSpace(storePath, true), 0);
57 }
58
59 HWTEST_F(StoreUnitTest, store_test_002, TestSize.Level1)
60 {
61 std::vector<uint8_t> buffer(4096, 0);
62 EXPECT_EQ(Store::WriteDataToStore("", "test_file1", buffer, buffer.size()), -1);
63 }
64
65 HWTEST_F(StoreUnitTest, store_test_003, TestSize.Level1)
66 {
67 std::string storePath = "data/updater/ut_test";
68 std::vector<uint8_t> buffer(4096, 0);
69 std::string filename1 = "test_file1";
70 EXPECT_EQ(Store::FreeStore("", filename1), -1);
71 Store::CreateNewSpace(storePath, true);
72 EXPECT_EQ(Store::WriteDataToStore(storePath, filename1, buffer, -1), -1);
73 }
74 }
75