1#!/bin/env bash
2#
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7FAKE_SYSCALLS_LIB=`pwd`/libfakesyscalls.so
8FAKE_SYSCALLS_DBG=/tmp/fake_syscalls.dbg
9TEST_EXE=`pwd`/fakesyscalls-exercise
10
11# build up the redirection envrionment variable
12FILE_REDIRECTIONS_PRELOAD=:
13FILE_REDIRECTIONS_PRELOAD=${FILE_REDIRECTIONS_PRELOAD}:/tmp/foo1=/tmp/foo2
14FILE_REDIRECTIONS_PRELOAD=${FILE_REDIRECTIONS_PRELOAD}:/tmp/foo3
15FILE_REDIRECTIONS_PRELOAD=${FILE_REDIRECTIONS_PRELOAD}:/tmp/foo4=.
16FILE_REDIRECTIONS_PRELOAD=${FILE_REDIRECTIONS_PRELOAD}:/tmp/foo5=/tmp/foo6
17export FILE_REDIRECTIONS_PRELOAD=$FILE_REDIRECTIONS_PRELOAD
18
19rm -f /tmp/foo1 /tmp/foo2 /tmp/foo3 /tmp/foo4 /tmp/foo5 /tmp/foo6 /tmp/foo99
20rm -f ${FAKE_SYSCALLS_DBG}
21
22# Successful redirection
23echo LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo1 /tmp/foo2
24LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo1 /tmp/foo2
25echo
26echo ${FAKE_SYSCALLS_DBG}
27cat ${FAKE_SYSCALLS_DBG}
28echo
29
30# Malformed map --> unsuccessful redirection
31echo LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo3 /tmp/foo3
32LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo3 /tmp/foo3
33echo
34echo ${FAKE_SYSCALLS_DBG}
35cat ${FAKE_SYSCALLS_DBG}
36echo
37
38# Relative path in map --> unsuccessful redirection
39echo LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo4 /tmp/foo4
40LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo4 /tmp/foo4
41echo
42echo ${FAKE_SYSCALLS_DBG}
43cat ${FAKE_SYSCALLS_DBG}
44echo
45
46# Does not exist in map --> no redirection.
47echo LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo99 /tmp/foo99
48LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} /tmp/foo99 /tmp/foo99
49echo
50echo ${FAKE_SYSCALLS_DBG}
51cat ${FAKE_SYSCALLS_DBG}
52echo
53
54
55pushd . >/dev/null
56cd /tmp >/dev/null
57# Relative path in open() --> unsuccessful redirection
58echo LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} foo5 foo5
59LD_PRELOAD=${FAKE_SYSCALLS_LIB} ${TEST_EXE} foo5 foo5
60if test -e /tmp/foo6
61then
62  echo Fail: /tmp/foo6 should not have been created. foo6:
63  cat /tmp/foo6
64fi
65echo
66echo ${FAKE_SYSCALLS_DBG}
67cat ${FAKE_SYSCALLS_DBG}
68echo
69popd >/dev/null
70