Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
# Copyright (c) 2024-2026, PostgreSQL Global Development Group
 
use strict;
use warnings FATAL => 'all';
use File::Path qw(mkpath);
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
my $node = PostgreSQL::Test::Cluster->new('node');
 
$node->init('auth_extra' => [ '--create-role', 'user01' ]);
 
# Create temporary directories for the extension control files
my $ext_dir = PostgreSQL::Test::Utils::tempdir();
mkpath("$ext_dir/extension");
my $ext_dir2 = PostgreSQL::Test::Utils::tempdir();
mkpath("$ext_dir2/extension");
 
my $ext_name = "test_custom_ext_paths";
create_extension($ext_name, $ext_dir);
create_extension($ext_name, $ext_dir2);
 
my $ext_name2 = "test_custom_ext_paths_using_directory";
mkpath("$ext_dir/$ext_name2");
create_extension($ext_name2, $ext_dir, $ext_name2);
 
# Make windows path use Unix slashes as canonicalize_path() is called when
# collecting extension control paths. See get_extension_control_directories().
my $ext_dir_canonicalized = $ext_dir;
$ext_dir_canonicalized =~ s!\\!/!g if $windows_os;
 
# Use the correct separator and escape \ when running on Windows.
my $sep = $windows_os ? ";" : ":";
$node->append_conf(
    'postgresql.conf', qq{
extension_control_path = '\$system$sep@{[ $windows_os ? ($ext_dir =~ s/\\/\\\\/gr) : $ext_dir ]}$sep@{[ $windows_os ? ($ext_dir2 =~ s/\\/\\\\/gr) : $ext_dir2 ]}'
});
 
# Start node
$node->start;
 
# Create a user to test permissions to read extension locations.
my $user = "user01";
$node->safe_psql('postgres', "CREATE USER $user");
 
my $ecp = $node->safe_psql('postgres', 'show extension_control_path;');
 
is( $ecp,
    "\$system$sep$ext_dir$sep$ext_dir2",
    "custom extension control directory path configured");
 
$node->safe_psql('postgres', "CREATE EXTENSION $ext_name");
$node->safe_psql('postgres', "CREATE EXTENSION $ext_name2");
 
my $ret = $node->safe_psql('postgres',
    "select * from pg_available_extensions where name = '$ext_name'");
is( $ret,
    "test_custom_ext_paths|1.0|1.0|$ext_dir_canonicalized/extension|Test extension_control_path",
    "extension is shown correctly in pg_available_extensions");
 
$ret = $node->safe_psql('postgres',
    "select * from pg_available_extension_versions where name = '$ext_name'");
is( $ret,
    "test_custom_ext_paths|1.0|t|t|f|t|||$ext_dir_canonicalized/extension|Test extension_control_path",
    "extension is shown correctly in pg_available_extension_versions");
 
$ret = $node->safe_psql('postgres',
    "select * from pg_available_extensions where name = '$ext_name2'");
is( $ret,
    "test_custom_ext_paths_using_directory|1.0|1.0|$ext_dir_canonicalized/extension|Test extension_control_path",
    "extension is shown correctly in pg_available_extensions");
 
$ret = $node->safe_psql('postgres',
    "select * from pg_available_extension_versions where name = '$ext_name2'"
);
is( $ret,
    "test_custom_ext_paths_using_directory|1.0|t|t|f|t|||$ext_dir_canonicalized/extension|Test extension_control_path",
    "extension is shown correctly in pg_available_extension_versions");
 
# Test that a non-superuser is not able to read the extension location in
# pg_available_extensions
$ret = $node->safe_psql(
    'postgres',
    "select location from pg_available_extensions where name = '$ext_name2'",
    connstr => "user=$user");
is( $ret,
    "<insufficient privilege>",
    "extension location is hidden in pg_available_extensions for users with insufficient privilege"
);
 
# Test that a non-superuser is not able to read the extension location in
# pg_available_extension_versions
$ret = $node->safe_psql(
    'postgres',
    "select location from pg_available_extension_versions where name = '$ext_name2'",
    connstr => "user=$user");
is( $ret,
    "<insufficient privilege>",
    "extension location is hidden in pg_available_extension_versions for users with insufficient privilege"
);
 
# Ensure that extensions installed in $system are still visible when used with
# custom extension control path.
$ret = $node->safe_psql('postgres',
    "select count(*) > 0 as ok from pg_available_extensions where name = 'plpgsql'"
);
is($ret, "t",
    "\$system extension is shown correctly in pg_available_extensions");
 
$ret = $node->safe_psql('postgres',
    "set extension_control_path = ''; select location from pg_available_extensions where name = 'plpgsql'"
);
is($ret, "\$system",
    "\$system location is shown correctly in pg_available_extensions with empty extension_control_path"
);
 
# Test with an extension that does not exists
my ($code, $stdout, $stderr) =
  $node->psql('postgres', "CREATE EXTENSION invalid");
is($code, 3, 'error creating an extension that does not exist');
like($stderr, qr/ERROR:  extension "invalid" is not available/);
 
sub create_extension
{
    my ($ext_name, $ext_dir, $directory) = @_;
 
    my $control_file = "$ext_dir/extension/$ext_name.control";
    my $sql_file;
 
    if (defined $directory)
    {
        $sql_file = "$ext_dir/$directory/$ext_name--1.0.sql";
    }
    else
    {
        $sql_file = "$ext_dir/extension/$ext_name--1.0.sql";
    }
 
    # Create .control .sql file
    open my $cf, '>', $control_file
      or die "Could not create control file: $!";
    print $cf "comment = 'Test extension_control_path'\n";
    print $cf "default_version = '1.0'\n";
    print $cf "relocatable = true\n";
    print $cf "directory = $directory" if defined $directory;
    close $cf;
 
    # Create --1.0.sql file
    open my $sqlf, '>', $sql_file or die "Could not create sql file: $!";
    print $sqlf "/* $sql_file */\n";
    print $sqlf
      "-- complain if script is sourced in psql, rather than via CREATE EXTENSION\n";
    print $sqlf
      qq'\\echo Use "CREATE EXTENSION $ext_name" to load this file. \\quit\n';
    close $sqlf;
}
 
done_testing();