github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/credentials/tls/certprovider/pemfile/builder_test.go (about) 1 /* 2 * 3 * Copyright 2020 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 pemfile 20 21 import ( 22 "encoding/json" 23 "testing" 24 ) 25 26 func TestParseConfig(t *testing.T) { 27 tests := []struct { 28 desc string 29 input interface{} 30 wantOutput string 31 wantErr bool 32 }{ 33 { 34 desc: "non JSON input", 35 input: new(int), 36 wantErr: true, 37 }, 38 { 39 desc: "invalid JSON", 40 input: json.RawMessage(`bad bad json`), 41 wantErr: true, 42 }, 43 { 44 desc: "JSON input does not match expected", 45 input: json.RawMessage(`["foo": "bar"]`), 46 wantErr: true, 47 }, 48 { 49 desc: "no credential files", 50 input: json.RawMessage(`{}`), 51 wantErr: true, 52 }, 53 { 54 desc: "only cert file", 55 input: json.RawMessage(` 56 { 57 "certificate_file": "/a/b/cert.pem" 58 }`), 59 wantErr: true, 60 }, 61 { 62 desc: "only key file", 63 input: json.RawMessage(` 64 { 65 "private_key_file": "/a/b/key.pem" 66 }`), 67 wantErr: true, 68 }, 69 { 70 desc: "cert and key in different directories", 71 input: json.RawMessage(` 72 { 73 "certificate_file": "/b/a/cert.pem", 74 "private_key_file": "/a/b/key.pem" 75 }`), 76 wantErr: true, 77 }, 78 { 79 desc: "bad refresh duration", 80 input: json.RawMessage(` 81 { 82 "certificate_file": "/a/b/cert.pem", 83 "private_key_file": "/a/b/key.pem", 84 "ca_certificate_file": "/a/b/ca.pem", 85 "refresh_interval": "duration" 86 }`), 87 wantErr: true, 88 }, 89 { 90 desc: "good config with default refresh interval", 91 input: json.RawMessage(` 92 { 93 "certificate_file": "/a/b/cert.pem", 94 "private_key_file": "/a/b/key.pem", 95 "ca_certificate_file": "/a/b/ca.pem" 96 }`), 97 wantOutput: "file_watcher:/a/b/cert.pem:/a/b/key.pem:/a/b/ca.pem:10m0s", 98 }, 99 { 100 desc: "good config", 101 input: json.RawMessage(` 102 { 103 "certificate_file": "/a/b/cert.pem", 104 "private_key_file": "/a/b/key.pem", 105 "ca_certificate_file": "/a/b/ca.pem", 106 "refresh_interval": "200s" 107 }`), 108 wantOutput: "file_watcher:/a/b/cert.pem:/a/b/key.pem:/a/b/ca.pem:3m20s", 109 }, 110 } 111 112 for _, test := range tests { 113 t.Run(test.desc, func(t *testing.T) { 114 builder := &pluginBuilder{} 115 116 bc, err := builder.ParseConfig(test.input) 117 if (err != nil) != test.wantErr { 118 t.Fatalf("ParseConfig(%+v) failed: %v", test.input, err) 119 } 120 if test.wantErr { 121 return 122 } 123 124 gotConfig := bc.String() 125 if gotConfig != test.wantOutput { 126 t.Fatalf("ParseConfig(%v) = %s, want %s", test.input, gotConfig, test.wantOutput) 127 } 128 }) 129 } 130 }