|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build requirefips |
| 19 | + |
| 20 | +package beater |
| 21 | + |
| 22 | +import ( |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + "github.com/elastic/beats/v7/libbeat/cfgfile" |
| 28 | +) |
| 29 | + |
| 30 | +type fipsUnawareInput struct{} |
| 31 | + |
| 32 | +func newFIPSUnawareInput() *fipsUnawareInput { return &fipsUnawareInput{} } |
| 33 | +func (f *fipsUnawareInput) String() string { return "fips_unaware_input" } |
| 34 | +func (f *fipsUnawareInput) Start() {} |
| 35 | +func (f *fipsUnawareInput) Stop() {} |
| 36 | + |
| 37 | +type fipsAwareInput struct{ isFIPSCapable bool } |
| 38 | + |
| 39 | +func newFIPSAwareInput(isFIPSCapable bool) *fipsAwareInput { |
| 40 | + return &fipsAwareInput{isFIPSCapable: isFIPSCapable} |
| 41 | +} |
| 42 | +func (f *fipsAwareInput) String() string { return "fips_aware_input" } |
| 43 | +func (f *fipsAwareInput) Start() {} |
| 44 | +func (f *fipsAwareInput) Stop() {} |
| 45 | +func (f *fipsAwareInput) IsFIPSCapable() bool { return f.isFIPSCapable } |
| 46 | + |
| 47 | +func TestCheckFIPSCapability(t *testing.T) { |
| 48 | + tests := map[string]struct { |
| 49 | + runner cfgfile.Runner |
| 50 | + expectedErr string |
| 51 | + }{ |
| 52 | + "input_is_not_fips_aware": { |
| 53 | + runner: newFIPSUnawareInput(), |
| 54 | + expectedErr: "", |
| 55 | + }, |
| 56 | + "input_is_fips_aware_but_not_fips_capable": { |
| 57 | + runner: newFIPSAwareInput(false), |
| 58 | + expectedErr: "running a FIPS-capable distribution but input [fips_aware_input] is not FIPS capable", |
| 59 | + }, |
| 60 | + "input_is_fips_aware_and_fips_capable": { |
| 61 | + runner: newFIPSAwareInput(true), |
| 62 | + expectedErr: "", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for name, test := range tests { |
| 67 | + t.Run(name, func(t *testing.T) { |
| 68 | + err := checkFIPSCapability(test.runner) |
| 69 | + if test.expectedErr == "" { |
| 70 | + require.NoError(t, err) |
| 71 | + } else { |
| 72 | + require.EqualError(t, err, test.expectedErr) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments