forked from MahletNigusse/write-after-read-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_context-t.cc
140 lines (119 loc) · 5.64 KB
/
security_context-t.cc
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#include "my_config.h"
#include "m_string.h"
#include <gtest/gtest.h>
#include "test_utils.h"
#include "sql_class.h"
namespace security_context_unittest
{
/*
Testing accessor functions of string type data members of class
Security_context.
*/
TEST(Security_context, string_data_member)
{
Security_context sctx;
// Case 1: Initialize Security context and check the values set.
EXPECT_EQ(sctx.user().length, (size_t)0);
EXPECT_EQ(sctx.host().length, (size_t)0);
EXPECT_EQ(sctx.ip().length, (size_t)0);
EXPECT_EQ(sctx.external_user().length, (size_t)0);
EXPECT_EQ(strcmp(sctx.host_or_ip().str, "connecting host"), 0);
EXPECT_EQ(sctx.priv_user().length, (size_t)0);
EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
EXPECT_EQ(sctx.priv_host().length, (size_t)0);
// Case 2: Set the empty string to Securtiy context members and check values.
sctx.set_user_ptr("", 0);
sctx.set_host_ptr("", 0);
sctx.set_ip_ptr("", 0);
sctx.set_host_or_ip_ptr();
sctx.set_external_user_ptr("", 0);
sctx.assign_priv_user("", 0);
sctx.assign_proxy_user("", 0);
sctx.assign_priv_host("", 0);
EXPECT_EQ(sctx.user().length, (size_t)0);
EXPECT_EQ(sctx.host().length, (size_t)0);
EXPECT_EQ(sctx.ip().length, (size_t)0);
EXPECT_EQ(sctx.external_user().length, (size_t)0);
EXPECT_EQ(sctx.host_or_ip().length, (size_t)0);
EXPECT_EQ(sctx.priv_user().length, (size_t)0);
EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
EXPECT_EQ(sctx.priv_host().length, (size_t)0);
// using method assign_xxxx();
sctx.assign_user("", 0);
sctx.assign_host("", 0);
sctx.assign_ip("", 0);
sctx.assign_external_user("", 0);
EXPECT_EQ(sctx.user().length, (size_t)0);
EXPECT_EQ(sctx.host().length, (size_t)0);
EXPECT_EQ(sctx.ip().length, (size_t)0);
EXPECT_EQ(sctx.external_user().length, (size_t)0);
EXPECT_EQ(sctx.host_or_ip().length, (size_t)0);
EXPECT_EQ(sctx.priv_user().length, (size_t)0);
EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
EXPECT_EQ(sctx.priv_host().length, (size_t)0);
// Case 3: Set non-empty string to Securtiy context members and check values.
sctx.set_user_ptr(STRING_WITH_LEN("user_test"));
sctx.set_host_ptr(STRING_WITH_LEN("localhost"));
sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.1"));
sctx.set_host_or_ip_ptr();
sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test"));
sctx.assign_priv_user(STRING_WITH_LEN("priv_user"));
sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user"));
sctx.assign_priv_host(STRING_WITH_LEN("localhost"));
EXPECT_EQ(0, strcmp(sctx.user().str, "user_test"));
EXPECT_EQ(0, strcmp(sctx.host().str, "localhost"));
EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1"));
EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test"));
EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost"));
EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user"));
EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user"));
EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost"));
sctx.set_host_or_ip_ptr(sctx.ip().str, sctx.ip().length);
EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "127.0.0.1"));
// Case 4: Change members with non-empty string and check values.
sctx.set_user_ptr(STRING_WITH_LEN("user_test_1"));
sctx.set_host_ptr(STRING_WITH_LEN("localhost_1"));
sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.2"));
sctx.set_host_or_ip_ptr();
sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test_1"));
sctx.assign_priv_user(STRING_WITH_LEN("priv_user_1"));
sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user_1"));
sctx.assign_priv_host(STRING_WITH_LEN("localhost_1"));
EXPECT_EQ(0, strcmp(sctx.user().str, "user_test_1"));
EXPECT_EQ(0, strcmp(sctx.host().str, "localhost_1"));
EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.2"));
EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test_1"));
EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost_1"));
EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user_1"));
EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user_1"));
EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost_1"));
// Case 5: Change members with non-empty string members with copy option.
sctx.assign_user(STRING_WITH_LEN("user_test"));
sctx.assign_host(STRING_WITH_LEN("localhost"));
sctx.assign_ip(STRING_WITH_LEN("127.0.0.1"));
sctx.set_host_or_ip_ptr();
sctx.assign_external_user(STRING_WITH_LEN("ext_user_test"));
sctx.assign_priv_user(STRING_WITH_LEN("priv_user"));
sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user"));
sctx.assign_priv_host(STRING_WITH_LEN("localhost"));
EXPECT_EQ(0, strcmp(sctx.user().str, "user_test"));
EXPECT_EQ(0, strcmp(sctx.host().str, "localhost"));
EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1"));
EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test"));
EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost"));
EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user"));
EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user"));
EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost"));
}
}