-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.rb
More file actions
executable file
·160 lines (145 loc) · 4.01 KB
/
Copy pathtest.rb
File metadata and controls
executable file
·160 lines (145 loc) · 4.01 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
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
#!/usr/bin/env ruby
require 'open3'
# Test Utilities
@tests = 0
@test_description = 0
def try(descr)
start = Time.now
@tests += 1
@test_description = descr
yield
delta = format('%.3f', Time.now - start)
# highlight slow tests
delta = "\e[7m#{delta}\e[27m" if (Time.now - start) > 0.03
puts "#{delta}: #{descr}"
end
def eq(result, expected)
a = result.to_s.gsub(/^/, '> ')
b = expected.to_s.gsub(/^/, '< ')
raise "\"#{@test_description}\"\n#{a}\n#{b}" unless result == expected
end
# Setup
@url = `pg_tmp -o "-c shared_preload_libraries=pg_stat_statements,#{Dir.pwd}/safeupdate"`
psql = "psql --no-psqlrc -At -q #{@url}"
puts "using #{@url}"
q = %{
CREATE EXTENSION pg_stat_statements;
CREATE TABLE employees (name varchar(30));
INSERT INTO employees VALUES ('Eric'),('kevin'),('Robert');
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, ''
eq status.success?, true
# Tests
try 'Block unqualified DELETE' do
q = %(
DELETE FROM employees;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: DELETE requires a WHERE clause\n"
eq status.success?, true
eq out, ''
end
try 'Block unqualified UPDATE' do
q = %(
UPDATE employees SET name='Kevin';
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: UPDATE requires a WHERE clause\n"
eq out, ''
eq status.success?, true
end
try 'Allow qualified DELETE' do
q = %(
BEGIN;
DELETE FROM employees WHERE name='Robert' RETURNING name;
ROLLBACK;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err.empty?, true
eq out, "Robert\n"
eq status.success?, true
end
try 'Allow qualified UPDATE' do
q = %(
BEGIN;
UPDATE employees SET name='Kevin'
WHERE name='kevin'
RETURNING name;
ROLLBACK;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "Kevin\n"
eq status.success?, true
end
try 'Block modifying CTE with unqualified UPDATE' do
q = %{
WITH updates AS (
UPDATE employees SET name='Kevin'
RETURNING name
)
SELECT *
FROM updates;
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, "ERROR: UPDATE requires a WHERE clause\n"
eq out, ''
eq status.success?, true
end
try 'Allow modifying CTE with qualified UPDATE' do
q = %{
BEGIN;
WITH updates AS (
UPDATE employees SET name='Kevin'
WHERE name='kevin'
RETURNING name
)
SELECT *
FROM updates;
ROLLBACK;
}
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "Kevin\n"
eq status.success?, true
end
try 'Disable safeupdate' do
q = %(
SHOW safeupdate.enabled;
SET safeupdate.enabled=0;
BEGIN;
DELETE FROM employees;
ROLLBACK;
SET safeupdate.enabled=1;
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "on\n"
eq status.success?, true
end
try 'Call previous hook when disabled' do
# Even when disabled, safeupdate must call the previous
# post_parse_analyze_hook. Witness: pg_stat_statements (loaded first)
# normalizes constants in that hook -- if the chain is dropped while
# disabled, the statement is recorded un-normalized (no '$n' params).
q = %(
SELECT 1 FROM pg_stat_statements_reset() WHERE false;
SET safeupdate.enabled=0;
SELECT 1 AS safeupdate_chain_probe WHERE false;
SET safeupdate.enabled=1;
SELECT CASE
WHEN count(*) = 0 THEN 'no pg_stat_statements entry'
WHEN bool_or(query LIKE '%$%') THEN 'ok'
ELSE 'un-normalized: previous hook was not called'
END
FROM pg_stat_statements
WHERE query LIKE '%safeupdate_chain_probe%';
)
out, err, status = Open3.capture3(psql, :stdin_data => q)
eq err, ''
eq out, "ok\n"
eq status.success?, true
end
puts "\n#{@tests} tests PASSED"