1// Copyright 2022 Google Inc. 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//////////////////////////////////////////////////////////////////////////////// 16 17package fakemonitoring_test 18 19import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/google/tink/go/monitoring" 24 "github.com/google/tink/go/testing/fakemonitoring" 25) 26 27func TestCreateNewClient(t *testing.T) { 28 client := fakemonitoring.NewClient("fake-client") 29 if client.Name != "fake-client" { 30 t.Errorf("client.Name = %q, want %q", client.Name, "fake-client") 31 } 32} 33 34func TestCreateNewLoggers(t *testing.T) { 35 client := fakemonitoring.NewClient("client") 36 ctxEnc := monitoring.NewContext("aead", "encrypt", monitoring.NewKeysetInfo(nil, 0, []*monitoring.Entry{})) 37 encLogger, err := client.NewLogger(ctxEnc) 38 if err != nil { 39 t.Fatalf("NewLogger() err = %v, want nil", err) 40 } 41 ctxDec := monitoring.NewContext("aead", "decrypt", monitoring.NewKeysetInfo(nil, 0, []*monitoring.Entry{})) 42 decLogger, err := client.NewLogger(ctxDec) 43 if err != nil { 44 t.Fatalf("NewLogger() err = %v, want nil", err) 45 } 46 gotEnc := encLogger.(*fakemonitoring.Logger).Context 47 if !cmp.Equal(gotEnc, ctxEnc) { 48 t.Errorf("got = %v, want = %v", gotEnc, ctxEnc) 49 } 50 gotDec := decLogger.(*fakemonitoring.Logger).Context 51 if !cmp.Equal(gotDec, ctxDec) { 52 t.Errorf("got = %v, want = %v", gotDec, ctxDec) 53 } 54} 55 56func TestLoggerRecordsEvents(t *testing.T) { 57 client := fakemonitoring.NewClient("client") 58 ctxEnc := monitoring.NewContext("aead", "encrypt", monitoring.NewKeysetInfo(nil, 0, []*monitoring.Entry{})) 59 logger, err := client.NewLogger(ctxEnc) 60 if err != nil { 61 t.Fatalf("NewLogger() err = %v, want nil", err) 62 } 63 logger.Log(5, 8) 64 logger.Log(10, 3) 65 logger.Log(12, 3) 66 got := client.Events() 67 want := []*fakemonitoring.LogEvent{ 68 { 69 Context: ctxEnc, 70 KeyID: 5, 71 NumBytes: 8, 72 }, 73 { 74 Context: ctxEnc, 75 KeyID: 10, 76 NumBytes: 3, 77 }, 78 { 79 Context: ctxEnc, 80 KeyID: 12, 81 NumBytes: 3, 82 }, 83 } 84 if !cmp.Equal(got, want) { 85 t.Errorf("Events() = %v, want = %v", got, want) 86 } 87} 88 89func TestLogFailureRecordsFailure(t *testing.T) { 90 client := fakemonitoring.NewClient("client") 91 ctxEnc := monitoring.NewContext("aead", "encrypt", monitoring.NewKeysetInfo(nil, 0, []*monitoring.Entry{})) 92 logger, err := client.NewLogger(ctxEnc) 93 if err != nil { 94 t.Fatalf("NewLogger() err = %v, want nil", err) 95 } 96 logger.LogFailure() 97 logger.LogFailure() 98 got := client.Failures() 99 want := []*fakemonitoring.LogFailure{ 100 { 101 Context: ctxEnc, 102 }, 103 { 104 Context: ctxEnc, 105 }, 106 } 107 if !cmp.Equal(got, want) { 108 t.Errorf("Failures() = %v, want = %v", got, want) 109 } 110} 111