github.com/abayer/test-infra@v0.0.5/prow/github/hmac_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import "testing"
    20  
    21  // echo -n 'BODY' | openssl dgst -sha1 -hmac KEY
    22  func TestValidatePayload(t *testing.T) {
    23  	var testcases = []struct {
    24  		payload string
    25  		sig     string
    26  		key     string
    27  		valid   bool
    28  	}{
    29  		{
    30  			"{}",
    31  			"sha1=db5c76f4264d0ad96cf21baec394964b4b8ce580",
    32  			"abc",
    33  			true,
    34  		},
    35  		{
    36  			"{}",
    37  			"db5c76f4264d0ad96cf21baec394964b4b8ce580",
    38  			"abc",
    39  			false,
    40  		},
    41  		{
    42  			"{}",
    43  			"",
    44  			"abc",
    45  			false,
    46  		},
    47  		{
    48  			"",
    49  			"sha1=cc47e3c0aa0c2984454476d061108c0b110177ae",
    50  			"abc",
    51  			true,
    52  		},
    53  		{
    54  			"",
    55  			"sha1=fbdb1d1b18aa6c08324b7d64b71fb76370690e1d",
    56  			"",
    57  			true,
    58  		},
    59  		{
    60  			"{}",
    61  			"",
    62  			"abc",
    63  			false,
    64  		},
    65  	}
    66  	for _, tc := range testcases {
    67  		if ValidatePayload([]byte(tc.payload), tc.sig, []byte(tc.key)) != tc.valid {
    68  			t.Errorf("Wrong validation for %+v", tc)
    69  		}
    70  	}
    71  }