1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-07-03 00:36:52 +02:00

Invalidate memory scanned into sql.RawBytes

The intention of sql.RawBytes is for it to hold memory owned by the
 database. When used, it's content is only valid until the `Next`,
 `Scan` or `Close` is called on the `Rows`

To ensure that we meet this behaviour, when `[]byte` is used in a
 column, it's value is copied to a buffer that we keep track of for
 later invalidation. By doing this, incorrect use of `sql.RawBytes`
 values is exposed in tests that use go-sqlmock. Without this, when a
 real database is used and it's driver does share memory, then those
 issues would not be exposed until runtime (and in non-obvious ways)
This commit is contained in:
David Ackroyd
2019-06-21 13:28:10 +10:00
parent 6c8a572d09
commit d5879ee4b7
4 changed files with 486 additions and 0 deletions

31
rows_go13_test.go Normal file
View File

@ -0,0 +1,31 @@
// +build go1.3
package sqlmock
import (
"database/sql"
"testing"
)
func TestQueryRowBytesNotInvalidatedByNext_stringIntoRawBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).
AddRow(`one binary value with some text!`).
AddRow(`two binary value with even more text than the first one`)
scan := func(rs *sql.Rows) ([]byte, error) {
var raw sql.RawBytes
return raw, rs.Scan(&raw)
}
want := [][]byte{[]byte(`one binary value with some text!`), []byte(`two binary value with even more text than the first one`)}
queryRowBytesNotInvalidatedByNext(t, rows, scan, want)
}
func TestQueryRowBytesNotInvalidatedByClose_stringIntoRawBytes(t *testing.T) {
t.Parallel()
rows := NewRows([]string{"raw"}).AddRow(`one binary value with some text!`)
scan := func(rs *sql.Rows) ([]byte, error) {
var raw sql.RawBytes
return raw, rs.Scan(&raw)
}
queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`one binary value with some text!`))
}