Debian Codegraph @

tests/linux_mtd.c

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2021 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 */
15
16 #include <stdlib.h>
17
18 #include "lifecycle.h"
19
20 #if CONFIG_LINUX_MTD == 1
21 struct linux_mtd_io_state {
22 char *fopen_path;
23 };
24
25 static FILE *linux_mtd_fopen(void *state, const char *pathname, const char *mode)
26 {
27 struct linux_mtd_io_state *io_state = state;
28
29 io_state->fopen_path = strdup(pathname);
30
31 return not_null();
32 }
33
34 static size_t linux_mtd_fread(void *state, void *buf, size_t size, size_t len, FILE *fp)
35 {
36 struct linux_mtd_fread_mock_entry {
37 const char *path;
38 const char *data;
39 };
40 const struct linux_mtd_fread_mock_entry fread_mock_map[] = {
41 { "/sys/class/mtd/mtd0//type", "nor" },
42 { "/sys/class/mtd/mtd0//name", "Device" },
43 { "/sys/class/mtd/mtd0//flags", "" },
44 { "/sys/class/mtd/mtd0//size", "1024" },
45 { "/sys/class/mtd/mtd0//erasesize", "512" },
46 { "/sys/class/mtd/mtd0//numeraseregions", "0" },
47 };
48
49 struct linux_mtd_io_state *io_state = state;
50 unsigned int i;
51
52 if (!io_state->fopen_path)
53 return 0;
54
55 for (i = 0; i < ARRAY_SIZE(fread_mock_map); i++) {
56 const struct linux_mtd_fread_mock_entry *entry = &fread_mock_map[i];
57
58 if (!strcmp(io_state->fopen_path, entry->path)) {
59 size_t data_len = min(size * len, strlen(entry->data));
60 memcpy(buf, entry->data, data_len);
61 return data_len;
62 }
63 }
64
65 return 0;
66 }
67
68 static int linux_mtd_fclose(void *state, FILE *fp)
69 {
70 struct linux_mtd_io_state *io_state = state;
71
72 free(io_state->fopen_path);
73
74 return 0;
75 }
76
77 void linux_mtd_probe_lifecycle_test_success(void **state)
78 {
79 struct linux_mtd_io_state linux_mtd_io_state = { NULL };
80 struct io_mock_fallback_open_state linux_mtd_fallback_open_state = {
81 .noc = 0,
82 .paths = { NULL },
83 };
84 const struct io_mock linux_mtd_io = {
85 .state = &linux_mtd_io_state,
86 .iom_fopen = linux_mtd_fopen,
87 .iom_fread = linux_mtd_fread,
88 .iom_fclose = linux_mtd_fclose,
89 .fallback_open_state = &linux_mtd_fallback_open_state,
90 };
91
92 const char *expected_matched_names[1] = {"Opaque flash chip"};
93 run_probe_v2_lifecycle(state, &linux_mtd_io, &programmer_linux_mtd, "", "Opaque flash chip",
94 expected_matched_names, 1);
95 }
96 #else
97 SKIP_TEST(linux_mtd_probe_lifecycle_test_success)
98 #endif /* CONFIG_LINUX_MTD */