<commit_before>package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.applet.ECTesterApplet;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.output.TestWriter;
import cz.crcs.ectester.common.test.CompoundTest;
import cz.crcs.ectester.common.test.Test;
import cz.crcs.ectester.common.util.CardUtil;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;
import javacard.security.KeyPair;

import java.util.LinkedList;
import java.util.List;

import static cz.crcs.ectester.common.test.Result.ExpectedValue;

/**
 * @author <NAME> <EMAIL>
 */
public class CardDefaultSuite extends CardTestSuite {

    public CardDefaultSuite(TestWriter writer, ECTesterReader.Config cfg, CardMngr cardManager) {
        super(writer, cfg, cardManager, "default", "The default test suite run basic support of ECDH and ECDSA.");
    }

    @Override
    protected void runTests() throws Exception {
        if (cfg.primeField) {
            runDefault(KeyPair.ALG_EC_FP);
        }
        if (cfg.binaryField) {
            runDefault(KeyPair.ALG_EC_F2M);
        }
    }

    private void runDefault(byte field) throws Exception {
        short[] keySizes = field == KeyPair.ALG_EC_FP ? EC_Consts.FP_SIZES : EC_Consts.F2M_SIZES;
        short domain = field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M;
        for (short keyLength : keySizes) {
            String description = "Tests of " + keyLength + "b " + CardUtil.getKeyTypeString(field) + " support.";

            List<Test> supportTests = new LinkedList<>();
            Test key = runTest(CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, keyLength, field), ExpectedValue.SUCCESS));
            if (!key.ok()) {
                doTest(CompoundTest.all(ExpectedValue.SUCCESS, description + " None.", key));
                continue;
            }
            supportTests.add(key);

            Test genDefault = runTest(CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), ExpectedValue.SUCCESS));
            Test setCustom = runTest(CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, EC_Consts.getCurve(keyLength, field), domain, null), ExpectedValue.SUCCESS));
            Test genCustom = runTest(CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), ExpectedValue.SUCCESS));
            supportTests.add(genDefault);
            supportTests.add(setCustom);
            supportTests.add(genCustom);

            for (byte kaType : EC_Consts.KA_TYPES) {
                Test allocate = runTest(CommandTest.expect(new Command.AllocateKeyAgreement(this.card, kaType), ExpectedValue.SUCCESS));
                if (allocate.ok()) {
                    Command ecdh = new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, kaType);
                    Test ka = runTest(CommandTest.expect(ecdh, ExpectedValue.SUCCESS));
                    Test kaCompressed = runTest(CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_COMPRESS, kaType), ExpectedValue.SUCCESS));
                    Test perfTest = null;
                    if (ka.ok()) {
                        perfTest = runTest(PerformanceTest.repeat(ecdh, 10));
                    }
                    Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getKATypeString(kaType) + " KeyAgreement.", allocate, ka, kaCompressed, perfTest));
                    supportTests.add(compound);
                } else {
                    runTest(allocate);
                    supportTests.add(allocate);
                }
            }
            for (byte sigType : EC_Consts.SIG_TYPES) {
                Test allocate = runTest(CommandTest.expect(new Command.AllocateSignature(this.card, sigType), ExpectedValue.SUCCESS));
                if (allocate.ok()) {
                    Command ecdsa = new Command.ECDSA(this.card, ECTesterApplet.KEYPAIR_LOCAL, sigType, ECTesterApplet.EXPORT_FALSE, null);
                    Test expect = runTest(CommandTest.expect(ecdsa, ExpectedValue.SUCCESS));
                    Test perfTest = null;
                    if (expect.ok()) {
                        perfTest = runTest(PerformanceTest.repeat(ecdsa, 10));
                    }
                    Test compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, "Test of the " + CardUtil.getSigTypeString(sigType) + " signature.", allocate, expect, perfTest));
                    supportTests.add(compound);
                } else {
                    supportTests.add(allocate);
                }
            }
            doTest(CompoundTest.all(ExpectedValue.SUCCESS, description + " Some.", supportTests.toArray(new Test[0])));
            new Command.Cleanup(this.card).send();
        }
    }
}
<commit_msg>Better evaluate results of Default test suite.<commit_after>package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.applet.ECTesterApplet;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.output.TestWriter;
import cz.crcs.ectester.common.test.CompoundTest;
import cz.crcs.ectester.common.test.Result;
import cz.crcs.ectester.common.test.Test;
import cz.crcs.ectester.common.util.CardUtil;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;
import javacard.security.KeyPair;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

import static cz.crcs.ectester.common.test.Result.ExpectedValue;

/**
 * @author <NAME> <EMAIL>
 */
public class CardDefaultSuite extends CardTestSuite {

    public CardDefaultSuite(TestWriter writer, ECTesterReader.Config cfg, CardMngr cardManager) {
        super(writer, cfg, cardManager, "default", "The default test suite run basic support of ECDH and ECDSA.");
    }

    @Override
    protected void runTests() throws Exception {
        if (cfg.primeField) {
            runDefault(KeyPair.ALG_EC_FP);
        }
        if (cfg.binaryField) {
            runDefault(KeyPair.ALG_EC_F2M);
        }
    }

    private void runDefault(byte field) throws Exception {
        short[] keySizes = field == KeyPair.ALG_EC_FP ? EC_Consts.FP_SIZES : EC_Consts.F2M_SIZES;
        short domain = field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M;
        for (short keyLength : keySizes) {
            String description = "Tests of " + keyLength + "b " + CardUtil.getKeyTypeString(field) + " support:";

            List<Test> supportTests = new LinkedList<>();
            Test allocateFirst = runTest(CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, keyLength, field), ExpectedValue.SUCCESS));
            if (!allocateFirst.ok()) {
                doTest(CompoundTest.all(ExpectedValue.SUCCESS, description + " None.", allocateFirst));
                continue;
            }
            supportTests.add(allocateFirst);

            Test genDefault = runTest(CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), ExpectedValue.SUCCESS));
            Test allocateSecond = runTest(CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, keyLength, field), ExpectedValue.SUCCESS));
            Test setCustom = runTest(CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, EC_Consts.getCurve(keyLength, field), domain, null), ExpectedValue.SUCCESS));
            Test genCustom = runTest(CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), ExpectedValue.SUCCESS));
            supportTests.add(genDefault);
            supportTests.add(allocateSecond);
            supportTests.add(setCustom);
            supportTests.add(genCustom);

            List<Test> kaTests = new LinkedList<>();
            for (byte kaType : EC_Consts.KA_TYPES) {
                Test allocate = runTest(CommandTest.expect(new Command.AllocateKeyAgreement(this.card, kaType), ExpectedValue.SUCCESS));
                if (allocate.ok()) {
                    Command ecdh = new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, kaType);
                    Test ka = runTest(CommandTest.expect(ecdh, ExpectedValue.SUCCESS));
                    Test kaCompressed = runTest(CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_COMPRESS, kaType), ExpectedValue.SUCCESS));

                    String kaDesc = "Test of the " + CardUtil.getKATypeString(kaType) + " KeyAgreement.";
                    Function<Test[], Result> kaCallback = (tests) -> {
                        if (tests[1].ok() || tests[2].ok()) {
                            return new Result(Result.Value.SUCCESS, "Some ECDH is supported.");
                        } else {
                            return new Result(Result.Value.FAILURE, "ECDH failed.");
                        }
                    };

                    Test compound;
                    if (ka.ok()) {
                        Test perfTest = runTest(PerformanceTest.repeat(ecdh, 10));
                        compound = runTest(CompoundTest.function(kaCallback, kaDesc, allocate, ka, kaCompressed, perfTest));
                    } else {
                        compound = runTest(CompoundTest.function(kaCallback, kaDesc, allocate, ka, kaCompressed));
                    }

                    kaTests.add(compound);
                } else {
                    runTest(allocate);
                    kaTests.add(allocate);
                }
            }
            Test kaTest = runTest(CompoundTest.any(ExpectedValue.SUCCESS, "KeyAgreement tests.", kaTests.toArray(new Test[0])));
            supportTests.add(kaTest);

            List<Test> signTests = new LinkedList<>();
            for (byte sigType : EC_Consts.SIG_TYPES) {
                Test allocate = runTest(CommandTest.expect(new Command.AllocateSignature(this.card, sigType), ExpectedValue.SUCCESS));
                if (allocate.ok()) {
                    Command ecdsa = new Command.ECDSA(this.card, ECTesterApplet.KEYPAIR_LOCAL, sigType, ECTesterApplet.EXPORT_FALSE, null);
                    Test expect = runTest(CommandTest.expect(ecdsa, ExpectedValue.SUCCESS));

                    String signDesc = "Test of the " + CardUtil.getSigTypeString(sigType) + " signature.";

                    Test compound;
                    if (expect.ok()) {
                        Test perfTest = runTest(PerformanceTest.repeat(ecdsa, 10));
                        compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, signDesc, allocate, expect, perfTest));
                    } else {
                        compound = runTest(CompoundTest.all(ExpectedValue.SUCCESS, signDesc, allocate, expect));
                    }
                    signTests.add(compound);
                } else {
                    signTests.add(allocate);
                }
            }
            Test signTest = runTest(CompoundTest.any(ExpectedValue.SUCCESS, "Signature tests.", signTests.toArray(new Test[0])));
            supportTests.add(signTest);

            doTest(CompoundTest.all(ExpectedValue.SUCCESS, description + " Some.", supportTests.toArray(new Test[0])));
            new Command.Cleanup(this.card).send();
        }
    }
}

{'fraction_non_alphanumeric': 0.10541479535632574, 'fraction_numerical': 0.0016947716295229217, 'mean_word_length': 2.4865583456425404, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1688578', 'n_tokens_mistral': 3496, 'n_tokens_neox': 3358, 'n_words': 596}