1+
2+ # Copyright (c) 2024, Cloudberry Database, HashData Technology Limited.
3+
4+ # Test drop directory table.
5+ #
6+ # This test can only run with Unix-domain sockets.
7+
8+ use strict;
9+ use warnings;
10+ use PostgresNode;
11+ use TestLib;
12+ use Test::More;
13+ if (!$use_unix_sockets )
14+ {
15+ plan skip_all =>
16+ " drop directory table tests cannot run without Unix-domain sockets" ;
17+ }
18+ else
19+ {
20+ plan tests => 10;
21+ }
22+
23+ # Delete pg_hba.conf from the given node, add a new entry to it
24+ # and then execute a reload to refresh it.
25+ sub reset_pg_hba
26+ {
27+ my $node = shift ;
28+ my $hba_method = shift ;
29+
30+ unlink ($node -> data_dir . ' /pg_hba.conf' );
31+ $node -> append_conf(' pg_hba.conf' , " local all all $hba_method " );
32+ $node -> reload;
33+ return ;
34+ }
35+
36+ # Initialize primary node. Force UTF-8 encoding, so that we can use non-ASCII
37+ # characters in the passwords below.
38+ my $node = get_new_node(' primary' );
39+ my ($ret , $stdout , $stderr );
40+ $node -> init(extra => [ ' --locale=C' , ' --encoding=UTF8' ]);
41+ $node -> append_conf(' postgresql.conf' , " log_connections = on\n " );
42+ $node -> start;
43+
44+ # Create test directory table.
45+ $node -> safe_psql(
46+ ' postgres' ,
47+ " CREATE DIRECTORY TABLE test_dir1;
48+ CREATE DIRECTORY TABLE test_dir2;
49+ " );
50+
51+ # Create test roles.
52+ $node -> safe_psql(
53+ ' postgres' ,
54+ " SET client_encoding='utf8';
55+ CREATE USER test_user1;
56+ CREATE USER test_user2 SUPERUSER;
57+ " );
58+
59+ # Test CREATE DIRECTORY TABLE
60+ ($ret , $stdout , $stderr ) =
61+ $node -> role_psql(
62+ ' test_user1' ,
63+ ' postgres' ,
64+ " CREATE DIRECTORY TABLE test_dir3;"
65+ );
66+ is($ret , 0, ' create directory table succeed' );
67+
68+ ($ret , $stdout , $stderr ) =
69+ $node -> role_psql(
70+ ' test_user2' ,
71+ ' postgres' ,
72+ " CREATE DIRECTORY TABLE test_dir4;"
73+ );
74+ is($ret , 0, ' create directory table succeed' );
75+
76+ # Test DROP DIRECTORY TABLE
77+ ($ret , $stdout , $stderr ) =
78+ $node -> role_psql(
79+ ' test_user1' ,
80+ ' postgres' ,
81+ " DROP DIRECTORY TABLE test_dir1;"
82+ );
83+ is($ret , 3, ' user has no privileges to drop directory table' );
84+ like(
85+ $stderr ,
86+ qr / must be owner of directory table test_dir1/ ,
87+ ' expected error from user can not drop directory table'
88+ );
89+
90+ ($ret , $stdout , $stderr ) =
91+ $node -> role_psql(
92+ ' test_user2' ,
93+ ' postgres' ,
94+ " DROP DIRECTORY TABLE test_dir2;"
95+ );
96+ is($ret , 0, ' drop directory table succeed' );
97+
98+ ($ret , $stdout , $stderr ) =
99+ $node -> role_psql(
100+ ' test_user1' ,
101+ ' postgres' ,
102+ " DROP DIRECTORY TABLE test_dir3;"
103+ );
104+ is($ret , 0, ' drop directory table succeed' );
105+
106+ ($ret , $stdout , $stderr ) =
107+ $node -> role_psql(
108+ ' test_user1' ,
109+ ' postgres' ,
110+ " DROP DIRECTORY TABLE test_dir4;"
111+ );
112+ is($ret , 3, ' user has no privileges to drop directory table' );
113+ like(
114+ $stderr ,
115+ qr / must be owner of directory table test_dir4/ ,
116+ ' expected error from user can not drop directory table'
117+ );
118+
119+ ($ret , $stdout , $stderr ) =
120+ $node -> role_psql(
121+ ' test_user2' ,
122+ ' postgres' ,
123+ " DROP DIRECTORY TABLE test_dir4;"
124+ );
125+ is($ret , 0, ' drop directory table succeed' );
126+
127+ ($ret , $stdout , $stderr ) =
128+ $node -> role_psql(
129+ ' test_user2' ,
130+ ' postgres' ,
131+ " DROP DIRECTORY TABLE test_dir1;"
132+ );
133+ is($ret , 0, ' drop directory table succeed' );
134+
135+ # cleanup
136+ reset_pg_hba($node , ' trust' );
137+ $node -> safe_psql(
138+ ' postgres' ,
139+ " DROP USER test_user1;
140+ DROP USER test_user2;
141+ " );
0 commit comments