1 /*
2    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
16 */
17 
18 /** Unit test case for the function explain_filename(). */
19 
20 #include <tap.h>
21 #include <mysqld_error.h>
22 #include <sql_class.h>
23 #include <sql_table.h>
24 
25 #define BUFLEN 1000
26 char to[BUFLEN];
27 char from[BUFLEN];
28 
29 const char *error_messages[1000];
30 
setup()31 int setup()
32 {
33   system_charset_info    = &my_charset_utf8_bin;
34   my_default_lc_messages = &my_locale_en_US;
35 
36   /* Populate the necessary error messages */
37   error_messages[ER_DATABASE_NAME - ER_ERROR_FIRST]     = "Database";
38   error_messages[ER_TABLE_NAME - ER_ERROR_FIRST]        = "Table";
39   error_messages[ER_PARTITION_NAME - ER_ERROR_FIRST]    = "Partition";
40   error_messages[ER_SUBPARTITION_NAME - ER_ERROR_FIRST] = "Subpartition";
41   error_messages[ER_TEMPORARY_NAME - ER_ERROR_FIRST]    = "Temporary";
42   error_messages[ER_RENAMED_NAME - ER_ERROR_FIRST]      = "Renamed";
43 
44   my_default_lc_messages->errmsgs->errmsgs = error_messages;
45 
46   return 0;
47 }
48 
test_1(const char * in,const char * exp,enum_explain_filename_mode mode)49 void test_1(const char *in, const char *exp, enum_explain_filename_mode mode)
50 {
51   char out[BUFLEN];
52 
53   uint len1 = explain_filename(0, in, out, BUFLEN, mode);
54 
55   /* expected output and actual output must be same */
56   bool pass = (strcmp(exp, out) == 0);
57 
58   /* length returned by explain_filename is fine */
59   bool length = (len1 == strlen(exp));
60 
61   ok( (pass && length) , "(%d): %s => %s\n", mode, in, out);
62 }
63 
main()64 int main()
65 {
66   setup();
67   plan(22);
68 
69   test_1("test/t1.ibd",
70          "Database \"test\", Table \"t1.ibd\"",
71          EXPLAIN_ALL_VERBOSE);
72 
73   test_1("test/t1.ibd",
74          "\"test\".\"t1.ibd\"",
75          EXPLAIN_PARTITIONS_VERBOSE);
76 
77   test_1("test/t1.ibd",
78          "\"test\".\"t1.ibd\"",
79          EXPLAIN_PARTITIONS_AS_COMMENT);
80 
81   test_1("test/t1#TMP#",
82          "Database \"test\", Table \"t1#TMP#\"",
83          EXPLAIN_ALL_VERBOSE);
84 
85   test_1("test/#sql-2882.ibd",
86          "Database \"test\", Table \"#sql-2882.ibd\"",
87          EXPLAIN_ALL_VERBOSE);
88 
89   test_1("test/t1#REN#",
90          "Database \"test\", Table \"t1#REN#\"",
91          EXPLAIN_ALL_VERBOSE);
92 
93   test_1("test/t1@0023REN@0023",
94          "Database \"test\", Table \"t1#REN#\"",
95          EXPLAIN_ALL_VERBOSE);
96 
97   test_1("test/t1#p#p1",
98          "Database \"test\", Table \"t1\", Partition \"p1\"",
99          EXPLAIN_ALL_VERBOSE);
100 
101   test_1("test/t1#P#p1",
102          "\"test\".\"t1\" /* Partition \"p1\" */",
103          EXPLAIN_PARTITIONS_AS_COMMENT);
104 
105   test_1("test/t1#P#p1@00231",
106          "\"test\".\"t1\" /* Partition \"p1#1\" */",
107          EXPLAIN_PARTITIONS_AS_COMMENT);
108 
109   test_1("test/t1#P#p1#SP#sp1",
110          "\"test\".\"t1\" /* Partition \"p1\", Subpartition \"sp1\" */",
111          EXPLAIN_PARTITIONS_AS_COMMENT);
112 
113   test_1("test/t1#p1#SP#sp1",
114          "\"test\".\"t1#p1#SP#sp1\"",
115          EXPLAIN_PARTITIONS_AS_COMMENT);
116 
117   test_1("test/t1#p#p1@00232#SP#sp1@00231#REN#",
118          "\"test\".\"t1\" /* Renamed Partition \"p1#2\", Subpartition \"sp1#1\" */",
119          EXPLAIN_PARTITIONS_AS_COMMENT);
120 
121   test_1("test/t1#p#p1#SP#sp1#TMP#",
122          "\"test\".\"t1\" /* Temporary Partition \"p1\", Subpartition \"sp1\" */",
123          EXPLAIN_PARTITIONS_AS_COMMENT);
124 
125   test_1("test/#sql-t1#P#p1#SP#sp1#TMP#",
126          "\"test\".\"#sql-t1#P#p1#SP#sp1#TMP#\" /* Temporary Partition \"p1\", Subpartition \"sp1\" */",
127          EXPLAIN_PARTITIONS_AS_COMMENT);
128 
129   test_1("test/#sql-t1#P#p1#SP#sp1",
130          "\"test\".\"#sql-t1#P#p1#SP#sp1\" /* Partition \"p1\", Subpartition \"sp1\" */",
131          EXPLAIN_PARTITIONS_AS_COMMENT);
132 
133   test_1("test/#sqlx-33",
134          "\"test\".\"#sqlx-33\"",
135          EXPLAIN_PARTITIONS_AS_COMMENT);
136 
137   test_1("test/#mysql50#t",
138          "\"test\".\"#mysql50#t\"",
139          EXPLAIN_PARTITIONS_AS_COMMENT);
140 
141   test_1("#mysql50#t",
142          "\"#mysql50#t\"",
143          EXPLAIN_PARTITIONS_AS_COMMENT);
144 
145   test_1("@0023t",
146          "\"#t\"",
147          EXPLAIN_PARTITIONS_AS_COMMENT);
148 
149   test_1("test/t@0023",
150          "\"test\".\"t#\"",
151          EXPLAIN_PARTITIONS_AS_COMMENT);
152 
153   /*
154     If a character not allowed in my_charset_filename is encountered,
155     then it will not be converted to system_charset_info!
156   */
157   test_1("test/t@0023#",
158          "\"test\".\"t@0023#\"",
159          EXPLAIN_PARTITIONS_AS_COMMENT);
160 
161   return exit_status();
162 }
163 
164