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_erased[] = {
40 	0x0a, 0x02, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
41 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42 };
43 
44 static const uint8_t response[] = {
45 	0x0a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
47 };
48 
49 static const uint8_t write_flush[] = {
50 	0x08, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
52 };
53 
54 const uint8_t start_data[] = { 0xaa, 0x55, 0xaa };
55 const uint8_t finish_data[] = { 0xaa, 0xff, 0xaa };
56 
57 #define MEM_SIZE	sizeof(start_data)
58 #define ERASE_SIZE	1
59 #define N_WINDOWS	1
60 #define WINDOW_SIZE	sizeof(start_data)
61 
62 int main(void)
63 {
64 	struct mbox_context *ctx;
65 	uint8_t *map;
66 	int rc;
67 
68 	system_set_reserved_size(MEM_SIZE);
69 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
70 
71 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
72 	rc = mbox_set_mtd_data(ctx, start_data, sizeof(start_data));
73 	assert(rc == 0);
74 
75 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
76 	assert(rc == 1);
77 
78 	rc = mbox_command_dispatch(ctx, create_write_window,
79 			sizeof(create_write_window));
80 	assert(rc == 1);
81 
82 	rc = mbox_command_dispatch(ctx, mark_write_erased,
83 			sizeof(mark_write_erased));
84 	assert(rc == 1);
85 
86 	rc = mbox_cmp(ctx, response, sizeof(response));
87 	assert(rc == 0);
88 
89 	rc = memcmp(ctx->mem, finish_data, sizeof(finish_data));
90 	assert(rc == 0);
91 
92 	map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE,
93 			ctx->fds[MTD_FD].fd, 0);
94 	assert(map != MAP_FAILED);
95 
96 	rc = memcmp(start_data, map, sizeof(start_data));
97 	assert(rc == 0);
98 
99 	rc = mbox_command_dispatch(ctx, write_flush, sizeof(write_flush));
100 	assert(rc == 1);
101 
102 	rc = memcmp(finish_data, map, sizeof(finish_data));
103 	assert(rc == 0);
104 
105 	return rc;
106 };
107