jdk/test/sun/security/krb5/auto/CommMatcher.java
changeset 43880 b5015f742ba6
parent 43879 a6dc784b18a8
parent 43854 76c52ad1e6c7
child 43881 4d99ca794b88
equal deleted inserted replaced
43879:a6dc784b18a8 43880:b5015f742ba6
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.util.ArrayList;
       
    25 import java.util.List;
       
    26 import java.util.regex.Matcher;
       
    27 import java.util.regex.Pattern;
       
    28 
       
    29 /**
       
    30  * Matches the krb5 debug output:
       
    31  * >>> KDCCommunication: kdc=host UDP:11555, timeout=100,Attempt =1, #bytes=138
       
    32  *
       
    33  * Example:
       
    34  *   CommMatcher cm = new CommMatcher();
       
    35  *   cm.addPort(12345).addPort(23456);
       
    36  *   for (String line : debugOutput) {
       
    37  *       if (cm.match(line)) {
       
    38  *           println("KDC: %c, %s, Timeout: %d\n",
       
    39  *              cm.kdc(), cm.protocol(), cm.timeout());
       
    40  *       }
       
    41  *   }
       
    42  */
       
    43 public class CommMatcher {
       
    44 
       
    45     static final Pattern re = Pattern.compile(
       
    46             ">>> KDCCommunication: kdc=\\S+ (TCP|UDP):(\\d+), " +
       
    47                     "timeout=(\\d+),Attempt\\s*=(\\d+)");
       
    48 
       
    49     List<Integer> kdcPorts = new ArrayList<>();
       
    50     Matcher matcher;
       
    51 
       
    52     /**
       
    53      * Add KDC ports one by one. The 1st KDC will be 'a' in {@link #kdc()},
       
    54      * 2nd is 'b', etc, etc.
       
    55      */
       
    56     public CommMatcher addPort(int port) {
       
    57         if (port > 0) {
       
    58             kdcPorts.add(port);
       
    59         } else {
       
    60             kdcPorts.clear();
       
    61         }
       
    62         return this;
       
    63     }
       
    64 
       
    65     public boolean match(String line) {
       
    66         matcher = re.matcher(line);
       
    67         return matcher.find();
       
    68     }
       
    69 
       
    70     public String protocol() {
       
    71         return matcher.group(1);
       
    72     }
       
    73 
       
    74     public char kdc() {
       
    75         int port = Integer.parseInt(matcher.group(2));
       
    76         return (char)(kdcPorts.indexOf(port) + 'a');
       
    77     }
       
    78 
       
    79     public int timeout() {
       
    80         return BadKdc.toSymbolicSec(Integer.parseInt(matcher.group(3)));
       
    81     }
       
    82 
       
    83     public int attempt() {
       
    84         return Integer.parseInt(matcher.group(4));
       
    85     }
       
    86 }