istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/keycertbundle/watcher_test.go (about) 1 // Copyright Istio Authors 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 package keycertbundle 16 17 import ( 18 "bytes" 19 "os" 20 "path" 21 "testing" 22 ) 23 24 func TestWatcher(t *testing.T) { 25 watcher := NewWatcher() 26 27 // 1. no key cert bundle 28 _, watch1 := watcher.AddWatcher() 29 select { 30 case bundle := <-watch1: 31 t.Errorf("watched unexpected keyCertBundle: %v", bundle) 32 return 33 default: 34 } 35 36 key := []byte("key") 37 cert := []byte("cert") 38 ca := []byte("caBundle") 39 // 2. set key cert bundle 40 watcher.SetAndNotify(key, cert, ca) 41 select { 42 case <-watch1: 43 keyCertBundle := watcher.GetKeyCertBundle() 44 if !bytes.Equal(keyCertBundle.KeyPem, key) || !bytes.Equal(keyCertBundle.CertPem, cert) || 45 !bytes.Equal(keyCertBundle.CABundle, ca) { 46 t.Errorf("got wrong keyCertBundle %v", keyCertBundle) 47 } 48 default: 49 t.Errorf("watched non keyCertBundle") 50 } 51 52 // 3. set new key cert bundle, notify all watchers 53 _, watch2 := watcher.AddWatcher() 54 55 key = []byte("key2") 56 cert = []byte("cert2") 57 ca = []byte("caBundle2") 58 watcher.SetAndNotify(key, cert, ca) 59 select { 60 case <-watch1: 61 keyCertBundle := watcher.GetKeyCertBundle() 62 if !bytes.Equal(keyCertBundle.KeyPem, key) || !bytes.Equal(keyCertBundle.CertPem, cert) || 63 !bytes.Equal(keyCertBundle.CABundle, ca) { 64 t.Errorf("got wrong keyCertBundle %v", keyCertBundle) 65 } 66 default: 67 t.Errorf("watcher1 watched non keyCertBundle") 68 } 69 select { 70 case <-watch2: 71 keyCertBundle := watcher.GetKeyCertBundle() 72 if !bytes.Equal(keyCertBundle.KeyPem, key) || !bytes.Equal(keyCertBundle.CertPem, cert) || 73 !bytes.Equal(keyCertBundle.CABundle, ca) { 74 t.Errorf("got wrong keyCertBundle %v", keyCertBundle) 75 } 76 default: 77 t.Errorf("watcher2 watched non keyCertBundle") 78 } 79 } 80 81 func TestWatcherFromFile(t *testing.T) { 82 watcher := NewWatcher() 83 84 // 1. no key cert bundle 85 _, watch1 := watcher.AddWatcher() 86 select { 87 case bundle := <-watch1: 88 t.Errorf("watched unexpected keyCertBundle: %v", bundle) 89 return 90 default: 91 } 92 93 tmpDir := t.TempDir() 94 95 key := []byte("key") 96 cert := []byte("cert") 97 ca := []byte("caBundle") 98 keyFile := path.Join(tmpDir, "key.pem") 99 certFile := path.Join(tmpDir, "cert.pem") 100 caFile := path.Join(tmpDir, "ca.pem") 101 102 os.WriteFile(keyFile, key, os.ModePerm) 103 os.WriteFile(certFile, cert, os.ModePerm) 104 os.WriteFile(caFile, ca, os.ModePerm) 105 106 // 2. set key cert bundle 107 watcher.SetFromFilesAndNotify(keyFile, certFile, caFile) 108 select { 109 case <-watch1: 110 keyCertBundle := watcher.GetKeyCertBundle() 111 if !bytes.Equal(keyCertBundle.KeyPem, key) || !bytes.Equal(keyCertBundle.CertPem, cert) || 112 !bytes.Equal(keyCertBundle.CABundle, ca) { 113 t.Errorf("got wrong keyCertBundle %v", keyCertBundle) 114 } 115 default: 116 t.Errorf("watched non keyCertBundle") 117 } 118 }