DBIO-ClickHouse

 view release on metacpan or  search on metacpan

t/63-bind-safety.t  view on Meta::CPAN

  __PACKAGE__->register_class(Val => 'My::CH::Bind::Result::Val');
}

my $schema = My::CH::Bind::Schema->connect(sub { });
$schema->storage(MyTest::CH::BindRecorder->new($schema));
my $rs = $schema->resultset('Val');

sub recorded {
  my @log = @MyTest::CH::BindRecorder::LOG;
  @MyTest::CH::BindRecorder::LOG = ();
  return @log;
}

#############################################################################
# 1. a non-number at a numeric column is refused before it is sent
#############################################################################

# WHY: the server answers '' / 'abc' / '5x' / '1.5' bound to an Int32 with
# success and a NULL in the row. A refusal that names the column is the only
# honest answer; sending it would be falsifying the data.
for my $case (
  [ i => '' ], [ i => 'abc' ], [ i => '5x' ], [ i => '1.5' ], [ i => '1e3' ],
  [ i => ' 5' ], [ i => '5 ' ], [ i => '0x10' ],
  [ u => '' ], [ big => 'abc' ],
  [ f => '' ], [ f => 'abc' ], [ f => ' 5' ],
  [ d => '' ], [ d => 'abc' ], [ d => 'inf' ],
) {
  my ($col, $value) = @$case;
  my $shown = length $value ? "'$value'" : q('');
  throws_ok { $rs->create({ id => 'u', $col => $value }) }
    qr/table val, column \Q$col\E\b.*refused.*Nothing was sent/s,
    "create: $shown at $col is refused, naming table and column";
  is scalar(recorded()), 0, "... and no statement was rendered for it";
}

unlike(
  (eval { $rs->create({ id => 'u', i => 'secret-abc' }); 1 } ? '' : "$@"),
  qr/secret-abc/, 'the message never quotes the value',
);

# the other write paths
throws_ok { $rs->search({ id => 'u' })->update({ i => '' }) }
  qr/table val, column i\b.*refused/s, 'update: the SET value is refused too';
is scalar(recorded()), 0, '... before anything is sent';

throws_ok { $rs->populate([ [qw(id i)], [ 'a', 1 ], [ 'b', 'x' ] ]) }
  qr/table val, column i\b.*refused/s, 'populate (insert_multi): refused';
is scalar(recorded()), 0, '... before anything is sent';

#############################################################################
# 2. what stays allowed
#############################################################################

# WHY: bind-digit-string-leading-zero. A string that the server reads as the
# number it looks like is not a falsification; the check is the server's own
# number format, per type, not "looks like a digit".
for my $case (
  [ i => '007' ], [ i => '-3' ], [ i => '+5' ], [ i => 5 ], [ i => 0 ], [ i => undef ],
  [ u => '255' ], [ big => '9223372036854775807' ],
  [ f => '1e3' ], [ f => '1.50' ], [ f => '.5' ], [ f => '5.' ], [ f => '-inf' ],
  [ f => 'NaN' ], [ f => 1.5 ], [ f => '007' ],
  [ d => '1.50' ], [ d => '007' ], [ d => '1E3' ], [ d => undef ],
  [ s => '' ], [ s => 'abc' ], [ s => '5x' ], [ b => '' ],
) {
  my ($col, $value) = @$case;
  my $shown = defined $value ? (length $value ? "'$value'" : q('')) : 'undef';
  lives_ok { $rs->create({ id => 'u', $col => $value }) }
    "$shown at $col is sent";
  is scalar(recorded()), 1, '... exactly one statement';
}

lives_ok { $rs->create({ id => 'u', i => \'toInt32(1)' }) }
  'literal SQL at an Int column is not looked at';
recorded();

# a read is not a write: the server reports a compare, it does not store
lives_ok {
  my $storage = $schema->storage;
  my $source  = $rs->result_source;
  my $q = $rs->search({ i => q() });
  my $attrs = $q->_resolved_attrs;
  my ($op, $ident, @args) = $storage->_select_args($source, $attrs->{select}, $q->{cond}, $attrs);
  $storage->_prep_for_execute($op, $ident, \@args);
}
  'a search condition is not refused (nothing is stored)';
recorded();

#############################################################################
# 2b. an integer outside the range of its type is refused before it is sent
#############################################################################

# WHY: the server stores NULL for 300 at a Nullable(UInt8) and 44 (300 mod 256)
# at a UInt8, with no error, and no session setting makes it throw (measured,
# karr k43). Writing a different value than the one given is falsifying data,
# so the driver is the only place left to say no. The range is the type's own:
# the edges are in, one past is out, for signed and unsigned, narrow and wide.
my $W = sub { Math::BigInt->new(2)->bpow($_[0]) };
for my $case (
  [ u => 300 ], [ u => '300' ], [ u => '-3' ], [ u => 256 ], [ u => '-1' ],
  [ m => 300 ], [ m => '-1' ],
  [ t8 => 128 ], [ t8 => '-129' ],
  [ i => '2147483648' ], [ i => '-2147483649' ],
  [ big => '9223372036854775808' ], [ big => '-9223372036854775809' ],
  [ ub => '-1' ], [ ub => $W->(64)->bstr ],
  [ w => '-1' ], [ w => $W->(256)->bstr ],
  [ lc => 32768 ],
  [ u => '+256' ], [ u => '0000256' ],
) {
  my ($col, $value) = @$case;
  throws_ok { $rs->create({ id => 'u', m => 1, lc => 1, $col => $value }) }
    qr/table val, column \Q$col\E\b.*outside the range of \w+.*Nothing was sent/s,
    "create: $value at $col is out of range and refused";
  is scalar(recorded()), 0, '... and no statement was rendered for it';
}

like(
  (eval { $rs->create({ id => 'u', m => 1, lc => 1, u => 300 }); 1 } ? '' : "$@"),
  qr/UInt8\b/, 'the message names the type'
);
unlike(
  (eval { $rs->create({ id => 'u', m => 1, lc => 1, u => 987654 }); 1 } ? '' : "$@"),



( run in 1.076 second using v1.01-cache-2.11-cpan-60884a8df76 )