summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/Test.java
blob: cd0cf496992222742b2aad12c039659a28f7e038 (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.reader.command.Command;
import cz.crcs.ectester.reader.response.Response;

import javax.smartcardio.CardException;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * An abstract test that can be run and has a Result.
 *
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class Test {
    boolean hasRun = false;
    Result result;

    public Result getResult() {
        if (!hasRun) {
            return null;
        }
        return result;
    }

    public boolean hasRun() {
        return hasRun;
    }

    public abstract boolean ok();

    public abstract void run() throws CardException;

    /**
     * A result of a Test.
     */
    public enum Result {
        SUCCESS,
        FAILURE,
        ANY
    }

    /**
     * A simple test that runs one Command to get and evaluate one Response
     * to get a Result and compare it with the expected one.
     */
    public static class Simple extends Test {
        private BiFunction<Command, Response, Result> callback;
        private Result expected;
        private Command command;
        private Response response;

        public Simple(Command command, Result expected) {
            this.command = command;
            this.expected = expected;
        }

        public Simple(Command command, Result expected, BiFunction<Command, Response, Result> callback) {
            this(command, expected);
            this.callback = callback;
        }

        public Command getCommand() {
            return command;
        }

        public Response getResponse() {
            return response;
        }

        public Result getExpected() {
            return expected;
        }

        @Override
        public boolean ok() {
            return result == expected || expected == Result.ANY;
        }

        @Override
        public void run() throws CardException {
            response = command.send();
            if (callback != null) {
                result = callback.apply(command, response);
            } else {
                if (response.successful()) {
                    result = Result.SUCCESS;
                } else {
                    result = Result.FAILURE;
                }
            }
            hasRun = true;
        }

        @Override
        public String toString() {
            if (hasRun) {
                return (ok() ? "OK " : "NOK") + " " + response.toString();
            } else {
                return "";
            }
        }
    }

    /**
     * A compound test that runs many Tests and has a Result dependent on all/some of their Results.
     */
    public static class Compound extends Test {
        private Function<Test[], Result> callback;
        private Test[] tests;

        private Compound(Function<Test[], Result> callback, Test... tests) {
            this.callback = callback;
            this.tests = tests;
        }

        public Compound function(Function<Test[], Result> callback, Test... tests) {
            return new Compound(callback, tests);
        }

        public Compound all(Result what, Test... all) {
            return new Compound((tests) -> {
                for (Test test : tests) {
                    if (test.getResult() != what) {
                        return Result.FAILURE;
                    }
                }
                return Result.SUCCESS;
            }, all);
        }

        public Compound any(Result what, Test... any) {
            return new Compound((tests) -> {
                for (Test test : tests) {
                    if (test.getResult() == what) {
                        return Result.SUCCESS;
                    }
                }
                return Result.FAILURE;
            }, any);
        }

        @Override
        public boolean ok() {
            return result == Result.SUCCESS;
        }

        @Override
        public void run() throws CardException {
            for (Test test: tests) {
                test.run();
            }
            result = callback.apply(tests);
        }
    }
}