📄 testproto.java
字号:
case RESET: reset(); break; case SKIP_BYTES: reader.skipBytes(); break; default: System.out.println("unknown command in line " + tkn.lineno()); // skip remainder of line while (tkn.nextToken() != StreamTokenizer.TT_EOL) ; } } /** * Skip a DSS communication */ private void skipDss() throws DRDAProtocolException { reader.readReplyDss(); reader.skipDss(); } /** * Skip the a Ddm communication */ private void skipDdm() throws DRDAProtocolException { reader.readLengthAndCodePoint(); reader.skipBytes(); } /** * Read an int from the command file * Negative numbers are preceded by "-" */ private int getInt() throws IOException { int mult = 1; int val = tkn.nextToken(); if (tkn.sval != null && tkn.sval.equals("-")) { mult = -1; val = tkn.nextToken(); } if (val != StreamTokenizer.TT_NUMBER) { if (tkn.sval == null) { System.err.println("Invalid string on line " + tkn.lineno()); System.exit(1); } String str = tkn.sval.toLowerCase(Locale.ENGLISH); if (!str.startsWith("0x")) { System.err.println("Expecting number, got " + tkn.sval + " on line " + tkn.lineno()); System.exit(1); } else return convertHex(str); } return (new Double(tkn.nval).intValue() * mult); } /** * Convert a token in hex format to int from the command file */ private int convertHex(String str) throws IOException { int retval = 0; int len = str.length(); if ((len % 2) == 1 || len > 10) { System.err.println("Invalid length for byte string, " + len + " on line " + tkn.lineno()); System.exit(1); } for (int i = 2; i < len; i++) { retval = retval << 4; retval += Byte.valueOf(str.substring(i, i+1), 16).byteValue(); } return retval; } /** * checks if value matches next int or cp. * Handles multiple legal values in protocol test file * FORMAT for Multiple Values * MULTIVALSTART 10 SEP 32 SEP 40 MULTIVALEND **/ private boolean checkIntOrCP(int val) throws IOException { boolean rval = false; int tknType = tkn.nextToken(); String reqVal = " "; if (tknType == StreamTokenizer.TT_WORD && tkn.sval.trim().equals(MULTIVAL_START)) { do { int nextVal = getIntOrCP(); reqVal = reqVal + nextVal + " "; // System.out.println("Checking MULTIVAL (" + val + "==" + nextVal + ")"); rval = rval || (val == nextVal); tkn.nextToken(); } while(tkn.sval.trim().equals(MULTIVAL_SEP)); if (! (tkn.sval.trim().equals(MULTIVAL_END))) fail("Invalid test file format requires " + MULTIVAL_END + " got: " + tkn.sval); } else { tkn.pushBack(); int nextVal = getIntOrCP(); reqVal = " " + nextVal; // System.out.println("Checking Single Value (" + val + "==" + nextVal + ")"); rval = (val == nextVal); } if (rval == false) fail("Failed - wrong val = " + val + " Required Value: " + reqVal); return rval; } /** * Read an int or codepoint - codepoint is given as a string */ private int getIntOrCP() throws IOException { int val = tkn.nextToken(); if (val == StreamTokenizer.TT_NUMBER) { return new Double(tkn.nval).intValue(); } else if (val == StreamTokenizer.TT_WORD) { return decodeCP(tkn.sval); } else { fail("Expecting number, got " + tkn.sval + " on line " + tkn.lineno()); System.exit(1); } return 0; } /** * Read an array of bytes from the command file * A byte string can start with 0x in which case the bytes are interpreted * in hex format or it can just be a string, in which case each char is * interpreted as 2 byte UNICODE * * @return byte array */ private byte [] getBytes() throws IOException { byte[] retval = null; int val = tkn.nextToken(); if (tkn.sval == null) { System.err.println("Invalid string on line " + tkn.lineno()); System.exit(1); } String str = tkn.sval.toLowerCase(Locale.ENGLISH); if (!str.startsWith("0x")) { //just convert the string to ebcdic byte array return ccsidManager.convertFromUCS2(str); } else { int len = str.length(); if ((len % 2) == 1) { System.err.println("Invalid length for byte string, " + len + " on line " + tkn.lineno()); System.exit(1); } retval = new byte[(len-2)/2]; int j = 0; for (int i = 2; i < len; i+=2, j++) { retval[j] = (byte)(Byte.valueOf(str.substring(i, i+1), 16).byteValue() << 4); retval[j] += Byte.valueOf(str.substring(i+1, i+2), 16).byteValue(); } } return retval; } /** * Read a string from the command file * * @return string found in file * @exception IOException error reading file */ private String getString() throws IOException { int val = tkn.nextToken(); if (val == StreamTokenizer.TT_NUMBER) { System.err.println("Expecting word, got " + tkn.nval + " on line " + tkn.lineno()); System.exit(1); } return tkn.sval; } /** * Read the string version of a CodePoint * * @exception IOException error reading file */ private int getCP() throws IOException { String strval = getString(); return decodeCP(strval); } /** * Translate a string codepoint such as ACCSEC to the equivalent int value * * @param strval string codepoint * @return integer value of codepoint */ private int decodeCP(String strval) { Integer cp = (Integer)codePointValueTable.get(strval); if (cp == null) { System.err.println("Unknown codepoint, "+ strval + " in line " + tkn.lineno()); Exception e = new Exception(); e.printStackTrace(); System.exit(1); } return cp.intValue(); } /** * Print failure message and skip to the next test * * @exception IOException error reading file */ private void fail(String msg) throws IOException { System.out.println("FAILED - " + msg + " in line " + tkn.lineno()); // skip remainder of the test look for endtest or end of file int val = tkn.nextToken(); while (val != StreamTokenizer.TT_EOF) { if (val == StreamTokenizer.TT_WORD && tkn.sval.toLowerCase(Locale.ENGLISH).equals("endtest")) break; val = tkn.nextToken(); } failed = true; // get ready for next test reset(); // print out stack trace so we know where the failure occurred Exception e = new Exception(); e.printStackTrace(); } /** * Check error sent back to application requester * * @exception IOException, DRDAProtocolException error reading file or protocol */ private void checkError() throws IOException, DRDAProtocolException { int svrcod = 0; int invalidCodePoint = 0; int prccnvcd = 0; int synerrcd = 0; int codepoint; int reqVal; Vector manager = new Vector(), managerLevel = new Vector() ; reader.readReplyDss(); int error = reader.readLengthAndCodePoint(); int reqCP = getCP(); if (error != reqCP) { cpError(error, reqCP); return; } while (reader.moreDssData()) { codepoint = reader.readLengthAndCodePoint(); switch (codepoint) { case CodePoint.SVRCOD: svrcod = reader.readNetworkShort(); break; case CodePoint.CODPNT: invalidCodePoint = reader.readNetworkShort(); break; case CodePoint.PRCCNVCD: prccnvcd = reader.readByte(); break; case CodePoint.SYNERRCD: synerrcd = reader.readByte(); break; case CodePoint.MGRLVLLS: while (reader.moreDdmData()) { manager.addElement(new Integer(reader.readNetworkShort())); managerLevel.addElement(new Integer(reader.readNetworkShort())); } break; default: //ignore codepoints we don't understand reader.skipBytes(); } } reqVal = getInt(); if (svrcod != reqVal) { fail("wrong svrcod val = " + Integer.toHexString(svrcod) + ", required val = " + Integer.toHexString(reqVal)); return; } if (error == CodePoint.PRCCNVRM) { reqVal = getInt(); if (prccnvcd != reqVal) { fail("wrong prccnvd, val = " + Integer.toHexString(prccnvcd) + ", required val = " + Integer.toHexString(reqVal)); return; } } if (error == CodePoint.SYNTAXRM) { reqVal = getInt(); if (synerrcd != reqVal) { fail("wrong synerrcd, val = " + Integer.toHexString(synerrcd) + ", required val = " + Integer.toHexString(reqVal)); return; } reqVal = getIntOrCP(); if (invalidCodePoint != reqVal) { cpError(invalidCodePoint, reqVal); return; } } if (error == CodePoint.MGRLVLRM) { int mgr, mgrLevel; for (int i = 0; i < manager.size(); i++) { reqVal = getCP(); mgr = ((Integer)(manager.elementAt(i))).intValue(); if (mgr != reqVal) { cpError(mgr, reqVal); return; } mgrLevel = ((Integer)(managerLevel.elementAt(i))).intValue(); reqVal = getInt(); if (mgrLevel != reqVal) { fail("wrong manager level, level = " + Integer.toHexString(mgrLevel) + ", required val = " + Integer.toHexString(reqVal)); return; } } } } /** * Read length and codepoint and check against required values * * @exception IOException, DRDAProtocolException error reading file or protocol */ private void readLengthAndCodePoint() throws IOException, DRDAProtocolException { int codepoint = reader.readLengthAndCodePoint(); int reqCP = getCP(); if (codepoint != reqCP) cpError(codepoint, reqCP); } /** * Codepoint error * * @exception IOException error reading command file */ private void cpError(int cp, int reqCP) throws IOException { String cpName = (String)codePointNameTable.get(new Integer(cp)); String reqCPName = (String)codePointNameTable.get(new Integer(reqCP)); fail("wrong codepoint val = " + Integer.toHexString(cp) + "("+cpName+")" + ", required codepoint = " + Integer.toHexString(reqCP) + "("+reqCPName+")"); } /** * Translate a string to EBCDIC for use in the protocol * * @param str string to transform * @return EBCDIC string */ private byte[] getEBCDIC(String str) { byte [] buf = new byte[str.length()]; ccsidManager.convertFromUCS2(str, buf, 0); return buf; } /** * Write an encoded string * * @param str string to write * @param encoding Java encoding to use * @exception IOException */ private void writeEncodedString(String str, String encoding) throws IOException { try { byte [] buf = str.getBytes(encoding); writer.writeBytes(buf); } catch (UnsupportedEncodingException e) { fail("Unsupported encoding " + encoding); } } /** * Write length and encoded string * * @param str string to write * @param encoding Java encoding to use * @param len Size of length value (2 or 4 bytes) * @exception IOException */ private void writeEncodedLDString(String str, String encoding, int len) throws IOException { try { byte [] buf = str.getBytes(encoding); if (len == 2) writer.writeShort(buf.length); else writer.writeInt(buf.length); writer.writeBytes(buf); } catch (UnsupportedEncodingException e) { fail("Unsupported encoding " + encoding); } } /** * Check the value of SQLCARD * * @param sqlCode SQLCODE value * @param sqlState SQLSTATE value * @exception IOException, DRDAProtocolException */ private void checkSQLCARD(int sqlCode, String sqlState) throws IOException, DRDAProtocolException { reader.readReplyDss(); int codepoint = reader.readLengthAndCodePoint(); if (codepoint != CodePoint.SQLCARD) { fail("Expecting SQLCARD got "+ Integer.toHexString(codepoint)); return; } int nullind = reader.readByte(); //cheating here and using readNetworkInt since the byteorder is the same int code = reader.readNetworkInt(); if (code != sqlCode) { fail("Expecting sqlCode " + sqlCode + " got "+ Integer.toHexString(code)); return; } String state = reader.readString(5, "UTF-8"); if (!state.equals(sqlState)) { fail("Expecting sqlState " + sqlState + " got "+ state); return; } // skip the rest of the SQLCARD reader.skipBytes(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -