194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/*
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Copyright (C) 2011 The Android Open Source Project
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * you may not use this file except in compliance with the License.
694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * You may obtain a copy of the License at
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
1094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Unless required by applicable law or agreed to in writing, software
1194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
1294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * See the License for the specific language governing permissions and
1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * limitations under the License.
1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <stdlib.h>
1894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <string.h>
1994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define LOG_TAG "utils_test"
2194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <utils/Log.h>
2294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <gtest/gtest.h>
2494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodextern "C" {
2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include "installd.h"
2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_DATA_DIR "/data/"
3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_APP_DIR "/data/app/"
3194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_APP_PRIVATE_DIR "/data/app-private/"
3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_ASEC_DIR "/mnt/asec/"
3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_SYSTEM_DIR1 "/system/app/"
3594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define TEST_SYSTEM_DIR2 "/vendor/app/"
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define REALLY_LONG_APP_NAME "com.example." \
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \
3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \
4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
4194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define REALLY_LONG_LEAF_NAME "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
4594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_"
4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodnamespace android {
4894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodclass UtilsTest : public testing::Test {
5094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodprotected:
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    virtual void SetUp() {
5294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_app_dir.path = TEST_APP_DIR;
5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_app_dir.len = strlen(TEST_APP_DIR);
5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_app_private_dir.path = TEST_APP_PRIVATE_DIR;
5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_app_private_dir.len = strlen(TEST_APP_PRIVATE_DIR);
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_data_dir.path = TEST_DATA_DIR;
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_data_dir.len = strlen(TEST_DATA_DIR);
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_asec_dir.path = TEST_ASEC_DIR;
6294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_asec_dir.len = strlen(TEST_ASEC_DIR);
6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.count = 2;
6594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
6794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.dirs[0].path = TEST_SYSTEM_DIR1;
6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.dirs[0].len = strlen(TEST_SYSTEM_DIR1);
6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.dirs[1].path = TEST_SYSTEM_DIR2;
7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        android_system_dirs.dirs[1].len = strlen(TEST_SYSTEM_DIR2);
7294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    virtual void TearDown() {
7594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        free(android_system_dirs.dirs);
7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood};
7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Bad prefixes directories
8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badprefix1 = "/etc/passwd";
8294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badprefix1))
8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badprefix1 << " should be allowed as a valid path";
8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
8694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badprefix2))
8794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badprefix2 << " should be allowed as a valid path";
8894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badprefix3 = "init.rc";
9094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badprefix3))
9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badprefix3 << " should be allowed as a valid path";
9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badprefix4 = "/init.rc";
9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badprefix4))
9594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badprefix4 << " should be allowed as a valid path";
9694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9894afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_Internal) {
9994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Internal directories
10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *internal1 = TEST_APP_DIR "example.apk";
10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_apk_path(internal1))
10294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << internal1 << " should be allowed as a valid path";
10394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badint1 = TEST_APP_DIR "../example.apk";
10594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badint1))
10694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badint1 << " should be rejected as a invalid path";
10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badint2 = TEST_APP_DIR "/../example.apk";
10994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badint2))
11094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badint2 << " should be rejected as a invalid path";
11194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
11294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badint3 = TEST_APP_DIR "example.com/pkg.apk";
11394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badint3))
11494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badint3 << " should be rejected as a invalid path";
11594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_Private) {
11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Internal directories
11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_apk_path(private1))
12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << private1 << " should be allowed as a valid path";
12294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badpriv1))
12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badpriv1 << " should be rejected as a invalid path";
12694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
12894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badpriv2))
12994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badpriv2 << " should be rejected as a invalid path";
13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badpriv3 = TEST_APP_PRIVATE_DIR "example.com/pkg.apk";
13294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badpriv3))
13394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badpriv3 << " should be rejected as a invalid path";
13494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *asec1 = TEST_ASEC_DIR "example.apk";
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_apk_path(asec1))
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << asec1 << " should be allowed as a valid path";
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
14594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_apk_path(asec2))
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << asec2 << " should be allowed as a valid path";
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec1 = TEST_ASEC_DIR "../example.apk";
15194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec1))
15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec1 << " should be rejected as a invalid path";
15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15594afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_DoubleSlashFail) {
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec2 = TEST_ASEC_DIR "com.example.asec//pkg.apk";
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec2))
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec2 << " should be rejected as a invalid path";
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec3))
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec3  << " should be rejected as a invalid path";
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
16694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
16894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
16994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec4))
17094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec4 << " should be rejected as a invalid path";
17194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec5 = TEST_ASEC_DIR ".//../..";
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec5))
17694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec5 << " should be rejected as a invalid path";
17794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec6))
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec6 << " should be rejected as a invalid path";
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) {
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk";
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_apk_path(badasec7))
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badasec7 << " should be rejected as a invalid path";
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CheckSystemApp_Dir1) {
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_system_app_path(sysapp1))
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << sysapp1 << " should be allowed as a system path";
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CheckSystemApp_Dir2) {
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, validate_system_app_path(sysapp2))
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << sysapp2 << " should be allowed as a system path";
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CheckSystemApp_EscapeFail) {
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk";
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_system_app_path(badapp1))
20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badapp1 << " should be rejected not a system path";
20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_system_app_path(badapp2))
21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badapp2 << " should be rejected not a system path";
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, validate_system_app_path(badapp3))
21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << badapp3 << " should be rejected not a system path";
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, GetPathFromString_NullPathFail) {
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t test1;
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, get_path_from_string(&test1, (const char *) NULL))
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should not allow NULL as a path.";
22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, GetPathFromString_EmptyPathFail) {
22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t test1;
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, get_path_from_string(&test1, ""))
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should not allow empty paths.";
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, GetPathFromString_RelativePathFail) {
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t test1;
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, get_path_from_string(&test1, "mnt/asec"))
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should not allow relative paths.";
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, GetPathFromString_NonCanonical) {
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t test1;
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, get_path_from_string(&test1, "/mnt/asec"))
24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should be able to canonicalize directory /mnt/asec";
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/mnt/asec/", test1.path)
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "/mnt/asec should be canonicalized to /mnt/asec/";
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(10, (ssize_t) test1.len)
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "path len should be equal to the length of /mnt/asec/ (10)";
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(test1.path);
24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, GetPathFromString_CanonicalPath) {
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t test3;
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, get_path_from_string(&test3, "/data/app/"))
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should be able to canonicalize directory /data/app/";
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/app/", test3.path)
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "/data/app/ should be canonicalized to /data/app/";
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(10, (ssize_t) test3.len)
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "path len should be equal to the length of /data/app/ (10)";
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(test3.path);
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) {
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Create long packagename of "aaaaa..."
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t pkgnameSize = PKG_NAME_MAX;
26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char pkgname[pkgnameSize + 1];
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(pkgname, 'a', pkgnameSize);
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pkgname[pkgnameSize] = '\0';
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0))
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should successfully be able to create package name.";
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *prefix = TEST_DATA_DIR PRIMARY_USER_PREFIX;
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t offset = strlen(prefix);
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ(pkgname, path + offset)
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood             << "Package path should be a really long string of a's";
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPath_LongPkgNameFail) {
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Create long packagename of "aaaaa..."
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t pkgnameSize = PKG_NAME_MAX + 1;
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char pkgname[pkgnameSize + 1];
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(pkgname, 'a', pkgnameSize);
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pkgname[pkgnameSize] = '\0';
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, create_pkg_path(path, pkgname, "", 0))
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should return error because package name is too long.";
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) {
29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Create long packagename of "aaaaa..."
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t postfixSize = PKG_PATH_MAX;
29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char postfix[postfixSize + 1];
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    memset(postfix, 'a', postfixSize);
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    postfix[postfixSize] = '\0';
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0))
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should return error because postfix is too long.";
30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPath_PrimaryUser) {
30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0))
31094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should return error because postfix is too long.";
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ(TEST_DATA_DIR PRIMARY_USER_PREFIX "com.example.package", path)
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Package path should be in /data/data/";
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPath_SecondaryUser) {
31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1))
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should successfully create package path.";
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ(TEST_DATA_DIR SECONDARY_USER_PREFIX "1/com.example.package", path)
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Package path should be in /data/user/";
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePkgPathInDir_ProtectedDir) {
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t dir;
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir.path = "/data/app-private/";
33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir.len = strlen(dir.path);
33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, create_pkg_path_in_dir(path, &dir, "com.example.package", ".apk"))
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should successfully create package path.";
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/app-private/com.example.package.apk", path)
33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Package path should be in /data/app-private/";
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePersonaPath_Primary) {
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
343abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    EXPECT_EQ(0, create_user_path(path, 0))
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should successfully build primary user path.";
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/data/", path)
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Primary user should have correct path";
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreatePersonaPath_Secondary) {
35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
353abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    EXPECT_EQ(0, create_user_path(path, 1))
35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should successfully build primary user path.";
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/user/1/", path)
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Primary user should have correct path";
35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreateMovePath_Primary) {
36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, create_move_path(path, "com.android.test", "shared_prefs", 0))
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should be able to create move path for primary user";
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/data/com.android.test/shared_prefs", path)
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Primary user package directory should be created correctly";
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreateMovePath_Fail_AppTooLong) {
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, create_move_path(path, REALLY_LONG_APP_NAME, "shared_prefs", 0))
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should fail to create move path for primary user";
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CreateMovePath_Fail_LeafTooLong) {
37894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, create_move_path(path, "com.android.test", REALLY_LONG_LEAF_NAME, 0))
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should fail to create move path for primary user";
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, CopyAndAppend_Normal) {
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    //int copy_and_append(dir_rec_t* dst, dir_rec_t* src, char* suffix)
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t dst;
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t src;
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    src.path = "/data/";
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    src.len = strlen(src.path);
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, copy_and_append(&dst, &src, "app/"))
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Should return error because postfix is too long.";
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("/data/app/", dst.path)
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Appended path should be correct";
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(10, (ssize_t) dst.len)
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "Appended path should be length of '/data/app/' (10)";
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, AppendAndIncrement_Normal) {
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t dst_size = 10;
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dst[dst_size];
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *dstp = dst;
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* src = "FOO";
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append successfully";
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("FOO", dst)
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append correctly";
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append successfully again";
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("FOOFOO", dst)
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append correctly again";
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike LockwoodTEST_F(UtilsTest, AppendAndIncrement_TooBig) {
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t dst_size = 5;
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dst[dst_size];
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *dstp = dst;
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* src = "FOO";
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append successfully";
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_STREQ("FOO", dst)
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should append correctly";
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    EXPECT_EQ(-1, append_and_increment(&dstp, src, &dst_size))
43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            << "String should fail because it's too large to fit";
43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
438