1 /*
2  * Copyright (C) 2023 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 <assert.h>
18 #include <lk/list.h>
19 #include <malloc.h>
20 #include <stdio.h>
21 
22 #include "fs.h"
23 
24 #include "error_reporting_mock.h"
25 
26 static struct list_node error_reports = LIST_INITIAL_VALUE(error_reports);
27 
mock_error_report_next(void)28 struct mock_storage_error_report* mock_error_report_next(void) {
29     return list_remove_head_type(&error_reports,
30                                  struct mock_storage_error_report, node);
31 }
32 
mock_error_report_clear(void)33 void mock_error_report_clear(void) {
34     struct mock_storage_error_report* report;
35     while (!list_is_empty(&error_reports)) {
36         report = list_remove_head_type(&error_reports,
37                                        struct mock_storage_error_report, node);
38         assert(report);
39         free(report);
40     }
41 }
42 
expect_errors(enum trusty_storage_error_type type,int count)43 void expect_errors(enum trusty_storage_error_type type, int count) {
44     int i;
45     struct mock_storage_error_report* err_report;
46 
47     for (i = 0; i < count; i++) {
48         err_report = mock_error_report_next();
49         assert(err_report && err_report->type == type);
50         free(err_report);
51     }
52     assert(!mock_error_report_next());
53 }
54 
do_error_report(enum trusty_storage_error_type type,const char * fs_name,enum trusty_block_type block_type)55 void do_error_report(enum trusty_storage_error_type type,
56                      const char* fs_name,
57                      enum trusty_block_type block_type) {
58     struct mock_storage_error_report* report = malloc(sizeof(*report));
59     assert(report);
60     report->type = type;
61     report->fs_name = fs_name;
62     report->block_type = block_type;
63 
64     list_add_tail(&error_reports, &report->node);
65 }
66