github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/authz/sdk_server_interceptors_test.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package authz_test 20 21 import ( 22 "fmt" 23 "io/ioutil" 24 "os" 25 "path" 26 "testing" 27 "time" 28 29 "github.com/hxx258456/ccgo/grpc/authz" 30 ) 31 32 func createTmpPolicyFile(t *testing.T, dirSuffix string, policy []byte) string { 33 t.Helper() 34 35 // Create a temp directory. Passing an empty string for the first argument 36 // uses the system temp directory. 37 dir, err := ioutil.TempDir("", dirSuffix) 38 if err != nil { 39 t.Fatalf("ioutil.TempDir() failed: %v", err) 40 } 41 t.Logf("Using tmpdir: %s", dir) 42 // Write policy into file. 43 filename := path.Join(dir, "policy.json") 44 if err := ioutil.WriteFile(filename, policy, os.ModePerm); err != nil { 45 t.Fatalf("ioutil.WriteFile(%q) failed: %v", filename, err) 46 } 47 t.Logf("Wrote policy %s to file at %s", string(policy), filename) 48 return filename 49 } 50 51 func (s) TestNewStatic(t *testing.T) { 52 tests := map[string]struct { 53 authzPolicy string 54 wantErr error 55 }{ 56 "InvalidPolicyFailsToCreateInterceptor": { 57 authzPolicy: `{}`, 58 wantErr: fmt.Errorf(`"name" is not present`), 59 }, 60 "ValidPolicyCreatesInterceptor": { 61 authzPolicy: `{ 62 "name": "authz", 63 "allow_rules": 64 [ 65 { 66 "name": "allow_all" 67 } 68 ] 69 }`, 70 }, 71 } 72 for name, test := range tests { 73 t.Run(name, func(t *testing.T) { 74 if _, err := authz.NewStatic(test.authzPolicy); fmt.Sprint(err) != fmt.Sprint(test.wantErr) { 75 t.Fatalf("NewStatic(%v) returned err: %v, want err: %v", test.authzPolicy, err, test.wantErr) 76 } 77 }) 78 } 79 } 80 81 func (s) TestNewFileWatcher(t *testing.T) { 82 tests := map[string]struct { 83 authzPolicy string 84 refreshDuration time.Duration 85 wantErr error 86 }{ 87 "InvalidRefreshDurationFailsToCreateInterceptor": { 88 refreshDuration: time.Duration(0), 89 wantErr: fmt.Errorf("requires refresh interval(0s) greater than 0s"), 90 }, 91 "InvalidPolicyFailsToCreateInterceptor": { 92 authzPolicy: `{}`, 93 refreshDuration: time.Duration(1), 94 wantErr: fmt.Errorf(`"name" is not present`), 95 }, 96 "ValidPolicyCreatesInterceptor": { 97 authzPolicy: `{ 98 "name": "authz", 99 "allow_rules": 100 [ 101 { 102 "name": "allow_all" 103 } 104 ] 105 }`, 106 refreshDuration: time.Duration(1), 107 }, 108 } 109 for name, test := range tests { 110 t.Run(name, func(t *testing.T) { 111 file := createTmpPolicyFile(t, name, []byte(test.authzPolicy)) 112 i, err := authz.NewFileWatcher(file, test.refreshDuration) 113 if fmt.Sprint(err) != fmt.Sprint(test.wantErr) { 114 t.Fatalf("NewFileWatcher(%v) returned err: %v, want err: %v", test.authzPolicy, err, test.wantErr) 115 } 116 if i != nil { 117 i.Close() 118 } 119 }) 120 } 121 }