1 /*
   2  * Copyright (c) 2000, 2018, 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 /*
  25  * @test
  26  * @bug 8198882
  27  * @summary Tests that we can get an attribute that has a nonstandard name from
  28  *          a DNS entry.
  29  * @modules java.base/sun.security.util
  30  * @library ../lib/
  31  * @run main GetNonstandard
  32  */
  33 
  34 import javax.naming.NamingException;
  35 import javax.naming.directory.Attribute;
  36 import javax.naming.directory.Attributes;
  37 import java.util.Arrays;
  38 
  39 public class GetNonstandard extends GetAttrsBase {
  40     {
  41         // set new test data instead of default value
  42         setMandatoryAttrs(new String[] { "29" // LOC
  43         });
  44     }
  45 
  46     // "40 2 0.373 N 105 17 23.528 W 1638m"
  47     private static final byte[] EXPECTED_VALUE = { (byte) 0,    // version
  48             (byte) 18,    // size
  49             (byte) 22,    // horiz pre
  50             (byte) 19,    // vert pre
  51             (byte) -120,    // latitude 1
  52             (byte) -105,    // latitude 2
  53             (byte) 26,    // longitude 1
  54             (byte) 53,    // longitude 2
  55             (byte) 105,    // altitude 1
  56             (byte) 104,    // altitude 2
  57             (byte) 65, (byte) 56, (byte) 0, (byte) -101, (byte) 22,
  58             (byte) 88, };
  59 
  60     public static void main(String[] args) throws Exception {
  61         new GetNonstandard().run(args);
  62     }
  63 
  64     @Override public void runTest() throws Exception {
  65         initContext();
  66         Attributes retAttrs = getAttributes();
  67         verifyAttributes(retAttrs);
  68         verifyLoc(retAttrs);
  69     }
  70 
  71     /*
  72      * Tests that we can get an attribute that has a nonstandard name from
  73      * a DNS entry.
  74      */
  75     @Override public Attributes getAttributes() throws Exception {
  76         return context().getAttributes(getKey(), getMandatoryAttrs());
  77     }
  78 
  79     private void verifyLoc(Attributes retAttrs) throws NamingException {
  80         Attribute loc = retAttrs.get(getMandatoryAttrs()[0]);
  81         byte[] val = (byte[]) loc.get(0);
  82 
  83         DNSTestUtils.debug("Expected: " + Arrays.toString(EXPECTED_VALUE));
  84         DNSTestUtils.debug("Actual:   " + Arrays.toString(val));
  85 
  86         if (val.length != EXPECTED_VALUE.length) {
  87             throw new RuntimeException(
  88                     "Failed: unexpected value length " + val.length
  89                             + ", expected " + EXPECTED_VALUE.length);
  90         }
  91 
  92         if (!Arrays.equals(val, EXPECTED_VALUE)) {
  93             throw new RuntimeException("Failed: values not match");
  94         }
  95     }
  96 }