aboutsummaryrefslogtreecommitdiff
path: root/test/pbdrv/send.cpp
blob: 7562ca4db134bb40598de0b477b2ebd225ad347f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <gtest/gtest.h>

#include "pb-mod.h"
#include "pb-msg.h"
#include "pb-send.h"

/**
 * Only one of each command type is tested due to the (de)serializers being the
 * same (i.e. you're not testing more by changing the command action).
 */

TEST(pb_send_rw, cmd_state_req) {
	pb_hook_mod_state_write(PB_GS_NOINIT);

	pb_buf_t buf = pb_send_state_req();
	ASSERT_NE(buf.data, nullptr);
	ASSERT_GE(buf.size, 0);

	pb_msg_t * msg = pb_msg_read(&buf);
	EXPECT_EQ(msg->type, PB_CMD_STATE);
	EXPECT_EQ(msg->action, PB_ACTION_REQ);
	EXPECT_EQ(msg->sender, PB_MOD_ADDR);
	EXPECT_NE(nullptr, msg->cmd);

	pb_cmd_state_t * cmd = (pb_cmd_state_t *) msg->cmd;
	EXPECT_EQ(cmd->state, pb_hook_mod_state_read());

	pb_buf_free(&buf);
	ASSERT_EQ(buf.data, nullptr);

	pb_msg_free(msg);
}

TEST(pb_send_rw, cmd_read_req) {
	pb_buf_t buf = pb_send_read_req(2);
	ASSERT_NE(buf.data, nullptr);
	ASSERT_GE(buf.size, 0);

	pb_msg_t * msg = pb_msg_read(&buf);

	EXPECT_EQ(msg->type, PB_CMD_PROP);
	EXPECT_EQ(msg->action, PB_ACTION_REQ);
	EXPECT_EQ(msg->sender, PB_MOD_ADDR);
	EXPECT_NE(nullptr, msg->cmd);

	pb_cmd_prop_t * cmd = (pb_cmd_prop_t *) msg->cmd;
	EXPECT_EQ(cmd->propid, 2);

	pb_buf_free(&buf);
	ASSERT_EQ(buf.data, nullptr);

	pb_msg_free(msg);
}

TEST(pb_send_rw, cmd_magic_req) {
	pb_buf_t buf = pb_send_magic_req();
	ASSERT_NE(buf.data, nullptr);
	ASSERT_GE(buf.size, 0);

	pb_msg_t * msg = pb_msg_read(&buf);

	EXPECT_EQ(msg->type, PB_CMD_MAGIC);
	EXPECT_EQ(msg->action, PB_ACTION_REQ);
	EXPECT_EQ(msg->sender, PB_MOD_ADDR);
	EXPECT_NE(msg->cmd, nullptr);

	pb_cmd_magic_t * cmd = (pb_cmd_magic_t *) msg->cmd;
	EXPECT_EQ(cmd->_magic_size, sizeof(pb_cmd_magic_req));
	EXPECT_EQ(0, memcmp(pb_cmd_magic_req, cmd->magic, cmd->_magic_size));

	pb_buf_free(&buf);
	ASSERT_EQ(buf.data, nullptr);

	pb_msg_free(msg);
}