MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
grant_cache.inc
1 ################### include/grant_cache.inc ####################
2 #
3 # Test grants with query cache
4 #
5 # Now normal protocol and ps-protocol produces the same result.
6 # (After splitting open and lock tables into different stages,
7 # to be able to prune locks of individual partitions.)
8 #
9 # Before this change, the below was true:
10 # Running this test with and without "--ps-protocol" produces different
11 # Qcache_not_cached results because of the following reason:
12 # In normal protocol, a SELECT failing due to insufficient privileges
13 # increments Qcache_not_cached, while in ps-protocol, no.
14 # In detail:
15 # - In normal protocol,
16 # the "access denied" errors on SELECT are issued at (stack trace):
17 # mysql_parse/mysql_execute_command/execute_sqlcom_select/handle_select/
18 # mysql_select/JOIN::prepare/setup_wild/insert_fields/
19 # check_grant_all_columns/my_error/my_message_sql, which then calls
20 # push_warning/query_cache_abort: at this moment,
21 # query_cache_store_query() has been called, so query exists in cache,
22 # so thd->net.query_cache_query!=NULL, so query_cache_abort() removes
23 # the query from cache, which causes a query_cache.refused++ (thus,
24 # a Qcache_not_cached++).
25 # - In ps-protocol,
26 # the error is issued at prepare time;
27 # for this mysql_test_select() is called, not execute_sqlcom_select()
28 # (and that also leads to JOIN::prepare/etc). Thus, as
29 # query_cache_store_query() has not been called,
30 # thd->net.query_cache_query==NULL, so query_cache_abort() does nothing:
31 # Qcache_not_cached is not incremented.
32 #
33 # A run of this tests with sp/cursor/view protocol does not make sense
34 # because these protocols serve totally different purposes than this test.
35 #
36 
37 --source include/add_anonymous_users.inc
38 
39 #
40 --disable_warnings
41 drop table if exists test.t1,mysqltest.t1,mysqltest.t2;
42 drop database if exists mysqltest;
43 --enable_warnings
44 
45 set GLOBAL query_cache_size=1355776;
46 
47 reset query cache;
48 flush status;
49 --echo ----- establish connection root -----
50 connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
51 connection root;
52 show grants for current_user;
53 show grants;
54 --disable_warnings
55 create database if not exists mysqltest;
56 --enable_warnings
57 
58 create table mysqltest.t1 (a int,b int,c int);
59 create table mysqltest.t2 (a int,b int,c int);
60 insert into mysqltest.t1 values (1,1,1),(2,2,2);
61 insert into mysqltest.t2 values (3,3,3);
62 create table test.t1 (a char (10));
63 insert into test.t1 values ("test.t1");
64 select * from t1;
65 --echo ----- establish connection root2 -----
66 connect (root2,localhost,root,,mysqltest,$MASTER_MYPORT,$MASTER_MYSOCK);
67 connection root2;
68 # put queries in cache
69 select * from t1;
70 select a from t1;
71 select c from t1;
72 select * from t2;
73 select * from mysqltest.t1,test.t1;
74 show status like "Qcache_queries_in_cache";
75 show status like "Qcache_hits%";
76 
77 # Create the test users
78 grant SELECT on mysqltest.* to mysqltest_1@localhost;
79 grant SELECT on mysqltest.t1 to mysqltest_2@localhost;
80 grant SELECT on test.t1 to mysqltest_2@localhost;
81 grant SELECT(a) on mysqltest.t1 to mysqltest_3@localhost;
82 
83 # The following queries should be fetched from cache
84 --echo ----- establish connection user1 (user=mysqltest_1) -----
85 connect (user1,localhost,mysqltest_1,,mysqltest,$MASTER_MYPORT,$MASTER_MYSOCK);
86 connection user1;
87 show grants for current_user();
88 show status like "Qcache_queries_in_cache";
89 show status like "Qcache_hits";
90 show status like "Qcache_not_cached";
91 select "user1";
92 show status like "Qcache_queries_in_cache";
93 show status like "Qcache_hits";
94 show status like "Qcache_not_cached";
95 select * from t1;
96 show status like "Qcache_queries_in_cache";
97 show status like "Qcache_hits";
98 show status like "Qcache_not_cached";
99 # The pre and end space are intentional
100  select a from t1 ;
101 show status like "Qcache_queries_in_cache";
102 show status like "Qcache_hits";
103 show status like "Qcache_not_cached";
104 select c from t1;
105 show status like "Qcache_queries_in_cache";
106 show status like "Qcache_hits";
107 show status like "Qcache_not_cached";
108 
109 
110 --echo ----- establish connection unkuser (user=unkuser) -----
111 # Don't use '' as user because it will pick Unix login
112 connect (unkuser,localhost,unkuser,,,$MASTER_MYPORT,$MASTER_MYSOCK);
113 connection unkuser;
114 show grants for current_user();
115 
116 # The following queries should be fetched from cache
117 --echo ----- establish connection user2 (user=mysqltest_2) -----
118 connect (user2,localhost,mysqltest_2,,mysqltest,$MASTER_MYPORT,$MASTER_MYSOCK);
119 connection user2;
120 select "user2";
121 select * from t1;
122 select a from t1;
123 select c from t1;
124 select * from mysqltest.t1,test.t1;
125 --replace_result 127.0.0.1 localhost
126 --error ER_TABLEACCESS_DENIED_ERROR
127 select * from t2;
128 show status like "Qcache_queries_in_cache";
129 show status like "Qcache_hits";
130 show status like "Qcache_not_cached";
131 
132 # The following queries should not be fetched from cache
133 --echo ----- establish connection user3 (user=mysqltest_3) -----
134 connect (user3,localhost,mysqltest_3,,mysqltest,$MASTER_MYPORT,$MASTER_MYSOCK);
135 connection user3;
136 select "user3";
137 --replace_result 127.0.0.1 localhost
138 --error ER_TABLEACCESS_DENIED_ERROR
139 select * from t1;
140 select a from t1;
141 --replace_result 127.0.0.1 localhost
142 --error ER_COLUMNACCESS_DENIED_ERROR
143 select c from t1;
144 --replace_result 127.0.0.1 localhost
145 --error ER_TABLEACCESS_DENIED_ERROR
146 select * from t2;
147 --replace_result 127.0.0.1 localhost
148 --error ER_COLUMNACCESS_DENIED_ERROR
149 select mysqltest.t1.c from test.t1,mysqltest.t1;
150 show status like "Qcache_queries_in_cache";
151 show status like "Qcache_hits";
152 show status like "Qcache_not_cached";
153 
154 # Connect without a database
155 --echo ----- establish connection user4 (user=mysqltest_1) -----
156 connect (user4,localhost,mysqltest_1,,*NO-ONE*,$MASTER_MYPORT,$MASTER_MYSOCK);
157 connection user4;
158 select "user4";
159 show grants;
160 --error ER_NO_DB_ERROR
161 select a from t1;
162 # The following query is not cached before (different database)
163 select * from mysqltest.t1,test.t1;
164 # Cache a query with 'no database'
165 select a from mysqltest.t1;
166 select a from mysqltest.t1;
167 show status like "Qcache_queries_in_cache";
168 show status like "Qcache_hits";
169 show status like "Qcache_not_cached";
170 
171 # Cleanup
172 
173 --echo ----- close connections -----
174 connection root;
175 disconnect root;
176 --source include/wait_until_disconnected.inc
177 connection root2;
178 disconnect root2;
179 --source include/wait_until_disconnected.inc
180 connection user1;
181 disconnect user1;
182 --source include/wait_until_disconnected.inc
183 connection user2;
184 disconnect user2;
185 --source include/wait_until_disconnected.inc
186 connection user3;
187 disconnect user3;
188 --source include/wait_until_disconnected.inc
189 connection user4;
190 disconnect user4;
191 --source include/wait_until_disconnected.inc
192 connection unkuser;
193 disconnect unkuser;
194 --source include/wait_until_disconnected.inc
195 --echo ----- switch to connection default -----
196 connection default;
197 
198 #
199 # A temporary 4.1 workaround to make this test pass if
200 # mysql was compiled with other than latin1 --with-charset=XXX.
201 # Without "set names binary" the below queries fail with
202 # "Illegal mix of collations" error.
203 # In 5.0 we will change grant tables to use NCHAR(N) instead
204 # of "CHAR(N) BINARY", and use cast-to-nchar: N'mysqltest_1'.
205 #
206 set names binary;
207 delete from mysql.user where user in ("mysqltest_1","mysqltest_2","mysqltest_3");
208 delete from mysql.db where user in ("mysqltest_1","mysqltest_2","mysqltest_3");
209 delete from mysql.tables_priv where user in ("mysqltest_1","mysqltest_2","mysqltest_3");
210 delete from mysql.columns_priv where user in ("mysqltest_1","mysqltest_2","mysqltest_3");
211 flush privileges;
212 drop table test.t1,mysqltest.t1,mysqltest.t2;
213 drop database mysqltest;
214 
215 set GLOBAL query_cache_size=default;
216 
217 --source include/delete_anonymous_users.inc