Repository navigation
Expand file tree
/
Copy pathquery_test.go
More file actions
135 lines (131 loc) · 5.12 KB
/
Copy pathquery_test.go
File metadata and controls
135 lines (131 loc) · 5.12 KB
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
/**
* fathomry
* Copyright (C) 2026 Frost Leo
* SPDX-License-Identifier: GPL-3.0-or-later
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
package mysql
import (
"context"
"database/sql"
"errors"
"reflect"
"testing"
"github.com/frost-leo/fathomry/internal/conformance"
sdk "github.com/go-sql-driver/mysql"
)
func TestPreparedQueriesAndOwnedTLS(t *testing.T) {
for _, mode := range []struct {
name string
tls, full bool
}{{"plain", false, false}, {"tls-fast", true, false}, {"tls-full", true, true}} {
t.Run(mode.name, func(t *testing.T) {
peer := newPeer(t, mode.tls, mode.full)
f := bindFixture(t, peer.options(), 1)
for _, test := range []struct {
query string
args []any
want [][]byte
}{
{"SELECT cells", nil, [][]byte{nil, {}, []byte("value-canary")}},
{"SELECT ?", []any{"parameter-canary"}, [][]byte{[]byte("parameter-canary")}},
{"SELECT ?", []any{nil}, [][]byte{nil}},
{"SELECT unsigned", nil, [][]byte{[]byte("18446744073709551615")}},
{"SELECT decimal", nil, [][]byte{[]byte("12345678901234567890.00100")}},
{"SELECT zero_date", nil, [][]byte{[]byte("0000-00-00 00:00:00")}},
{"SELECT json", nil, [][]byte{[]byte("{\"exact\":18446744073709551615}")}},
} {
receipt, err := f.db.Query(context.Background(), correlation("query"), test.query, test.args...)
result := observe(t, receipt, err)
if result.Err() != nil {
t.Fatalf("query failed (%s): %v", test.query, result.Err())
}
row, err := result.Outcome.Value.First()
if err != nil || !reflect.DeepEqual(row.ValuesCopy(), test.want) {
t.Fatalf("native cell representation changed for %s", test.query)
}
values := row.ValuesCopy()
for _, cell := range values {
if len(cell) > 0 {
cell[0] = 'X'
}
}
if !reflect.DeepEqual(row.ValuesCopy(), test.want) {
t.Fatal("copy mutated independent evidence")
}
conformance.Runtime(t, result.Outcome.Value, new(Result), "value-canary", "parameter-canary")
drain(t, f.inbox, 1)
}
receipt, err := f.db.Query(context.Background(), correlation("empty"), "SELECT empty")
result := observe(t, receipt, err)
if result.Err() != nil || !result.Outcome.Value.Complete() || result.Outcome.Value.RowsCopy() == nil {
t.Fatal("empty query lost completeness")
}
if _, err = result.Outcome.Value.First(); !errors.Is(err, sql.ErrNoRows) {
t.Fatal("no-row identity lost")
}
drain(t, f.inbox, 1)
if mode.tls && peer.secure.Load() != 1 {
t.Fatal("queries did not reuse the verified TLS connection")
}
})
}
}
func TestStatementBoundsAndPartialEvidence(t *testing.T) {
peer := newPeer(t, true, false)
options := peer.options()
options.MaxRows = 1
options.MaxResultBytes = 1024
options.MaxPacketBytes = 8192
f := bindFixture(t, options, 1)
for _, test := range []struct {
query string
kind error
rows int
}{
{"SELECT over", ErrLimit, 2}, {"SELECT wide", ErrLimit, 0}, {"SELECT large", ErrLimit, 1}, {"SELECT huge_header", ErrLimit, 0}, {"SELECT partial", ErrQuery, 1},
} {
receipt, err := f.db.Query(context.Background(), correlation("bounded"), test.query)
result := observe(t, receipt, err)
if !errors.Is(result.Err(), test.kind) || result.Outcome.Value.Complete() || result.Outcome.Value.RowsRead() != test.rows {
t.Fatalf("wrong bounded/partial outcome for %s: %v, rows=%d", test.query, result.Err(), result.Outcome.Value.RowsRead())
}
if test.query == "SELECT partial" {
var native *sdk.MySQLError
if !errors.As(result.Err(), &native) || native.Number != 1213 || string(native.SQLState[:]) != "40001" {
t.Fatal("original native code/state lost")
}
conformance.Private(t, result.Err(), "native-error-canary")
}
drain(t, f.inbox, 1)
}
receipt, err := f.db.Query(context.Background(), correlation("replacement"), "SELECT cells")
if result := observe(t, receipt, err); result.Err() != nil {
t.Fatal("failed connection was not replaced", result.Err())
}
drain(t, f.inbox, 1)
}
func TestExecUsesPreparedSingleConnection(t *testing.T) {
peer := newPeer(t, true, true)
f := bindFixture(t, peer.options(), 1)
receipt, err := f.db.Exec(context.Background(), correlation("exec"), "INSERT INTO fixture VALUES (?)", "payload")
result := observe(t, receipt, err)
affected, known := result.Outcome.Value.RowsAffected()
id, idKnown := result.Outcome.Value.LastInsertID()
if result.Err() != nil || !result.Outcome.Value.Complete() || !known || affected != 1 || !idKnown || id != 7 || peer.queries.Load() != 1 {
t.Fatal("native execution evidence changed", result.Err())
}
drain(t, f.inbox, 1)
}