github.com/google/osv-scalibr@v0.4.1/veles/validate_test.go (about)

     1  // Copyright 2025 Google LLC
     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 veles_test
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"testing"
    21  
    22  	"github.com/google/osv-scalibr/veles"
    23  	"github.com/google/osv-scalibr/veles/velestest"
    24  )
    25  
    26  type testValidationEngineSubCase struct {
    27  	name  string
    28  	input veles.Secret
    29  	want  veles.ValidationStatus
    30  }
    31  
    32  func TestValidationEngine(t *testing.T) {
    33  	cases := []struct {
    34  		name   string
    35  		engine *veles.ValidationEngine
    36  		sub    []testValidationEngineSubCase
    37  	}{
    38  		{
    39  			name:   "empty engine",
    40  			engine: veles.NewValidationEngine(),
    41  			sub: []testValidationEngineSubCase{
    42  				{
    43  					name:  "string unsupported",
    44  					input: velestest.NewFakeStringSecret("foo"),
    45  					want:  veles.ValidationUnsupported,
    46  				},
    47  				{
    48  					name:  "int unsupported",
    49  					input: velestest.NewFakeIntSecret(123),
    50  					want:  veles.ValidationUnsupported,
    51  				},
    52  			},
    53  		},
    54  		{
    55  			name:   "single validator",
    56  			engine: veles.NewValidationEngine(veles.WithValidator(velestest.NewFakeStringSecretValidator(veles.ValidationValid, nil))),
    57  			sub: []testValidationEngineSubCase{
    58  				{
    59  					name:  "supported",
    60  					input: velestest.NewFakeStringSecret("foo"),
    61  					want:  veles.ValidationValid,
    62  				},
    63  				{
    64  					name:  "unsupported",
    65  					input: velestest.NewFakeIntSecret(123),
    66  					want:  veles.ValidationUnsupported,
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name: "multiple_validators",
    72  			engine: veles.NewValidationEngine(
    73  				veles.WithValidator(velestest.NewFakeStringSecretValidator(veles.ValidationValid, nil)),
    74  				veles.WithValidator(velestest.NewFakeIntSecretValidator(veles.ValidationInvalid, nil)),
    75  			),
    76  			sub: []testValidationEngineSubCase{
    77  				{
    78  					name:  "string supported",
    79  					input: velestest.NewFakeStringSecret("foo"),
    80  					want:  veles.ValidationValid,
    81  				},
    82  				{
    83  					name:  "int supported",
    84  					input: velestest.NewFakeIntSecret(123),
    85  					want:  veles.ValidationInvalid,
    86  				},
    87  			},
    88  		},
    89  	}
    90  	for _, tc := range cases {
    91  		t.Run(tc.name, func(t *testing.T) {
    92  			t.Parallel()
    93  			for _, sc := range tc.sub {
    94  				t.Run(sc.name, func(t *testing.T) {
    95  					t.Parallel()
    96  					got, err := tc.engine.Validate(t.Context(), sc.input)
    97  					if err != nil {
    98  						t.Errorf("Validate() error: %v, want nil", err)
    99  					}
   100  					if got != sc.want {
   101  						t.Errorf("Validate() = %q, want %q", got, sc.want)
   102  					}
   103  				})
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func TestValidationEngine_respectsContext(t *testing.T) {
   110  	engine := veles.NewValidationEngine()
   111  	ctx, cancel := context.WithCancel(t.Context())
   112  	cancel()
   113  	status, err := engine.Validate(ctx, velestest.NewFakeStringSecret("foo"))
   114  	if !errors.Is(err, context.Canceled) {
   115  		t.Errorf("Validate() error: %v, want context.Canceled", err)
   116  	}
   117  	if status != veles.ValidationFailed {
   118  		t.Errorf("Validate() = %q, want %q", status, veles.ValidationFailed)
   119  	}
   120  }
   121  
   122  func TestValidationEngine_errors(t *testing.T) {
   123  	errTest := errors.New("some error")
   124  	cases := []struct {
   125  		name   string
   126  		engine *veles.ValidationEngine
   127  		input  veles.Secret
   128  	}{
   129  		{
   130  			name: "validation_error",
   131  			engine: veles.NewValidationEngine(
   132  				veles.WithValidator(velestest.NewFakeStringSecretValidator(veles.ValidationFailed, errTest)),
   133  			),
   134  			input: velestest.NewFakeStringSecret("foo"),
   135  		},
   136  	}
   137  	for _, tc := range cases {
   138  		t.Run(tc.name, func(t *testing.T) {
   139  			t.Parallel()
   140  			status, err := tc.engine.Validate(t.Context(), tc.input)
   141  			if !errors.Is(err, errTest) {
   142  				t.Errorf("Validate() error: %v, want %v", err, errTest)
   143  			}
   144  			if status != veles.ValidationFailed {
   145  				t.Errorf("Validate() = %q, want %q", status, veles.ValidationFailed)
   146  			}
   147  		})
   148  	}
   149  }
   150  
   151  func TestAddValidator(t *testing.T) {
   152  	validator := velestest.NewFakeStringSecretValidator(veles.ValidationValid, nil)
   153  	cases := []struct {
   154  		name        string
   155  		engine      *veles.ValidationEngine
   156  		wantPresent bool
   157  	}{
   158  		{
   159  			name:        "not present",
   160  			engine:      veles.NewValidationEngine(),
   161  			wantPresent: false,
   162  		},
   163  		{
   164  			name: "present",
   165  			engine: veles.NewValidationEngine(
   166  				veles.WithValidator(velestest.NewFakeStringSecretValidator(veles.ValidationInvalid, nil)),
   167  			),
   168  			wantPresent: true,
   169  		},
   170  	}
   171  	for _, tc := range cases {
   172  		t.Run(tc.name, func(t *testing.T) {
   173  			t.Parallel()
   174  			if got, want := veles.AddValidator(tc.engine, validator), tc.wantPresent; got != want {
   175  				t.Errorf("AddValidator() = %t, want %t", got, want)
   176  			}
   177  			status, err := tc.engine.Validate(t.Context(), velestest.NewFakeStringSecret("foo"))
   178  			if err != nil {
   179  				t.Errorf("Validate() error: %v, want nil", err)
   180  			}
   181  			if got, want := status, veles.ValidationValid; got != want {
   182  				t.Errorf("Validate() = %q, want %q", got, want)
   183  			}
   184  		})
   185  	}
   186  }