186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root/* 286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * Copyright (C) 2011 The Android Open Source Project 386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * 486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * Licensed under the Apache License, Version 2.0 (the "License"); 586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * you may not use this file except in compliance with the License. 686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * You may obtain a copy of the License at 786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * 886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * http://www.apache.org/licenses/LICENSE-2.0 986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * 1086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * Unless required by applicable law or agreed to in writing, software 1186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * distributed under the License is distributed on an "AS IS" BASIS, 1286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * See the License for the specific language governing permissions and 1486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root * limitations under the License. 1586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root */ 1686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 1786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#include <stdlib.h> 1886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#include <string.h> 1986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 2086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define LOG_TAG "utils_test" 2186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#include <utils/Log.h> 2286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 2386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#include <gtest/gtest.h> 2486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 2586c9584559439504fc57ece2ccd9b6cbd568430cKenny Rootextern "C" { 2686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#include "installd.h" 2786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 2886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 2986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_DATA_DIR "/data/" 3086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_APP_DIR "/data/app/" 3186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_APP_PRIVATE_DIR "/data/app-private/" 3286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_ASEC_DIR "/mnt/asec/" 3386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 3486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_SYSTEM_DIR1 "/system/app/" 3586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root#define TEST_SYSTEM_DIR2 "/vendor/app/" 3686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 37ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root#define REALLY_LONG_APP_NAME "com.example." \ 38ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \ 39ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \ 40ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 41ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 42ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root#define REALLY_LONG_LEAF_NAME "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 43ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 44ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 45ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" 46ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 4786c9584559439504fc57ece2ccd9b6cbd568430cKenny Rootnamespace android { 4886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 4986c9584559439504fc57ece2ccd9b6cbd568430cKenny Rootclass UtilsTest : public testing::Test { 5086c9584559439504fc57ece2ccd9b6cbd568430cKenny Rootprotected: 5186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root virtual void SetUp() { 5286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_app_dir.path = TEST_APP_DIR; 5386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_app_dir.len = strlen(TEST_APP_DIR); 5486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 5586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_app_private_dir.path = TEST_APP_PRIVATE_DIR; 5686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_app_private_dir.len = strlen(TEST_APP_PRIVATE_DIR); 5786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 5886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_data_dir.path = TEST_DATA_DIR; 5986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_data_dir.len = strlen(TEST_DATA_DIR); 6086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 6186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_asec_dir.path = TEST_ASEC_DIR; 6286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_asec_dir.len = strlen(TEST_ASEC_DIR); 6386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 6486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.count = 2; 6586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 6686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 6786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.dirs[0].path = TEST_SYSTEM_DIR1; 6886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.dirs[0].len = strlen(TEST_SYSTEM_DIR1); 6986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 7086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.dirs[1].path = TEST_SYSTEM_DIR2; 7186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root android_system_dirs.dirs[1].len = strlen(TEST_SYSTEM_DIR2); 7286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root } 7386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 7486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root virtual void TearDown() { 7586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root free(android_system_dirs.dirs); 7686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root } 7786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root}; 7886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 7986c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_BadPrefix) { 8086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Bad prefixes directories 8186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badprefix1 = "/etc/passwd"; 8286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badprefix1)) 8386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badprefix1 << " should be allowed as a valid path"; 8486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 8586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah"; 8686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badprefix2)) 8786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badprefix2 << " should be allowed as a valid path"; 8886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 8986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badprefix3 = "init.rc"; 9086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badprefix3)) 9186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badprefix3 << " should be allowed as a valid path"; 9286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 9386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badprefix4 = "/init.rc"; 9486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badprefix4)) 9586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badprefix4 << " should be allowed as a valid path"; 9686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 9786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 9886c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_Internal) { 9986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Internal directories 10086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *internal1 = TEST_APP_DIR "example.apk"; 10186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_apk_path(internal1)) 10286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << internal1 << " should be allowed as a valid path"; 10386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 10486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badint1 = TEST_APP_DIR "../example.apk"; 10586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badint1)) 10686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badint1 << " should be rejected as a invalid path"; 10786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 10886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badint2 = TEST_APP_DIR "/../example.apk"; 10986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badint2)) 11086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badint2 << " should be rejected as a invalid path"; 11186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 11286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badint3 = TEST_APP_DIR "example.com/pkg.apk"; 11386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badint3)) 11486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badint3 << " should be rejected as a invalid path"; 11586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 11686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 11786c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_Private) { 11886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Internal directories 11986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *private1 = TEST_APP_PRIVATE_DIR "example.apk"; 12086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_apk_path(private1)) 12186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << private1 << " should be allowed as a valid path"; 12286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 12386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk"; 12486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badpriv1)) 12586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badpriv1 << " should be rejected as a invalid path"; 12686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 12786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk"; 12886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badpriv2)) 12986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badpriv2 << " should be rejected as a invalid path"; 13086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 13186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badpriv3 = TEST_APP_PRIVATE_DIR "example.com/pkg.apk"; 13286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badpriv3)) 13386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badpriv3 << " should be rejected as a invalid path"; 13486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 13586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 13686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 13786c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_AsecGood1) { 13886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *asec1 = TEST_ASEC_DIR "example.apk"; 13986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_apk_path(asec1)) 14086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << asec1 << " should be allowed as a valid path"; 14186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 14286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 14386c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_AsecGood2) { 14486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk"; 14586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_apk_path(asec2)) 14686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << asec2 << " should be allowed as a valid path"; 14786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 14886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 14986c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_EscapeFail) { 15086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec1 = TEST_ASEC_DIR "../example.apk"; 15186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec1)) 15286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec1 << " should be rejected as a invalid path"; 15386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 15486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 15586c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_DoubleSlashFail) { 15686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec2 = TEST_ASEC_DIR "com.example.asec//pkg.apk"; 15786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec2)) 15886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec2 << " should be rejected as a invalid path"; 15986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 16086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 16186c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) { 16286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk"; 16386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec3)) 16486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec3 << " should be rejected as a invalid path"; 16586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 16686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 16786c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) { 16886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec4 = TEST_ASEC_DIR "/../example.apk"; 16986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec4)) 17086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec4 << " should be rejected as a invalid path"; 17186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 17286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 17386c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) { 17486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec5 = TEST_ASEC_DIR ".//../.."; 17586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec5)) 17686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec5 << " should be rejected as a invalid path"; 17786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 17886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 17986c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) { 18086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk"; 18186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec6)) 18286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec6 << " should be rejected as a invalid path"; 18386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 18486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 18586c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) { 18686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk"; 18786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_apk_path(badasec7)) 18886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badasec7 << " should be rejected as a invalid path"; 18986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 19086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 19186c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CheckSystemApp_Dir1) { 19286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk"; 19386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_system_app_path(sysapp1)) 19486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << sysapp1 << " should be allowed as a system path"; 19586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 19686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 19786c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CheckSystemApp_Dir2) { 19886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk"; 19986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, validate_system_app_path(sysapp2)) 20086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << sysapp2 << " should be allowed as a system path"; 20186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 20286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 20386c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CheckSystemApp_EscapeFail) { 20486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk"; 20586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_system_app_path(badapp1)) 20686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badapp1 << " should be rejected not a system path"; 20786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 20886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 20986c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) { 21086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk"; 21186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_system_app_path(badapp2)) 21286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badapp2 << " should be rejected not a system path"; 21386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 21486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 21586c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) { 21686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *badapp3 = TEST_APP_DIR "/../../com.example.apk"; 21786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, validate_system_app_path(badapp3)) 21886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << badapp3 << " should be rejected not a system path"; 21986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 22086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 22186c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, GetPathFromString_NullPathFail) { 22286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t test1; 223ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(-1, get_path_from_string(&test1, (const char *) NULL)) 22486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should not allow NULL as a path."; 22586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 22686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 22786c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, GetPathFromString_EmptyPathFail) { 22886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t test1; 22986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, get_path_from_string(&test1, "")) 23086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should not allow empty paths."; 23186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 23286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 23386c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, GetPathFromString_RelativePathFail) { 23486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t test1; 23586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, get_path_from_string(&test1, "mnt/asec")) 23686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should not allow relative paths."; 23786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 23886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 23986c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, GetPathFromString_NonCanonical) { 24086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t test1; 24186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 24286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, get_path_from_string(&test1, "/mnt/asec")) 24386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should be able to canonicalize directory /mnt/asec"; 24486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("/mnt/asec/", test1.path) 24586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "/mnt/asec should be canonicalized to /mnt/asec/"; 24686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(10, (ssize_t) test1.len) 24786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "path len should be equal to the length of /mnt/asec/ (10)"; 24886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root free(test1.path); 24986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 25086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 25186c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, GetPathFromString_CanonicalPath) { 25286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t test3; 25386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, get_path_from_string(&test3, "/data/app/")) 25486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should be able to canonicalize directory /data/app/"; 25586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("/data/app/", test3.path) 25686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "/data/app/ should be canonicalized to /data/app/"; 25786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(10, (ssize_t) test3.len) 25886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "path len should be equal to the length of /data/app/ (10)"; 25986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root free(test3.path); 26086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 26186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 26286c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) { 26386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 26486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 26586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Create long packagename of "aaaaa..." 26686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t pkgnameSize = PKG_NAME_MAX; 26786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char pkgname[pkgnameSize + 1]; 26886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root memset(pkgname, 'a', pkgnameSize); 26986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root pkgname[pkgnameSize] = '\0'; 27086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 27186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0)) 27286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should successfully be able to create package name."; 27386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 27486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char *prefix = TEST_DATA_DIR PRIMARY_USER_PREFIX; 27586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t offset = strlen(prefix); 27686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ(pkgname, path + offset) 27786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Package path should be a really long string of a's"; 27886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 27986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 28086c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPath_LongPkgNameFail) { 28186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 28286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 28386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Create long packagename of "aaaaa..." 28486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t pkgnameSize = PKG_NAME_MAX + 1; 28586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char pkgname[pkgnameSize + 1]; 28686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root memset(pkgname, 'a', pkgnameSize); 28786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root pkgname[pkgnameSize] = '\0'; 28886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 28986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, create_pkg_path(path, pkgname, "", 0)) 29086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should return error because package name is too long."; 29186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 29286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 29386c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) { 29486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 29586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 29686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root // Create long packagename of "aaaaa..." 29786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t postfixSize = PKG_PATH_MAX; 29886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char postfix[postfixSize + 1]; 29986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root memset(postfix, 'a', postfixSize); 30086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root postfix[postfixSize] = '\0'; 30186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 30286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0)) 30386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should return error because postfix is too long."; 30486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 30586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 30686c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPath_PrimaryUser) { 30786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 30886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 30986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0)) 31086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should return error because postfix is too long."; 31186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 31286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ(TEST_DATA_DIR PRIMARY_USER_PREFIX "com.example.package", path) 31386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Package path should be in /data/data/"; 31486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 31586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 31686c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPath_SecondaryUser) { 31786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 31886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 31986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1)) 32086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should successfully create package path."; 32186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 32286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ(TEST_DATA_DIR SECONDARY_USER_PREFIX "1/com.example.package", path) 32386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Package path should be in /data/user/"; 32486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 32586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 32686c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CreatePkgPathInDir_ProtectedDir) { 32786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char path[PKG_PATH_MAX]; 32886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 32986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t dir; 33086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir.path = "/data/app-private/"; 33186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir.len = strlen(dir.path); 33286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 33386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, create_pkg_path_in_dir(path, &dir, "com.example.package", ".apk")) 33486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should successfully create package path."; 33586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 33686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("/data/app-private/com.example.package.apk", path) 33786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Package path should be in /data/app-private/"; 33886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 33986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 340ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny RootTEST_F(UtilsTest, CreatePersonaPath_Primary) { 341ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root char path[PKG_PATH_MAX]; 342ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 343ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(0, create_persona_path(path, 0)) 344ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Should successfully build primary user path."; 345ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 346ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_STREQ("/data/data/", path) 347ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Primary user should have correct path"; 348ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root} 349ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 350ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny RootTEST_F(UtilsTest, CreatePersonaPath_Secondary) { 351ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root char path[PKG_PATH_MAX]; 352ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 353ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(0, create_persona_path(path, 1)) 354ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Should successfully build primary user path."; 355ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 356ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_STREQ("/data/user/1/", path) 357ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Primary user should have correct path"; 358ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root} 359ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 360ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny RootTEST_F(UtilsTest, CreateMovePath_Primary) { 361ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root char path[PKG_PATH_MAX]; 362ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 363ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(0, create_move_path(path, "com.android.test", "shared_prefs", 0)) 364ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Should be able to create move path for primary user"; 365ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 366ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_STREQ("/data/data/com.android.test/shared_prefs", path) 367ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Primary user package directory should be created correctly"; 368ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root} 369ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 370ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny RootTEST_F(UtilsTest, CreateMovePath_Fail_AppTooLong) { 371ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root char path[PKG_PATH_MAX]; 372ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 373ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(-1, create_move_path(path, REALLY_LONG_APP_NAME, "shared_prefs", 0)) 374ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Should fail to create move path for primary user"; 375ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root} 376ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 377ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny RootTEST_F(UtilsTest, CreateMovePath_Fail_LeafTooLong) { 378ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root char path[PKG_PATH_MAX]; 379ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 380ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root EXPECT_EQ(-1, create_move_path(path, "com.android.test", REALLY_LONG_LEAF_NAME, 0)) 381ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root << "Should fail to create move path for primary user"; 382ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root} 383ad757e9b36d6bc6b8e39be00612d0e60863aaafcKenny Root 38486c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, CopyAndAppend_Normal) { 38586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root //int copy_and_append(dir_rec_t* dst, dir_rec_t* src, char* suffix) 38686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t dst; 38786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root dir_rec_t src; 38886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 38986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root src.path = "/data/"; 39086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root src.len = strlen(src.path); 39186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 39286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, copy_and_append(&dst, &src, "app/")) 39386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Should return error because postfix is too long."; 39486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 39586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("/data/app/", dst.path) 39686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Appended path should be correct"; 39786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 39886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(10, (ssize_t) dst.len) 39986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "Appended path should be length of '/data/app/' (10)"; 40086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 40186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 40286c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, AppendAndIncrement_Normal) { 40386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t dst_size = 10; 40486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char dst[dst_size]; 40586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char *dstp = dst; 40686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char* src = "FOO"; 40786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 40886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 40986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append successfully"; 41086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 41186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("FOO", dst) 41286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append correctly"; 41386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 41486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 41586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append successfully again"; 41686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 41786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("FOOFOO", dst) 41886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append correctly again"; 41986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 42086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 42186c9584559439504fc57ece2ccd9b6cbd568430cKenny RootTEST_F(UtilsTest, AppendAndIncrement_TooBig) { 42286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root size_t dst_size = 5; 42386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char dst[dst_size]; 42486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root char *dstp = dst; 42586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root const char* src = "FOO"; 42686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 42786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 42886c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append successfully"; 42986c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 43086c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_STREQ("FOO", dst) 43186c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should append correctly"; 43286c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 43386c9584559439504fc57ece2ccd9b6cbd568430cKenny Root EXPECT_EQ(-1, append_and_increment(&dstp, src, &dst_size)) 43486c9584559439504fc57ece2ccd9b6cbd568430cKenny Root << "String should fail because it's too large to fit"; 43586c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 43686c9584559439504fc57ece2ccd9b6cbd568430cKenny Root 43786c9584559439504fc57ece2ccd9b6cbd568430cKenny Root} 438