aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/output/YAMLTestWriter.java
blob: 7c99a4ac728e44402235c371dd9c9cce28c34254 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package cz.crcs.ectester.reader.output;

import cz.crcs.ectester.common.output.BaseYAMLTestWriter;
import cz.crcs.ectester.common.test.TestSuite;
import cz.crcs.ectester.common.test.Testable;
import cz.crcs.ectester.common.util.ByteUtil;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
import cz.crcs.ectester.reader.command.Command;
import cz.crcs.ectester.reader.response.Response;
import cz.crcs.ectester.reader.test.CardTestSuite;
import cz.crcs.ectester.reader.test.CommandTestable;

import javax.smartcardio.CardException;
import java.io.PrintStream;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class YAMLTestWriter extends BaseYAMLTestWriter {
    public YAMLTestWriter(PrintStream output) {
        super(output);
    }

    private Map<String, Object> commandObject(Command c) {
        Map<String, Object> commandObj = new LinkedHashMap<>();
        if (c == null) {
            return commandObj;
        }
        commandObj.put("apdu", ByteUtil.bytesToHex(c.getAPDU().getBytes()));
        commandObj.put("desc", c.getDescription());
        return commandObj;
    }

    private Map<String, Object> responseObject(Response r) {
        Map<String, Object> responseObj = new LinkedHashMap<>();
        if (r == null) {
            return responseObj;
        }
        responseObj.put("successful", r.successful());
        responseObj.put("apdu", ByteUtil.bytesToHex(r.getAPDU().getBytes()));
        responseObj.put("natural_sw", Short.toUnsignedInt(r.getNaturalSW()));
        List<Integer> sws = new LinkedList<>();
        for (int i = 0; i < r.getNumSW(); ++i) {
            sws.add(Short.toUnsignedInt(r.getSW(i)));
        }
        responseObj.put("sws", sws);
        responseObj.put("duration", r.getDuration());
        responseObj.put("desc", r.getDescription());
        return responseObj;
    }

    @Override
    protected Map<String, Object> testableObject(Testable t) {
        if (t instanceof CommandTestable) {
            CommandTestable cmd = (CommandTestable) t;
            Map<String, Object> result = new LinkedHashMap<>();
            result.put("type", "command");
            result.put("command", commandObject(cmd.getCommand()));
            result.put("response", responseObject(cmd.getResponse()));
            return result;
        }
        return null;
    }

    private Map<String, Object> cplcObject(CardMngr card) {
        Map<String, Object> result = new LinkedHashMap<>();
        try {
            CardMngr.CPLC cplc = card.getCPLC();
            if (!cplc.values().isEmpty()) {
                for (Map.Entry<CardMngr.CPLC.Field, byte[]> entry : cplc.values().entrySet()) {
                    CardMngr.CPLC.Field field = entry.getKey();
                    byte[] value = entry.getValue();
                    result.put(field.name(), ByteUtil.bytesToHex(value, false));
                }
            }
        } catch (CardException ignored) {
        }
        return result;
    }

    @Override
    protected Map<String, Object> deviceObject(TestSuite suite) {
        if (suite instanceof CardTestSuite) {
            CardTestSuite cardSuite = (CardTestSuite) suite;
            Map<String, Object> result = new LinkedHashMap<>();
            result.put("type", "card");
            result.put("ectester", ECTesterReader.VERSION + ECTesterReader.GIT_COMMIT);
            result.put("cplc", cplcObject(cardSuite.getCard()));
            result.put("ATR", ByteUtil.bytesToHex(cardSuite.getCard().getATR().getBytes(), false));
            return result;
        }
        return null;
    }
}