|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.auth; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.auth.entity.PrivilegeType; |
| 23 | +import org.apache.iotdb.commons.auth.entity.User; |
| 24 | +import org.apache.iotdb.db.queryengine.plan.relational.security.TreeAccessCheckContext; |
| 25 | +import org.apache.iotdb.db.queryengine.plan.relational.security.TreeAccessCheckVisitor; |
| 26 | +import org.apache.iotdb.db.queryengine.plan.statement.AuthorType; |
| 27 | +import org.apache.iotdb.db.queryengine.plan.statement.sys.AuthorStatement; |
| 28 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 29 | + |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.mockito.Mockito; |
| 35 | + |
| 36 | +public class TreeAccessTest { |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setup() { |
| 40 | + AuthorityChecker.getAuthorityFetcher().getAuthorCache().invalidAllCache(); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void teardown() { |
| 45 | + AuthorityChecker.getAuthorityFetcher().getAuthorCache().invalidAllCache(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void test1() { |
| 50 | + User mockUser = Mockito.mock(User.class); |
| 51 | + Mockito.when(mockUser.getName()).thenReturn("user1"); |
| 52 | + Mockito.when(mockUser.getUserId()).thenReturn(10000L); |
| 53 | + Mockito.when(mockUser.checkSysPriGrantOpt(PrivilegeType.SYSTEM)).thenReturn(false); |
| 54 | + AuthorityChecker.getAuthorityFetcher() |
| 55 | + .getAuthorCache() |
| 56 | + .putUserCache(mockUser.getName(), mockUser); |
| 57 | + User mockUser2 = Mockito.mock(User.class); |
| 58 | + Mockito.when(mockUser2.getName()).thenReturn("user2"); |
| 59 | + Mockito.when(mockUser2.getUserId()).thenReturn(10001L); |
| 60 | + AuthorityChecker.getAuthorityFetcher() |
| 61 | + .getAuthorCache() |
| 62 | + .putUserCache(mockUser.getName(), mockUser); |
| 63 | + AuthorityChecker.getAuthorityFetcher() |
| 64 | + .getAuthorCache() |
| 65 | + .putUserCache(mockUser2.getName(), mockUser2); |
| 66 | + TreeAccessCheckVisitor treeAccessCheckVisitor = new TreeAccessCheckVisitor(); |
| 67 | + |
| 68 | + AuthorStatement authorStatement = new AuthorStatement(AuthorType.GRANT_USER); |
| 69 | + authorStatement.setPrivilegeList(new String[] {"SYSTEM"}); |
| 70 | + authorStatement.setUserName("user2"); |
| 71 | + authorStatement.setGrantOpt(true); |
| 72 | + Assert.assertEquals( |
| 73 | + TSStatusCode.NO_PERMISSION.getStatusCode(), |
| 74 | + treeAccessCheckVisitor |
| 75 | + .visitAuthor(authorStatement, new TreeAccessCheckContext(10000L, "user1", "")) |
| 76 | + .getCode()); |
| 77 | + Mockito.when(mockUser.checkSysPriGrantOpt(PrivilegeType.SYSTEM)).thenReturn(true); |
| 78 | + Assert.assertEquals( |
| 79 | + TSStatusCode.SUCCESS_STATUS.getStatusCode(), |
| 80 | + treeAccessCheckVisitor |
| 81 | + .visitAuthor(authorStatement, new TreeAccessCheckContext(10000L, "user1", "")) |
| 82 | + .getCode()); |
| 83 | + } |
| 84 | +} |
0 commit comments