cuelang.org/go@v0.10.1/pkg/crypto/ed25519/ed25519_test.go (about)

     1  // Copyright 2021 CUE 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 ed25519_test
    16  
    17  import (
    18  	"bytes"
    19  	"crypto/ed25519"
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  
    24  	"cuelang.org/go/cue/format"
    25  	"cuelang.org/go/internal/cuetest"
    26  	"cuelang.org/go/pkg/internal/builtintest"
    27  )
    28  
    29  func TestBuiltin(t *testing.T) {
    30  	if cuetest.UpdateGoldenFiles {
    31  		updateGoldenFiles(t)
    32  	}
    33  
    34  	builtintest.Run("ed25519", t)
    35  }
    36  
    37  // updateGoldenFiles generates deterministic test input.
    38  func updateGoldenFiles(t *testing.T) {
    39  	key := ed25519.NewKeyFromSeed(make([]byte, ed25519.SeedSize))
    40  
    41  	var inputs bytes.Buffer
    42  	fmt.Fprintln(&inputs, `import "encoding/hex"`)
    43  	fmt.Fprintln(&inputs, `import "crypto/ed25519"`)
    44  
    45  	testCases := []struct {
    46  		key       ed25519.PublicKey
    47  		message   []byte
    48  		signature []byte
    49  	}{
    50  		{
    51  			key:       key.Public().(ed25519.PublicKey),
    52  			message:   []byte("valid"),
    53  			signature: ed25519.Sign(key, []byte("valid")),
    54  		},
    55  		{
    56  			key:       key.Public().(ed25519.PublicKey),
    57  			message:   []byte("message mismatch"),
    58  			signature: ed25519.Sign(key, []byte("mismatching message")),
    59  		},
    60  		{
    61  			key:       make(ed25519.PublicKey, ed25519.PublicKeySize),
    62  			message:   []byte("wrong key"),
    63  			signature: ed25519.Sign(key, []byte("wrong key")),
    64  		},
    65  		{
    66  			key:       ed25519.PublicKey{},
    67  			message:   []byte("wrong key size"),
    68  			signature: ed25519.Sign(key, []byte("wrong key size")),
    69  		},
    70  	}
    71  	for i, tc := range testCases {
    72  		fmt.Fprintf(
    73  			&inputs,
    74  			"t%d: ed25519.Valid(hex.Decode(\"%x\"), hex.Decode(\"%x\"), hex.Decode(\"%x\"))\n",
    75  			i, tc.key, tc.message, tc.signature,
    76  		)
    77  	}
    78  
    79  	fInputs, err := format.Source(inputs.Bytes())
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	var buf bytes.Buffer
    85  	fmt.Fprintln(&buf, "-- in.cue --")
    86  	fmt.Fprintf(&buf, "%s", fInputs)
    87  	if err := os.WriteFile("testdata/gen.txtar", buf.Bytes(), 0666); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  }