mirror of https://github.com/hashicorp/boundary
This adds a SQL function that generates a secure random ID of at least 14 characters suitable for use as a public or private ID. IDs generated with the function will only contain characters from the unreserved character set defined in RFC 3986 Section 2.3 making them safe for use in a URL.pull/3251/head
parent
94f77d36b3
commit
327d950a1e
@ -0,0 +1,34 @@
|
||||
-- Copyright (c) HashiCorp, Inc.
|
||||
-- SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
begin;
|
||||
select plan(7);
|
||||
-- Verify the function exists and is declared properly
|
||||
select has_function('wt_url_safe_id');
|
||||
select volatility_is('wt_url_safe_id', 'volatile');
|
||||
select isnt_strict('wt_url_safe_id');
|
||||
|
||||
create table test_table (
|
||||
id wt_url_safe_id default wt_url_safe_id() primary key,
|
||||
test_num integer not null
|
||||
);
|
||||
|
||||
create function test_setup_data(count integer) returns integer
|
||||
as $$
|
||||
begin
|
||||
for i in 1..count loop
|
||||
insert into test_table (test_num) values (i);
|
||||
end loop;
|
||||
return count;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
select is(test_setup_data(100000), 100000);
|
||||
select is(count(*), 100000::bigint, 'test_table should have 100000 rows') from test_table;
|
||||
select is(count(*), 0::bigint, 'no id should be longer than 14 characters')
|
||||
from test_table where length(id) > 14;
|
||||
select is(count(*), 0::bigint, 'no id should be shorter than 14 characters')
|
||||
from test_table where length(id) < 14;
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
Loading…
Reference in new issue