2017-04-28 17:00:03 +01:00
|
|
|
// run_cavp.go processes CAVP input files and generates suitable response
|
|
|
|
// files, optionally comparing the results against the provided FAX files.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2017-05-04 00:34:42 +01:00
|
|
|
"runtime"
|
2017-04-28 17:00:03 +01:00
|
|
|
"strings"
|
2017-05-04 00:34:42 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-04-28 17:00:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-03 22:12:39 +01:00
|
|
|
oraclePath = flag.String("oracle-bin", "", "Path to the oracle binary")
|
|
|
|
suiteDir = flag.String("suite-dir", "", "Base directory containing the CAVP test suite")
|
2017-04-28 17:00:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// test describes a single request file.
|
|
|
|
type test struct {
|
|
|
|
// inFile is the base of the filename without an extension, i.e.
|
|
|
|
// “ECBMCT128”.
|
|
|
|
inFile string
|
|
|
|
// args are the arguments (not including the input filename) to the
|
|
|
|
// oracle binary.
|
2017-04-28 22:08:45 +01:00
|
|
|
args []string
|
2017-04-28 17:00:03 +01:00
|
|
|
// noFAX, if true, indicates that the output cannot be compared against
|
|
|
|
// the FAX file. (E.g. because the primitive is non-deterministic.)
|
|
|
|
noFAX bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSuite describes a series of tests that are handled by a single oracle
|
|
|
|
// binary.
|
|
|
|
type testSuite struct {
|
|
|
|
// directory is the name of the directory in the CAVP input, i.e. “AES”.
|
|
|
|
directory string
|
2017-05-03 22:12:39 +01:00
|
|
|
// suite names the test suite to pass as the first command-line argument.
|
|
|
|
suite string
|
2017-05-02 20:34:47 +01:00
|
|
|
// faxScanFunc, if not nil, is the function to use instead of
|
|
|
|
// (*bufio.Scanner).Scan. This can be used to skip lines.
|
|
|
|
faxScanFunc func(*bufio.Scanner) bool
|
|
|
|
tests []test
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:21:58 +01:00
|
|
|
func (t *testSuite) getDirectory() string {
|
|
|
|
return filepath.Join(*suiteDir, t.directory)
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:00:03 +01:00
|
|
|
var aesGCMTests = testSuite{
|
|
|
|
"AES_GCM",
|
2017-05-03 22:12:39 +01:00
|
|
|
"aes_gcm",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 17:00:03 +01:00
|
|
|
[]test{
|
|
|
|
{"gcmDecrypt128", []string{"dec", "aes-128-gcm"}, false},
|
|
|
|
{"gcmDecrypt256", []string{"dec", "aes-256-gcm"}, false},
|
|
|
|
{"gcmEncryptIntIV128", []string{"enc", "aes-128-gcm"}, true},
|
|
|
|
{"gcmEncryptIntIV256", []string{"enc", "aes-256-gcm"}, true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var aesTests = testSuite{
|
|
|
|
"AES",
|
2017-05-03 22:12:39 +01:00
|
|
|
"aes",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 17:00:03 +01:00
|
|
|
[]test{
|
2017-04-28 22:21:58 +01:00
|
|
|
{"CBCGFSbox128", []string{"kat", "aes-128-cbc"}, false},
|
|
|
|
{"CBCGFSbox192", []string{"kat", "aes-192-cbc"}, false},
|
|
|
|
{"CBCGFSbox256", []string{"kat", "aes-256-cbc"}, false},
|
|
|
|
{"CBCKeySbox128", []string{"kat", "aes-128-cbc"}, false},
|
|
|
|
{"CBCKeySbox192", []string{"kat", "aes-192-cbc"}, false},
|
|
|
|
{"CBCKeySbox256", []string{"kat", "aes-256-cbc"}, false},
|
|
|
|
{"CBCMMT128", []string{"kat", "aes-128-cbc"}, false},
|
|
|
|
{"CBCMMT192", []string{"kat", "aes-192-cbc"}, false},
|
|
|
|
{"CBCMMT256", []string{"kat", "aes-256-cbc"}, false},
|
|
|
|
{"CBCVarKey128", []string{"kat", "aes-128-cbc"}, false},
|
|
|
|
{"CBCVarKey192", []string{"kat", "aes-192-cbc"}, false},
|
|
|
|
{"CBCVarKey256", []string{"kat", "aes-256-cbc"}, false},
|
|
|
|
{"CBCVarTxt128", []string{"kat", "aes-128-cbc"}, false},
|
|
|
|
{"CBCVarTxt192", []string{"kat", "aes-192-cbc"}, false},
|
|
|
|
{"CBCVarTxt256", []string{"kat", "aes-256-cbc"}, false},
|
|
|
|
{"ECBGFSbox128", []string{"kat", "aes-128-ecb"}, false},
|
|
|
|
{"ECBGFSbox192", []string{"kat", "aes-192-ecb"}, false},
|
|
|
|
{"ECBGFSbox256", []string{"kat", "aes-256-ecb"}, false},
|
|
|
|
{"ECBKeySbox128", []string{"kat", "aes-128-ecb"}, false},
|
|
|
|
{"ECBKeySbox192", []string{"kat", "aes-192-ecb"}, false},
|
|
|
|
{"ECBKeySbox256", []string{"kat", "aes-256-ecb"}, false},
|
|
|
|
{"ECBMMT128", []string{"kat", "aes-128-ecb"}, false},
|
|
|
|
{"ECBMMT192", []string{"kat", "aes-192-ecb"}, false},
|
|
|
|
{"ECBMMT256", []string{"kat", "aes-256-ecb"}, false},
|
|
|
|
{"ECBVarKey128", []string{"kat", "aes-128-ecb"}, false},
|
|
|
|
{"ECBVarKey192", []string{"kat", "aes-192-ecb"}, false},
|
|
|
|
{"ECBVarKey256", []string{"kat", "aes-256-ecb"}, false},
|
|
|
|
{"ECBVarTxt128", []string{"kat", "aes-128-ecb"}, false},
|
|
|
|
{"ECBVarTxt192", []string{"kat", "aes-192-ecb"}, false},
|
|
|
|
{"ECBVarTxt256", []string{"kat", "aes-256-ecb"}, false},
|
|
|
|
// AES Monte-Carlo tests
|
|
|
|
{"ECBMCT128", []string{"mct", "aes-128-ecb"}, false},
|
|
|
|
{"ECBMCT192", []string{"mct", "aes-192-ecb"}, false},
|
|
|
|
{"ECBMCT256", []string{"mct", "aes-256-ecb"}, false},
|
|
|
|
{"CBCMCT128", []string{"mct", "aes-128-cbc"}, false},
|
|
|
|
{"CBCMCT192", []string{"mct", "aes-192-cbc"}, false},
|
|
|
|
{"CBCMCT256", []string{"mct", "aes-256-cbc"}, false},
|
2017-04-28 17:00:03 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:56:22 +01:00
|
|
|
var ecdsa2KeyPairTests = testSuite{
|
|
|
|
"ECDSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"ecdsa2_keypair",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-05-01 19:56:22 +01:00
|
|
|
[]test{{"KeyPair", nil, true}},
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:08:45 +01:00
|
|
|
var ecdsa2PKVTests = testSuite{
|
|
|
|
"ECDSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"ecdsa2_pkv",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 22:08:45 +01:00
|
|
|
[]test{{"PKV", nil, false}},
|
|
|
|
}
|
|
|
|
|
2017-04-30 19:37:16 +01:00
|
|
|
var ecdsa2SigGenTests = testSuite{
|
|
|
|
"ECDSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"ecdsa2_siggen",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-30 19:37:16 +01:00
|
|
|
[]test{
|
|
|
|
{"SigGen", []string{"SigGen"}, true},
|
|
|
|
{"SigGenComponent", []string{"SigGenComponent"}, true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:41:28 +01:00
|
|
|
var ecdsa2SigVerTests = testSuite{
|
|
|
|
"ECDSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"ecdsa2_sigver",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 22:41:28 +01:00
|
|
|
[]test{{"SigVer", nil, false}},
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:23:27 +01:00
|
|
|
var rsa2KeyGenTests = testSuite{
|
|
|
|
"RSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"rsa2_keygen",
|
2017-05-03 16:23:27 +01:00
|
|
|
nil,
|
|
|
|
[]test{
|
|
|
|
{"KeyGen_RandomProbablyPrime3_3", nil, true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:32:13 +01:00
|
|
|
var rsa2SigGenTests = testSuite{
|
|
|
|
"RSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"rsa2_siggen",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-05-02 16:32:13 +01:00
|
|
|
[]test{
|
|
|
|
{"SigGen15_186-3", []string{"pkcs15"}, true},
|
2017-05-02 20:02:01 +01:00
|
|
|
{"SigGenPSS_186-3", []string{"pss"}, true},
|
2017-05-02 16:32:13 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var rsa2SigVerTests = testSuite{
|
|
|
|
"RSA2",
|
2017-05-03 22:12:39 +01:00
|
|
|
"rsa2_sigver",
|
2017-05-02 20:34:47 +01:00
|
|
|
func(s *bufio.Scanner) bool {
|
|
|
|
for {
|
|
|
|
if !s.Scan() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
line := s.Text()
|
|
|
|
if strings.HasPrefix(line, "p = ") || strings.HasPrefix(line, "d = ") || strings.HasPrefix(line, "SaltVal = ") || strings.HasPrefix(line, "EM with ") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(line, "q = ") {
|
|
|
|
// Skip the "q = " line and an additional blank line.
|
|
|
|
if !s.Scan() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(strings.TrimSpace(s.Text())) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
},
|
2017-05-02 16:32:13 +01:00
|
|
|
[]test{
|
2017-05-02 20:34:47 +01:00
|
|
|
{"SigVer15_186-3", []string{"pkcs15"}, false},
|
|
|
|
{"SigVerPSS_186-3", []string{"pss"}, false},
|
2017-05-02 16:32:13 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-01 22:02:01 +01:00
|
|
|
var hmacTests = testSuite{
|
|
|
|
"HMAC",
|
2017-05-03 22:12:39 +01:00
|
|
|
"hmac",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-05-01 22:02:01 +01:00
|
|
|
[]test{{"HMAC", nil, false}},
|
|
|
|
}
|
|
|
|
|
2017-04-28 21:17:54 +01:00
|
|
|
var shaTests = testSuite{
|
|
|
|
"SHA",
|
2017-05-03 22:12:39 +01:00
|
|
|
"sha",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 21:17:54 +01:00
|
|
|
[]test{
|
|
|
|
{"SHA1LongMsg", []string{"SHA1"}, false},
|
|
|
|
{"SHA1ShortMsg", []string{"SHA1"}, false},
|
|
|
|
{"SHA224LongMsg", []string{"SHA224"}, false},
|
|
|
|
{"SHA224ShortMsg", []string{"SHA224"}, false},
|
|
|
|
{"SHA256LongMsg", []string{"SHA256"}, false},
|
|
|
|
{"SHA256ShortMsg", []string{"SHA256"}, false},
|
|
|
|
{"SHA384LongMsg", []string{"SHA384"}, false},
|
|
|
|
{"SHA384ShortMsg", []string{"SHA384"}, false},
|
|
|
|
{"SHA512LongMsg", []string{"SHA512"}, false},
|
|
|
|
{"SHA512ShortMsg", []string{"SHA512"}, false},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var shaMonteTests = testSuite{
|
|
|
|
"SHA",
|
2017-05-03 22:12:39 +01:00
|
|
|
"sha_monte",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-04-28 21:17:54 +01:00
|
|
|
[]test{
|
|
|
|
{"SHA1Monte", []string{"SHA1"}, false},
|
|
|
|
{"SHA224Monte", []string{"SHA224"}, false},
|
|
|
|
{"SHA256Monte", []string{"SHA256"}, false},
|
|
|
|
{"SHA384Monte", []string{"SHA384"}, false},
|
|
|
|
{"SHA512Monte", []string{"SHA512"}, false},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:03 +01:00
|
|
|
var ctrDRBGTests = testSuite{
|
|
|
|
"DRBG800-90A",
|
2017-05-03 22:12:39 +01:00
|
|
|
"ctr_drbg",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-05-01 18:54:03 +01:00
|
|
|
[]test{{"CTR_DRBG", nil, false}},
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:56:43 +01:00
|
|
|
var tdesTests = testSuite{
|
|
|
|
"TDES",
|
2017-05-03 22:12:39 +01:00
|
|
|
"tdes",
|
2017-05-02 20:34:47 +01:00
|
|
|
nil,
|
2017-05-01 19:56:43 +01:00
|
|
|
[]test{
|
2017-05-01 23:31:43 +01:00
|
|
|
{"TCBCMMT2", []string{"kat", "des-ede-cbc"}, false},
|
|
|
|
{"TCBCMMT3", []string{"kat", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCMonte2", []string{"mct", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCMonte3", []string{"mct", "des-ede3-cbc"}, false},
|
2017-05-01 19:56:43 +01:00
|
|
|
{"TCBCinvperm", []string{"kat", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCpermop", []string{"kat", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCsubtab", []string{"kat", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCvarkey", []string{"kat", "des-ede3-cbc"}, false},
|
|
|
|
{"TCBCvartext", []string{"kat", "des-ede3-cbc"}, false},
|
2017-05-01 23:31:43 +01:00
|
|
|
{"TECBMMT2", []string{"kat", "des-ede"}, false},
|
|
|
|
{"TECBMMT3", []string{"kat", "des-ede3"}, false},
|
|
|
|
{"TECBMonte2", []string{"mct", "des-ede3"}, false},
|
|
|
|
{"TECBMonte3", []string{"mct", "des-ede3"}, false},
|
2017-05-01 19:56:43 +01:00
|
|
|
{"TECBinvperm", []string{"kat", "des-ede3"}, false},
|
|
|
|
{"TECBpermop", []string{"kat", "des-ede3"}, false},
|
|
|
|
{"TECBsubtab", []string{"kat", "des-ede3"}, false},
|
|
|
|
{"TECBvarkey", []string{"kat", "des-ede3"}, false},
|
|
|
|
{"TECBvartext", []string{"kat", "des-ede3"}, false},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:17:50 +01:00
|
|
|
var keyWrapTests = testSuite{
|
|
|
|
"KeyWrap38F",
|
2017-05-03 22:12:39 +01:00
|
|
|
"keywrap",
|
2017-05-03 19:17:50 +01:00
|
|
|
nil,
|
|
|
|
[]test{
|
|
|
|
{"KW_AD_128", []string{"dec", "128"}, false},
|
|
|
|
{"KW_AD_256", []string{"dec", "256"}, false},
|
|
|
|
{"KW_AE_128", []string{"enc", "128"}, false},
|
|
|
|
{"KW_AE_256", []string{"enc", "256"}, false},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:00:03 +01:00
|
|
|
var allTestSuites = []*testSuite{
|
|
|
|
&aesGCMTests,
|
|
|
|
&aesTests,
|
2017-05-01 18:54:03 +01:00
|
|
|
&ctrDRBGTests,
|
2017-05-01 19:56:22 +01:00
|
|
|
&ecdsa2KeyPairTests,
|
2017-04-28 22:08:45 +01:00
|
|
|
&ecdsa2PKVTests,
|
2017-04-30 19:37:16 +01:00
|
|
|
&ecdsa2SigGenTests,
|
2017-04-28 22:41:28 +01:00
|
|
|
&ecdsa2SigVerTests,
|
2017-05-03 19:17:50 +01:00
|
|
|
&hmacTests,
|
|
|
|
&keyWrapTests,
|
2017-05-03 16:23:27 +01:00
|
|
|
&rsa2KeyGenTests,
|
2017-05-02 16:32:13 +01:00
|
|
|
&rsa2SigGenTests,
|
|
|
|
&rsa2SigVerTests,
|
2017-04-28 21:17:54 +01:00
|
|
|
&shaTests,
|
|
|
|
&shaMonteTests,
|
2017-05-01 19:56:43 +01:00
|
|
|
&tdesTests,
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:34:42 +01:00
|
|
|
// testInstance represents a specific test in a testSuite.
|
|
|
|
type testInstance struct {
|
|
|
|
suite *testSuite
|
|
|
|
testIndex int
|
|
|
|
}
|
|
|
|
|
|
|
|
func worker(wg *sync.WaitGroup, work <-chan testInstance) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for ti := range work {
|
|
|
|
test := ti.suite.tests[ti.testIndex]
|
|
|
|
|
|
|
|
if err := doTest(ti.suite, test); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !test.noFAX {
|
|
|
|
if err := compareFAX(ti.suite, test); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:00:03 +01:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2017-05-03 22:12:39 +01:00
|
|
|
if len(*oraclePath) == 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "Must give -oracle-bin\n")
|
2017-04-28 17:00:03 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-05-04 00:34:42 +01:00
|
|
|
work := make(chan testInstance)
|
|
|
|
var wg sync.WaitGroup
|
2017-04-28 17:00:03 +01:00
|
|
|
|
2017-05-04 00:34:42 +01:00
|
|
|
for i := 0; i < runtime.NumCPU(); i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go worker(&wg, work)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, suite := range allTestSuites {
|
|
|
|
for i := range suite.tests {
|
|
|
|
work <- testInstance{suite, i}
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 00:34:42 +01:00
|
|
|
|
|
|
|
close(work)
|
|
|
|
wg.Wait()
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func doTest(suite *testSuite, test test) error {
|
2017-05-03 22:12:39 +01:00
|
|
|
args := []string{suite.suite}
|
2017-04-28 17:00:03 +01:00
|
|
|
args = append(args, test.args...)
|
2017-04-28 22:21:58 +01:00
|
|
|
args = append(args, filepath.Join(suite.getDirectory(), "req", test.inFile+".req"))
|
2017-04-28 17:00:03 +01:00
|
|
|
|
2017-05-03 21:35:47 +01:00
|
|
|
outPath := filepath.Join(suite.getDirectory(), "resp", test.inFile+".rsp")
|
2017-04-28 17:00:03 +01:00
|
|
|
outFile, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("cannot open output file for %q %q: %s", suite.getDirectory(), test.inFile, err)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2017-05-03 22:12:39 +01:00
|
|
|
cmd := exec.Command(*oraclePath, args...)
|
2017-04-28 17:00:03 +01:00
|
|
|
cmd.Stdout = outFile
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
2017-05-03 22:12:39 +01:00
|
|
|
cmdLine := strings.Join(append([]string{*oraclePath}, args...), " ")
|
2017-05-04 00:34:42 +01:00
|
|
|
startTime := time.Now()
|
2017-04-28 17:00:03 +01:00
|
|
|
if err := cmd.Run(); err != nil {
|
2017-05-03 22:12:39 +01:00
|
|
|
return fmt.Errorf("cannot run command for %q %q (%s): %s", suite.getDirectory(), test.inFile, cmdLine, err)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 00:34:42 +01:00
|
|
|
fmt.Printf("%s (%ds)\n", cmdLine, int(time.Since(startTime).Seconds()))
|
|
|
|
|
2017-04-28 17:00:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:08:45 +01:00
|
|
|
func canonicalizeLine(in string) string {
|
|
|
|
if strings.HasPrefix(in, "Result = P (") {
|
|
|
|
return "Result = P"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(in, "Result = F (") {
|
|
|
|
return "Result = F"
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:00:03 +01:00
|
|
|
func compareFAX(suite *testSuite, test test) error {
|
2017-05-02 20:34:47 +01:00
|
|
|
faxScanFunc := suite.faxScanFunc
|
|
|
|
if faxScanFunc == nil {
|
|
|
|
faxScanFunc = (*bufio.Scanner).Scan
|
|
|
|
}
|
|
|
|
|
2017-05-03 21:35:47 +01:00
|
|
|
respPath := filepath.Join(suite.getDirectory(), "resp", test.inFile+".rsp")
|
2017-04-28 17:00:03 +01:00
|
|
|
respFile, err := os.Open(respPath)
|
|
|
|
if err != nil {
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("cannot read output of %q %q: %s", suite.getDirectory(), test.inFile, err)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
defer respFile.Close()
|
|
|
|
|
2017-04-28 22:21:58 +01:00
|
|
|
faxPath := filepath.Join(suite.getDirectory(), "fax", test.inFile+".fax")
|
2017-04-28 17:00:03 +01:00
|
|
|
faxFile, err := os.Open(faxPath)
|
|
|
|
if err != nil {
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("cannot open fax file for %q %q: %s", suite.getDirectory(), test.inFile, err)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
defer faxFile.Close()
|
|
|
|
|
|
|
|
respScanner := bufio.NewScanner(respFile)
|
|
|
|
faxScanner := bufio.NewScanner(faxFile)
|
|
|
|
|
|
|
|
lineNo := 0
|
|
|
|
inHeader := true
|
|
|
|
|
|
|
|
for respScanner.Scan() {
|
|
|
|
lineNo++
|
|
|
|
respLine := respScanner.Text()
|
|
|
|
var faxLine string
|
|
|
|
|
|
|
|
if inHeader && (len(respLine) == 0 || respLine[0] == '#') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
haveFaxLine := false
|
|
|
|
|
|
|
|
if inHeader {
|
2017-05-02 20:34:47 +01:00
|
|
|
for faxScanFunc(faxScanner) {
|
2017-04-28 17:00:03 +01:00
|
|
|
faxLine = faxScanner.Text()
|
|
|
|
if len(faxLine) != 0 && faxLine[0] != '#' {
|
|
|
|
haveFaxLine = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inHeader = false
|
|
|
|
} else {
|
2017-05-02 20:34:47 +01:00
|
|
|
if faxScanFunc(faxScanner) {
|
2017-04-28 17:00:03 +01:00
|
|
|
faxLine = faxScanner.Text()
|
|
|
|
haveFaxLine = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !haveFaxLine {
|
2017-05-01 22:02:01 +01:00
|
|
|
// Ignore blank lines at the end of the generated file.
|
|
|
|
if len(respLine) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("resp file is longer than fax for %q %q", suite.getDirectory(), test.inFile)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(faxLine, " (Reason: ") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:08:45 +01:00
|
|
|
if canonicalizeLine(faxLine) == canonicalizeLine(respLine) {
|
2017-04-28 17:00:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("resp and fax differ at line %d for %q %q: %q vs %q", lineNo, suite.getDirectory(), test.inFile, respLine, faxLine)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-02 20:34:47 +01:00
|
|
|
if faxScanFunc(faxScanner) {
|
2017-04-28 22:21:58 +01:00
|
|
|
return fmt.Errorf("fax file is longer than resp for %q %q", suite.getDirectory(), test.inFile)
|
2017-04-28 17:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|