1 /*
2  * MBox Daemon Test File
3  *
4  * Copyright 2017 IBM
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #include <assert.h>
21 #include <sys/mman.h>
22 
23 #include "mbox.h"
24 #include "mboxd_msg.h"
25 
26 #include "test/mbox.h"
27 #include "test/system.h"
28 
29 static const uint8_t get_info[] = {
30 	0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
31 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32 };
33 
34 static const uint8_t create_write_window[] = {
35 	0x06, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
36 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
37 };
38 
39 static const uint8_t mark_write_dirty_left[] = {
40 	0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
41 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42 };
43 
44 static const uint8_t mark_write_dirty_right[] = {
45 	0x07, 0x03, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
46 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
47 };
48 
49 static const uint8_t mark_write_erase_middle[] = {
50 	0x0a, 0x04, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52 };
53 
54 static const uint8_t mark_write_erase_left[] = {
55 	0x0a, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
56 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
57 };
58 
59 static const uint8_t mark_write_erase_right[] = {
60 	0x0a, 0x06, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62 };
63 
64 static const uint8_t mark_write_dirty_middle[] = {
65 	0x07, 0x07, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
66 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67 };
68 
69 static const uint8_t write_flush[] = {
70 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72 };
73 
74 static const uint8_t flush_response[] = {
75 	0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
77 };
78 
79 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
80 const uint8_t flush_dirty_erased_dirty_data[] = { 0x55, 0xff, 0x55 };
81 const uint8_t flush_erased_dirty_erased_data[] = { 0xff, 0x55, 0xff };
82 
83 #define MEM_SIZE	sizeof(start_data)
84 #define ERASE_SIZE	1
85 #define N_WINDOWS	1
86 #define WINDOW_SIZE	sizeof(start_data)
87 
88 int main(void)
89 {
90 	struct mbox_context *ctx;
91 	uint8_t *map;
92 	int rc;
93 
94 	system_set_reserved_size(MEM_SIZE);
95 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
96 
97 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
98 	rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
99 	assert(rc == 0);
100 
101 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
102 	assert(rc == 1);
103 
104 	rc = mbox_command_dispatch(ctx, create_write_window,
105 			sizeof(create_write_window));
106 	assert(rc == 1);
107 
108 	/* { dirty, erased, dirty } */
109 
110 	((uint8_t *)ctx->mem)[0] = 0x55;
111 
112 	rc = mbox_command_dispatch(ctx, mark_write_dirty_left,
113 			sizeof(mark_write_dirty_left));
114 	assert(rc == 1);
115 
116 	((uint8_t *)ctx->mem)[2] = 0x55;
117 
118 	rc = mbox_command_dispatch(ctx, mark_write_dirty_right,
119 			sizeof(mark_write_dirty_right));
120 	assert(rc == 1);
121 
122 	rc = mbox_command_dispatch(ctx, mark_write_erase_middle,
123 			sizeof(mark_write_erase_middle));
124 	assert(rc == 1);
125 
126 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
127 	assert(rc == 1);
128 
129 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
130 	assert(rc == 0);
131 
132 	map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
133 			ctx->fds[MTD_FD].fd, 0);
134 	assert(map != MAP_FAILED);
135 
136 	rc = memcmp(flush_dirty_erased_dirty_data, map,
137 			sizeof(flush_dirty_erased_dirty_data));
138 	assert(rc == 0);
139 
140 	/* { erased, dirty, erased } */
141 
142 	((uint8_t *)ctx->mem)[1] = 0x55;
143 
144 	rc = mbox_command_dispatch(ctx, mark_write_dirty_middle,
145 			sizeof(mark_write_dirty_middle));
146 	assert(rc == 1);
147 
148 	rc = mbox_command_dispatch(ctx, mark_write_erase_left,
149 			sizeof(mark_write_erase_left));
150 	assert(rc == 1);
151 
152 	rc = mbox_command_dispatch(ctx, mark_write_erase_right,
153 			sizeof(mark_write_erase_right));
154 	assert(rc == 1);
155 
156 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
157 	assert(rc == 1);
158 
159 	rc = mbox_cmp(ctx, flush_response, sizeof(flush_response));
160 	assert(rc == 0);
161 
162 	rc = memcmp(flush_erased_dirty_erased_data, map,
163 			sizeof(flush_erased_dirty_erased_data));
164 	assert(rc == 0);
165 
166 	return rc;
167 }
168