summaryrefslogtreecommitdiffstats
path: root/libmariadb/unittest/libmariadb/basic-t.c
blob: 40a44962b9a593efb846f6138edb33377ea86194 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.

The MySQL Connector/C is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
MySQL Connectors. There are special exceptions to the terms and
conditions of the GPLv2 as it is applied to this software, see the
FLOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.

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 St, Fifth Floor, Boston, MA 02110-1301  USA
*/


/**
  Some basic tests of the client API.
*/

#include "my_test.h"
#include "ma_common.h"

static int test_conc75(MYSQL *my)
{
  int rc;
  MYSQL *mysql;
  int i;
  my_bool reconnect= 1;

  SKIP_SKYSQL;
  SKIP_MAXSCALE;
  SKIP_XPAND;

  mysql= mysql_init(NULL);

  mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
  my_test_connect(mysql, hostname, username, password, schema, port, socketname, 0| CLIENT_MULTI_RESULTS | CLIENT_REMEMBER_OPTIONS);

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS a");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE TABLE a (a varchar(200))");
  check_mysql_rc(rc, mysql);

  rc= mysql_set_character_set(mysql, "utf8");
  check_mysql_rc(rc, mysql);

  for (i=0; i < 10; i++)
  {
    ulong thread_id= mysql_thread_id(mysql);
    /* force reconnect */
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
    diag("killing connection");
    mysql_kill(my, thread_id);
    mysql_ping(mysql);
    rc= mysql_query(mysql, "load data local infile './nonexistingfile.csv' into table a (`a`)");
    FAIL_IF(!test(mysql->options.client_flag | CLIENT_LOCAL_FILES), "client_flags not correct");
    diag("thread1: %ld %ld", thread_id, mysql_thread_id(mysql));
    FAIL_IF(thread_id == mysql_thread_id(mysql), "new thread id expected");
    //diag("cs: %s", mysql->charset->csname);
    //FAIL_IF(strcmp(mysql->charset->csname, "utf8"), "wrong character set");
  }
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS a");
  check_mysql_rc(rc, mysql);
  mysql_close(mysql);
  return OK;
}


static int test_conc74(MYSQL *unused __attribute__((unused)))
{
  int rc;
  MYSQL *mysql;

  mysql= mysql_init(NULL);


  if (!my_test_connect(mysql, hostname, username, password, schema, port, socketname, 0| CLIENT_MULTI_RESULTS | CLIENT_REMEMBER_OPTIONS))
  {
    diag("Error: %s", mysql_error(mysql));
    mysql_close(mysql);
    return FAIL;
  }

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS a");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE TABLE a (a varchar(200))");
  check_mysql_rc(rc, mysql);

  mysql->options.client_flag&= ~CLIENT_LOCAL_FILES;

  rc= mysql_query(mysql, "load data local infile './nonexistingfile.csv' into table a (`a`)");
  FAIL_IF(!rc, "Error expected");

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS a");
  check_mysql_rc(rc, mysql);

  mysql_close(mysql);
  return OK;
}


static int test_conc71(MYSQL *my)
{
  int rc;
  MYSQL *mysql;
  
  /* uncomment if you want to test manually */
  return SKIP;

  mysql= mysql_init(NULL);


  mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8");
  mysql_options(mysql, MYSQL_OPT_COMPRESS, 0);
  mysql_options(mysql, MYSQL_INIT_COMMAND, "/*!40101 SET SQL_MODE='' */");
  mysql_options(mysql, MYSQL_INIT_COMMAND, "/*!40101 set @@session.wait_timeout=28800 */");

  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                         port, socketname, 0), mysql_error(my));

  diag("kill server");

  rc= mysql_query(mysql, "SELECT 'foo' FROM DUAL");
  check_mysql_rc(rc, mysql);

  mysql_close(mysql);
  return OK;
}

static int test_conc70(MYSQL *my)
{
  int rc;
  MYSQL_RES *res;
  MYSQL_ROW row;
  MYSQL *mysql;

  SKIP_CONNECTION_HANDLER;

  SKIP_SKYSQL;
  SKIP_MAXSCALE;

  mysql= mysql_init(NULL);

  rc= mysql_query(my, "SET @a:=@@max_allowed_packet");
  check_mysql_rc(rc, my);

  mysql_query(my, "SET global max_allowed_packet=1024*1024*22");
  check_mysql_rc(rc, my);

  mysql_options(mysql, MYSQL_OPT_COMPRESS, (void *)1);
  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                         port, socketname, 0), mysql_error(my));

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE TABLE t1 (a LONGBLOB) engine=MyISAM");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (REPEAT('A', 1024 * 1024 * 20))");
  check_mysql_rc(rc, mysql);

  if (mysql_warning_count(mysql))
  {
    diag("server doesn't accept package size");
    return SKIP;
  }


  rc= mysql_query(mysql, "SELECT a FROM t1");
  check_mysql_rc(rc, mysql);

  if (!(res= mysql_store_result(mysql)))
  {
    diag("Error: %s", mysql_error(mysql));
    return FAIL;
  }

  row= mysql_fetch_row(res);
  diag("Length: %ld", (long)strlen(row[0]));
  FAIL_IF(strlen(row[0]) != 1024 * 1024 * 20, "Wrong length");

  mysql_free_result(res);
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  check_mysql_rc(rc, mysql);

  mysql_close(mysql);

  rc= mysql_query(my, "SET global max_allowed_packet=@a");
  check_mysql_rc(rc, my);

  return OK;
}

static int test_conc68(MYSQL *my)
{
  int rc;
  MYSQL_RES *res;
  MYSQL_ROW row;
  MYSQL *mysql;

  SKIP_CONNECTION_HANDLER;
  SKIP_SKYSQL;
  SKIP_MAXSCALE;
  
  mysql= mysql_init(NULL);

  rc= mysql_query(my, "SET @a:=@@max_allowed_packet");
  check_mysql_rc(rc, my);

  mysql_query(my, "SET global max_allowed_packet=1024*1024*22");

  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                         port, socketname, 0), mysql_error(my));

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE TABLE t1 (a LONGBLOB) ENGINE=MyISAM");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (REPEAT('A', 1024 * 1024 * 20))");
  check_mysql_rc(rc, mysql); 
  if (mysql_warning_count(mysql))
  {
    diag("server doesn't accept package size");
    return SKIP;
  }

  rc= mysql_query(mysql, "SELECT a FROM t1");
  check_mysql_rc(rc, mysql);

  if (!(res= mysql_store_result(mysql)))
  {
    diag("Error: %s", mysql_error(mysql));
    return FAIL;
  }

  row= mysql_fetch_row(res);
  diag("Length: %ld", (long)strlen(row[0]));
  FAIL_IF(strlen(row[0]) != 1024 * 1024 * 20, "Wrong length");

  mysql_free_result(res);
  mysql_close(mysql);

  rc= mysql_query(my, "SET global max_allowed_packet=@a");
  check_mysql_rc(rc, my);

  return OK;
}


static int basic_connect(MYSQL *unused __attribute__((unused)))
{
  MYSQL_ROW row;
  MYSQL_RES *res;
  MYSQL_FIELD *field;
  int rc;
  MYSQL *my;

  my= mysql_init(NULL);
  FAIL_IF(!my, "mysql_init() failed");

  FAIL_IF(!my_test_connect(my, hostname, username, password, schema,
                         port, socketname, 0), mysql_error(my));

  rc= mysql_query(my, "SELECT @@version");
  check_mysql_rc(rc, my);

  res= mysql_store_result(my);
  FAIL_IF(!res, mysql_error(my));
  field= mysql_fetch_fields(res);
  FAIL_IF(!field, "couldn't fetch field");
  while ((row= mysql_fetch_row(res)) != NULL)
  {
    FAIL_IF(mysql_num_fields(res) != 1, "Got the wrong number of fields");
  }
  FAIL_IF(mysql_errno(my), mysql_error(my));

  mysql_free_result(res);
  mysql_close(my);

  return OK;
}


static int use_utf8(MYSQL *my)
{
  MYSQL_ROW row;
  MYSQL_RES *res;
  int rc;

  /* Make sure that we actually ended up with utf8. */
  rc= mysql_query(my, "SELECT @@character_set_connection");
  check_mysql_rc(rc, my);

  res= mysql_store_result(my);
  FAIL_IF(!res, mysql_error(my));

  while ((row= mysql_fetch_row(res)) != NULL)
  {
    FAIL_IF(strncmp(row[0], "utf8", 4), "wrong character set");
  }
  FAIL_IF(mysql_errno(my), mysql_error(my));
  mysql_free_result(res);

  return OK;
}

int client_query(MYSQL *mysql) {
  int rc;

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "CREATE TABLE t1("
                         "id int primary key auto_increment, "
                         "name varchar(20))");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "CREATE TABLE t1(id int, name varchar(20))");
  FAIL_IF(!rc, "Error expected");
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('mysql')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('monty')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('venu')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "INSERT INTO t1(name) VALUES('deleted')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "UPDATE t1 SET name= 'updated' "
                          "WHERE name= 'deleted'");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "UPDATE t1 SET id= 3 WHERE name= 'updated'");
  FAIL_IF(!rc, "Error expected");
  rc= mysql_query(mysql, "drop table t1");
  check_mysql_rc(rc, mysql);

  return OK;
}

static int test_bug12001(MYSQL *mysql)
{
  MYSQL_RES *result;
  const char *query= "DROP TABLE IF EXISTS test_table;"
                     "CREATE TABLE test_table(id INT);"
                     "INSERT INTO test_table VALUES(10);"
                     "UPDATE test_table SET id=20 WHERE id=10;"
                     "SELECT * FROM test_table;"
                     "INSERT INTO non_existent_table VALUES(11);";
  int rc, res;


  rc= mysql_query(mysql, query);
  check_mysql_rc(rc, mysql);

  do
  {
    if (mysql_field_count(mysql) &&
        (result= mysql_use_result(mysql)))
    {
      mysql_free_result(result);
    }
  }
  while (!(res= mysql_next_result(mysql)));

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_table");
  check_mysql_rc(rc, mysql);

  FAIL_UNLESS(res==1, "res != 1");

  return OK;
}


/* connection options */
struct my_option_st opt_utf8[] = {
  {MYSQL_SET_CHARSET_NAME, (char *)"utf8"},
  {0, NULL}
};

static int test_bad_union(MYSQL *mysql)
{
  MYSQL_STMT *stmt;
  int        rc;
  const char *query= "SELECT 1, 2 union SELECT 1";

  stmt= mysql_stmt_init(mysql);
  FAIL_IF(!stmt, mysql_error(mysql));
  rc= mysql_stmt_prepare(stmt, SL(query));
  FAIL_UNLESS(rc && mysql_errno(mysql) == 1222, "Error expected");

  mysql_stmt_close(stmt);
  return OK;
}

/*
   Test that mysql_insert_id() behaves as documented in our manual
*/
static int test_mysql_insert_id(MYSQL *mysql)
{
  unsigned long long res;
  int rc;

  if (mysql_get_server_version(mysql) < 50100) {
    diag("Test requires MySQL Server version 5.1 or above");
    return SKIP;
  }

  rc= mysql_query(mysql, "drop table if exists t1");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "drop table if exists t2");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "drop table if exists t3");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "drop table if exists t4");
  check_mysql_rc(rc, mysql);
  /* table without auto_increment column */
  rc= mysql_query(mysql, "create table t1 (f1 int, f2 varchar(255), key(f1))");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "create table t2 (f1 int not null primary key auto_increment, f2 varchar(255))");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "create table t3 (f1 int not null primary key auto_increment, f2 varchar(255)) engine=MyISAM");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "create table t4 (f1 int not null primary key "
                  "auto_increment, f2 varchar(200), unique (f2)) engine=MyISAM");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "FLUSH TABLES");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "START TRANSACTION");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "insert into t1 values (1,'a')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert into t1 values (null,'b')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");

  /*
    Test for bug #34889: mysql_client_test::test_mysql_insert_id test fails
    sporadically
  */
  rc= mysql_query(mysql, "insert into t2 values (null,'b')");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "insert into t1 select 5,'c'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert into t1 select null,'d'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert into t1 values (null,last_insert_id(300))");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 300, "");
  rc= mysql_query(mysql, "insert into t1 select null,last_insert_id(400)");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  /*
    Behaviour change: old code used to return 0; but 400 is consistent
    with INSERT VALUES, and the manual's section of mysql_insert_id() does not
    say INSERT SELECT should be different.
  */
  FAIL_UNLESS(res == 400, "");

  /* table with auto_increment column */
  rc= mysql_query(mysql, "insert into t3 values (1,'a')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 1, "");
  /* this should not influence next INSERT if it doesn't have auto_inc */
  rc= mysql_query(mysql, "insert into t1 values (10,'e')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");

  rc= mysql_query(mysql, "insert into t3 values (null,'b')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 2, "");
  rc= mysql_query(mysql, "insert into t3 select 5,'c'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  /*
    Manual says that for multirow insert this should have been 5, but does not
    say for INSERT SELECT. This is a behaviour change: old code used to return
    0. We try to be consistent with INSERT VALUES.
  */
  FAIL_UNLESS(res == 5, "");
  rc= mysql_query(mysql, "insert into t3 select null,'d'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 6, "");
  /* with more than one row */
  rc= mysql_query(mysql, "insert into t3 values (10,'a'),(11,'b')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 11, "");
  rc= mysql_query(mysql, "insert into t3 select 12,'a' union select 13,'b'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  /*
    Manual says that for multirow insert this should have been 13, but does
    not say for INSERT SELECT. This is a behaviour change: old code used to
    return 0. We try to be consistent with INSERT VALUES.
  */
  FAIL_UNLESS(res == 13, "");
  rc= mysql_query(mysql, "insert into t3 values (null,'a'),(null,'b')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 14, "");
  rc= mysql_query(mysql, "insert into t3 select null,'a' union select null,'b'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 16, "");
  rc= mysql_query(mysql, "insert into t3 select 12,'a' union select 13,'b'");
  FAIL_IF(!rc, "Error expected");
  rc= mysql_query(mysql, "insert ignore into t3 select 12,'a' union select 13,'b'");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert into t3 values (12,'a'),(13,'b')");
  FAIL_IF(!rc, "Error expected");
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "insert ignore into t3 values (12,'a'),(13,'b')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  /* mixing autogenerated and explicit values */
  rc= mysql_query(mysql, "insert into t3 values (null,'e'),(12,'a'),(13,'b')");
  FAIL_IF(!rc, "Error expected");
  rc= mysql_query(mysql, "insert into t3 values (null,'e'),(12,'a'),(13,'b'),(25,'g')");
  FAIL_IF(!rc, "Error expected");
  rc= mysql_query(mysql, "insert into t3 values (null,last_insert_id(300))");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  /*
    according to the manual, this might be 20 or 300, but it looks like
    auto_increment column takes priority over last_insert_id().
  */
  diag("res: %lld", res);
  FAIL_UNLESS(res == 20, "");
  /* If first autogenerated number fails and 2nd works: */
  rc= mysql_query(mysql, "insert into t4 values (null,'e')");
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 1, "");
  rc= mysql_query(mysql, "insert ignore into t4 values (null,'e'),(null,'a'),(null,'e')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 2, "");
  /* If autogenerated fails and explicit works: */
  rc= mysql_query(mysql, "insert ignore into t4 values (null,'e'),(12,'c'),(null,'d')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  /*
    Behaviour change: old code returned 3 (first autogenerated, even if it
    fails); we now return first successful autogenerated.
  */
  FAIL_UNLESS(res == 13, "");
  /* UPDATE may update mysql_insert_id() if it uses LAST_INSERT_ID(#) */
  rc= mysql_query(mysql, "update t4 set f1=14 where f1=12");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "update t4 set f1=0 where f1=14");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  rc= mysql_query(mysql, "update t4 set f2=last_insert_id(372) where f1=0");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 372, "");
  /* check that LAST_INSERT_ID() does not update mysql_insert_id(): */
  rc= mysql_query(mysql, "insert into t4 values (null,'g')");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 15, "");
  rc= mysql_query(mysql, "update t4 set f2=(@li:=last_insert_id()) where f1=15");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 0, "");
  /*
    Behaviour change: now if ON DUPLICATE KEY UPDATE updates a row,
    mysql_insert_id() returns the id of the row, instead of not being
    affected.
  */
  rc= mysql_query(mysql, "insert into t4 values (null,@li) on duplicate key "
                  "update f2=concat('we updated ',f2)");
  check_mysql_rc(rc, mysql);
  res= mysql_insert_id(mysql);
  FAIL_UNLESS(res == 15, "");

  rc= mysql_query(mysql, "drop table t1,t2,t3,t4");
  check_mysql_rc(rc, mysql);
  return OK;
}

/* Test simple select to debug */

static int test_select_direct(MYSQL *mysql)
{
  int        rc;
  MYSQL_RES  *result;


  rc= mysql_autocommit(mysql, TRUE);
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE TABLE test_select(id int, id1 tinyint, "
                                                 " id2 float, "
                                                 " id3 double, "
                                                 " name varchar(50))");
  check_mysql_rc(rc, mysql);

  /* insert a row and commit the transaction */
  rc= mysql_query(mysql, "INSERT INTO test_select VALUES(10, 5, 2.3, 4.5, 'venu')");
  check_mysql_rc(rc, mysql);

  rc= mysql_commit(mysql);
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "SELECT * FROM test_select");
  check_mysql_rc(rc, mysql);

  /* get the result */
  result= mysql_store_result(mysql);
  FAIL_IF(!result, "Invalid result set");

  mysql_free_result(result);
  rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_select");
  check_mysql_rc(rc, mysql);
  return OK;
}

/*
  Ensure we execute the status code while testing
*/

static int test_status(MYSQL *mysql)
{
  mysql_stat(mysql);
  check_mysql_rc(mysql_errno(mysql), mysql);
  return OK;
}

static int bug_conc1(MYSQL *mysql)
{
  my_test_connect(mysql, hostname, username, password, schema,
                     port, socketname, 0);
  diag("errno: %d", mysql_errno(mysql));
  FAIL_IF(mysql_errno(mysql) != CR_ALREADY_CONNECTED,
          "Expected errno=CR_ALREADY_CONNECTED");
  return OK;
}

static int test_options_initcmd(MYSQL *unused __attribute__((unused)))
{
  MYSQL *mysql= mysql_init(NULL);
  MYSQL_RES *res;
  int rc;

  mysql_options(mysql, MYSQL_INIT_COMMAND, "DROP TABLE IF EXISTS t1; CREATE TABLE t1 (a int)");
  mysql_options(mysql, MYSQL_INIT_COMMAND, "INSERT INTO t1 VALUES (1),(2),(3)");
  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                              port, socketname, 
                              CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS), mysql_error(mysql));

  rc= mysql_query(mysql, "SELECT a FROM t1");
  check_mysql_rc(rc, mysql);

  res= mysql_store_result(mysql);
  FAIL_IF(mysql_num_rows(res) != 3, "Expected 3 rows");

  mysql_free_result(res);

  rc= mysql_query(mysql, "DROP TABLE t1");
  check_mysql_rc(rc, mysql);
  mysql_close(mysql);
  return OK;
}

static int test_extended_init_values(MYSQL *unused __attribute__((unused)))
{
  MYSQL *mysql= mysql_init(NULL);

  mysql_options(mysql, MYSQL_DEFAULT_AUTH, "unknown");
  FAIL_IF(strcmp(mysql->options.extension->default_auth, "unknown"), "option not set");

  mysql_options(mysql, MYSQL_PLUGIN_DIR, "/tmp/foo");
  FAIL_IF(strcmp(mysql->options.extension->plugin_dir, "/tmp/foo"), "option not set");

  mysql_close(mysql);
  return OK;
}

static int test_reconnect_maxpackage(MYSQL *unused __attribute__((unused)))
{
  int rc;
  ulong max_packet= 0;
  MYSQL *mysql;
  MYSQL_RES *res;
  MYSQL_ROW row;
  char *query;
  my_bool reconnect= 1;

  SKIP_CONNECTION_HANDLER;

  mysql= mysql_init(NULL);

  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                              port, socketname,
                              CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS), mysql_error(mysql));
  mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

  rc= mysql_query(mysql, "SELECT @@max_allowed_packet");
  check_mysql_rc(rc, mysql);
  res= mysql_store_result(mysql);
  row= mysql_fetch_row(res);
  max_packet= atol(row[0]);
  diag("max_allowed_packet=%lu", max_packet);
  mysql_free_result(res);

  query= (char *)malloc(max_packet + 30);
  memset(query, 0, max_packet + 30);

  strcpy(query, "SELECT '");
  memset(query + 8, 'A', max_packet);
  strcat(query, "' FROM DUAL");

  rc= mysql_query(mysql, query);
  free(query);
  if (!rc)
  {
    diag("expected error");
    mysql_close(mysql);
    return FAIL;
  }
  else
    diag("Error (expected): %s", mysql_error(mysql));

  rc= mysql_ping(mysql);
  /* if the server is under load, poll might not report closed
     socket since FIN packet came too late */
  if (rc)
    rc= mysql_ping(mysql);
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "SELECT @@max_allowed_packet");
  check_mysql_rc(rc, mysql);
   res= mysql_store_result(mysql);
  row= mysql_fetch_row(res);
  max_packet= atol(row[0]);
  diag("max_allowed_packet=%lu", max_packet);
  mysql_free_result(res);


  mysql_close(mysql);
  return OK;
}

static int test_compressed(MYSQL *unused __attribute__((unused)))
{
  int rc;
  MYSQL *mysql= mysql_init(NULL);
  MYSQL_RES *res;
  my_bool reconnect= 1;

  mysql_options(mysql, MYSQL_OPT_COMPRESS, (void *)1);
  FAIL_IF(!my_test_connect(mysql, hostname, username, password, schema,
                              port, socketname, 
                              CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS), mysql_error(mysql));
  mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

  rc= mysql_query(mysql, "SHOW VARIABLES");
  check_mysql_rc(rc, mysql);

  if ((res= mysql_store_result(mysql)))
    mysql_free_result(res);

  mysql_close(mysql);

  return OK;
}

static int test_conc624(MYSQL *mysql)
{
  MYSQL_STMT *stmt= mysql_stmt_init(mysql);
  char errmsg[MYSQL_ERRMSG_SIZE];

  stmt_set_error(stmt, 9000, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 9000);
  diag("stmt_error: %s", mysql_stmt_error(stmt));
  FAIL_IF(strcmp(mysql_stmt_error(stmt), errmsg), "expected undefined error 9000");

  stmt_set_error(stmt, 0, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 0);
  FAIL_IF(strcmp(mysql_stmt_error(stmt), errmsg), "expected undefined error 0");

  stmt_set_error(stmt, 4999, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 4999);
  FAIL_IF(strcmp(mysql_stmt_error(stmt), errmsg), "expected undefined error 4999");

  my_set_error(mysql, 4999, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 4999);
  FAIL_IF(strcmp(mysql_error(mysql), errmsg), "expected undefined error 4999");

  my_set_error(mysql, 0, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 0);
  FAIL_IF(strcmp(mysql_error(mysql), errmsg), "expected undefined error 0");

  my_set_error(mysql, 9000, SQLSTATE_UNKNOWN, 0);
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER_UNKNOWN_ERROR_CODE, 9000);
  FAIL_IF(strcmp(mysql_error(mysql), errmsg), "expected undefined error 9000");

  /* test if  stmt_set_error works with variadic arguments */
  stmt_set_error(stmt, CR_STMT_CLOSED, SQLSTATE_UNKNOWN, 0, "foobar");
  snprintf(errmsg, MYSQL_ERRMSG_SIZE, ER(CR_STMT_CLOSED), "foobar");
  FAIL_IF(strcmp(mysql_stmt_error(stmt), errmsg), "error when passing variadic arguments to prepared stmt error function");

  mysql_stmt_close(stmt);

  return OK;
}

struct my_tests_st my_tests[] = {
  {"test_conc624", test_conc624, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
  {"test_conc75", test_conc75, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_conc74", test_conc74, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_conc71", test_conc71, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_conc70", test_conc70, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_conc68", test_conc68, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_compressed", test_compressed, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"test_reconnect_maxpackage", test_reconnect_maxpackage, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"basic_connect", basic_connect, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"use_utf8", use_utf8, TEST_CONNECTION_NEW, 0,  opt_utf8,  NULL},
  {"client_query", client_query, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_bad_union", test_bad_union, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_select_direct", test_select_direct, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_mysql_insert_id", test_mysql_insert_id, TEST_CONNECTION_DEFAULT, 0,  NULL,  NULL},
  {"test_bug12001", test_bug12001, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS,  NULL,  NULL},
  {"test_status", test_status, TEST_CONNECTION_NEW, CLIENT_MULTI_STATEMENTS,  NULL,  NULL},
  {"bug_conc1", bug_conc1, TEST_CONNECTION_NEW, 0, NULL, NULL},
  {"test_options_initcmd", test_options_initcmd, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {"test_extended_init_values", test_extended_init_values, TEST_CONNECTION_NONE, 0,  NULL,  NULL},
  {NULL, NULL, 0, 0, NULL, NULL}
};


int main(int argc, char **argv)
{
  if (argc > 1)
    get_options(argc, argv);

  get_envvars();

  diag("user: %s", username);

  run_tests(my_tests);

  return(exit_status());
}