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