File indexing completed on 2024-04-28 16:08:40

0001 /***************************************************************************
0002  *   Copyright (C) 2014 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include "testhome.h"
0022 
0023 #include <errno.h>
0024 #include <stdio.h>
0025 #include <stdlib.h>
0026 #include <string.h>
0027 #include <string>
0028 
0029 #include "src/test/oomtestutil.h"
0030 
0031 TestHome::TestHome() : delegate(0), fakeHome(0) {
0032     std::string tmpdir("/tmp/t_home_lsmXXXXXX");
0033     tmpdir.c_str(); // ensure trailing null is present
0034     if (mkdtemp(&tmpdir[0])) {
0035         std::string::size_type bufferSize = tmpdir.length() + 1;
0036         fakeHome = (char *) malloc(bufferSize);
0037         if (fakeHome) {
0038             strncpy(fakeHome, tmpdir.c_str(), bufferSize);
0039         } else {
0040             printf("Could not create temporary directory: Out Of Memory!\n");
0041         }
0042     } else {
0043         printf("Could not create temporary directory; error code: %d\n",
0044                 errno);
0045     }
0046 }
0047 
0048 TestHome::~TestHome() {
0049 }
0050 
0051 void TestHome::setDelegate(MockableFileSystem* mfs) {
0052     delegate = mfs;
0053 }
0054 
0055 FILE* TestHome::fopen(const char* filename, const char* mode) {
0056     return delegate->fopen(filename, mode);
0057 }
0058 
0059 FILE* TestHome::freopen(const char* filename, const char* mode, FILE* fh) {
0060     return delegate->freopen(filename, mode, fh);
0061 }
0062 
0063 int TestHome::fclose(FILE* fh) {
0064     return delegate->fclose(fh);
0065 }
0066 
0067 int TestHome::fflush(FILE* fh) {
0068     return delegate->fflush(fh);
0069 }
0070 
0071 size_t TestHome::fread(void *out, size_t blockSize,
0072         size_t blockCount, FILE *fh) {
0073     return delegate->fread(out, blockSize, blockCount, fh);
0074 }
0075 
0076 size_t TestHome::fwrite(const void *in, size_t blockSize,
0077         size_t blockCount, FILE *fh) {
0078     return delegate->fwrite(in, blockSize, blockCount, fh);
0079 }
0080 
0081 int TestHome::access(const char *name, int type) {
0082     return delegate->access(name, type);
0083 }
0084 
0085 int TestHome::ferror(FILE* fh) {
0086     return delegate->ferror(fh);
0087 }
0088 
0089 int TestHome::unlink(const char *name) {
0090     return delegate->unlink(name);
0091 }
0092 
0093 char *TestHome::getenv(const char *name) {
0094     if (0 == strcmp(name, "HOME")) {
0095         return fakeHome;
0096     }
0097     return delegate->getenv(name);
0098 }