0001: /*
0002:
0003: Derby - Class org.apache.derby.impl.drda.TestProto
0004:
0005: Licensed to the Apache Software Foundation (ASF) under one or more
0006: contributor license agreements. See the NOTICE file distributed with
0007: this work for additional information regarding copyright ownership.
0008: The ASF licenses this file to You under the Apache License, Version 2.0
0009: (the "License"); you may not use this file except in compliance with
0010: the License. You may obtain a copy of the License at
0011:
0012: http://www.apache.org/licenses/LICENSE-2.0
0013:
0014: Unless required by applicable law or agreed to in writing, software
0015: distributed under the License is distributed on an "AS IS" BASIS,
0016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017: See the License for the specific language governing permissions and
0018: limitations under the License.
0019:
0020: */
0021:
0022: package org.apache.derby.impl.drda;
0023:
0024: import java.io.BufferedReader;
0025: import java.io.FileInputStream;
0026: import java.io.FileNotFoundException;
0027: import java.io.IOException;
0028: import java.io.InputStream;
0029: import java.io.InputStreamReader;
0030: import java.io.OutputStream;
0031: import java.io.StreamTokenizer;
0032: import java.io.UnsupportedEncodingException;
0033: import java.net.Socket;
0034: import java.net.UnknownHostException;
0035: import java.util.Enumeration;
0036: import java.util.Hashtable;
0037: import java.util.Locale;
0038: import java.util.Vector;
0039:
0040: /**
0041: This class is used to test error conditions in the protocol.
0042: The protocol to send to the Net server is contained in a file encoded
0043: as calls to routines in ddmreader and ddmwriter.
0044: Additional commands have been added for testing purposes.
0045: To add tests, modify the file protocol.tests.
0046: Tests can also be done as separate files and given as an argument to
0047: this class.
0048: */
0049:
0050: public class TestProto {
0051:
0052: private static final CodePointNameTable codePointNameTable = new CodePointNameTable();
0053: private static final Hashtable codePointValueTable = new Hashtable();
0054: private static final Hashtable commandTable = new Hashtable();
0055: private static final CcsidManager ccsidManager = new EbcdicCcsidManager();
0056: //commands
0057: private static final int CREATE_DSS_REQUEST = 1;
0058: private static final int CREATE_DSS_OBJECT = 2;
0059: private static final int END_DSS = 3;
0060: private static final int END_DDM_AND_DSS = 4;
0061: private static final int START_DDM = 5;
0062: private static final int END_DDM = 6;
0063: private static final int WRITE_BYTE = 7;
0064: private static final int WRITE_NETWORK_SHORT = 8;
0065: private static final int WRITE_NETWORK_INT = 9;
0066: private static final int WRITE_BYTES = 10;
0067: private static final int WRITE_CODEPOINT_4BYTES = 11;
0068: private static final int WRITE_SCALAR_1BYTE = 12;
0069: private static final int WRITE_SCALAR_2BYTES = 13;
0070: private static final int WRITE_SCALAR_BYTES = 14;
0071: private static final int WRITE_SCALAR_HEADER = 15;
0072: private static final int WRITE_SCALAR_STRING = 16;
0073: private static final int WRITE_SCALAR_PADDED_STRING = 17;
0074: private static final int WRITE_SCALAR_PADDED_BYTES = 18;
0075: private static final int WRITE_SHORT = 19;
0076: private static final int WRITE_INT = 20;
0077: private static final int WRITE_LONG = 21;
0078: private static final int WRITE_FLOAT = 22;
0079: private static final int WRITE_DOUBLE = 23;
0080: private static final int READ_REPLY_DSS = 24;
0081: private static final int READ_LENGTH_AND_CODEPOINT = 25;
0082: private static final int READ_CODEPOINT = 26;
0083: private static final int MARK_COLLECTION = 27;
0084: private static final int GET_CODEPOINT = 28;
0085: private static final int READ_BYTE = 29;
0086: private static final int READ_NETWORK_SHORT = 30;
0087: private static final int READ_SHORT = 31;
0088: private static final int READ_NETWORK_INT = 32;
0089: private static final int READ_INT = 33;
0090: private static final int READ_LONG = 34;
0091: private static final int READ_BOOLEAN = 35;
0092: private static final int READ_STRING = 36;
0093: private static final int READ_BYTES = 37;
0094: private static final int FLUSH = 38;
0095: private static final int DISPLAY = 39;
0096: private static final int CHECKERROR = 40;
0097: private static final int RESET = 41;
0098: private static final int CREATE_DSS_REPLY = 42;
0099: private static final int SKIP_DSS = 43;
0100: private static final int READ_SCALAR_2BYTES = 44;
0101: private static final int READ_SCALAR_1BYTE = 45;
0102: private static final int END_TEST = 46;
0103: private static final int SKIP_DDM = 47;
0104: private static final int INCLUDE = 48;
0105: private static final int SKIP_BYTES = 49;
0106: private static final int WRITE_PADDED_STRING = 50;
0107: private static final int WRITE_STRING = 51;
0108: private static final int WRITE_ENCODED_STRING = 52;
0109: private static final int WRITE_ENCODED_LDSTRING = 53;
0110: private static final int CHECK_SQLCARD = 54;
0111: private static final int MORE_DATA = 55;
0112: private static final int COMPLETE_TEST = 56;
0113: private static final int READ_SECMEC_SECCHKCD = 57;
0114:
0115: private static final String MULTIVAL_START = "MULTIVALSTART";
0116: private static final String MULTIVAL_SEP = "SEP";
0117: private static final String MULTIVAL_END = "MULTIVALEND";
0118: // initialize hash tables
0119: static {
0120: init();
0121: }
0122:
0123: private Socket monitorSocket = null;
0124: private InputStream monitorIs = null;
0125: private OutputStream monitorOs = null;
0126: private DDMWriter writer = new DDMWriter(ccsidManager, null, null);
0127: private DDMReader reader;
0128: private boolean failed = false;
0129: private StreamTokenizer tkn;
0130: private String current_filename;
0131:
0132: // constructor
0133: public TestProto(String filename) {
0134: current_filename = filename;
0135: getConnection();
0136:
0137: try {
0138: reader = new DDMReader(ccsidManager, monitorIs);
0139: processFile(filename);
0140: } catch (Exception e) {
0141: int line = 0;
0142: if (tkn != null)
0143: line = tkn.lineno();
0144: System.err.println("Unexpected exception in line " + line
0145: + " file: " + current_filename);
0146: e.printStackTrace();
0147: } finally {
0148: closeConnection();
0149: }
0150:
0151: }
0152:
0153: /**
0154: * Process include file
0155: *
0156: * @exception IOException, DRDAProtocolException error reading file or protocol
0157: */
0158: private void processIncludeFile() throws IOException,
0159: DRDAProtocolException {
0160: String fileName = getString();
0161: StreamTokenizer saveTkn = tkn;
0162: processFile(fileName);
0163: tkn = saveTkn;
0164: }
0165:
0166: /**
0167: * Process a command file
0168: *
0169: * @param filename
0170: * @exception IOException, DRDAProtocolException error reading file or protocol
0171: */
0172: private void processFile(String filename) throws IOException,
0173: DRDAProtocolException {
0174: String prev_filename = current_filename;
0175: current_filename = filename;
0176: String hostName = getHostName();
0177: BufferedReader fr;
0178: try {
0179: fr = new BufferedReader(new InputStreamReader(
0180: new FileInputStream(filename), "UTF-8"));
0181: } catch (FileNotFoundException fnfe) {
0182: // if useprocess=false & we're running in a suite,
0183: // the location is different, try it
0184: String userdir = System.getProperty("user.dir");
0185: String sep = System.getProperty("file.separator");
0186: fr = new BufferedReader(new InputStreamReader(
0187: new FileInputStream(userdir + sep + ".." + sep
0188: + filename), "UTF-8"));
0189: }
0190: tkn = new StreamTokenizer(fr);
0191: int val;
0192: while ((val = tkn.nextToken()) != StreamTokenizer.TT_EOF) {
0193: switch (val) {
0194: case StreamTokenizer.TT_NUMBER:
0195: break;
0196: case StreamTokenizer.TT_WORD:
0197: processCommand();
0198: break;
0199: case StreamTokenizer.TT_EOL:
0200: break;
0201: }
0202: }
0203: current_filename = prev_filename;
0204: }
0205:
0206: /**
0207: * Set up a connection to the Network server
0208: */
0209: private void getConnection() {
0210: String hostName = getHostName();
0211: try {
0212: monitorSocket = new Socket(hostName, 1527);
0213: } catch (UnknownHostException e) {
0214: System.err.println("Don't know about host: " + hostName);
0215: System.exit(1);
0216: } catch (IOException e) {
0217: System.err
0218: .println("Couldn't get I/O for the connection to: "
0219: + hostName);
0220: System.exit(1);
0221: }
0222: try {
0223: monitorIs = monitorSocket.getInputStream();
0224: monitorOs = monitorSocket.getOutputStream();
0225: } catch (IOException e) {
0226: System.err
0227: .println("Couldn't get I/O for the connection to: "
0228: + hostName);
0229: System.exit(1);
0230: }
0231: }
0232:
0233: /**
0234: * Close connection to the network server
0235: */
0236: private void closeConnection() {
0237: try {
0238: monitorIs.close();
0239: monitorOs.close();
0240: monitorSocket.close();
0241: } catch (Exception e) {
0242: } //Ignore exceptions when closing the connection
0243: }
0244:
0245: /**
0246: * Reset connection for another test
0247: */
0248: private void reset() {
0249: closeConnection();
0250: getConnection();
0251: reader.initialize(monitorIs);
0252: writer.reset(null);
0253: }
0254:
0255: /**
0256: * finish by cleaning up the last connection
0257: */
0258: private void completeTest() {
0259: closeConnection();
0260: }
0261:
0262: /**
0263: * Initialize hashtable for commands and set up a table to translate from
0264: * the codepoint name to the codepoint value
0265: */
0266: private static void init() {
0267: commandTable.put("createdssrequest", new Integer(
0268: CREATE_DSS_REQUEST));
0269: commandTable.put("createdssobject", new Integer(
0270: CREATE_DSS_OBJECT));
0271: commandTable.put("createdssreply",
0272: new Integer(CREATE_DSS_REPLY));
0273: commandTable.put("enddss", new Integer(END_DSS));
0274: commandTable.put("enddss", new Integer(END_DSS));
0275: commandTable.put("endddmanddss", new Integer(END_DDM_AND_DSS));
0276: commandTable.put("startddm", new Integer(START_DDM));
0277: commandTable.put("endddm", new Integer(END_DDM));
0278: commandTable.put("writebyte", new Integer(WRITE_BYTE));
0279: commandTable.put("writenetworkshort", new Integer(
0280: WRITE_NETWORK_SHORT));
0281: commandTable.put("writenetworkint", new Integer(
0282: WRITE_NETWORK_INT));
0283: commandTable.put("writebytes", new Integer(WRITE_BYTES));
0284: commandTable.put("writecodepoint4bytes", new Integer(
0285: WRITE_CODEPOINT_4BYTES));
0286: commandTable.put("writescalar1byte", new Integer(
0287: WRITE_SCALAR_1BYTE));
0288: commandTable.put("writescalar2bytes", new Integer(
0289: WRITE_SCALAR_2BYTES));
0290: commandTable.put("writescalarbytes", new Integer(
0291: WRITE_SCALAR_BYTES));
0292: commandTable.put("writescalarheader", new Integer(
0293: WRITE_SCALAR_HEADER));
0294: commandTable.put("writescalarstring", new Integer(
0295: WRITE_SCALAR_STRING));
0296: commandTable.put("writescalarpaddedstring", new Integer(
0297: WRITE_SCALAR_PADDED_STRING));
0298: commandTable.put("writescalarpaddedbytes", new Integer(
0299: WRITE_SCALAR_PADDED_BYTES));
0300: commandTable.put("writeshort", new Integer(WRITE_SHORT));
0301: commandTable.put("writeint", new Integer(WRITE_INT));
0302: commandTable.put("writelong", new Integer(WRITE_LONG));
0303: commandTable.put("writefloat", new Integer(WRITE_FLOAT));
0304: commandTable.put("writedouble", new Integer(WRITE_DOUBLE));
0305: commandTable.put("readreplydss", new Integer(READ_REPLY_DSS));
0306: commandTable.put("readlengthandcodepoint", new Integer(
0307: READ_LENGTH_AND_CODEPOINT));
0308: commandTable.put("readcodepoint", new Integer(READ_CODEPOINT));
0309: commandTable
0310: .put("markcollection", new Integer(MARK_COLLECTION));
0311: commandTable.put("getcodepoint", new Integer(GET_CODEPOINT));
0312: commandTable.put("readbyte", new Integer(READ_BYTE));
0313: commandTable.put("readnetworkshort", new Integer(
0314: READ_NETWORK_SHORT));
0315: commandTable.put("readshort", new Integer(READ_SHORT));
0316: commandTable.put("readint", new Integer(READ_INT));
0317: commandTable.put("readlong", new Integer(READ_LONG));
0318: commandTable.put("readboolean", new Integer(READ_BOOLEAN));
0319: commandTable.put("readstring", new Integer(READ_STRING));
0320: commandTable.put("readbytes", new Integer(READ_BYTES));
0321: commandTable.put("flush", new Integer(FLUSH));
0322: commandTable.put("display", new Integer(DISPLAY));
0323: commandTable.put("checkerror", new Integer(CHECKERROR));
0324: commandTable.put("reset", new Integer(RESET));
0325: commandTable.put("skipdss", new Integer(SKIP_DSS));
0326: commandTable.put("skipddm", new Integer(SKIP_DDM));
0327: commandTable.put("readscalar2bytes", new Integer(
0328: READ_SCALAR_2BYTES));
0329: commandTable.put("readscalar1byte", new Integer(
0330: READ_SCALAR_1BYTE));
0331: commandTable.put("endtest", new Integer(END_TEST));
0332: commandTable.put("include", new Integer(INCLUDE));
0333: commandTable.put("skipbytes", new Integer(SKIP_BYTES));
0334: commandTable.put("writepaddedstring", new Integer(
0335: WRITE_PADDED_STRING));
0336: commandTable.put("writestring", new Integer(WRITE_STRING));
0337: commandTable.put("writeencodedstring", new Integer(
0338: WRITE_ENCODED_STRING));
0339: commandTable.put("writeencodedldstring", new Integer(
0340: WRITE_ENCODED_LDSTRING));
0341: commandTable.put("checksqlcard", new Integer(CHECK_SQLCARD));
0342: commandTable.put("moredata", new Integer(MORE_DATA));
0343: commandTable.put("completetest", new Integer(COMPLETE_TEST));
0344: commandTable.put("readsecmecandsecchkcd", new Integer(
0345: READ_SECMEC_SECCHKCD));
0346:
0347: Integer key;
0348: for (Enumeration e = codePointNameTable.keys(); e
0349: .hasMoreElements();) {
0350: key = (Integer) e.nextElement();
0351: codePointValueTable.put(codePointNameTable.get(key), key);
0352: }
0353:
0354: }
0355:
0356: /**
0357: * Process a command
0358: */
0359: private void processCommand() throws IOException,
0360: DRDAProtocolException {
0361: Integer icmd = (Integer) commandTable.get(tkn.sval
0362: .toLowerCase(Locale.ENGLISH));
0363: if (icmd == null) {
0364: System.err.println("Unknown command, " + tkn.sval
0365: + " in line " + tkn.lineno());
0366: System.exit(1);
0367: }
0368: int cmd = icmd.intValue();
0369: int codepoint;
0370: int val;
0371: int reqVal;
0372: String str;
0373:
0374: switch (cmd) {
0375: case INCLUDE:
0376: processIncludeFile();
0377: break;
0378: case CREATE_DSS_REQUEST:
0379: writer.createDssRequest();
0380: break;
0381: case CREATE_DSS_OBJECT:
0382: writer.createDssObject();
0383: break;
0384: case CREATE_DSS_REPLY:
0385: writer.createDssReply();
0386: break;
0387: case END_DSS:
0388: tkn.nextToken();
0389: tkn.pushBack();
0390: if ((tkn.sval != null) && tkn.sval.startsWith("0x"))
0391: // use specified chaining.
0392: writer.endDss((getBytes())[0]);
0393: else
0394: // use default chaining
0395: writer.endDss();
0396: break;
0397: case END_DDM:
0398: writer.endDdm();
0399: break;
0400: case END_DDM_AND_DSS:
0401: writer.endDdmAndDss();
0402: break;
0403: case START_DDM:
0404: writer.startDdm(getCP());
0405: break;
0406: case WRITE_SCALAR_STRING:
0407: writer.writeScalarString(getCP(), getString());
0408: break;
0409: case WRITE_SCALAR_2BYTES:
0410: writer.writeScalar2Bytes(getCP(), getIntOrCP());
0411: break;
0412: case WRITE_SCALAR_1BYTE:
0413: writer.writeScalar1Byte(getCP(), getInt());
0414: break;
0415: case WRITE_SCALAR_BYTES:
0416: writer.writeScalarBytes(getCP(), getBytes());
0417: break;
0418: case WRITE_SCALAR_PADDED_BYTES:
0419: writer.writeScalarPaddedBytes(getCP(), getBytes(),
0420: getInt(), ccsidManager.space);
0421: break;
0422: case WRITE_BYTE:
0423: writer.writeByte(getInt());
0424: break;
0425: case WRITE_BYTES:
0426: writer.writeBytes(getBytes());
0427: break;
0428: case WRITE_SHORT:
0429: writer.writeShort(getInt());
0430: break;
0431: case WRITE_INT:
0432: writer.writeInt(getInt());
0433: break;
0434: case WRITE_CODEPOINT_4BYTES:
0435: writer.writeCodePoint4Bytes(getCP(), getInt());
0436: break;
0437: case WRITE_STRING:
0438: str = getString();
0439: writer.writeBytes(getEBCDIC(str));
0440: break;
0441: case WRITE_ENCODED_STRING:
0442: writeEncodedString(getString(), getString());
0443: break;
0444: case WRITE_ENCODED_LDSTRING:
0445: writeEncodedLDString(getString(), getString(), getInt());
0446: break;
0447: case WRITE_PADDED_STRING:
0448: str = getString();
0449: writer.writeBytes(getEBCDIC(str));
0450: int reqLen = getInt();
0451: int strLen = str.length();
0452: if (strLen < reqLen)
0453: writer.padBytes(ccsidManager.space, reqLen - strLen);
0454: break;
0455: case READ_REPLY_DSS:
0456: reader.readReplyDss();
0457: break;
0458: case SKIP_DSS:
0459: skipDss();
0460: break;
0461: case SKIP_DDM:
0462: skipDdm();
0463: break;
0464: case MORE_DATA:
0465: boolean expbool;
0466: str = getString();
0467: if (str.equalsIgnoreCase("true"))
0468: expbool = true;
0469: else
0470: expbool = false;
0471: if (reader.moreData() && expbool == false)
0472: fail("Failed - more data left");
0473: if (!reader.moreData() && expbool == true)
0474: fail("Failed - no data left");
0475: break;
0476: case READ_LENGTH_AND_CODEPOINT:
0477: readLengthAndCodePoint();
0478: break;
0479: case READ_SCALAR_2BYTES:
0480: readLengthAndCodePoint();
0481: val = reader.readNetworkShort();
0482: checkIntOrCP(val);
0483: break;
0484: case READ_SCALAR_1BYTE:
0485: readLengthAndCodePoint();
0486: val = reader.readByte();
0487: checkIntOrCP(val);
0488: break;
0489: case READ_SECMEC_SECCHKCD:
0490: readSecMecAndSECCHKCD();
0491: break;
0492: case READ_BYTES:
0493: byte[] byteArray = reader.readBytes();
0494: byte[] reqArray = getBytes();
0495: if (byteArray.length != reqArray.length)
0496: fail("Failed - byte array didn't match");
0497: for (int i = 0; i < byteArray.length; i++)
0498: if (byteArray[i] != reqArray[i])
0499: fail("Failed - byte array didn't match");
0500: break;
0501: case READ_NETWORK_SHORT:
0502: val = reader.readNetworkShort();
0503: checkIntOrCP(val);
0504: break;
0505: case FLUSH:
0506: writer.finalizeChain(reader.getCurrChainState(), monitorOs);
0507: writer.reset(null);
0508: break;
0509: case DISPLAY:
0510: System.out.println(getString());
0511: break;
0512: case CHECKERROR:
0513: checkError();
0514: break;
0515: case CHECK_SQLCARD:
0516: checkSQLCARD(getInt(), getString());
0517: break;
0518: case COMPLETE_TEST:
0519: completeTest();
0520: break;
0521: case END_TEST:
0522: // print that we passed the test if we haven't failed
0523: if (failed == false)
0524: System.out.println("PASSED");
0525: failed = false;
0526: reset();
0527: break;
0528: case RESET:
0529: reset();
0530: break;
0531: case SKIP_BYTES:
0532: reader.skipBytes();
0533: break;
0534: default:
0535: System.out.println("unknown command in line "
0536: + tkn.lineno());
0537: // skip remainder of line
0538: while (tkn.nextToken() != StreamTokenizer.TT_EOL)
0539: ;
0540:
0541: }
0542: }
0543:
0544: /**
0545: * Skip a DSS communication
0546: */
0547: private void skipDss() throws DRDAProtocolException {
0548: reader.readReplyDss();
0549: reader.skipDss();
0550: }
0551:
0552: /**
0553: * Skip the a Ddm communication
0554: */
0555: private void skipDdm() throws DRDAProtocolException {
0556: reader.readLengthAndCodePoint();
0557: reader.skipBytes();
0558: }
0559:
0560: /**
0561: * Read an int from the command file
0562: * Negative numbers are preceded by "-"
0563: */
0564: private int getInt() throws IOException {
0565: int mult = 1;
0566: int val = tkn.nextToken();
0567: if (tkn.sval != null && tkn.sval.equals("-")) {
0568: mult = -1;
0569: val = tkn.nextToken();
0570: }
0571:
0572: if (val != StreamTokenizer.TT_NUMBER) {
0573: if (tkn.sval == null) {
0574: System.err.println("Invalid string on line "
0575: + tkn.lineno());
0576: System.exit(1);
0577: }
0578: String str = tkn.sval.toLowerCase(Locale.ENGLISH);
0579: if (!str.startsWith("0x")) {
0580: System.err.println("Expecting number, got " + tkn.sval
0581: + " on line " + tkn.lineno());
0582: System.exit(1);
0583: } else
0584: return convertHex(str);
0585: }
0586: return (new Double(tkn.nval).intValue() * mult);
0587: }
0588:
0589: /**
0590: * Convert a token in hex format to int from the command file
0591: */
0592: private int convertHex(String str) throws IOException {
0593: int retval = 0;
0594: int len = str.length();
0595: if ((len % 2) == 1 || len > 10) {
0596: System.err.println("Invalid length for byte string, " + len
0597: + " on line " + tkn.lineno());
0598: System.exit(1);
0599: }
0600: for (int i = 2; i < len; i++) {
0601: retval = retval << 4;
0602: retval += Byte.valueOf(str.substring(i, i + 1), 16)
0603: .byteValue();
0604: }
0605: return retval;
0606: }
0607:
0608: /**
0609: * checks if value matches next int or cp.
0610: * Handles multiple legal values in protocol test file
0611: * FORMAT for Multiple Values
0612: * MULTIVALSTART 10 SEP 32 SEP 40 MULTIVALEND
0613: **/
0614: private boolean checkIntOrCP(int val) throws IOException {
0615: boolean rval = false;
0616: int tknType = tkn.nextToken();
0617: String reqVal = " ";
0618:
0619: if (tknType == StreamTokenizer.TT_WORD
0620: && tkn.sval.trim().equals(MULTIVAL_START)) {
0621: do {
0622: int nextVal = getIntOrCP();
0623: reqVal = reqVal + nextVal + " ";
0624: // System.out.println("Checking MULTIVAL (" + val + "==" + nextVal + ")");
0625: rval = rval || (val == nextVal);
0626: tkn.nextToken();
0627: } while (tkn.sval.trim().equals(MULTIVAL_SEP));
0628:
0629: if (!(tkn.sval.trim().equals(MULTIVAL_END)))
0630: fail("Invalid test file format requires "
0631: + MULTIVAL_END + " got: " + tkn.sval);
0632:
0633: } else {
0634: tkn.pushBack();
0635: int nextVal = getIntOrCP();
0636: reqVal = " " + nextVal;
0637: // System.out.println("Checking Single Value (" + val + "==" + nextVal + ")");
0638: rval = (val == nextVal);
0639: }
0640: if (rval == false)
0641: fail("Failed - wrong val = " + val + " Required Value: "
0642: + reqVal);
0643:
0644: return rval;
0645: }
0646:
0647: /**
0648: * Read an int or codepoint - codepoint is given as a string
0649: */
0650: private int getIntOrCP() throws IOException {
0651: int val = tkn.nextToken();
0652: if (val == StreamTokenizer.TT_NUMBER) {
0653: return new Double(tkn.nval).intValue();
0654: } else if (val == StreamTokenizer.TT_WORD) {
0655: return decodeCP(tkn.sval);
0656: } else {
0657: fail("Expecting number, got " + tkn.sval + " on line "
0658: + tkn.lineno());
0659: System.exit(1);
0660: }
0661: return 0;
0662: }
0663:
0664: /**
0665: * Read an array of bytes from the command file
0666: * A byte string can start with 0x in which case the bytes are interpreted
0667: * in hex format or it can just be a string, in which case each char is
0668: * interpreted as 2 byte UNICODE
0669: *
0670: * @return byte array
0671: */
0672: private byte[] getBytes() throws IOException {
0673: byte[] retval = null;
0674: int val = tkn.nextToken();
0675: if (tkn.sval == null) {
0676: System.err
0677: .println("Invalid string on line " + tkn.lineno());
0678: System.exit(1);
0679: }
0680: String str = tkn.sval.toLowerCase(Locale.ENGLISH);
0681: if (!str.startsWith("0x")) {
0682: //just convert the string to ebcdic byte array
0683: return ccsidManager.convertFromUCS2(str);
0684: } else {
0685: int len = str.length();
0686: if ((len % 2) == 1) {
0687: System.err.println("Invalid length for byte string, "
0688: + len + " on line " + tkn.lineno());
0689: System.exit(1);
0690: }
0691: retval = new byte[(len - 2) / 2];
0692: int j = 0;
0693: for (int i = 2; i < len; i += 2, j++) {
0694: retval[j] = (byte) (Byte.valueOf(
0695: str.substring(i, i + 1), 16).byteValue() << 4);
0696: retval[j] += Byte.valueOf(str.substring(i + 1, i + 2),
0697: 16).byteValue();
0698: }
0699: }
0700: return retval;
0701: }
0702:
0703: /**
0704: * Read a string from the command file
0705: *
0706: * @return string found in file
0707: * @exception IOException error reading file
0708: */
0709: private String getString() throws IOException {
0710: int val = tkn.nextToken();
0711: if (val == StreamTokenizer.TT_NUMBER) {
0712: System.err.println("Expecting word, got " + tkn.nval
0713: + " on line " + tkn.lineno());
0714: System.exit(1);
0715: }
0716: return tkn.sval;
0717: }
0718:
0719: /**
0720: * Read the string version of a CodePoint
0721: *
0722: * @exception IOException error reading file
0723: */
0724: private int getCP() throws IOException {
0725: String strval = getString();
0726: return decodeCP(strval);
0727: }
0728:
0729: /**
0730: * Translate a string codepoint such as ACCSEC to the equivalent int value
0731: *
0732: * @param strval string codepoint
0733: * @return integer value of codepoint
0734: */
0735: private int decodeCP(String strval) {
0736: Integer cp = (Integer) codePointValueTable.get(strval);
0737: if (cp == null) {
0738: System.err.println("Unknown codepoint, " + strval
0739: + " in line " + tkn.lineno());
0740: Exception e = new Exception();
0741: e.printStackTrace();
0742: System.exit(1);
0743: }
0744: return cp.intValue();
0745: }
0746:
0747: /**
0748: * Print failure message and skip to the next test
0749: *
0750: * @exception IOException error reading file
0751: */
0752: private void fail(String msg) throws IOException {
0753: System.out.println("FAILED - " + msg + " in line "
0754: + tkn.lineno());
0755: // skip remainder of the test look for endtest or end of file
0756: int val = tkn.nextToken();
0757: while (val != StreamTokenizer.TT_EOF) {
0758: if (val == StreamTokenizer.TT_WORD
0759: && tkn.sval.toLowerCase(Locale.ENGLISH).equals(
0760: "endtest"))
0761: break;
0762:
0763: val = tkn.nextToken();
0764: }
0765: failed = true;
0766: // get ready for next test
0767: reset();
0768: // print out stack trace so we know where the failure occurred
0769: Exception e = new Exception();
0770: e.printStackTrace();
0771: }
0772:
0773: /**
0774: * Check error sent back to application requester
0775: *
0776: * @exception IOException, DRDAProtocolException error reading file or protocol
0777: */
0778: private void checkError() throws IOException, DRDAProtocolException {
0779: int svrcod = 0;
0780: int invalidCodePoint = 0;
0781: int prccnvcd = 0;
0782: int synerrcd = 0;
0783: int codepoint;
0784: int reqVal;
0785: Vector manager = new Vector(), managerLevel = new Vector();
0786: reader.readReplyDss();
0787: int error = reader.readLengthAndCodePoint();
0788: int reqCP = getCP();
0789: if (error != reqCP) {
0790: cpError(error, reqCP);
0791: return;
0792: }
0793: while (reader.moreDssData()) {
0794: codepoint = reader.readLengthAndCodePoint();
0795: switch (codepoint) {
0796: case CodePoint.SVRCOD:
0797: svrcod = reader.readNetworkShort();
0798: break;
0799: case CodePoint.CODPNT:
0800: invalidCodePoint = reader.readNetworkShort();
0801: break;
0802: case CodePoint.PRCCNVCD:
0803: prccnvcd = reader.readByte();
0804: break;
0805: case CodePoint.SYNERRCD:
0806: synerrcd = reader.readByte();
0807: break;
0808: case CodePoint.MGRLVLLS:
0809: while (reader.moreDdmData()) {
0810: manager.addElement(new Integer(reader
0811: .readNetworkShort()));
0812: managerLevel.addElement(new Integer(reader
0813: .readNetworkShort()));
0814: }
0815: break;
0816: default:
0817: //ignore codepoints we don't understand
0818: reader.skipBytes();
0819:
0820: }
0821: }
0822: reqVal = getInt();
0823: if (svrcod != reqVal) {
0824: fail("wrong svrcod val = " + Integer.toHexString(svrcod)
0825: + ", required val = " + Integer.toHexString(reqVal));
0826: return;
0827: }
0828: if (error == CodePoint.PRCCNVRM) {
0829: reqVal = getInt();
0830: if (prccnvcd != reqVal) {
0831: fail("wrong prccnvd, val = "
0832: + Integer.toHexString(prccnvcd)
0833: + ", required val = "
0834: + Integer.toHexString(reqVal));
0835: return;
0836: }
0837: }
0838: if (error == CodePoint.SYNTAXRM) {
0839: reqVal = getInt();
0840: if (synerrcd != reqVal) {
0841: fail("wrong synerrcd, val = "
0842: + Integer.toHexString(synerrcd)
0843: + ", required val = "
0844: + Integer.toHexString(reqVal));
0845: return;
0846: }
0847: reqVal = getIntOrCP();
0848: if (invalidCodePoint != reqVal) {
0849: cpError(invalidCodePoint, reqVal);
0850: return;
0851: }
0852: }
0853: if (error == CodePoint.MGRLVLRM) {
0854: int mgr, mgrLevel;
0855: for (int i = 0; i < manager.size(); i++) {
0856: reqVal = getCP();
0857: mgr = ((Integer) (manager.elementAt(i))).intValue();
0858: if (mgr != reqVal) {
0859: cpError(mgr, reqVal);
0860: return;
0861: }
0862: mgrLevel = ((Integer) (managerLevel.elementAt(i)))
0863: .intValue();
0864: reqVal = getInt();
0865: if (mgrLevel != reqVal) {
0866: fail("wrong manager level, level = "
0867: + Integer.toHexString(mgrLevel)
0868: + ", required val = "
0869: + Integer.toHexString(reqVal));
0870: return;
0871: }
0872: }
0873: }
0874: }
0875:
0876: /**
0877: * Read length and codepoint and check against required values
0878: *
0879: * @exception IOException, DRDAProtocolException error reading file or protocol
0880: */
0881: private void readLengthAndCodePoint() throws IOException,
0882: DRDAProtocolException {
0883: int codepoint = reader.readLengthAndCodePoint();
0884: int reqCP = getCP();
0885: if (codepoint != reqCP)
0886: cpError(codepoint, reqCP);
0887: }
0888:
0889: /**
0890: * Handle the case of testing the reading of SECMEC and SECCHKCD,
0891: * where on an invalid SECMEC value for ACCSEC, the server can send
0892: * valid supported SECMEC values. One of the valid supported value can be
0893: * EUSRIDPWD (secmec value of 9) depending on if the server JVM
0894: * can actually support it or not.
0895: * @exception IOException, DRDAProtocolException error reading file or protocol
0896: */
0897: private void readSecMecAndSECCHKCD() throws IOException,
0898: DRDAProtocolException {
0899: int codepoint;
0900: boolean notDone = true;
0901: int val = -1;
0902: do {
0903: codepoint = reader.readLengthAndCodePoint();
0904: switch (codepoint) {
0905: case CodePoint.SECMEC: {
0906: System.out.print("SECMEC=");
0907: val = reader.readNetworkShort();
0908: System.out.print(val + " ");
0909: }
0910: break;
0911: case CodePoint.SECCHKCD: {
0912: System.out.print("SECCHKCD=");
0913: val = reader.readByte();
0914: System.out.println(val);
0915: notDone = false;
0916: }
0917: break;
0918: default:
0919: notDone = false;
0920: }
0921: } while (notDone);
0922: }
0923:
0924: /**
0925: * Codepoint error
0926: *
0927: * @exception IOException error reading command file
0928: */
0929: private void cpError(int cp, int reqCP) throws IOException {
0930: String cpName = codePointNameTable.lookup(cp);
0931: String reqCPName = codePointNameTable.lookup(reqCP);
0932: fail("wrong codepoint val = " + Integer.toHexString(cp) + "("
0933: + cpName + ")" + ", required codepoint = "
0934: + Integer.toHexString(reqCP) + "(" + reqCPName + ")");
0935: }
0936:
0937: /**
0938: * Translate a string to EBCDIC for use in the protocol
0939: *
0940: * @param str string to transform
0941: * @return EBCDIC string
0942: */
0943: private byte[] getEBCDIC(String str) {
0944: byte[] buf = new byte[str.length()];
0945: ccsidManager.convertFromUCS2(str, buf, 0);
0946: return buf;
0947: }
0948:
0949: /**
0950: * Write an encoded string
0951: *
0952: * @param str string to write
0953: * @param encoding Java encoding to use
0954: * @exception IOException
0955: */
0956: private void writeEncodedString(String str, String encoding)
0957: throws IOException {
0958: try {
0959: byte[] buf = str.getBytes(encoding);
0960: writer.writeBytes(buf);
0961: } catch (UnsupportedEncodingException e) {
0962: fail("Unsupported encoding " + encoding);
0963: }
0964: }
0965:
0966: /**
0967: * Write length and encoded string
0968: *
0969: * @param str string to write
0970: * @param encoding Java encoding to use
0971: * @param len Size of length value (2 or 4 bytes)
0972: * @exception IOException
0973: */
0974: private void writeEncodedLDString(String str, String encoding,
0975: int len) throws IOException {
0976: try {
0977: byte[] buf = str.getBytes(encoding);
0978: if (len == 2)
0979: writer.writeShort(buf.length);
0980: else
0981: writer.writeInt(buf.length);
0982: writer.writeBytes(buf);
0983: } catch (UnsupportedEncodingException e) {
0984: fail("Unsupported encoding " + encoding);
0985: }
0986: }
0987:
0988: /**
0989: * Check the value of SQLCARD
0990: *
0991: * @param sqlCode SQLCODE value
0992: * @param sqlState SQLSTATE value
0993: * @exception IOException, DRDAProtocolException
0994: */
0995: private void checkSQLCARD(int sqlCode, String sqlState)
0996: throws IOException, DRDAProtocolException {
0997: reader.readReplyDss();
0998: int codepoint = reader.readLengthAndCodePoint();
0999: if (codepoint != CodePoint.SQLCARD) {
1000: fail("Expecting SQLCARD got "
1001: + Integer.toHexString(codepoint));
1002: return;
1003: }
1004: int nullind = reader.readByte();
1005: //cheating here and using readNetworkInt since the byteorder is the same
1006: int code = reader.readNetworkInt();
1007: if (code != sqlCode) {
1008: fail("Expecting sqlCode " + sqlCode + " got "
1009: + Integer.toHexString(code));
1010: return;
1011: }
1012: String state = reader.readString(5, "UTF-8");
1013: if (!state.equals(sqlState)) {
1014: fail("Expecting sqlState " + sqlState + " got " + state);
1015: return;
1016: }
1017: // skip the rest of the SQLCARD
1018: reader.skipBytes();
1019: }
1020:
1021: private static String getHostName() {
1022: String hostName = (System.getProperty("hostName"));
1023: if (hostName == null)
1024: hostName = "localhost";
1025: return hostName;
1026: }
1027: }
|