LCOV - differential code coverage report
Current view: top level - src/test/modules/test_rls_hooks - test_rls_hooks.c (source / functions) Coverage Total Hit CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 100.0 % 53 53 53
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 4 4 4
Baseline: lcov-20250906-005545-baseline Branches: 100.0 % 8 8 8
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 100.0 % 53 53 53
Function coverage date bins:
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(360..) days: 100.0 % 8 8 8

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*--------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * test_rls_hooks.c
                                  4                 :                :  *      Code for testing RLS hooks.
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2015-2025, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *      src/test/modules/test_rls_hooks/test_rls_hooks.c
                                 10                 :                :  *
                                 11                 :                :  * -------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : 
                                 14                 :                : #include "postgres.h"
                                 15                 :                : 
                                 16                 :                : #include "catalog/pg_type.h"
                                 17                 :                : #include "fmgr.h"
                                 18                 :                : #include "nodes/makefuncs.h"
                                 19                 :                : #include "parser/parse_clause.h"
                                 20                 :                : #include "parser/parse_collate.h"
                                 21                 :                : #include "parser/parse_node.h"
                                 22                 :                : #include "parser/parse_relation.h"
                                 23                 :                : #include "rewrite/rowsecurity.h"
                                 24                 :                : #include "test_rls_hooks.h"
                                 25                 :                : #include "utils/acl.h"
                                 26                 :                : #include "utils/rel.h"
                                 27                 :                : #include "utils/relcache.h"
                                 28                 :                : 
 3790 sfrost@snowman.net         29                 :CBC           1 : PG_MODULE_MAGIC;
                                 30                 :                : 
                                 31                 :                : /* Install hooks */
                                 32                 :                : void
 3759 bruce@momjian.us           33                 :              1 : _PG_init(void)
                                 34                 :                : {
                                 35                 :                :     /* Set our hooks */
 3790 sfrost@snowman.net         36                 :              1 :     row_security_policy_hook_permissive = test_rls_hooks_permissive;
                                 37                 :              1 :     row_security_policy_hook_restrictive = test_rls_hooks_restrictive;
                                 38                 :              1 : }
                                 39                 :                : 
                                 40                 :                : /*
                                 41                 :                :  * Return permissive policies to be added
                                 42                 :                :  */
                                 43                 :                : List *
                                 44                 :             30 : test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
                                 45                 :                : {
 3759 bruce@momjian.us           46                 :             30 :     List       *policies = NIL;
                                 47                 :             30 :     RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
                                 48                 :                :     Datum       role;
                                 49                 :                :     FuncCall   *n;
                                 50                 :                :     Node       *e;
                                 51                 :                :     ColumnRef  *c;
                                 52                 :                :     ParseState *qual_pstate;
                                 53                 :                :     ParseNamespaceItem *nsitem;
                                 54                 :                : 
 2339 michael@paquier.xyz        55         [ +  + ]:             30 :     if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
                                 56         [ +  + ]:             21 :         strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
 3790 sfrost@snowman.net         57                 :             10 :         return NIL;
                                 58                 :                : 
                                 59                 :             20 :     qual_pstate = make_parsestate(NULL);
                                 60                 :                : 
 2074 tgl@sss.pgh.pa.us          61                 :             20 :     nsitem = addRangeTableEntryForRelation(qual_pstate,
                                 62                 :                :                                            relation, AccessShareLock,
                                 63                 :                :                                            NULL, false, false);
                                 64                 :             20 :     addNSItemToQuery(qual_pstate, nsitem, false, true, true);
                                 65                 :                : 
 3790 sfrost@snowman.net         66                 :             20 :     role = ObjectIdGetDatum(ACL_ID_PUBLIC);
                                 67                 :                : 
                                 68                 :             20 :     policy->policy_name = pstrdup("extension policy");
                                 69                 :             20 :     policy->polcmd = '*';
 1163 peter@eisentraut.org       70                 :             20 :     policy->roles = construct_array_builtin(&role, 1, OIDOID);
                                 71                 :                : 
                                 72                 :                :     /*
                                 73                 :                :      * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
                                 74                 :                :      * sizeof(bool), BoolGetDatum(true), false, true);
                                 75                 :                :      */
                                 76                 :                : 
 3790 sfrost@snowman.net         77                 :             20 :     n = makeFuncCall(list_make2(makeString("pg_catalog"),
                                 78                 :                :                                 makeString("current_user")),
                                 79                 :                :                      NIL,
                                 80                 :                :                      COERCE_EXPLICIT_CALL,
                                 81                 :                :                      -1);
                                 82                 :                : 
                                 83                 :             20 :     c = makeNode(ColumnRef);
                                 84                 :             20 :     c->fields = list_make1(makeString("username"));
                                 85                 :             20 :     c->location = 0;
                                 86                 :                : 
 3759 bruce@momjian.us           87                 :             20 :     e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
                                 88                 :                : 
                                 89                 :             20 :     policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
                                 90                 :                :                                                  EXPR_KIND_POLICY,
                                 91                 :                :                                                  "POLICY");
                                 92                 :                :     /* Fix up collation information */
 2461 tgl@sss.pgh.pa.us          93                 :             20 :     assign_expr_collations(qual_pstate, (Node *) policy->qual);
                                 94                 :                : 
 3790 sfrost@snowman.net         95                 :             20 :     policy->with_check_qual = copyObject(policy->qual);
                                 96                 :             20 :     policy->hassublinks = false;
                                 97                 :                : 
                                 98                 :             20 :     policies = list_make1(policy);
                                 99                 :                : 
                                100                 :             20 :     return policies;
                                101                 :                : }
                                102                 :                : 
                                103                 :                : /*
                                104                 :                :  * Return restrictive policies to be added
                                105                 :                :  *
                                106                 :                :  * Note that a permissive policy must exist or the default-deny policy
                                107                 :                :  * will be included and nothing will be visible.  If no filtering should
                                108                 :                :  * be done except for the restrictive policy, then a single "USING (true)"
                                109                 :                :  * permissive policy can be used; see the regression tests.
                                110                 :                :  */
                                111                 :                : List *
                                112                 :             30 : test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
                                113                 :                : {
 3759 bruce@momjian.us          114                 :             30 :     List       *policies = NIL;
                                115                 :             30 :     RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
                                116                 :                :     Datum       role;
                                117                 :                :     FuncCall   *n;
                                118                 :                :     Node       *e;
                                119                 :                :     ColumnRef  *c;
                                120                 :                :     ParseState *qual_pstate;
                                121                 :                :     ParseNamespaceItem *nsitem;
                                122                 :                : 
 2339 michael@paquier.xyz       123         [ +  + ]:             30 :     if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 &&
                                124         [ +  + ]:             20 :         strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
 3790 sfrost@snowman.net        125                 :              9 :         return NIL;
                                126                 :                : 
                                127                 :             21 :     qual_pstate = make_parsestate(NULL);
                                128                 :                : 
 2074 tgl@sss.pgh.pa.us         129                 :             21 :     nsitem = addRangeTableEntryForRelation(qual_pstate,
                                130                 :                :                                            relation, AccessShareLock,
                                131                 :                :                                            NULL, false, false);
                                132                 :             21 :     addNSItemToQuery(qual_pstate, nsitem, false, true, true);
                                133                 :                : 
 3790 sfrost@snowman.net        134                 :             21 :     role = ObjectIdGetDatum(ACL_ID_PUBLIC);
                                135                 :                : 
                                136                 :             21 :     policy->policy_name = pstrdup("extension policy");
                                137                 :             21 :     policy->polcmd = '*';
 1163 peter@eisentraut.org      138                 :             21 :     policy->roles = construct_array_builtin(&role, 1, OIDOID);
                                139                 :                : 
 3790 sfrost@snowman.net        140                 :             21 :     n = makeFuncCall(list_make2(makeString("pg_catalog"),
                                141                 :                :                                 makeString("current_user")),
                                142                 :                :                      NIL,
                                143                 :                :                      COERCE_EXPLICIT_CALL,
                                144                 :                :                      -1);
                                145                 :                : 
                                146                 :             21 :     c = makeNode(ColumnRef);
                                147                 :             21 :     c->fields = list_make1(makeString("supervisor"));
                                148                 :             21 :     c->location = 0;
                                149                 :                : 
 3759 bruce@momjian.us          150                 :             21 :     e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
                                151                 :                : 
                                152                 :             21 :     policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
                                153                 :                :                                                  EXPR_KIND_POLICY,
                                154                 :                :                                                  "POLICY");
                                155                 :                :     /* Fix up collation information */
 2461 tgl@sss.pgh.pa.us         156                 :             21 :     assign_expr_collations(qual_pstate, (Node *) policy->qual);
                                157                 :                : 
 3790 sfrost@snowman.net        158                 :             21 :     policy->with_check_qual = copyObject(policy->qual);
                                159                 :             21 :     policy->hassublinks = false;
                                160                 :                : 
                                161                 :             21 :     policies = list_make1(policy);
                                162                 :                : 
                                163                 :             21 :     return policies;
                                164                 :                : }
        

Generated by: LCOV version 2.4-beta