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_middle[] = {
40 	0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
41 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42 };
43 
44 static const uint8_t write_flush[] = {
45 	0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
47 };
48 
49 static const uint8_t response[] = {
50 	0x08, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
52 };
53 
54 static const uint8_t mark_write_dirty_left[] = {
55 	0x07, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
56 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
57 };
58 
59 static const uint8_t mark_write_dirty_right[] = {
60 	0x07, 0x05, 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_all[] = {
65 	0x07, 0x06, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
66 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67 };
68 
69 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
70 const uint8_t flush_middle_data[] = { 0xaa, 0x00, 0xaa };
71 const uint8_t flush_ends_data[] = { 0x55, 0x00, 0x55 };
72 const uint8_t flush_all_data[] = { 0x01, 0x02, 0x03 };
73 
74 #define MEM_SIZE	sizeof(start_data)
75 #define ERASE_SIZE	1
76 #define N_WINDOWS	1
77 #define WINDOW_SIZE	sizeof(start_data)
78 
79 int main(void)
80 {
81 	struct mbox_context *ctx;
82 	uint8_t *map;
83 	int rc;
84 
85 	system_set_reserved_size(MEM_SIZE);
86 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
87 
88 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
89 	rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
90 	assert(rc == 0);
91 
92 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
93 	assert(rc == 1);
94 
95 	rc = mbox_command_dispatch(ctx, create_write_window,
96 			sizeof(create_write_window));
97 	assert(rc == 1);
98 
99 	/* { clean, dirty, clean } */
100 
101 	((uint8_t *)ctx->mem)[1] = 0x00;
102 
103 	rc = mbox_command_dispatch(ctx, mark_write_dirty_middle,
104 			sizeof(mark_write_dirty_middle));
105 	assert(rc == 1);
106 
107 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
108 	assert(rc == 1);
109 
110 	rc = mbox_cmp(ctx, response, sizeof(response));
111 	assert(rc == 0);
112 
113 	map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
114 			ctx->fds[MTD_FD].fd, 0);
115 	assert(map != MAP_FAILED);
116 
117 	rc = memcmp(flush_middle_data, map, sizeof(flush_middle_data));
118 	assert(rc == 0);
119 
120 	/* { dirty, clean, dirty } */
121 
122 	((uint8_t *)ctx->mem)[0] = 0x55;
123 
124 	rc = mbox_command_dispatch(ctx, mark_write_dirty_left,
125 			sizeof(mark_write_dirty_left));
126 	assert(rc == 1);
127 
128 	((uint8_t *)ctx->mem)[2] = 0x55;
129 
130 	rc = mbox_command_dispatch(ctx, mark_write_dirty_right,
131 			sizeof(mark_write_dirty_right));
132 	assert(rc == 1);
133 
134 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
135 	assert(rc == 1);
136 
137 	rc = mbox_cmp(ctx, response, sizeof(response));
138 	assert(rc == 0);
139 
140 	rc = memcmp(flush_ends_data, map, sizeof(flush_ends_data));
141 	assert(rc == 0);
142 
143 	/* { dirty, dirty, dirty } */
144 
145 	memcpy(ctx->mem, flush_all_data, sizeof(flush_all_data));
146 
147 	rc = mbox_command_dispatch(ctx, mark_write_dirty_all,
148 			sizeof(mark_write_dirty_all));
149 	assert(rc == 1);
150 
151 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
152 	assert(rc == 1);
153 
154 	rc = mbox_cmp(ctx, response, sizeof(response));
155 	assert(rc == 0);
156 
157 	rc = memcmp(flush_all_data, map, sizeof(flush_all_data));
158 	assert(rc == 0);
159 
160 	return rc;
161 };
162