github.com/google/cloudprober@v0.11.3/validators/integrity/integrity_test.go (about) 1 // Copyright 2018 The Cloudprober 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 integrity 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/google/cloudprober/logger" 22 configpb "github.com/google/cloudprober/validators/integrity/proto" 23 ) 24 25 func TestInvalidConfig(t *testing.T) { 26 testConfig := &configpb.Validator{} 27 v := Validator{} 28 err := v.Init(testConfig, &logger.Logger{}) 29 if err == nil { 30 t.Errorf("v.Init(%v, l): expected error but got nil", testConfig) 31 } 32 } 33 34 func verifyValidate(t *testing.T, v Validator, testPattern string) { 35 t.Helper() 36 37 rows := []struct { 38 respBody []byte 39 expected bool 40 wantErr bool 41 }{ 42 { 43 // Response smaller than the pattern but bytes matche the pattern bytes. 44 // It should pass when the pattern is specified (TestPatternString) 45 // and fail when pattern is supposed to be derived from the payload by 46 // looking at first N bytes (TestPatternNumBytes). 47 respBody: append([]byte{}, []byte(testPattern[:3])...), 48 expected: v.patternNumBytes == 0, // Pass when pattern num bytes are not given. 49 wantErr: v.patternNumBytes > 3, 50 }, 51 { 52 respBody: []byte(strings.Repeat(testPattern, 4)), // "test-ctest-ctest-ctest-c" 53 expected: true, 54 }, 55 { 56 respBody: []byte(strings.Repeat(testPattern, 4) + "-123"), // "test-ctest-ctest-ctest-c-123" 57 expected: false, 58 }, 59 { 60 // "test-ctest-c" with partial testPattern in the end. 61 respBody: append([]byte(strings.Repeat(testPattern, 2)), testPattern[:2]...), 62 expected: true, 63 }, 64 } 65 66 for i, r := range rows { 67 result, err := v.Validate(r.respBody) 68 if (err != nil) != r.wantErr { 69 t.Errorf("v.Validate(nil, %s), row #%d: err=%v, expectedError(bool)=%v", string(r.respBody), i, err, r.wantErr) 70 } 71 if result != r.expected { 72 t.Errorf("v.Validate(nil, %s), row #%d: result=%v expected=%v", string(r.respBody), i, result, r.expected) 73 } 74 } 75 } 76 77 func TestPatternString(t *testing.T) { 78 testPattern := "test-c" 79 80 // Test initializing with pattern string. 81 testConfig := &configpb.Validator{ 82 Pattern: &configpb.Validator_PatternString{ 83 PatternString: testPattern, 84 }, 85 } 86 87 v := Validator{} 88 err := v.Init(testConfig, &logger.Logger{}) 89 if err != nil { 90 t.Errorf("v.Init(%v, l): got error: %v", testConfig, err) 91 } 92 93 if string(v.pattern) != testPattern { 94 t.Errorf("v.Init(%v): v.patternString=%s, expected=%s", testConfig, string(v.pattern), testPattern) 95 } 96 97 verifyValidate(t, v, testPattern) 98 } 99 100 func TestPatternNumBytes(t *testing.T) { 101 testNumBytes := int32(8) 102 103 // Test initializing with pattern with prefix num bytes. 104 v, err := PatternNumBytesValidator(testNumBytes, &logger.Logger{}) 105 if err != nil { 106 t.Errorf("PatternNumBytesValidator(%d, l): got error: %v", testNumBytes, err) 107 } 108 109 if v.patternNumBytes != testNumBytes { 110 t.Errorf("PatternNumBytesValidator(%d, l): v.patternNumBytes=%d, expected=%d", testNumBytes, v.patternNumBytes, testNumBytes) 111 } 112 113 // 8-byte long test pattern to be used for respBody generation 114 testPattern := "njk1120sasnl123"[:8] 115 verifyValidate(t, *v, testPattern) 116 }