~johnemb/randgen/xml-report

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
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# 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

package GenTest::Generator::FromGrammar;

require Exporter;
@ISA = qw(GenTest GenTest::Generator);

use strict;
use GenTest::Constants;
use GenTest::Random;
use GenTest::Generator;
use GenTest::Grammar;
use GenTest::Grammar::Rule;
use GenTest::Stack::Stack;
use GenTest;
use Cwd;

use Data::Dumper;

use constant GENERATOR_GRAMMAR_FILE	=> 0;
use constant GENERATOR_GRAMMAR_STRING	=> 1;
use constant GENERATOR_GRAMMAR		=> 2;
use constant GENERATOR_SEED		=> 3;
use constant GENERATOR_PRNG		=> 4;
use constant GENERATOR_TMPNAM		=> 5;
use constant GENERATOR_THREAD_ID	=> 6;
use constant GENERATOR_SEQ_ID		=> 7;
use constant GENERATOR_MASK		=> 8;
use constant GENERATOR_MASK_LEVEL => 9;
use constant GENERATOR_VARCHAR_LENGTH	=> 10;
use constant GENERATOR_MASKED_GRAMMAR => 11;
use constant GENERATOR_GLOBAL_FRAME => 12;

use constant GENERATOR_MAX_OCCURRENCES	=> 500;
use constant GENERATOR_MAX_LENGTH	=> 3072;

my $field_pos;

sub new {
        my $class = shift;
	my $generator = $class->SUPER::new({
		'grammar_file'		=> GENERATOR_GRAMMAR_FILE,
		'grammar_string'	=> GENERATOR_GRAMMAR_STRING,
		'grammar'		=> GENERATOR_GRAMMAR,
		'seed'			=> GENERATOR_SEED,
		'prng'			=> GENERATOR_PRNG,
		'thread_id'		=> GENERATOR_THREAD_ID,
		'mask'			=> GENERATOR_MASK,
        'mask_level'    => GENERATOR_MASK_LEVEL,
		'varchar_length'	=> GENERATOR_VARCHAR_LENGTH
	}, @_);

	if (not defined $generator->grammar()) {
#		say("Loading grammar file '".$generator->grammarFile()."' ...");
		$generator->[GENERATOR_GRAMMAR] = GenTest::Grammar->new(
			grammar_file	=> $generator->grammarFile(),
			grammar_string	=> $generator->grammarString()
		);
		return undef if not defined $generator->[GENERATOR_GRAMMAR];
	}

	if (not defined $generator->prng()) {
		$generator->[GENERATOR_PRNG] = GenTest::Random->new(
			seed => $generator->[GENERATOR_SEED] || 0,
			varchar_length => $generator->[GENERATOR_VARCHAR_LENGTH]
		);
	}
        
    if (not defined $generator->maskLevel()) {
        $generator->[GENERATOR_MASK_LEVEL] = 1;    
    }

	$generator->[GENERATOR_SEQ_ID] = 0;

    if ($generator->mask() > 0) {
        my $grammar = $generator->grammar();
        my $top = $grammar->topGrammar($generator->maskLevel(),
                                       "thread".$generator->threadId(),
                                       "query");
        my $maskedTop = $top->mask($generator->mask());
        $generator->[GENERATOR_MASKED_GRAMMAR] = $grammar->patch($maskedTop);
    }

	return $generator;
}

sub prng {
	return $_[0]->[GENERATOR_PRNG];
}

sub grammar {
	return $_[0]->[GENERATOR_GRAMMAR];
}

sub grammarFile {
	return $_[0]->[GENERATOR_GRAMMAR_FILE];
}

sub grammarString {
	return $_[0]->[GENERATOR_GRAMMAR_STRING];
}

sub threadId {
	return $_[0]->[GENERATOR_THREAD_ID];
}

sub seqId {
	return $_[0]->[GENERATOR_SEQ_ID];
}

sub mask {
	return $_[0]->[GENERATOR_MASK];
}

sub maskLevel {
	return $_[0]->[GENERATOR_MASK_LEVEL];
}

sub maskedGrammar {
	return $_[0]->[GENERATOR_MASKED_GRAMMAR];
}

sub globalFrame {
    my ($self) = @_;
    $self->[GENERATOR_GLOBAL_FRAME] = GenTest::Stack::StackFrame->new()
        if not defined $self->[GENERATOR_GLOBAL_FRAME];
    return $self->[GENERATOR_GLOBAL_FRAME];
}

#
# Generate a new query. We do this by iterating over the array containing grammar rules and expanding each grammar rule
# to one of its right-side components . We do that in-place in the array.
#
# Finally, we walk the array and replace all lowercase keywors with literals and such.
#

sub next {
	my ($generator, $executors) = @_;
    
	my $grammar = $generator->grammar();
	my $prng = $generator->prng();
	my $mask = $generator->mask();
	my $mask_level = $generator->maskLevel();

    my $stack = GenTest::Stack::Stack->new();
    my $global = $generator->globalFrame();
    
	#
	# If a temporary file has been left from a previous statement, unlink it.
	#

	unlink($generator->[GENERATOR_TMPNAM]) if defined $generator->[GENERATOR_TMPNAM];
	$generator->[GENERATOR_TMPNAM] = undef;

	my $starting_rule;

	# If this is our first query, we look for a rule named "threadN_init" or "query_init"
	if ($generator->seqId() == 0) {
		$starting_rule = 
            $grammar->firstMatchingRule("thread".$generator->threadId()."_init",
                                        "query_init");
		$mask = 0 if defined $starting_rule;
        # Do not apply mask on _init rules.
	}

    ## Apply mask if any
	if (defined $generator->maskedGrammar()) {
        $grammar = $generator->maskedGrammar();
	}

	# If no init starting rule, we look for rules named "threadN" or "query"
	$starting_rule = 
        $grammar->firstMatchingRule("thread".$generator->threadId(),
                                    "query") 
        if not defined $starting_rule;
    
	my @sentence = ($starting_rule);

	my $grammar_rules = $grammar->rules();

	# And we do multiple iterations, continuously expanding grammar rules and replacing the original rule with its expansion.
	
	my %rule_counters;
	my %invariants;

	my $last_table;
	my $last_database;

	my $pos = 0;
	while ($pos <= $#sentence) {
		if ($#sentence > GENERATOR_MAX_LENGTH) {
			say("Sentence is now longer than ".GENERATOR_MAX_LENGTH()." symbols. Possible endless loop in grammar. Aborting.");
			return undef;
		}
		if (ref($sentence[$pos]) eq 'GenTest::Grammar::Rule') {
			splice (@sentence, $pos, 1 , map {

				# Check if we just picked a grammar rule. If yes, then return its Rule object.	
				# If not, use the original literal, stored in $_

				if (exists $grammar_rules->{$_}) {
					$rule_counters{$_}++;
					if ($rule_counters{$_} > GENERATOR_MAX_OCCURRENCES) {
						say("Rule $_ occured more than ".GENERATOR_MAX_OCCURRENCES()." times. Possible endless loop in grammar. Aborting.");
						return undef;
					} else {
						$grammar_rules->{$_};
					}
				} else {
					$_;
				}
			} @{$prng->arrayElement($sentence[$pos]->[GenTest::Grammar::Rule::RULE_COMPONENTS])});
			if ($@ ne '') {
				say("Internal grammar problem: $@");
				return undef;
			}
		} else {
			$pos++;
		}
	}

	# Once the SQL sentence has been constructed, iterate over it to replace variable items with their final values
	
	my $item_nodash;
	my $orig_item;
	foreach (@sentence) {
		$orig_item = $_;
		next if $_ eq ' ';

		if (
			($_ =~ m{^\{}so) &&
			($_ =~ m{\}$}so)
		) {
			$_ = eval("no strict;\n".$_);		# Code

			if ($@ =~ m{at \(.*?\) line}o) {
				say("Internal grammar error: $@");
				return undef;			# Code called die()
			} elsif ($@ ne '') {
				warn("Syntax error in Perl snippet $orig_item : $@");
				return undef;
			}
			next;
		} elsif ($_ =~ m{^\$}so) {
			$_ = eval("no strict;\n".$_.";\n");	# Variable
			next;
		}

		my $modifier;

		my $invariant_substitution=0;
		if ($_ =~ m{^(_[a-z_]*?)\[(.*?)\]}sio) {
			$modifier = $2;
			if ($modifier eq 'invariant') {
				$invariant_substitution=1;
				$_ = exists $invariants{$orig_item} ? $invariants{$orig_item} : $1 ;
			} else {
				$_ = $1;
			}
		}

		next if $_ eq uc($_);				# Short-cut for UPPERCASE literals

		if ( ($_ eq 'letter') || ($_ eq '_letter') ) {
			$_ = $prng->letter();
		} elsif ($_ eq '_hex') {
			$_ = $prng->hex();
		} elsif ( ($_ eq 'digit')  || ($_ eq '_digit') ) {
			$_ = $prng->digit();
		} elsif ($_ eq '_cwd') {
			$_ = "'".cwd()."'";
		} elsif (
			($_ eq '_tmpnam') ||
			($_ eq 'tmpnam') ||
			($_ eq '_tmpfile')
		) {
			# Create a new temporary file name and record it for unlinking at the next statement
			$generator->[GENERATOR_TMPNAM] = tmpdir()."gentest".$$.".tmp" if not defined $generator->[GENERATOR_TMPNAM];
			$_ = "'".$generator->[GENERATOR_TMPNAM]."'";
			$_ =~ s{\\}{\\\\}sgio if osWindows();	# Backslash-escape backslashes on Windows
		} elsif ($_ eq '_tmptable') {
			$_ = "tmptable".$$;
		} elsif ($_ eq '_unix_timestamp') {
			$_ = time();
		} elsif ($_ eq '_pid') {
			$_ = $$;
		} elsif ($_ eq '_thread_count') {
			$_ = $ENV{RQG_THREADS};
		} elsif (($_ eq '_database') || ($_ eq '_db') || ($_ eq '_schema')) {
			my $databases = $executors->[0]->metaSchemas();
			$last_database = $prng->arrayElement($databases);
			$_ = '`'.$last_database.'`';
		} elsif ($_ eq '_table') {
			my $tables = $executors->[0]->metaTables($last_database);
			$last_table = $prng->arrayElement($tables);
			$_ = '`'.$last_table.'`';
		} elsif ($_ eq '_field') {
			my $fields = $executors->[0]->metaColumns($last_table, $last_database);
			$_ = '`'.$prng->arrayElement($fields).'`';
		} elsif ($_ eq '_field_list') {
			my $fields = $executors->[0]->metaColumns($last_table, $last_database);
			$_ = '`'.join('`,`', @$fields).'`';
		} elsif ($_ eq '_field_count') {
			my $fields = $executors->[0]->metaColumns($last_table, $last_database);
			$_ = $#$fields + 1;
		} elsif ($_ eq '_field_next') {
			# Pick the next field that has not been picked recently and increment the $field_pos counter
			my $fields = $executors->[0]->metaColumns($last_table, $last_database);
			$_ = '`'.$fields->[$field_pos++ % $#$fields].'`';
		} elsif ($_ eq '_field_no_pk') {
			my $fields = $executors->[0]->metaColumnsTypeNot('primary',$last_table, $last_database);
			$_ = '`'.$prng->arrayElement($fields).'`';
		} elsif (($_ eq '_field_indexed') || ($_ eq '_field_key')) {
			my $fields_indexed = $executors->[0]->metaColumnsType('indexed',$last_table, $last_database);
			$_ = '`'.$prng->arrayElement($fields_indexed).'`';
		} elsif ($_ eq '_collation') {
			my $collations = $executors->[0]->metaCollations();
			$_ = '_'.$prng->arrayElement($collations);
		} elsif ($_ eq '_charset') {
			my $charsets = $executors->[0]->metaCharactersets();
			$_ = '_'.$prng->arrayElement($charsets);
		} elsif ($_ eq '_data') {
			$_ = $prng->file(cwd()."/data");
		} elsif (
			($prng->isFieldType($_) == FIELD_TYPE_NUMERIC) ||
			($prng->isFieldType($_) == FIELD_TYPE_BLOB) 
		) {
			$_ = $prng->fieldType($_);
		} elsif ($prng->isFieldType($_)) {
			$_ = $prng->fieldType($_);
			if (($orig_item =~ m{`$}so) || ($_ =~ m{^(b'|0x)}so)) {
				# Do not quote, quotes are already present
			} elsif ($_ =~ m{'}so) {
				$_ = '"'.$_.'"';
			} else {
				$_ = "'".$_."'";
			}
		} elsif ($_ =~ m{^_(.*)}sio) {
			$item_nodash = $1;
			if ($prng->isFieldType($item_nodash)) {
				$_ = "'".$prng->fieldType($item_nodash)."'";
				if ($_ =~ m{'}so) {
					$_ = '"'.$_.'"';
				} else {
					$_ = "'".$_."'";
				}
			}
		}

		# If the grammar initially contained a ` , restore it. This allows
		# The generation of constructs such as `table _digit` => `table 5`

		if (
			($orig_item =~ m{`$}so) && 
			($_ !~ m{`}so)
		) {
			$_ = $_.'`';
		}
	
		$invariants{$orig_item} = $_ if $modifier eq 'invariant';
	}

	$generator->[GENERATOR_SEQ_ID]++;

	my $sentence = join ('', @sentence);

	# If this is a BEGIN ... END block then send it to server without splitting.
	# Otherwise, split it into individual statements so that the error and the result set from each statement
	# can be examined

	if (
		($sentence =~ m{CREATE}sio) && 
		($sentence =~ m{BEGIN|END}sio)
	) {
		return [ $sentence ];
	} elsif ($sentence =~ m{;}) {
		my @sentences = split (';', $sentence);
		return \@sentences;
	} else {
		return [ $sentence ];
	}
}

1;