]> granicus.if.org Git - postgresql/commitdiff
adminpack: Add test suite
authorPeter Eisentraut <peter_e@gmx.net>
Sat, 12 Aug 2017 01:04:04 +0000 (21:04 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Fri, 15 Sep 2017 02:22:59 +0000 (22:22 -0400)
Reviewed-by: David Steele <david@pgmasters.net>
contrib/adminpack/.gitignore [new file with mode: 0644]
contrib/adminpack/Makefile
contrib/adminpack/expected/adminpack.out [new file with mode: 0644]
contrib/adminpack/sql/adminpack.sql [new file with mode: 0644]

diff --git a/contrib/adminpack/.gitignore b/contrib/adminpack/.gitignore
new file mode 100644 (file)
index 0000000..5dcb3ff
--- /dev/null
@@ -0,0 +1,4 @@
+# Generated subdirectories
+/log/
+/results/
+/tmp_check/
index f065f84bfb1114ca4685520293a6be75d71539fa..89c249bc0dfb552e18e6f259546e43a6a339480f 100644 (file)
@@ -8,6 +8,8 @@ EXTENSION = adminpack
 DATA = adminpack--1.0.sql
 PGFILEDESC = "adminpack - support functions for pgAdmin"
 
+REGRESS = adminpack
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/contrib/adminpack/expected/adminpack.out b/contrib/adminpack/expected/adminpack.out
new file mode 100644 (file)
index 0000000..b0d72dd
--- /dev/null
@@ -0,0 +1,146 @@
+CREATE EXTENSION adminpack;
+-- create new file
+SELECT pg_file_write('test_file1', 'test1', false);
+ pg_file_write 
+---------------
+             5
+(1 row)
+
+SELECT pg_read_file('test_file1');
+ pg_read_file 
+--------------
+ test1
+(1 row)
+
+-- append
+SELECT pg_file_write('test_file1', 'test1', true);
+ pg_file_write 
+---------------
+             5
+(1 row)
+
+SELECT pg_read_file('test_file1');
+ pg_read_file 
+--------------
+ test1test1
+(1 row)
+
+-- error, already exists
+SELECT pg_file_write('test_file1', 'test1', false);
+ERROR:  file "test_file1" exists
+SELECT pg_read_file('test_file1');
+ pg_read_file 
+--------------
+ test1test1
+(1 row)
+
+-- disallowed file paths
+SELECT pg_file_write('../test_file0', 'test0', false);
+ERROR:  path must be in or below the current directory
+SELECT pg_file_write('/tmp/test_file0', 'test0', false);
+ERROR:  absolute path not allowed
+SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4', false);
+ pg_file_write 
+---------------
+             5
+(1 row)
+
+SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false);
+ERROR:  reference to parent directory ("..") not allowed
+-- rename file
+SELECT pg_file_rename('test_file1', 'test_file2');
+ pg_file_rename 
+----------------
+ t
+(1 row)
+
+SELECT pg_read_file('test_file1');  -- not there
+ERROR:  could not stat file "test_file1": No such file or directory
+SELECT pg_read_file('test_file2');
+ pg_read_file 
+--------------
+ test1test1
+(1 row)
+
+-- error
+SELECT pg_file_rename('test_file1', 'test_file2');
+WARNING:  file "test_file1" is not accessible: No such file or directory
+ pg_file_rename 
+----------------
+ f
+(1 row)
+
+-- rename file and archive
+SELECT pg_file_write('test_file3', 'test3', false);
+ pg_file_write 
+---------------
+             5
+(1 row)
+
+SELECT pg_file_rename('test_file2', 'test_file3', 'test_file3_archive');
+ pg_file_rename 
+----------------
+ t
+(1 row)
+
+SELECT pg_read_file('test_file2');  -- not there
+ERROR:  could not stat file "test_file2": No such file or directory
+SELECT pg_read_file('test_file3');
+ pg_read_file 
+--------------
+ test1test1
+(1 row)
+
+SELECT pg_read_file('test_file3_archive');
+ pg_read_file 
+--------------
+ test3
+(1 row)
+
+-- unlink
+SELECT pg_file_unlink('test_file1');  -- does not exist
+ pg_file_unlink 
+----------------
+ f
+(1 row)
+
+SELECT pg_file_unlink('test_file2');  -- does not exist
+ pg_file_unlink 
+----------------
+ f
+(1 row)
+
+SELECT pg_file_unlink('test_file3');
+ pg_file_unlink 
+----------------
+ t
+(1 row)
+
+SELECT pg_file_unlink('test_file3_archive');
+ pg_file_unlink 
+----------------
+ t
+(1 row)
+
+SELECT pg_file_unlink('test_file4');
+ pg_file_unlink 
+----------------
+ t
+(1 row)
+
+-- superuser checks
+CREATE USER regress_user1;
+SET ROLE regress_user1;
+SELECT pg_file_write('test_file0', 'test0', false);
+ERROR:  only superuser may access generic file functions
+SELECT pg_file_rename('test_file0', 'test_file0');
+ERROR:  only superuser may access generic file functions
+CONTEXT:  SQL function "pg_file_rename" statement 1
+SELECT pg_file_unlink('test_file0');
+ERROR:  only superuser may access generic file functions
+SELECT pg_logdir_ls();
+ERROR:  only superuser can list the log directory
+RESET ROLE;
+DROP USER regress_user1;
+-- no further tests for pg_logdir_ls() because it depends on the
+-- server's logging setup
diff --git a/contrib/adminpack/sql/adminpack.sql b/contrib/adminpack/sql/adminpack.sql
new file mode 100644 (file)
index 0000000..13621bd
--- /dev/null
@@ -0,0 +1,60 @@
+CREATE EXTENSION adminpack;
+
+-- create new file
+SELECT pg_file_write('test_file1', 'test1', false);
+SELECT pg_read_file('test_file1');
+
+-- append
+SELECT pg_file_write('test_file1', 'test1', true);
+SELECT pg_read_file('test_file1');
+
+-- error, already exists
+SELECT pg_file_write('test_file1', 'test1', false);
+SELECT pg_read_file('test_file1');
+
+-- disallowed file paths
+SELECT pg_file_write('../test_file0', 'test0', false);
+SELECT pg_file_write('/tmp/test_file0', 'test0', false);
+SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4', false);
+SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false);
+
+
+-- rename file
+SELECT pg_file_rename('test_file1', 'test_file2');
+SELECT pg_read_file('test_file1');  -- not there
+SELECT pg_read_file('test_file2');
+
+-- error
+SELECT pg_file_rename('test_file1', 'test_file2');
+
+-- rename file and archive
+SELECT pg_file_write('test_file3', 'test3', false);
+SELECT pg_file_rename('test_file2', 'test_file3', 'test_file3_archive');
+SELECT pg_read_file('test_file2');  -- not there
+SELECT pg_read_file('test_file3');
+SELECT pg_read_file('test_file3_archive');
+
+
+-- unlink
+SELECT pg_file_unlink('test_file1');  -- does not exist
+SELECT pg_file_unlink('test_file2');  -- does not exist
+SELECT pg_file_unlink('test_file3');
+SELECT pg_file_unlink('test_file3_archive');
+SELECT pg_file_unlink('test_file4');
+
+
+-- superuser checks
+CREATE USER regress_user1;
+SET ROLE regress_user1;
+
+SELECT pg_file_write('test_file0', 'test0', false);
+SELECT pg_file_rename('test_file0', 'test_file0');
+SELECT pg_file_unlink('test_file0');
+SELECT pg_logdir_ls();
+
+RESET ROLE;
+DROP USER regress_user1;
+
+
+-- no further tests for pg_logdir_ls() because it depends on the
+-- server's logging setup