/***************************************************************** * test-overtype.c the executed module contains test that have to * cause Integer Underflow for intger and smallint * are sql types. * * Copyright (C) 2009 Andrey Y. Rjavskov(aka rjaan) * * 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; either version 2 * of the License, or (at your option) any later version. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *****************************************************************/ #include "test-common.h" #include "test-config.h" static int overtype_smallint_test ( PGconn* testdb, int start_step ,int is_unsigned ); int main( int argc, char** argv ) { test_begin ( argv[0] ); fprintf(stdout,"\n"); fflush ( stdout ); PGconn* testdb = upg_connect_db ( BT_DBHOST, BT_DBPORT, BT_DBNAME, NULL, NULL ); if ( !testdb ) { return EXIT_FAILURE; } if ( upg_sqlexec ( testdb, "DELETE FROM substances;" ) == -EXIT_FAILURE ) { return -EXIT_FAILURE; } /* test at Integer Underflow smallint sql-type (signed short)*/ overtype_smallint_test ( testdb, 0, 0 ); /* test at Integer Underflow smallint sql-type (unsigned short)*/ overtype_smallint_test ( testdb, 100, 1 ); test_done(); upg_disconnect_db ( testdb ); return 0; } static int overtype_smallint_test ( PGconn* testdb, int start_step ,int is_unsigned ) { int step = start_step + 10; int yes_overtype = 0; long long int test_smallint_val; signed short int smallint_val; unsigned short int usmallint_val; if ( !is_unsigned ) { test_smallint_val = SQLTYPE_LIMIT_SMALINT - 5; smallint_val = test_smallint_val; }else{ test_smallint_val = SQLTYPE_LIMIT_UNSIGNED_SMALINT - 5; usmallint_val = test_smallint_val; } if( is_unsigned ) { fprintf(stdout,"test at Integer Underflow for short int!\n" ); }else{ fprintf(stdout,"test at Integer Underflow for unsigned short int!\n" ); } while ( step-- >= start_step ) { fprintf ( stdout, "%d step: test %lld value\n", step + 1, test_smallint_val ); fflush ( stdout ); if ( !is_unsigned ) { smallint_val = (signed short int)test_smallint_val; if ( !yes_overtype && smallint_val <= 0 ) { fprintf(stdout,"to cause Integer Underflow(for short)!\n" ); yes_overtype = 1; step = start_step + 5; } }else{ usmallint_val = test_smallint_val; if ( !yes_overtype && usmallint_val <= 0 ) { fprintf(stdout,"to cause Integer Underflow(for unsigned short)!\n" ); yes_overtype = 1; step = start_step + 5; } } if ( upg_sqlexec ( testdb, "INSERT INTO substances(kind,class,id) values(%lld,0,%d);", test_smallint_val,step + 1) == -EXIT_FAILURE ) { return -EXIT_FAILURE; } test_smallint_val++; } return EXIT_SUCCESS; }