github.com/google/cloudprober@v0.11.3/probes/probeutils/probeutils_test.go (about) 1 // Copyright 2017-2019 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 probeutils 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 func TestPayloadVerification(t *testing.T) { 23 testBytes := []byte("test bytes") 24 25 // Verify that for the larger payload sizes we get replicas of the same 26 // bytes. 27 for _, size := range []int{4, 256, 999, 2048, 4 * len(testBytes)} { 28 payload := make([]byte, size) 29 30 PatternPayload(payload, testBytes) 31 32 var expectedBuf []byte 33 for i := 0; i < size/len(testBytes); i++ { 34 expectedBuf = append(expectedBuf, testBytes...) 35 } 36 // Remaining bytes 37 expectedBuf = append(expectedBuf, testBytes[:size-len(expectedBuf)]...) 38 if !reflect.DeepEqual(payload, expectedBuf) { 39 t.Errorf("Bytes array:\n%o\n\nExpected:\n%o", payload, expectedBuf) 40 } 41 42 // Verify payload. 43 err := VerifyPayloadPattern(payload, testBytes) 44 if err != nil { 45 t.Errorf("Data verification error: %v", err) 46 } 47 } 48 } 49 50 func benchmarkVerifyPayloadPattern(size int, b *testing.B) { 51 testBytes := []byte("test bytes") 52 payload := make([]byte, size) 53 PatternPayload(payload, testBytes) 54 55 for n := 0; n < b.N; n++ { 56 VerifyPayloadPattern(payload, testBytes) 57 } 58 } 59 60 func BenchmarkVerifyPayloadPattern56(b *testing.B) { benchmarkVerifyPayloadPattern(56, b) } 61 func BenchmarkVerifyPayloadPattern256(b *testing.B) { benchmarkVerifyPayloadPattern(256, b) } 62 func BenchmarkVerifyPayloadPattern1999(b *testing.B) { benchmarkVerifyPayloadPattern(1999, b) } 63 func BenchmarkVerifyPayloadPattern9999(b *testing.B) { benchmarkVerifyPayloadPattern(9999, b) }