aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJed Estep2015-12-15 18:04:53 -0600
committerTao Bao2016-02-10 12:49:38 -0600
commita7b9a4660c18a88413280c84c03917ae953d62b0 (patch)
treeda799688bdf8d673220eccd24e21f20722578ffd /otafault
parent6a7bf1d9aa7cd52a594e91e6a8f9111a18ae3551 (diff)
downloadplatform-bootable-recovery-a7b9a4660c18a88413280c84c03917ae953d62b0.tar.gz
platform-bootable-recovery-a7b9a4660c18a88413280c84c03917ae953d62b0.tar.xz
platform-bootable-recovery-a7b9a4660c18a88413280c84c03917ae953d62b0.zip
IO fault injection for OTA packages
Bug: 25951086 Change-Id: I31c74c735eb7a975b7f41fe2b2eff042e5699c0c (cherry-picked from commit f1fc48c6e62cfee42d25ad12f443e22d50c15d0b)
Diffstat (limited to 'otafault')
-rw-r--r--otafault/Android.mk58
-rw-r--r--otafault/ota_io.cpp160
-rw-r--r--otafault/ota_io.h49
-rw-r--r--otafault/test.cpp32
4 files changed, 299 insertions, 0 deletions
diff --git a/otafault/Android.mk b/otafault/Android.mk
new file mode 100644
index 00000000..75617a14
--- /dev/null
+++ b/otafault/Android.mk
@@ -0,0 +1,58 @@
1# Copyright 2015 The ANdroid Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific languae governing permissions and
13# limitations under the License.
14
15LOCAL_PATH := $(call my-dir)
16
17empty :=
18space := $(empty) $(empty)
19comma := ,
20
21ifneq ($(TARGET_INJECT_FAULTS),)
22TARGET_INJECT_FAULTS := $(subst $(comma),$(space),$(strip $(TARGET_INJECT_FAULTS)))
23endif
24
25include $(CLEAR_VARS)
26
27LOCAL_SRC_FILES := ota_io.cpp
28LOCAL_MODULE_TAGS := eng
29LOCAL_MODULE := libotafault
30LOCAL_CLANG := true
31
32ifneq ($(TARGET_INJECT_FAULTS),)
33$(foreach ft,$(TARGET_INJECT_FAULTS),\
34 $(eval LOCAL_CFLAGS += -DTARGET_$(ft)_FAULT=$(TARGET_$(ft)_FAULT_FILE)))
35LOCAL_CFLAGS += -Wno-unused-parameter
36LOCAL_CFLAGS += -DTARGET_INJECT_FAULTS
37endif
38
39LOCAL_STATIC_LIBRARIES := libc
40
41include $(BUILD_STATIC_LIBRARY)
42
43include $(CLEAR_VARS)
44
45LOCAL_SRC_FILES := ota_io.cpp test.cpp
46LOCAL_MODULE_TAGS := tests
47LOCAL_MODULE := otafault_test
48LOCAL_STATIC_LIBRARIES := libc
49LOCAL_FORCE_STATIC_EXECUTABLE := true
50LOCAL_CFLAGS += -Wno-unused-parameter -Wno-writable-strings
51
52ifneq ($(TARGET_INJECT_FAULTS),)
53$(foreach ft,$(TARGET_INJECT_FAULTS),\
54 $(eval LOCAL_CFLAGS += -DTARGET_$(ft)_FAULT=$(TARGET_$(ft)_FAULT_FILE)))
55LOCAL_CFLAGS += -DTARGET_INJECT_FAULTS
56endif
57
58include $(BUILD_EXECUTABLE)
diff --git a/otafault/ota_io.cpp b/otafault/ota_io.cpp
new file mode 100644
index 00000000..02e80f9b
--- /dev/null
+++ b/otafault/ota_io.cpp
@@ -0,0 +1,160 @@
1/*
2 * Copyright (C) 2015 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#if defined (TARGET_INJECT_FAULTS)
18#include <map>
19#endif
20
21#include <errno.h>
22#include <fcntl.h>
23#include <stdio.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27#include "ota_io.h"
28
29#if defined (TARGET_INJECT_FAULTS)
30static std::map<int, const char*> FilenameCache;
31static std::string FaultFileName =
32#if defined (TARGET_READ_FAULT)
33 TARGET_READ_FAULT;
34#elif defined (TARGET_WRITE_FAULT)
35 TARGET_WRITE_FAULT;
36#elif defined (TARGET_FSYNC_FAULT)
37 TARGET_FSYNC_FAULT;
38#endif // defined (TARGET_READ_FAULT)
39#endif // defined (TARGET_INJECT_FAULTS)
40
41int ota_open(const char* path, int oflags) {
42#if defined (TARGET_INJECT_FAULTS)
43 // Let the caller handle errors; we do not care if open succeeds or fails
44 int fd = open(path, oflags);
45 FilenameCache[fd] = path;
46 return fd;
47#else
48 return open(path, oflags);
49#endif
50}
51
52int ota_open(const char* path, int oflags, mode_t mode) {
53#if defined (TARGET_INJECT_FAULTS)
54 int fd = open(path, oflags, mode);
55 FilenameCache[fd] = path;
56 return fd;
57#else
58 return open(path, oflags, mode);
59#endif
60}
61
62FILE* ota_fopen(const char* path, const char* mode) {
63#if defined (TARGET_INJECT_FAULTS)
64 FILE* fh = fopen(path, mode);
65 FilenameCache[(intptr_t)fh] = path;
66 return fh;
67#else
68 return fopen(path, mode);
69#endif
70}
71
72int ota_close(int fd) {
73#if defined (TARGET_INJECT_FAULTS)
74 // descriptors can be reused, so make sure not to leave them in the cahce
75 FilenameCache.erase(fd);
76#endif
77 return close(fd);
78}
79
80int ota_fclose(FILE* fh) {
81#if defined (TARGET_INJECT_FAULTS)
82 FilenameCache.erase((intptr_t)fh);
83#endif
84 return fclose(fh);
85}
86
87size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
88#if defined (TARGET_READ_FAULT)
89 if (FilenameCache.find((intptr_t)stream) != FilenameCache.end()
90 && FilenameCache[(intptr_t)stream] == FaultFileName) {
91 FaultFileName = "";
92 errno = EIO;
93 return 0;
94 } else {
95 return fread(ptr, size, nitems, stream);
96 }
97#else
98 return fread(ptr, size, nitems, stream);
99#endif
100}
101
102ssize_t ota_read(int fd, void* buf, size_t nbyte) {
103#if defined (TARGET_READ_FAULT)
104 if (FilenameCache.find(fd) != FilenameCache.end()
105 && FilenameCache[fd] == FaultFileName) {
106 FaultFileName = "";
107 errno = EIO;
108 return -1;
109 } else {
110 return read(fd, buf, nbyte);
111 }
112#else
113 return read(fd, buf, nbyte);
114#endif
115}
116
117size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
118#if defined (TARGET_WRITE_FAULT)
119 if (FilenameCache.find((intptr_t)stream) != FilenameCache.end()
120 && FilenameCache[(intptr_t)stream] == FaultFileName) {
121 FaultFileName = "";
122 errno = EIO;
123 return 0;
124 } else {
125 return fwrite(ptr, size, count, stream);
126 }
127#else
128 return fwrite(ptr, size, count, stream);
129#endif
130}
131
132ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
133#if defined (TARGET_WRITE_FAULT)
134 if (FilenameCache.find(fd) != FilenameCache.end()
135 && FilenameCache[fd] == FaultFileName) {
136 FaultFileName = "";
137 errno = EIO;
138 return -1;
139 } else {
140 return write(fd, buf, nbyte);
141 }
142#else
143 return write(fd, buf, nbyte);
144#endif
145}
146
147int ota_fsync(int fd) {
148#if defined (TARGET_FSYNC_FAULT)
149 if (FilenameCache.find(fd) != FilenameCache.end()
150 && FilenameCache[fd] == FaultFileName) {
151 FaultFileName = "";
152 errno = EIO;
153 return -1;
154 } else {
155 return fsync(fd);
156 }
157#else
158 return fsync(fd);
159#endif
160}
diff --git a/otafault/ota_io.h b/otafault/ota_io.h
new file mode 100644
index 00000000..641a5ae0
--- /dev/null
+++ b/otafault/ota_io.h
@@ -0,0 +1,49 @@
1/*
2 * Copyright (C) 2015 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/*
18 * Provide a series of proxy functions for basic file accessors.
19 * The behavior of these functions can be changed to return different
20 * errors under a variety of conditions.
21 */
22
23#ifndef _UPDATER_OTA_IO_H_
24#define _UPDATER_OTA_IO_H_
25
26#include <stdio.h>
27#include <sys/stat.h>
28
29int ota_open(const char* path, int oflags);
30
31int ota_open(const char* path, int oflags, mode_t mode);
32
33FILE* ota_fopen(const char* filename, const char* mode);
34
35int ota_close(int fd);
36
37int ota_fclose(FILE* fh);
38
39size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream);
40
41ssize_t ota_read(int fd, void* buf, size_t nbyte);
42
43size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream);
44
45ssize_t ota_write(int fd, const void* buf, size_t nbyte);
46
47int ota_fsync(int fd);
48
49#endif
diff --git a/otafault/test.cpp b/otafault/test.cpp
new file mode 100644
index 00000000..a0f73151
--- /dev/null
+++ b/otafault/test.cpp
@@ -0,0 +1,32 @@
1/*
2 * Copyright (C) 2015 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#include <errno.h>
18#include <fcntl.h>
19#include <stdio.h>
20
21#include "ota_io.h"
22
23int main(int argc, char **argv) {
24 int fd = open("testdata/test.file", O_RDWR);
25 char buf[8];
26 char *out = "321";
27 int readv = ota_read(fd, buf, 4);
28 printf("Read returned %d\n", readv);
29 int writev = ota_write(fd, out, 4);
30 printf("Write returned %d\n", writev);
31 return 0;
32}