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 
22 #include "mbox.h"
23 #include "mboxd_msg.h"
24 
25 #include "test/mbox.h"
26 #include "test/system.h"
27 
28 static const uint8_t get_info[] = {
29 	0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
30 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
31 };
32 
33 static const uint8_t create_read_window[] = {
34 	0x04, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
35 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
36 };
37 
38 static const uint8_t close_window_no_flag[] = {
39 	0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
41 };
42 
43 static const uint8_t response_no_flag[] = {
44 	0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
46 };
47 
48 static const uint8_t close_window_short_lifetime[] = {
49 	0x05, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
50 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
51 };
52 
53 static const uint8_t response_short_lifetime[] = {
54 	0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 	0x00, 0x00, 0x00, 0x00, 0x00, 0x01
56 };
57 
58 #define MEM_SIZE	3
59 #define ERASE_SIZE	1
60 #define N_WINDOWS	1
61 #define WINDOW_SIZE	3
62 
63 int setup(struct mbox_context *ctx)
64 {
65 	int rc;
66 
67 	rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
68 	assert(rc == 1);
69 
70 	rc = mbox_command_dispatch(ctx, create_read_window,
71 			sizeof(create_read_window));
72 	assert(rc == 1);
73 
74 	return rc;
75 }
76 
77 int no_flag(struct mbox_context *ctx)
78 {
79 	int rc;
80 
81 	setup(ctx);
82 
83 	rc = mbox_command_dispatch(ctx, close_window_no_flag,
84 			sizeof(close_window_no_flag));
85 	assert(rc == 1);
86 
87 	rc = mbox_cmp(ctx, response_no_flag, sizeof(response_no_flag));
88 	assert(rc == 0);
89 
90 	return rc;
91 }
92 
93 int short_lifetime(struct mbox_context *ctx)
94 {
95 	int rc;
96 
97 	setup(ctx);
98 
99 	rc = mbox_command_dispatch(ctx, close_window_short_lifetime,
100 			sizeof(close_window_short_lifetime));
101 	assert(rc == 1);
102 
103 	rc = mbox_cmp(ctx, response_short_lifetime,
104 			sizeof(response_short_lifetime));
105 	assert(rc == 0);
106 
107 	return rc;
108 }
109 
110 int main(void)
111 {
112 	struct mbox_context *ctx;
113 
114 	system_set_reserved_size(MEM_SIZE);
115 	system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
116 
117 	ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
118 
119 	no_flag(ctx);
120 
121 	short_lifetime(ctx);
122 
123 	return 0;
124 };
125