1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "BackupHelpers_test"
18 #include <androidfw/BackupHelpers.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include <fcntl.h>
23 #include <utils/String8.h>
24 #include <android-base/file.h>
25 
26 namespace android {
27 
28 class BackupHelpersTest : public testing::Test {
29 protected:
30 
SetUp()31     virtual void SetUp() {
32     }
TearDown()33     virtual void TearDown() {
34     }
35 };
36 
TEST_F(BackupHelpersTest,WriteTarFileWithSizeLessThan2GB)37 TEST_F(BackupHelpersTest, WriteTarFileWithSizeLessThan2GB) {
38   TemporaryFile tf;
39   // Allocate a 1 KB file.
40   off64_t fileSize = 1024;
41   ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, fileSize));
42   off64_t tarSize = 0;
43   int err = write_tarfile(/* packageName */ String8("test-pkg"), /* domain */ String8(""), /* rootpath */ String8(""), /* filePath */ String8(tf.path), /* outSize */ &tarSize, /* writer */ NULL);
44   ASSERT_EQ(err, 0);
45   // Returned tarSize includes 512 B for the header.
46   off64_t expectedTarSize = fileSize + 512;
47   ASSERT_EQ(tarSize, expectedTarSize);
48 }
49 
TEST_F(BackupHelpersTest,WriteTarFileWithSizeGreaterThan2GB)50 TEST_F(BackupHelpersTest, WriteTarFileWithSizeGreaterThan2GB) {
51   TemporaryFile tf;
52   // Allocate a 2 GB file.
53   off64_t fileSize = 2LL * 1024LL * 1024LL * 1024LL + 512LL;
54   ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, fileSize));
55   off64_t tarSize = 0;
56   int err = write_tarfile(/* packageName */ String8("test-pkg"), /* domain */ String8(""), /* rootpath */ String8(""), /* filePath */ String8(tf.path), /* outSize */ &tarSize, /* writer */ NULL);
57   ASSERT_EQ(err, 0);
58   // Returned tarSize includes 512 B for the header.
59   off64_t expectedTarSize = fileSize + 512;
60   ASSERT_EQ(tarSize, expectedTarSize);
61 }
62 }
63 
64