Skip to content

Commit 78c1ab0

Browse files
author
dudarev
committed
OwnerPin test added
1 parent 8fd6249 commit 78c1ab0

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright 2016 Licel Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package javacard.framework;
17+
18+
import junit.framework.TestCase;
19+
20+
public class OwnerPinTest extends TestCase {
21+
22+
public void testConstructor1() {
23+
boolean isException = false;
24+
try {
25+
OwnerPIN o = new OwnerPIN((byte) 0, (byte) 1);
26+
} catch (Exception e) {
27+
assertEquals(true, (e instanceof PINException));
28+
assertEquals(PINException.ILLEGAL_VALUE, ((PINException) e).getReason());
29+
isException = true;
30+
}
31+
assertEquals(true, isException);
32+
}
33+
34+
public void testConstructor2() {
35+
boolean isException = false;
36+
try {
37+
OwnerPIN o = new OwnerPIN((byte) 1, (byte) 0);
38+
} catch (Exception e) {
39+
assertEquals(true, (e instanceof PINException));
40+
assertEquals(PINException.ILLEGAL_VALUE, ((PINException) e).getReason());
41+
isException = true;
42+
}
43+
assertEquals(true, isException);
44+
}
45+
46+
public void testConstructor3() {
47+
byte tries = 3;
48+
OwnerPIN o = new OwnerPIN(tries, (byte) 16);
49+
assertEquals(tries, o.getTriesRemaining());
50+
}
51+
52+
public void testConstructor4() {
53+
boolean isException = false;
54+
try {
55+
OwnerPIN o = new OwnerPIN((byte) 3, (byte) 16);
56+
byte[] pin = new byte[17];
57+
o.update(pin, (short) 0, (byte) pin.length);
58+
} catch (Exception e) {
59+
assertEquals(true, (e instanceof PINException));
60+
assertEquals(PINException.ILLEGAL_VALUE, ((PINException) e).getReason());
61+
isException = true;
62+
}
63+
assertEquals(true, isException);
64+
}
65+
66+
public void testUpdate() {
67+
byte tries = 3;
68+
byte[] pin = new byte[]{(byte) 0, (byte) 1, (byte) 3};
69+
OwnerPIN o = new OwnerPIN(tries, (byte) 3);
70+
o.update(pin, (short) 0, (byte) pin.length);
71+
assertEquals(true, o.check(pin, (short) 0, (byte) pin.length));
72+
assertEquals(tries, o.getTriesRemaining());
73+
}
74+
75+
public void testCheck() {
76+
byte tries = 4;
77+
byte[] pin = new byte[]{(byte) 0, (byte) 1, (byte) 3};
78+
byte[] pin2 = new byte[]{(byte) 0, (byte) 1, (byte) 2};
79+
OwnerPIN o = new OwnerPIN(tries, (byte) 3);
80+
o.update(pin, (short) 0, (byte) pin.length);
81+
// correct
82+
assertEquals(true, o.check(pin, (short) 0, (byte) pin.length));
83+
assertEquals(tries, o.getTriesRemaining());
84+
assertEquals(true, o.isValidated());
85+
// incorrect
86+
assertEquals(false, o.check(pin2, (short) 0, (byte) pin2.length));
87+
assertEquals(tries - 1, o.getTriesRemaining());
88+
assertEquals(false, o.isValidated());
89+
// incorrect
90+
assertEquals(false, o.check(pin2, (short) 0, (byte) (pin2.length - 1)));
91+
assertEquals(tries - 2, o.getTriesRemaining());
92+
assertEquals(false, o.isValidated());
93+
// incorrect
94+
try {
95+
assertEquals(false, o.check(null, (short) 0, (byte) (pin2.length)));
96+
} catch (Exception e) {
97+
assertEquals(true, (e instanceof NullPointerException));
98+
}
99+
assertEquals(tries - 3, o.getTriesRemaining());
100+
assertEquals(false, o.isValidated());
101+
// incorrect
102+
try {
103+
assertEquals(false, o.check(pin2, (short) 0, (byte) (pin2.length + 1)));
104+
} catch (Exception e) {
105+
assertEquals(true, (e instanceof ArrayIndexOutOfBoundsException));
106+
}
107+
assertEquals(0, o.getTriesRemaining());
108+
assertEquals(false, o.isValidated());
109+
}
110+
111+
public void testReset1() {
112+
byte tries = 1;
113+
byte[] pin = new byte[]{(byte) 0, (byte) 1, (byte) 3};
114+
byte[] pin2 = new byte[]{(byte) 0, (byte) 1, (byte) 2};
115+
OwnerPIN o = new OwnerPIN(tries, (byte) 3);
116+
o.update(pin, (short) 0, (byte) pin.length);
117+
assertEquals(false, o.check(pin2, (short) 0, (byte) pin2.length));
118+
assertEquals(0, o.getTriesRemaining());
119+
o.reset();
120+
assertEquals(0, o.getTriesRemaining());
121+
}
122+
123+
public void testReset2() {
124+
byte tries = 2;
125+
byte[] pin = new byte[]{(byte) 0, (byte) 1, (byte) 3};
126+
byte[] pin2 = new byte[]{(byte) 0, (byte) 1, (byte) 2};
127+
OwnerPIN o = new OwnerPIN(tries, (byte) 3);
128+
o.update(pin, (short) 0, (byte) pin.length);
129+
assertEquals(false, o.check(pin2, (short) 0, (byte) pin2.length));
130+
assertEquals(tries - 1, o.getTriesRemaining());
131+
assertEquals(true, o.check(pin, (short) 0, (byte) pin.length));
132+
o.reset();
133+
assertEquals(tries, o.getTriesRemaining());
134+
}
135+
136+
public void testResetAndUnblock() {
137+
byte tries = 1;
138+
byte[] pin = new byte[]{(byte) 0, (byte) 1, (byte) 3};
139+
byte[] pin2 = new byte[]{(byte) 0, (byte) 1, (byte) 2};
140+
OwnerPIN o = new OwnerPIN(tries, (byte) 3);
141+
o.update(pin, (short) 0, (byte) pin.length);
142+
assertEquals(false, o.check(pin2, (short) 0, (byte) pin2.length));
143+
assertEquals(0, o.getTriesRemaining());
144+
o.resetAndUnblock();
145+
assertEquals(tries, o.getTriesRemaining());
146+
assertEquals(false, o.isValidated());
147+
}
148+
149+
}

0 commit comments

Comments
 (0)