jdk/test/sun/security/smartcardio/TestPresent.java
author igerasim
Fri, 12 Feb 2016 00:20:16 +0300
changeset 35778 e2a3c03097de
parent 25656 676435b3c625
child 41106 f37e9bfeae01
permissions -rw-r--r--
8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35778
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
     2
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6293769 6294527
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test that the isCardPresent()/waitForX() APIs work correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/manual TestPresent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    32
// This test requires special hardware.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    33
25656
676435b3c625 8050893: (smartcardio) Invert reset argument in tests in sun/security/smartcardio
igerasim
parents: 23357
diff changeset
    34
import java.util.List;
676435b3c625 8050893: (smartcardio) Invert reset argument in tests in sun/security/smartcardio
igerasim
parents: 23357
diff changeset
    35
import javax.smartcardio.CardTerminal;
676435b3c625 8050893: (smartcardio) Invert reset argument in tests in sun/security/smartcardio
igerasim
parents: 23357
diff changeset
    36
import javax.smartcardio.TerminalFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
35778
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    38
public class TestPresent extends Utils {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static class Timer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        private long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        long update() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            long t = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            long diff = t - time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            time = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        long print() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            long t = update();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.out.println("Elapsed time: " + t + " ms.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static boolean isFalse(boolean b) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new Exception("not false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static boolean isTrue(boolean b) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new Exception("not true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static void main(String[] args) throws Exception {
35778
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    70
        CardTerminal terminal = getTerminal(args);
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    71
        if (terminal == null) {
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    72
            System.out.println("Skipping the test: " +
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    73
                    "no card terminals available");
e2a3c03097de 8059212: Modify sun/security/smartcardio manual regression tests so that they do not just fail if no cardreader found
igerasim
parents: 25656
diff changeset
    74
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        while (terminal.isCardPresent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("*** Remove card!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Timer timer = new Timer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("Testing waitForCardAbsent() with card already absent...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        isTrue(terminal.waitForCardAbsent(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        isTrue(terminal.waitForCardAbsent(100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        isTrue(terminal.waitForCardAbsent(10000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        isTrue(terminal.waitForCardAbsent(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Testing waitForCardPresent() timeout...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        isFalse(terminal.waitForCardPresent(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        isFalse(terminal.waitForCardPresent(100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        isFalse(terminal.waitForCardPresent(1000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        isFalse(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        isFalse(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println("*** Insert card!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        isTrue(terminal.waitForCardPresent(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        isTrue(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        isTrue(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("Testing waitForCardPresent() with card already present...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        isTrue(terminal.waitForCardPresent(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        isTrue(terminal.waitForCardPresent(10000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        isTrue(terminal.waitForCardPresent(100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        isTrue(terminal.waitForCardPresent(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println("Testing waitForCardAbsent() timeout...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        isFalse(terminal.waitForCardAbsent(1000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        isFalse(terminal.waitForCardAbsent(100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        isFalse(terminal.waitForCardAbsent(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println("*** Remove card!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        isTrue(terminal.waitForCardAbsent(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        timer.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        isFalse(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        isFalse(terminal.isCardPresent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.out.println("OK.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}