blob: 44bbbdb8aee37f3e313575269cafe21bcdb96a96 [file] [log] [blame]
Nikolai Artemiev702c58a2020-09-16 14:12:22 +10001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2020 Google LLC
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100015
16#include <include/test.h>
Anastasia Klimchuk21e22ba2021-05-10 10:19:25 +100017#include "io_mock.h"
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100018#include "tests.h"
19
20#include <stdio.h>
Anastasia Klimchuk98534e72021-04-23 15:47:03 +100021#include <stdint.h>
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100022
23/* redefinitions/wrapping */
Anastasia Klimchukab9d1462021-04-19 14:47:35 +100024#define LOG_ME printf("%s is called\n", __func__)
25
Anastasia Klimchuk21e22ba2021-05-10 10:19:25 +100026static const struct io_mock *current_io = NULL;
27
28void io_mock_register(const struct io_mock *io)
29{
30 current_io = io;
31}
32
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100033void __wrap_physunmap(void *virt_addr, size_t len)
34{
Anastasia Klimchukab9d1462021-04-19 14:47:35 +100035 LOG_ME;
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100036}
Anastasia Klimchukab9d1462021-04-19 14:47:35 +100037
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100038void *__wrap_physmap(const char *descr, uintptr_t phys_addr, size_t len)
39{
Anastasia Klimchukab9d1462021-04-19 14:47:35 +100040 LOG_ME;
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +100041 return NULL;
42}
43
Anastasia Klimchuk98534e72021-04-23 15:47:03 +100044void __wrap_sio_write(uint16_t port, uint8_t reg, uint8_t data)
45{
46 LOG_ME;
47}
48
49uint8_t __wrap_sio_read(uint16_t port, uint8_t reg)
50{
51 LOG_ME;
52 return (uint8_t)mock();
53}
54
55int __wrap_open(const char *pathname, int flags)
56{
57 LOG_ME;
58 return 2021;
59}
60
61int __wrap_open64(const char *pathname, int flags)
62{
63 LOG_ME;
64 return 2021;
65}
66
67int __wrap_ioctl(int fd, unsigned long int request, ...)
68{
69 LOG_ME;
70 return 2021;
71}
72
73FILE *__wrap_fopen(const char *pathname, const char *mode)
74{
75 LOG_ME;
76 return NULL;
77}
78
79FILE *__wrap_fopen64(const char *pathname, const char *mode)
80{
81 LOG_ME;
82 return NULL;
83}
84
Anastasia Klimchuk21e22ba2021-05-10 10:19:25 +100085int __wrap_rget_io_perms(void)
86{
87 LOG_ME;
88 return 0;
89}
90
91void __wrap_test_outb(unsigned char value, unsigned short port) {
92 /* LOG_ME; */
93 if (current_io && current_io->outb)
94 current_io->outb(current_io->state, value, port);
95}
96
97unsigned char __wrap_test_inb(unsigned short port) {
98 /* LOG_ME; */
99 if (current_io && current_io->inb)
100 return current_io->inb(current_io->state, port);
101 return 0;
102}
103
104void __wrap_test_outw(unsigned short value, unsigned short port) {
105 /* LOG_ME; */
106 if (current_io && current_io->outw)
107 current_io->outw(current_io->state, value, port);
108}
109
110unsigned short __wrap_test_inw(unsigned short port) {
111 /* LOG_ME; */
112 if (current_io && current_io->inw)
113 return current_io->inw(current_io->state, port);
114 return 0;
115}
116
117void __wrap_test_outl(unsigned int value, unsigned short port) {
118 /* LOG_ME; */
119 if (current_io && current_io->outl)
120 current_io->outl(current_io->state, value, port);
121}
122
123unsigned int __wrap_test_inl(unsigned short port) {
124 /* LOG_ME; */
125 if (current_io && current_io->inl)
126 return current_io->inl(current_io->state, port);
127 return 0;
128}
129
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +1000130int main(void)
131{
132 int ret = 0;
133
Anastasia Klimchukab9d1462021-04-19 14:47:35 +1000134 cmocka_set_message_output(CM_OUTPUT_STDOUT);
135
Edward O'Callaghan3cc70c22020-05-22 18:42:11 +1000136 const struct CMUnitTest helpers_tests[] = {
137 cmocka_unit_test(address_to_bits_test_success),
138 cmocka_unit_test(bitcount_test_success),
139 cmocka_unit_test(minmax_test_success),
140 cmocka_unit_test(strcat_realloc_test_success),
141 cmocka_unit_test(tolower_string_test_success),
142 cmocka_unit_test(reverse_byte_test_success),
143 cmocka_unit_test(reverse_bytes_test_success),
144 };
145 ret |= cmocka_run_group_tests_name("helpers.c tests", helpers_tests, NULL, NULL);
146
Edward O'Callaghan629b8f02020-05-22 16:46:52 +1000147 const struct CMUnitTest flashrom_tests[] = {
148 cmocka_unit_test(flashbuses_to_text_test_success),
149 };
150 ret |= cmocka_run_group_tests_name("flashrom.c tests", flashrom_tests, NULL, NULL);
151
Edward O'Callaghan41f48c72020-05-22 16:46:26 +1000152 const struct CMUnitTest spi25_tests[] = {
153 cmocka_unit_test(spi_write_enable_test_success),
154 cmocka_unit_test(spi_write_disable_test_success),
155 cmocka_unit_test(probe_spi_rdid_test_success),
156 cmocka_unit_test(probe_spi_rdid4_test_success),
157 cmocka_unit_test(probe_spi_rems_test_success),
158 cmocka_unit_test(probe_spi_res1_test_success),
159 cmocka_unit_test(probe_spi_res2_test_success),
160 cmocka_unit_test(probe_spi_res3_test_success),
161 cmocka_unit_test(probe_spi_at25f_test_success),
Edward O'Callaghan76d24452020-05-22 19:09:49 +1000162 cmocka_unit_test(probe_spi_st95_test_success), /* spi95.c */
Edward O'Callaghan41f48c72020-05-22 16:46:26 +1000163 };
164 ret |= cmocka_run_group_tests_name("spi25.c tests", spi25_tests, NULL, NULL);
165
Anastasia Klimchukcffaac12021-04-21 07:58:30 +1000166 const struct CMUnitTest init_shutdown_tests[] = {
167 cmocka_unit_test(dummy_init_and_shutdown_test_success),
Anastasia Klimchuk21e22ba2021-05-10 10:19:25 +1000168 cmocka_unit_test(mec1308_init_and_shutdown_test_success),
169 cmocka_unit_test(ene_lpc_init_and_shutdown_test_success),
Anastasia Klimchuk98534e72021-04-23 15:47:03 +1000170 cmocka_unit_test(linux_spi_init_and_shutdown_test_success),
Anastasia Klimchukcffaac12021-04-21 07:58:30 +1000171 };
172 ret |= cmocka_run_group_tests_name("init_shutdown.c tests", init_shutdown_tests, NULL, NULL);
173
Edward O'Callaghan46f2d4e2020-05-20 20:10:20 +1000174 return ret;
175}