|  | 
|  | 1 | +// Copyright 2023 Datafuse Labs. | 
|  | 2 | +// | 
|  | 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +// you may not use this file except in compliance with the License. | 
|  | 5 | +// You may obtain a copy of the License at | 
|  | 6 | +// | 
|  | 7 | +//     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +// | 
|  | 9 | +// Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +// distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +// See the License for the specific language governing permissions and | 
|  | 13 | +// limitations under the License. | 
|  | 14 | + | 
|  | 15 | +use std::collections::HashSet; | 
|  | 16 | + | 
|  | 17 | +use chrono::DateTime; | 
|  | 18 | +use chrono::Utc; | 
|  | 19 | +use databend_common_meta_app as mt; | 
|  | 20 | +use databend_common_meta_app::principal::OwnershipObject; | 
|  | 21 | +use databend_common_meta_app::principal::UserGrantSet; | 
|  | 22 | +use databend_common_meta_app::principal::UserPrivilegeType; | 
|  | 23 | +use enumflags2::make_bitflags; | 
|  | 24 | +use fastrace::func_name; | 
|  | 25 | + | 
|  | 26 | +use crate::common; | 
|  | 27 | + | 
|  | 28 | +// These bytes are built when a new version in introduced, | 
|  | 29 | +// and are kept for backward compatibility test. | 
|  | 30 | +// | 
|  | 31 | +// ************************************************************* | 
|  | 32 | +// * These messages should never be updated,                   * | 
|  | 33 | +// * only be added when a new version is added,                * | 
|  | 34 | +// * or be removed when an old version is no longer supported. * | 
|  | 35 | +// ************************************************************* | 
|  | 36 | +// | 
|  | 37 | + | 
|  | 38 | +#[test] | 
|  | 39 | +fn test_decode_v120_grant_object() -> anyhow::Result<()> { | 
|  | 40 | +    let role_info_v120 = vec![ | 
|  | 41 | +        10, 2, 114, 49, 18, 172, 1, 10, 21, 10, 8, 10, 0, 160, 6, 120, 168, 6, 24, 16, 128, 128, | 
|  | 42 | +        128, 1, 160, 6, 120, 168, 6, 24, 10, 31, 10, 21, 18, 13, 10, 7, 100, 101, 102, 97, 117, | 
|  | 43 | +        108, 116, 18, 2, 100, 98, 160, 6, 120, 168, 6, 24, 16, 2, 160, 6, 120, 168, 6, 24, 10, 35, | 
|  | 44 | +        10, 25, 26, 17, 10, 7, 100, 101, 102, 97, 117, 108, 116, 18, 2, 100, 98, 26, 2, 116, 98, | 
|  | 45 | +        160, 6, 120, 168, 6, 24, 16, 2, 160, 6, 120, 168, 6, 24, 10, 22, 10, 12, 34, 4, 10, 2, 102, | 
|  | 46 | +        49, 160, 6, 120, 168, 6, 24, 16, 1, 160, 6, 120, 168, 6, 24, 10, 24, 10, 12, 42, 4, 10, 2, | 
|  | 47 | +        115, 49, 160, 6, 120, 168, 6, 24, 16, 128, 128, 32, 160, 6, 120, 168, 6, 24, 10, 21, 10, 8, | 
|  | 48 | +        10, 0, 160, 6, 120, 168, 6, 24, 16, 254, 255, 191, 1, 160, 6, 120, 168, 6, 24, 160, 6, 120, | 
|  | 49 | +        168, 6, 24, 26, 23, 49, 57, 55, 48, 45, 48, 49, 45, 48, 49, 32, 48, 48, 58, 48, 48, 58, 48, | 
|  | 50 | +        48, 32, 85, 84, 67, 34, 23, 49, 57, 55, 48, 45, 48, 49, 45, 48, 49, 32, 48, 48, 58, 48, 48, | 
|  | 51 | +        58, 48, 48, 32, 85, 84, 67, 160, 6, 120, 168, 6, 24, | 
|  | 52 | +    ]; | 
|  | 53 | +    let want = || mt::principal::RoleInfo { | 
|  | 54 | +        name: "r1".to_string(), | 
|  | 55 | +        grants: UserGrantSet::new( | 
|  | 56 | +            vec![ | 
|  | 57 | +                mt::principal::GrantEntry::new( | 
|  | 58 | +                    mt::principal::GrantObject::Global, | 
|  | 59 | +                    make_bitflags!(UserPrivilegeType::{CreateWarehouse}), | 
|  | 60 | +                ), | 
|  | 61 | +                mt::principal::GrantEntry::new( | 
|  | 62 | +                    mt::principal::GrantObject::Database("default".to_string(), "db".to_string()), | 
|  | 63 | +                    make_bitflags!(UserPrivilegeType::{Create}), | 
|  | 64 | +                ), | 
|  | 65 | +                mt::principal::GrantEntry::new( | 
|  | 66 | +                    mt::principal::GrantObject::Table( | 
|  | 67 | +                        "default".to_string(), | 
|  | 68 | +                        "db".to_string(), | 
|  | 69 | +                        "tb".to_string(), | 
|  | 70 | +                    ), | 
|  | 71 | +                    make_bitflags!(UserPrivilegeType::{Create}), | 
|  | 72 | +                ), | 
|  | 73 | +                mt::principal::GrantEntry::new( | 
|  | 74 | +                    mt::principal::GrantObject::UDF("f1".to_string()), | 
|  | 75 | +                    make_bitflags!(UserPrivilegeType::{Usage}), | 
|  | 76 | +                ), | 
|  | 77 | +                mt::principal::GrantEntry::new( | 
|  | 78 | +                    mt::principal::GrantObject::Stage("s1".to_string()), | 
|  | 79 | +                    make_bitflags!(UserPrivilegeType::{Write}), | 
|  | 80 | +                ), | 
|  | 81 | +                // test new global privilege CreateWarehouse | 
|  | 82 | +                mt::principal::GrantEntry::new( | 
|  | 83 | +                    mt::principal::GrantObject::Global, | 
|  | 84 | +                    make_bitflags!(UserPrivilegeType::{Create | Select | Insert | Update | Delete | Drop | Alter | Super | CreateUser | DropUser | CreateRole | DropRole | Grant | CreateStage | Set | CreateDataMask | Ownership | Read | Write | CreateWarehouse }), | 
|  | 85 | +                ), | 
|  | 86 | +            ], | 
|  | 87 | +            HashSet::new(), | 
|  | 88 | +        ), | 
|  | 89 | +        created_on: DateTime::<Utc>::default(), | 
|  | 90 | +        update_on: DateTime::<Utc>::default(), | 
|  | 91 | +    }; | 
|  | 92 | + | 
|  | 93 | +    common::test_pb_from_to(func_name!(), want())?; | 
|  | 94 | +    common::test_load_old(func_name!(), role_info_v120.as_slice(), 120, want())?; | 
|  | 95 | + | 
|  | 96 | +    Ok(()) | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +#[test] | 
|  | 100 | +fn test_decode_v120_ownership() -> anyhow::Result<()> { | 
|  | 101 | +    let ownership_info_v120 = vec![ | 
|  | 102 | +        10, 2, 114, 49, 18, 19, 42, 11, 10, 9, 97, 117, 110, 105, 113, 117, 101, 105, 100, 160, 6, | 
|  | 103 | +        120, 168, 6, 24, 160, 6, 120, 168, 6, 24, | 
|  | 104 | +    ]; | 
|  | 105 | + | 
|  | 106 | +    let want = || mt::principal::OwnershipInfo { | 
|  | 107 | +        role: "r1".to_string(), | 
|  | 108 | +        object: OwnershipObject::Warehouse { | 
|  | 109 | +            id: "auniqueid".to_string(), | 
|  | 110 | +        }, | 
|  | 111 | +    }; | 
|  | 112 | +    common::test_pb_from_to(func_name!(), want())?; | 
|  | 113 | +    common::test_load_old(func_name!(), ownership_info_v120.as_slice(), 120, want())?; | 
|  | 114 | + | 
|  | 115 | +    Ok(()) | 
|  | 116 | +} | 
0 commit comments