Debian Codegraph @

tests/io_mock.h

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (c) 2021 Nico Huber <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifndef _IO_MOCK_H_
32 #define _IO_MOCK_H_
33
34 #include <include/test.h>
35
36 /* Required for `FILE *` */
37 #include <stdio.h>
38
39 #include <stdint.h>
40
41 /* Required for struct timeval and mode_t */
42 #include <sys/types.h>
43 #include <sys/time.h>
44
45 #include "usb_unittests.h"
46
47 /* Address value needs fit into uint8_t. */
48 #define USB_DEVICE_ADDRESS 19
49
50 /* Define struct pci_dev to avoid dependency on pci.h */
51 struct pci_dev {
52 char padding[18];
53 unsigned int device_id;
54 };
55
56 /* Linux I2C interface constants, avoiding linux/i2c-dev.h */
57 #define I2C_SLAVE 0x0703
58
59 /* Always return success for tests. */
60 #define S_ISREG(x) 0
61
62 /* Maximum number of open calls to mock. This number is arbitrary. */
63 #define MAX_MOCK_OPEN 4
64
65 struct io_mock_fallback_open_state {
66 unsigned int noc;
67 const char *paths[MAX_MOCK_OPEN];
68 int flags[MAX_MOCK_OPEN];
69 };
70
71 struct io_mock {
72 void *state;
73
74 /* Port I/O */
75 void (*outb)(void *state, unsigned char value, unsigned short port);
76 unsigned char (*inb)(void *state, unsigned short port);
77
78 void (*outw)(void *state, unsigned short value, unsigned short port);
79 unsigned short (*inw)(void *state, unsigned short port);
80
81 void (*outl)(void *state, unsigned int value, unsigned short port);
82 unsigned int (*inl)(void *state, unsigned short port);
83
84 /* USB I/O */
85 int (*libusb_init)(void *state, libusb_context **ctx);
86 int (*libusb_control_transfer)(void *state,
87 libusb_device_handle *devh,
88 uint8_t bmRequestType,
89 uint8_t bRequest,
90 uint16_t wValue,
91 uint16_t wIndex,
92 unsigned char *data,
93 uint16_t wLength,
94 unsigned int timeout);
95 ssize_t (*libusb_get_device_list)(void *state, libusb_context *, libusb_device ***list);
96 void (*libusb_free_device_list)(void *state, libusb_device **list, int unref_devices);
97 int (*libusb_get_device_descriptor)(void *state, libusb_device *, struct libusb_device_descriptor *);
98 int (*libusb_get_config_descriptor)(void *state,
99 libusb_device *,
100 uint8_t config_index,
101 struct libusb_config_descriptor **);
102 void (*libusb_free_config_descriptor)(void *state, struct libusb_config_descriptor *);
103 struct libusb_transfer* (*libusb_alloc_transfer)(void *state, int iso_packets);
104 int (*libusb_submit_transfer)(void *state, struct libusb_transfer *transfer);
105 void (*libusb_free_transfer)(void *state, struct libusb_transfer *transfer);
106 int (*libusb_handle_events_timeout)(void *state, libusb_context *ctx, struct timeval *tv);
107
108 /* POSIX File I/O */
109 int (*iom_open)(void *state, const char *pathname, int flags, mode_t mode);
110 int (*iom_fcntl)(void *state, int fd, unsigned long cmd, va_list args);
111 int (*iom_ioctl)(void *state, int fd, unsigned long request, va_list args);
112 int (*iom_read)(void *state, int fd, void *buf, size_t sz);
113 int (*iom_write)(void *state, int fd, const void *buf, size_t sz);
114
115 /* Standard I/O */
116 FILE* (*iom_fopen)(void *state, const char *pathname, const char *mode);
117 char* (*iom_fgets)(void *state, char *buf, int len, FILE *fp);
118 size_t (*iom_fread)(void *state, void *buf, size_t size, size_t len, FILE *fp);
119 size_t (*iom_fwrite)(void *state, const void *buf, size_t size, size_t len, FILE *fp);
120 int (*iom_fprintf)(void *state, FILE *fp, const char *fmt, va_list args);
121 int (*iom_fclose)(void *state, FILE *fp);
122 FILE *(*iom_fdopen)(void *state, int fd, const char *mode);
123
124 /*
125 * An alternative to custom open mock. A test can either register its
126 * own mock open function or fallback_open_state.
127 */
128 struct io_mock_fallback_open_state *fallback_open_state;
129 };
130
131 void io_mock_register(const struct io_mock *io);
132
133 const struct io_mock *get_io(void);
134
135 #endif