Skip to content

Commit

Permalink
remove legacy example tests
Browse files Browse the repository at this point in the history
Signed-off-by: qmuntal <qmuntaldiaz@microsoft.com>
  • Loading branch information
qmuntal committed Mar 29, 2022
1 parent bb84bb8 commit cfab627
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 238 deletions.
129 changes: 3 additions & 126 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package cose

import (
"crypto/ecdsa"
"crypto/elliptic"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
)

// consts and helper functions for loading tests
Expand Down Expand Up @@ -656,10 +650,10 @@ var RSA_PARAMS = COSERustSignatureParameters{
}

type RustTestCase struct {
Title string // test fn name in cose-rust/examples/sign_verify/main.rs
Title string // test fn name in cose-rust/examples/sign_verify/main.rs
SignAlg *Algorithm // COSE signing algorithm to use
SignPayload []byte // payload to sign
VerifyPayload []byte // payload to verify (defaults to SignPayload)
SignPayload []byte // payload to sign
VerifyPayload []byte // payload to verify (defaults to SignPayload)
Certs [][]byte
Params []COSERustSignatureParameters
// SignError // expected error nil for success/ok
Expand Down Expand Up @@ -829,127 +823,10 @@ var RustTestCases = []RustTestCase{
var algTag = GetCommonHeaderTagOrPanic("alg")
var kidTag = GetCommonHeaderTagOrPanic("kid")

// WGExample
// autogenerated from pass and fail examples on https://mholt.github.io/json-to-go/
// then combined (added .Fail and .Input.Failures)
type WGExample struct {
Title string `json:"title"`
Fail bool `json:"fail"`
Input struct {
Plaintext string `json:"plaintext"`
Sign struct {
Protected struct {
Ctyp int `json:"ctyp"`
} `json:"protected"`
Signers []struct {
Key struct {
Kty string `json:"kty"`
Kid string `json:"kid"`
Crv string `json:"crv"`
X string `json:"x"`
Y string `json:"y"`
D string `json:"d"`
} `json:"key"`
Unprotected struct {
Kid string `json:"kid"`
} `json:"unprotected"`
Protected struct {
Alg string `json:"alg"`
} `json:"protected"`
External string `json:"external"`
} `json:"signers"`
} `json:"sign"`
Failures struct {
ChangeCBORTag int `json:"ChangeCBORTag"`
} `json:"failures"`
RngDescription string `json:"rng_description"`
} `json:"input"`
Intermediates struct {
Signers []struct {
ToBeSignHex string `json:"ToBeSign_hex"`
} `json:"signers"`
} `json:"intermediates"`
Output struct {
CborDiag string `json:"cbor_diag"`
Cbor string `json:"cbor"`
} `json:"output"`
}

func HexToBytesOrDie(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(fmt.Sprintf("Error decoding hex string: %s", err))
}
return b
}

func LoadPrivateKey(example *WGExample) (key ecdsa.PrivateKey) {
if len(example.Input.Sign.Signers) != 1 {
panic(fmt.Sprintf("Not one signer in example: %s", example.Title))
}
signerInput := example.Input.Sign.Signers[0]

var curve elliptic.Curve
switch signerInput.Key.Crv {
case "P-256":
curve = elliptic.P256()
case "P-384":
curve = elliptic.P384()
case "P-521":
curve = elliptic.P521()
default:
log.Fatalf("Can't load Private key with curve type: %s", signerInput.Key.Crv)
}

return ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: FromBase64Int(signerInput.Key.X),
Y: FromBase64Int(signerInput.Key.Y),
},
D: FromBase64Int(signerInput.Key.D),
}
}

func LoadExample(path string) WGExample {
var content, err = ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}

var example WGExample
err = json.Unmarshal(content, &example)
if err != nil {
log.Fatal(err)
}
return example
}

// LoadExamples
func LoadExamples(path string) []WGExample {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}

examples := make([]WGExample, 0)
for _, file := range files {
if file.IsDir() {
continue
}

var content, err = ioutil.ReadFile(filepath.Join(path, file.Name()))
if err != nil {
log.Fatal(err)
}

var example WGExample
err = json.Unmarshal(content, &example)
if err != nil {
log.Fatal(err)
}

examples = append(examples, example)
}
return examples
}
112 changes: 0 additions & 112 deletions sign_verify_cose_wg_examples_test.go

This file was deleted.

0 comments on commit cfab627

Please sign in to comment.