github.com/coreos/mantle@v0.13.0/sdk/verify_test.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     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 sdk
    16  
    17  import (
    18  	"encoding/base64"
    19  	"io"
    20  	"strings"
    21  	"testing"
    22  
    23  	"golang.org/x/crypto/openpgp/errors"
    24  )
    25  
    26  const (
    27  	versionTxt = `COREOS_BUILD=723
    28  COREOS_BRANCH=1
    29  COREOS_PATCH=0
    30  COREOS_VERSION=723.1.0
    31  COREOS_VERSION_ID=723.1.0
    32  COREOS_BUILD_ID=""
    33  COREOS_SDK_VERSION=717.0.0
    34  `
    35  	versionSig = `
    36  iQIcBAABAgAGBQJVjgCWAAoJEKWpZjXlZ278G/kQAKSqkurFrKywkPhCe3VejSUp
    37  GSS2MmHT4UAhHzopof33eV1mwI7NxPP7oDOeg5ovLxiHbawo/fHYUI9Wt2r9ZUCB
    38  QxXt1fk9yBbUlVd6vdsrLmUZpVNfFmnxUL8iurRJczgSKmqyxbk+HcD+fidkgSAU
    39  5xpLYEfCp1VSZ61a+3NZO8NHval4x3+AYZXOBNqfWz1s7Sewmvm/YbIs0BlwZxrY
    40  CUiYzuNCgDl0qZLRx8C2EmnHk675XvN4Nr0xAHRsARIXfgFR1AVSqVdvzW2ZW3Bq
    41  KBNiF0zfhZ2cdG6Rj9Dp39+skazUYW8bzn1fr374prSALef/WZIAUkLWvUpPdEli
    42  ZnQr9Ufm+ZW2XM+Nm/Ks5Zf5f+0axHESF1ANSKNM7gp7a6+cbXLniXQKUxMLlTGL
    43  TCz29ZdK1M8Wx2V8bisOk24yneOJyVzn5jSO4zCr6xWxBH8yf0B6UQnXWK8fhVDR
    44  V/mehjhms7/8xCfRlTo42h69UqCzp/ZMlJZOTikw4Q7yZwhAu1bERlOWUVSHik8W
    45  UjVp1b0FyuBYEJA3ht2QuIdf54M3bKGsFGMUB0/ro6sm00UF3pjVkG+a7WEU4zpp
    46  nhqSIf7YIqso/oohdpmc338F7G3RgfYoZ2+THXGTxpIvMvkEqxCaKPsprXLZQd94
    47  ULECDto3pYB5cT5A/blA
    48  =/zkZ
    49  `
    50  )
    51  
    52  func b64reader(s string) io.Reader {
    53  	return base64.NewDecoder(base64.StdEncoding, strings.NewReader(s))
    54  }
    55  
    56  func TestValidSig(t *testing.T) {
    57  	err := Verify(strings.NewReader(versionTxt), b64reader(versionSig), buildbot_coreos_PubKey)
    58  	if err != nil {
    59  		t.Errorf("Verify failed: %v", err)
    60  	}
    61  }
    62  
    63  func TestInvalidSig(t *testing.T) {
    64  	err := Verify(strings.NewReader(versionTxt+"bad"), b64reader(versionSig), buildbot_coreos_PubKey)
    65  	if err == nil {
    66  		t.Errorf("Verify failed to report bad signature")
    67  	} else if _, ok := err.(errors.SignatureError); !ok {
    68  		t.Errorf("Verify failed: %v", err)
    69  	}
    70  }