github.com/tunabay/go-bitarray@v1.3.1/cavp_test.go (about)

     1  // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be found in
     3  // the LICENSE file.
     4  
     5  package bitarray_test
     6  
     7  import (
     8  	"bufio"
     9  	"compress/bzip2"
    10  	"embed"
    11  	"encoding/hex"
    12  	"fmt"
    13  	"regexp"
    14  	"strconv"
    15  	"strings"
    16  
    17  	"github.com/tunabay/go-bitarray"
    18  )
    19  
    20  //go:embed testdata/*.rsp.bz2
    21  var cavpTestDataFS embed.FS
    22  
    23  type cavpTestCase struct {
    24  	ba *bitarray.BitArray
    25  	md []byte
    26  }
    27  
    28  var cavpRspLineRE = regexp.MustCompile(`^([a-zA-Z0-9]+)\s*=\s*([a-zA-Z0-9]+)$`)
    29  
    30  func cavpTestCases(name string) ([]*cavpTestCase, error) {
    31  	fname := fmt.Sprintf("testdata/%s.rsp.bz2", name)
    32  	file, err := cavpTestDataFS.Open(fname)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("%s: %w", fname, err)
    35  	}
    36  	defer file.Close()
    37  
    38  	bz2r := bzip2.NewReader(file)
    39  	scanner := bufio.NewScanner(bz2r)
    40  
    41  	var tcs []*cavpTestCase
    42  
    43  	lineNo := 0
    44  	st := 0
    45  	var nBits int
    46  	var buf []byte
    47  	for scanner.Scan() {
    48  		line := strings.TrimSpace(scanner.Text())
    49  		lineNo++
    50  		if line == "" || strings.HasPrefix(line, "#") {
    51  			continue
    52  		}
    53  		m := cavpRspLineRE.FindStringSubmatch(line)
    54  		if len(m) == 0 {
    55  			continue
    56  		}
    57  		key, val := strings.ToLower(m[1]), m[2]
    58  		switch st {
    59  		case 0:
    60  			if key != "len" {
    61  				return nil, fmt.Errorf("unexpected line (Len): %s:L%d: %s", fname, lineNo, line)
    62  			}
    63  			n64, err := strconv.ParseUint(val, 10, 32)
    64  			if err != nil {
    65  				return nil, fmt.Errorf("invalid Len = %q: %s:L%d: %w", val, fname, lineNo, err)
    66  			}
    67  			nBits = int(n64)
    68  			st = 1
    69  		case 1:
    70  			if key != "msg" {
    71  				return nil, fmt.Errorf("unexpected line (Msg): %s:L%d: %s", fname, lineNo, line)
    72  			}
    73  			b, err := hex.DecodeString(val)
    74  			if err != nil {
    75  				return nil, fmt.Errorf("invalid Msg = %q: %s:L%d: %w", val, fname, lineNo, err)
    76  			}
    77  			buf = b
    78  			st = 2
    79  		case 2:
    80  			if key != "md" {
    81  				return nil, fmt.Errorf("unexpected line (MD): %s:L%d: %s", fname, lineNo, line)
    82  			}
    83  			md, err := hex.DecodeString(val)
    84  			if err != nil {
    85  				return nil, fmt.Errorf("invalid MD = %q: %s:L%d: %w", val, fname, lineNo, err)
    86  			}
    87  			tcs = append(tcs, &cavpTestCase{
    88  				ba: bitarray.NewFromBytes(buf, 0, nBits),
    89  				md: md,
    90  			})
    91  			st = 0
    92  		}
    93  	}
    94  
    95  	return tcs, nil
    96  }