aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/CardTestSuite.java
blob: 87f6271f53623e643cc251b9b282183714fdb22d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.applet.ECTesterApplet;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.ec.EC_Curve;
import cz.crcs.ectester.common.ec.EC_Params;
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.test.TestSuite;
import cz.crcs.ectester.common.util.ECUtil;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;

import java.util.Arrays;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class CardTestSuite extends TestSuite {
    ECTesterReader.Config cfg;
    CardMngr card;
    String[] options;

    CardTestSuite(TestWriter writer, ECTesterReader.Config cfg, CardMngr cardManager, String name, String[] options, String... description) {
        super(writer, name, description);
        this.card = cardManager;
        this.cfg = cfg;
        this.options = options;
    }

    public CardMngr getCard() {
        return card;
    }

    public ECTesterReader.Config getCfg() {
        return cfg;
    }

    public String[] getOptions() {
        if (options != null) {
            return options.clone();
        } else {
            return options;
        }
    }

    public Test genOrPreset(EC_Curve curve, Result.ExpectedValue expected, byte keyPair) {
        if (Arrays.asList(options).contains("preset") && cfg.testOptions.contains("preset")) {
            Test setLocal = null;
            if ((keyPair & ECTesterApplet.KEYPAIR_LOCAL) != 0) {
                EC_Params priv = ECUtil.fixedRandomKey(curve);
                setLocal = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.CURVE_external, priv.getParams(), priv.flatten()), expected);
            }
            Test setRemote = null;
            if ((keyPair & ECTesterApplet.KEYPAIR_REMOTE) != 0) {
                EC_Params pub = ECUtil.fixedRandomPoint(curve);
                if (pub == null) {
                    setRemote = CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_REMOTE), expected);
                } else {
                    setRemote = CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_REMOTE, EC_Consts.CURVE_external, pub.getParams(), pub.flatten()), expected);
                }
            }

            if (keyPair == ECTesterApplet.KEYPAIR_LOCAL) {
                return setLocal;
            } else if (keyPair == ECTesterApplet.KEYPAIR_REMOTE) {
                return setRemote;
            } else {
                return CompoundTest.all(expected, "Set semi-random parameters.", setLocal, setRemote);
            }
        } else {
            return CommandTest.expect(new Command.Generate(this.card, keyPair), expected);
        }
    }
}