summaryrefslogtreecommitdiff
path: root/plugin/test_sql_service/test_sql_service.c
blob: 8b326c6fa46512e71076e7a1db47b575c5323672 (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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* Copyright (C) 2019, Alexey Botchkov and MariaDB Corporation

   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-1335 USA */


#define PLUGIN_VERSION 0x200

#include <mysql/plugin_audit.h>
#define STRING_WITH_LEN(X) (X), ((size_t) (sizeof(X) - 1))

/* Status variables for SHOW STATUS */
static long test_passed= 0;
static char *sql_text_local, *sql_text_global;
static char qwe_res[1024]= "";

static struct st_mysql_show_var test_sql_status[]=
{
  {"test_sql_service_passed", (char *)&test_passed, SHOW_LONG},
  {"test_sql_query_result", qwe_res, SHOW_CHAR},
  {0,0,0}
};

static my_bool do_test= 1;
static int run_test(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                    struct st_mysql_value *value);
static int run_sql_local(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                         struct st_mysql_value *value);
static int run_sql_global(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                          struct st_mysql_value *value);

static void noop_update(MYSQL_THD thd, struct st_mysql_sys_var *var,
                        void *var_ptr, const void *save);

static MYSQL_SYSVAR_BOOL(run_test, do_test,
                         PLUGIN_VAR_OPCMDARG,
                         "Perform the test now.",
                         run_test, NULL, 0);

static MYSQL_SYSVAR_STR(execute_sql_local, sql_text_local,
                        PLUGIN_VAR_OPCMDARG,
                        "Create the new local connection, execute SQL statement with it.",
                        run_sql_local, noop_update, 0);

static MYSQL_SYSVAR_STR(execute_sql_global, sql_text_global,
                        PLUGIN_VAR_OPCMDARG,
                        "Execute SQL statement using the global connection.",
                        run_sql_global, noop_update, 0);

static struct st_mysql_sys_var* test_sql_vars[]=
{
  MYSQL_SYSVAR(run_test),
  MYSQL_SYSVAR(execute_sql_local),
  MYSQL_SYSVAR(execute_sql_global),
  NULL
};

static MYSQL *global_mysql;


static int run_queries(MYSQL *mysql)
{
  MYSQL_RES *res;

  if (mysql_real_query(mysql,
        STRING_WITH_LEN("CREATE TABLE test.ts_table"
          " ( hash varbinary(512),"
          " time timestamp default current_time,"
          " primary key (hash), index tm (time) )")))
    return 1;

  if (mysql_real_query(mysql,
       STRING_WITH_LEN("INSERT INTO test.ts_table VALUES('1234567890', NULL)")))
    return 1;

  if (mysql_real_query(mysql, STRING_WITH_LEN("select * from test.ts_table")))
    return 1;

  if (!(res= mysql_store_result(mysql)))
    return 1;

  mysql_free_result(res);

  if (mysql_real_query(mysql, STRING_WITH_LEN("DROP TABLE test.ts_table")))
    return 1;

  return 0;
}


static int do_tests()
{
  MYSQL *mysql;
  int result= 1;

  mysql= mysql_init(NULL);
  if (mysql_real_connect_local(mysql) == NULL)
    return 1;

  if (run_queries(mysql))
    goto exit;

  if (run_queries(global_mysql))
    goto exit;

  result= 0;
exit:
  mysql_close(mysql);

  return result;
}
 

void auditing(MYSQL_THD thd, unsigned int event_class, const void *ev)
{
}


static int run_test(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                    struct st_mysql_value *value)
{
  *(my_bool*) save= 0;          /* Set value for sql_service_run_test */
  return (test_passed= (do_tests() == 0)) == 0;
}


static int run_sql(MYSQL *mysql, void *save, struct st_mysql_value *value)
{
  const char *str;
  int len= 0;
  MYSQL_RES *res;

  str= value->val_str(value, NULL, &len);

  if (mysql_real_query(mysql, str, len))
  {
    if (mysql_error(mysql)[0])
    {
      my_snprintf(qwe_res, sizeof(qwe_res), "Error %d returned. %s",
          mysql_errno(mysql), mysql_error(mysql));
      return 0;
    }

    return 1;
  }

  if ((res= mysql_store_result(mysql)))
  {
    my_snprintf(qwe_res, sizeof(qwe_res), "Query returned %lld rows.",
        mysql_num_rows(res));
    mysql_free_result(res);
  }
  else
  {
    if (mysql_error(mysql)[0])
    {
      my_snprintf(qwe_res, sizeof(qwe_res), "Error %d returned. %s",
          mysql_errno(mysql), mysql_error(mysql));
    }
    else
      my_snprintf(qwe_res, sizeof(qwe_res), "Query affected %lld rows.",
          mysql_affected_rows(mysql));
  }

  return 0;
}


static void noop_update(MYSQL_THD thd, struct st_mysql_sys_var *var,
                        void *var_ptr, const void *save)
{
  sql_text_local= sql_text_global= qwe_res;
}

static int run_sql_local(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                         struct st_mysql_value *value)
{
  MYSQL *mysql;
  int result= 1;

  mysql= mysql_init(NULL);
  if (mysql_real_connect_local(mysql) == NULL)
    return 1;

  if (run_sql(mysql, save, value))
    goto exit;

  result= 0;

exit:
  mysql_close(mysql);

  return result;
}


static int run_sql_global(MYSQL_THD thd, struct st_mysql_sys_var *var, void *save,
                          struct st_mysql_value *value)
{
  return run_sql(global_mysql, save, value);
}


static int init_done= 0;

static int test_sql_service_plugin_init(void *p)
{
  (void) p;
  global_mysql= mysql_init(NULL);

  if (!global_mysql ||
      mysql_real_connect_local(global_mysql) == NULL)
    return 1;

  init_done= 1;

  test_passed= (do_tests() == 0);

  return 0;
}


static int test_sql_service_plugin_deinit(void *p)
{
  (void) p;
  if (!init_done)
    return 0;

  mysql_close(global_mysql);

  return 0;
}


static struct st_mysql_audit maria_descriptor =
{
  MYSQL_AUDIT_INTERFACE_VERSION,
  NULL,
  auditing,
  { MYSQL_AUDIT_GENERAL_CLASSMASK |
    MYSQL_AUDIT_TABLE_CLASSMASK |
    MYSQL_AUDIT_CONNECTION_CLASSMASK }
};
maria_declare_plugin(test_sql_service)
{
  MYSQL_AUDIT_PLUGIN,
  &maria_descriptor,
  "TEST_SQL_SERVICE",
  "Alexey Botchkov (MariaDB Corporation)",
  "Test SQL service",
  PLUGIN_LICENSE_GPL,
  test_sql_service_plugin_init,
  test_sql_service_plugin_deinit,
  PLUGIN_VERSION,
  test_sql_status,
  test_sql_vars,
  NULL,
  MariaDB_PLUGIN_MATURITY_EXPERIMENTAL
}
maria_declare_plugin_end;