jdk/test/com/sun/jdi/UTF8Test.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 24973 8c4bc3fa4c4e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3432
diff changeset
     2
 * Copyright (c) 2004, 2007, 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: 3432
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3432
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3432
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 5033550
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary  JDWP back end uses modified UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  @author jjh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
3432
8acd97c69118 6868533: 3/4 JDI: remove '-source 1.5' and '-target 1.5' options from com.sun.jdi tests
dcubed
parents: 2
diff changeset
    32
 *  @run compile -g UTF8Test.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main UTF8Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  There is UTF-8 and there is modified UTF-8, which I will call M-UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  The two differ in the representation of binary 0, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  in some other more esoteric representations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
      http://java.sun.com/developer/technicalArticles/Intl/Supplementary/#Modified_UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
      http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/types.html#wp16542
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  All the following are observations of the treatment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  of binary 0.  In UTF-8, this represented as one byte:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
      0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  while in modified UTF-8, it is represented as two bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
      0xc0 0x80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  ** I haven't investigated if the other differences between UTF-8 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     M-UTF-8 are handled in the same way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 Here is how these our handled in our BE, JDWP, and FE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 - Strings in .class files are M-UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 - To get the value of a string object from the VM, our BE calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      char * utf = JNI_FUNC_PTR(env,GetStringUTFChars)(env, string, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   which returns M-UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
- To create a string object in the VM, our BE VirtualMachine.createString() calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      string = JNI_FUNC_PTR(env,NewStringUTF)(env, cstring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      This function expects the string to be M-UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      BUG:  If the string came from JDWP, then it is actually UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
- I haven't investigated strings in JVMTI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
- The JDWP spec says that strings are UTF-8.  The intro
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  says this for all strings, and the createString command and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  the StringRefernce.value command say it explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
- Our FE java writes strings to JDWP as UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
- BE function outStream_writeString uses strlen meaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  it expects no 0 bytes, meaning that it expects M-UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  This function writes the byte length and then calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  outStream.c::writeBytes which just writes the bytes to JDWP as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  BUG: If such a string came from the VM via JNI, it is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
       M-UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  FIX:  - scan string to see if contains an M-UTF-8 char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
          if yes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
             - call String(bytes, 0, len, "UTF8")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
               to get a java string.  Will this work -ie, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
               input is M-UTF-8 instead of real UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             - call some java method (NOT JNI which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
               would just come back with M-UTF-8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
               on the String to get real UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
- The JDWP StringReference.value command does reads a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  from the BE out of the JDWP stream and does this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  createe a Java String for it (see PacketStream.readString):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         String readString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
          String ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
          int len = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
              ret = new String(pkt.data, inCursor, len, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          } catch(java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  This String ctor converts _both- the M-UTF-8 0xc0 0x80
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  and UTF-8 0x00  into a Java char containing 0x0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  Does it do this for the other differences too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
Summary:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
1.  JDWP says strings are UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    We interpret this to mean standard UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
2.  JVMTI will be changed to match JNI saying that strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    are M-UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
3.  The BE gets UTF-8 strings off JDWP and must convert them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    M-UTF-8 before giving it to JVMTI or JNI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
4.  The BE gets M-UTF-8 strings from JNI and JVMTI and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    must convert them to UTF-8 when writing to JDWP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 Here is how the supplementals are represented in java Strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 This from java.lang.Character doc:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    The Java 2 platform uses the UTF-16 representation in char arrays and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    in the String and StringBuffer classes. In this representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    supplementary characters are represented as a pair of char values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    the first from the high-surrogates range, (\uD800-\uDBFF), the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    from the low-surrogates range (\uDC00-\uDFFF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  See utf8.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
NSK Packet.java in the nsk/share/jdwp framework does this to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
a string to JDWP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 public void addString(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        final int count = JDWP.TypeSize.INT + value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        addInt(value.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            addBytes(value.getBytes("UTF-8"), 0, value.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new Failure("Unsupported UTF-8 ecnoding while adding string value to JDWP packet:\n\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 ?? Does this get the standard UTF-8?  I would expect so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
and the readString method does this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            s[i] = getByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return new String(s, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Failure("Unsupported UTF-8 ecnoding while extracting string value from JDWP packet:\n\t"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
Thus, this won't notice the modified UTF-8 coming in from JDWP .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * The debuggee has a few Strings the debugger reads via JDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
class UTF8Targ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static String[] vals = new String[] {"xx\u0000yy",           // standard UTF-8 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                         "xx\ud800\udc00yy",     // first supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                         "xx\udbff\udfffyy"      // last supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                         // d800 = 1101 1000 0000 0000   dc00 = 1101 1100 0000 0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                         // dbff = 1101 1011 1111 1111   dfff = 1101 1111 1111 1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static String aField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        System.out.println("Howdy!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        gus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.out.println("Goodbye from UTF8Targ!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static void gus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
public class UTF8Test extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    ClassType targetClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    Field targetField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    UTF8Test (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        new UTF8Test(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * Get to the top of main()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * to determine targetClass and mainThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        BreakpointEvent bpe = startToMain("UTF8Targ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        targetClass = (ClassType)bpe.location().declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        targetField = targetClass.fieldByName("aField");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        ArrayReference targetVals = (ArrayReference)targetClass.getValue(targetClass.fieldByName("vals"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        /* For each string in the debuggee's 'val' array, verify that we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * read that value via JDI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (int ii = 0; ii < UTF8Targ.vals.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            StringReference val = (StringReference)targetVals.getValue(ii);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            String valStr = val.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
             * Verify that we can read a value correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
             * We read it via JDI, and access it directly from the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
             * var in the debuggee class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (!valStr.equals(UTF8Targ.vals[ii]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                valStr.length() != UTF8Targ.vals[ii].length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                failure("     FAILED: Expected /" + printIt(UTF8Targ.vals[ii]) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        "/, but got /" + printIt(valStr) + "/, length = " + valStr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /* Test 'all' unicode chars - send them to the debuggee via JDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * and then read them back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        doFancyVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        resumeTo("UTF8Targ", "gus", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * resume the target listening for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            println("UTF8Test: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new Exception("UTF8Test: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * For each unicode value, send a string containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * it to the debuggee via JDI, read it back via JDI, and see if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * we get the same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    void doFancyVersion() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // This does 4 chars at a time just to save time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        for (int ii = Character.MIN_CODE_POINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
             ii < Character.MIN_SUPPLEMENTARY_CODE_POINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
             ii += 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // Skip the surrogates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (ii == Character.MIN_SURROGATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                ii = Character.MAX_SURROGATE - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            doFancyTest(ii, ii + 1, ii + 2, ii + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // Do the supplemental chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        for (int ii = Character.MIN_SUPPLEMENTARY_CODE_POINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
             ii <= Character.MAX_CODE_POINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
             ii += 2000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Too many of these so just do a few
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            doFancyTest(ii, ii + 1, ii + 2, ii + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    void doFancyTest(int ... args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String ss = new String(args, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        targetClass.setValue(targetField, vm().mirrorOf(ss));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        StringReference returnedVal = (StringReference)targetClass.getValue(targetField);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String returnedStr = returnedVal.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!ss.equals(returnedStr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            failure("Set: FAILED: Expected /" + printIt(ss) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    "/, but got /" + printIt(returnedStr) + "/, length = " + returnedStr.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Return a String containing binary representations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the chars in a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     String printIt(String arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        char[] carray = arg.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        StringBuffer bb = new StringBuffer(arg.length() * 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        for (int ii = 0; ii < arg.length(); ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            int ccc = arg.charAt(ii);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            bb.append(String.format("%1$04x ", ccc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return bb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    String printIt1(String arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        byte[] barray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             barray = arg.getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (UnsupportedEncodingException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        StringBuffer bb = new StringBuffer(barray.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (int ii = 0; ii < barray.length; ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            bb.append(String.format("%1$02x ", barray[ii]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return bb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}