示例#1
0
  public void check(String name, String in, String[] out, BreakIterator bi, TestHarness harness) {
    harness.checkPoint(name);
    bi.setText(in);

    int index = 0;
    int from = bi.current();
    harness.check(from, 0);

    while (true) {
      int to = bi.next();
      if (to == BreakIterator.DONE) break;
      harness.check(in.substring(from, to), out[index]);
      ++index;
      from = to;
    }

    harness.check(index, out.length);

    harness.checkPoint("backwards " + name);
    bi.last();
    index = out.length - 1;
    from = bi.current();
    harness.check(from, in.length());

    while (true) {
      int to = bi.previous();
      if (to == BreakIterator.DONE) break;
      harness.check(in.substring(to, from), out[index]);
      --index;
      from = to;
    }

    harness.check(index, -1);
  }
  /** implemented. */
  public void test_getProperty() {
    th.checkPoint("getProperty(java.lang.String)java.lang.String");
    Properties p = new Properties();
    try {
      p.getProperty(null);
      th.fail("should throw a NullPointerException -- 1");
    } catch (NullPointerException ne) {
      th.check(true);
    }

    p = new Properties(defProps);
    try {
      p.getProperty(null);
      th.fail("should throw a NullPointerException -- 2");
    } catch (NullPointerException ne) {
      th.check(true);
    }
    try {
      p.load(bin);
    } catch (Exception e) {
    }
    try {
      p.getProperty(null);
      th.fail("should throw a NullPointerException -- 1");
    } catch (NullPointerException ne) {
      th.check(true);
    }
    try {
      th.check(p.getProperty("dog").equals("no_cat"), "check returnvalue");
      th.check(p.getProperty("name").equals("no"), "return property from main property table");
      th.check(p.getProperty("Smart").equals("move"), "check returnvalue from default table");
      th.check(p.getProperty("NoEntry") == null, "check for null if not there");
    } catch (Exception e) {
      th.fail("got unexpected exception: " + e);
    }
    th.checkPoint("getProperty(java.lang.String,java.lang.String)java.lang.String");
    try {
      p.getProperty(null, "Noooo...");
      th.fail("should throw a NullPointerException -- 1");
    } catch (NullPointerException ne) {
      th.check(true);
    }
    try {
      th.check(p.getProperty("Noooo...", null) == null, "defVal may be null");
    } catch (NullPointerException ne) {
      th.fail("shouldn't throw a NullPointerException -- 1");
    }
    th.check(p.getProperty("dog", "not found").equals("no_cat"), "check returnvalue");
    th.check(
        p.getProperty("name", "not found").equals("no"),
        "return property from main property table");
    th.check(
        p.getProperty("Smart", "not found").equals("move"), "check returnvalue from default table");
    th.check(
        p.getProperty("NoEntry", "not found").equals("not found"), "check for defVal if not there");
  }
示例#3
0
  public void test(TestHarness h)
  {
    String name;
    Level level;
    int value;

    /* Pass a name. */
    for (int i = 0; i < TestUtils.LEVELS.length; i++)
      {
        name = TestUtils.NAMES[i];

        h.checkPoint(name);
        h.check(Level.parse(name) == TestUtils.LEVELS[i]);

	// Simulate a new "parsed" string as name.
        h.check(Level.parse((" " + name + " ").trim()) == TestUtils.LEVELS[i]);
      }

    /* Pass a number. */
    for (int i = 0; i < TestUtils.LEVELS.length; i++)
      {
        value = TestUtils.VALUES[i];
        name = String.valueOf(value);
        h.checkPoint(name);
        h.check(Level.parse(name), TestUtils.LEVELS[i]);
      }

    /* Parse a non-standard name. */
    h.checkPoint("non-standard name");
    try {
      Level.parse("non-standard name");
      h.check(false);
    } catch (IllegalArgumentException _) {
      h.check(true);
    } catch (Exception _) {
      h.check(false);
    }

    /* Parse a null string. */
    h.checkPoint("parse(null)");
    try {
      Level.parse(null);
      h.check(false);
    } catch (NullPointerException _) {
      h.check(true);
    } catch (Exception _) {
      h.check(false);
    }
  }
 /**
  * implemented. <br>
  * might need extra testcode
  */
 public void test_Properties() {
   th.checkPoint("Properties()");
   // not much to check for this one !
   Properties p = new Properties();
   th.check(p.isEmpty(), "nothing in there");
   th.checkPoint("Properties(java.util.Properties)");
   p = new Properties(defProps);
   th.check(p.isEmpty(), "nothing in there");
   th.check(p.getProperty("name").equals("yes"), "default field is not empty");
   try {
     p = new Properties(null);
     th.check(true);
   } catch (Exception e) {
     th.fail("should not throw an Exeption. Got: " + e);
   }
 }
 public void test(TestHarness harness) {
   harness.checkPoint("TestOfSquare");
   cipher = new Square();
   HashMap attrib = new HashMap();
   attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(16));
   attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
   try {
     cipher.init(attrib);
     String algorithm = cipher.name();
     harness.check(validityTest(), "validityTest(" + algorithm + ")");
     harness.check(cloneabilityTest(), "cloneabilityTest(" + algorithm + ")");
     harness.check(katVK(vk_128, cipher, 16), "KAT VK " + algorithm + "-128");
     harness.check(katVT(vt_128, cipher, 16), "KAT VT " + algorithm + "-128");
     harness.check(
         mctEncryptECB(mct_ecb_e_128, cipher, 16), "MCT ECB Encryption " + algorithm + "-128");
     harness.check(
         mctDecryptECB(mct_ecb_d_128, cipher, 16), "MCT ECB Decryption " + algorithm + "-128");
     harness.check(
         mctEncryptCBC(mct_cbc_e_128, cipher, 16), "MCT CBC Encryption " + algorithm + "-128");
     harness.check(
         mctDecryptCBC(mct_cbc_d_128, cipher, 16), "MCT CBC Decryption " + algorithm + "-128");
   } catch (Exception x) {
     harness.debug(x);
     harness.fail("TestOfSquare");
   }
 }
示例#6
0
  private void testObject(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(Object[], Object)");
    Object[] b1 = new Object[] {"1", "2", "3"};
    harness.check(Arrays.binarySearch(b1, "0") == -1);
    harness.check(Arrays.binarySearch(b1, "1") == 0);
    harness.check(Arrays.binarySearch(b1, "2") == 1);
    harness.check(Arrays.binarySearch(b1, "3") == 2);
    harness.check(Arrays.binarySearch(b1, "4") == -4);

    // searching for null throws NullPointerException
    boolean pass = false;
    try {
      Arrays.binarySearch(b1, null);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    pass = false;
    try {
      Arrays.binarySearch((Object[]) null, "0");
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    harness.checkPoint("Arrays.binarySearch(Object[], Object, Comparator)");
    harness.check(Arrays.binarySearch(b1, "0", (Comparator) null) == -1);
    harness.check(Arrays.binarySearch(b1, "1", (Comparator) null) == 0);
    harness.check(Arrays.binarySearch(b1, "2", (Comparator) null) == 1);
    harness.check(Arrays.binarySearch(b1, "3", (Comparator) null) == 2);
    harness.check(Arrays.binarySearch(b1, "4", (Comparator) null) == -4);

    Arrays.sort(b1, new ReverseComparator());
    harness.check(Arrays.binarySearch(b1, "0", new ReverseComparator()) == -4);
    harness.check(Arrays.binarySearch(b1, "1", new ReverseComparator()) == 2);
    harness.check(Arrays.binarySearch(b1, "2", new ReverseComparator()) == 1);
    harness.check(Arrays.binarySearch(b1, "3", new ReverseComparator()) == 0);
    harness.check(Arrays.binarySearch(b1, "4", new ReverseComparator()) == -1);

    b1 = new Object[0];
    harness.check(Arrays.binarySearch(b1, ""), -1);
  }
示例#7
0
 public void test(TestHarness harness) {
   harness.checkPoint("TestOfToByteArray");
   try {
     BigInteger x = new BigInteger(BYTES);
     harness.verbose("*** x = 0x" + x.toString(16));
     byte[] ba = x.toByteArray();
     harness.check(Arrays.equals(ba, BYTES), true, "Byte arrays MUST be equal");
   } catch (Exception x) {
     harness.debug(x);
     harness.fail("TestOfToByteArray: " + x);
   }
 }
示例#8
0
  public void test(TestHarness harness) {
    harness.checkPoint("getBytes13");

    test1Encoding(harness, "ASCII", "abc", ABC1);
    test1Encoding(harness, "Cp1252", "abc", ABC1);
    test1Encoding(harness, "ISO8859_1", "abc", ABC1);
    test1Encoding(harness, "UTF8", "abc", ABC1);
    test1Encoding(harness, "UTF-16", "abc", ABC2);
    test1Encoding(harness, "UnicodeBig", "abc", ABC2);
    test1Encoding(harness, "UnicodeBigUnmarked", "abc", ABC3);
    test1Encoding(harness, "UnicodeLittle", "abc", ABC4);
    test1Encoding(harness, "UnicodeLittleUnmarked", "abc", ABC5);
  }
  /** implemented. */
  public void test_propertyNames() {
    th.checkPoint("propertyNames()java.util.Enumeration");
    Properties p = new Properties();
    try {
      p.load(bin);
    } catch (Exception e) {
    }

    Enumeration en = p.propertyNames();
    Enumeration ek = p.keys();
    boolean ok = true;
    Vector v = new Vector();
    Enumeration ek2 = p.keys();
    while (ek2.hasMoreElements()) {
      v.add(ek2.nextElement());
    }
    while (ek.hasMoreElements() && en.hasMoreElements()) {
      ek.nextElement();
      Object next = en.nextElement();
      if (!v.contains(next)) {
        ok = false;
        th.debug(next + " not in " + v);
      }
    }
    th.check(ok, "all elements are the same");
    th.check(
        !ek.hasMoreElements() && !en.hasMoreElements(), "make sure both enumerations are empty");
    p = new Properties(defProps);
    resetStreams();
    try {
      p.load(bin);
    } catch (Exception e) {
    }
    v.add("Smart");
    v.add("animal");
    en = p.propertyNames();
    ok = true;
    Object o;
    while (en.hasMoreElements()) {
      o = en.nextElement();
      if (v.contains(o)) v.removeElement(o);
      else {
        ok = false;
        th.debug("got extra element: " + o);
      }
    }
    th.check(ok, "see if no double were generated");
    th.check(v.isEmpty(), "check if all names were mentioned -- got:" + v);
  }
示例#10
0
 public void test(TestHarness harness) {
   harness.checkPoint("TestOfCast5");
   cipher = new Cast5();
   HashMap attrib = new HashMap();
   attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(8));
   attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
   try {
     harness.check(validityTest(), "validityTest()");
     harness.check(cloneabilityTest(), "cloneabilityTest()");
     harness.check(vectorsTest(), "vectorsTest()");
   } catch (Exception x) {
     harness.debug(x);
     harness.fail("TestOfCast5");
   }
 }
 /** implemented. */
 public void test_setProperty() {
   th.checkPoint("setProperty(java.lang.String,java.lang.String)java.lang.Object");
   Properties p = new Properties();
   try {
     p.setProperty(null, "Noooo...");
     th.fail("should throw NullPointerException -- 1");
   } catch (NullPointerException ne) {
     th.check(true);
   }
   try {
     p.setProperty("Noooo...", null);
     th.fail("should throw NullPointerException -- 2");
   } catch (NullPointerException ne) {
     th.check(true);
   }
   p = new Properties(defProps);
   try {
     p.load(bin);
   } catch (Exception e) {
   }
   try {
     p.setProperty(null, "Noooo...");
     th.fail("should throw NullPointerException -- 3");
   } catch (NullPointerException ne) {
     th.check(true);
   }
   try {
     p.setProperty("No again...", null);
     th.fail("should throw NullPointerException -- 4");
   } catch (NullPointerException ne) {
     th.check(true);
   }
   try {
     th.check(((String) p.setProperty("test", "null")).equals(""), "returns \"\" in our case");
   } catch (NullPointerException ne) {
     th.fail("the value a of property cannot be null, got:" + ne);
   }
   th.check(p.getProperty("test").equals("null"), "check new value in our case null");
   th.check(p.setProperty("testing", "null") == null, "returns value null, name not in list");
   th.check(p.getProperty("test").equals("null"), "check new value in our case null");
   String s = (String) p.setProperty("Smart", "nul");
   th.check(s == null, "returnvalue, is null default list not touched, got");
   th.check(p.getProperty("Smart").equals("nul"), "check new value in our case null");
   s = ((String) p.setProperty("name", "nu"));
   th.check(s.equals("no"), "return value in our case no, got: " + s);
   th.check(p.getProperty("name").equals("nu"), "check new value in our case nu");
 }
示例#12
0
  private void testDouble(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(double[], double)");
    double[] b1 = new double[] {1.0, 2.0, 3.0};
    harness.check(Arrays.binarySearch(b1, 0.0) == -1);
    harness.check(Arrays.binarySearch(b1, 1.0) == 0);
    harness.check(Arrays.binarySearch(b1, 2.0) == 1);
    harness.check(Arrays.binarySearch(b1, 3.0) == 2);
    harness.check(Arrays.binarySearch(b1, 4.0) == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((double[]) null, 0.0);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new double[0];
    harness.check(Arrays.binarySearch(b1, 0.0), -1);
  }
示例#13
0
  private void testByte(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(byte[], byte)");
    byte[] b1 = new byte[] {1, 2, 3};
    harness.check(Arrays.binarySearch(b1, (byte) 0) == -1);
    harness.check(Arrays.binarySearch(b1, (byte) 1) == 0);
    harness.check(Arrays.binarySearch(b1, (byte) 2) == 1);
    harness.check(Arrays.binarySearch(b1, (byte) 3) == 2);
    harness.check(Arrays.binarySearch(b1, (byte) 4) == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((byte[]) null, (byte) 0);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new byte[0];
    harness.check(Arrays.binarySearch(b1, (byte) 0), -1);
  }
示例#14
0
  private void testChar(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(char[], char)");
    char[] b1 = new char[] {'1', '2', '3'};
    harness.check(Arrays.binarySearch(b1, '0') == -1);
    harness.check(Arrays.binarySearch(b1, '1') == 0);
    harness.check(Arrays.binarySearch(b1, '2') == 1);
    harness.check(Arrays.binarySearch(b1, '3') == 2);
    harness.check(Arrays.binarySearch(b1, '4') == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((char[]) null, '0');
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new char[0];
    harness.check(Arrays.binarySearch(b1, '0'), -1);
  }
示例#15
0
  private void testFloat(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(float[], float)");
    float[] b1 = new float[] {1.0f, 2.0f, 3.0f};
    harness.check(Arrays.binarySearch(b1, 0.0f) == -1);
    harness.check(Arrays.binarySearch(b1, 1.0f) == 0);
    harness.check(Arrays.binarySearch(b1, 2.0f) == 1);
    harness.check(Arrays.binarySearch(b1, 3.0f) == 2);
    harness.check(Arrays.binarySearch(b1, 4.0f) == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((float[]) null, 0.0f);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new float[0];
    harness.check(Arrays.binarySearch(b1, 0.0f), -1);
  }
示例#16
0
  private void testShort(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(short[], short)");
    short[] b1 = new short[] {1, 2, 3};
    harness.check(Arrays.binarySearch(b1, (short) 0) == -1);
    harness.check(Arrays.binarySearch(b1, (short) 1) == 0);
    harness.check(Arrays.binarySearch(b1, (short) 2) == 1);
    harness.check(Arrays.binarySearch(b1, (short) 3) == 2);
    harness.check(Arrays.binarySearch(b1, (short) 4) == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((short[]) null, (short) 0);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new short[0];
    harness.check(Arrays.binarySearch(b1, (short) 0), -1);
  }
示例#17
0
  private void testLong(TestHarness harness) {
    harness.checkPoint("Arrays.binarySearch(long[], long)");
    long[] b1 = new long[] {1, 2, 3};
    harness.check(Arrays.binarySearch(b1, 0) == -1);
    harness.check(Arrays.binarySearch(b1, 1) == 0);
    harness.check(Arrays.binarySearch(b1, 2) == 1);
    harness.check(Arrays.binarySearch(b1, 3) == 2);
    harness.check(Arrays.binarySearch(b1, 4) == -4);

    boolean pass = false;
    try {
      Arrays.binarySearch((long[]) null, 0);
    } catch (NullPointerException e) {
      pass = true;
    }
    harness.check(pass);

    b1 = new long[0];
    harness.check(Arrays.binarySearch(b1, 0), -1);
  }
 /** this test will check if all forms lines are accepted in the load. */
 public void test_loadextra() {
   th.checkPoint("load(java.io.InputStream)void");
   Properties p = new Properties();
   buffer =
       new String(
               "   !comment\n \t  \nname = no\r    #morec\tomm\\\nents\r\n  dog=no\\\\cat   \nburps    :\ntest=\ndate today\n\n\nlong\\\n   value=tryin \\\n gto\n4:vier\nvier     :4")
           .getBytes();
   bin = new ByteArrayInputStream(buffer);
   try {
     p.load(bin);
   } catch (Exception e) {
   }
   Enumeration e = p.keys();
   Vector v = new Vector();
   // Note that there used to be code here checking whether the
   // "!comment" and "#morec" keys were found, on the theory that
   // leading whitespace mattered.  This no longer seems to be the
   // case, however.  In the past it apparently varied between JVMs,
   // but the 1.4 docs are unambiguous on this topic.  We check for
   // "ents" since line-continuation doesn't affect comments.
   v.add("ents=");
   v.add("name=no");
   v.add("dog=no\\cat   ");
   v.add("burps=");
   v.add("test=");
   v.add("date=today");
   v.add("longvalue=tryin gto");
   v.add("4=vier");
   v.add("vier=4");
   String s;
   while (e.hasMoreElements()) {
     s = (String) e.nextElement();
     th.debug("checkvalue -- got:$" + s + "=" + p.getProperty(s) + "$");
     th.check(
         v.contains(s + "=" + p.getProperty(s)),
         "checkvalue -- got:$" + s + "=" + p.getProperty(s) + "$");
     v.removeElement(s + "=" + p.getProperty(s));
   }
   th.check(v.isEmpty(), "check if all elements were found -- got: " + v);
 }
  private void testSymmetry(TestHarness harness, int ndx) {
    harness.checkPoint("TestOfAssembly.testSymmetry#" + ndx);

    byte[] km = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8};
    byte[] iv = new byte[] {-1, -2, -3, -4, -5, -6, -7, -8, -9};
    byte[] pt = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    byte[] tpt = new byte[11 * pt.length];

    // forward
    modeAttributes.put(IBlockCipher.KEY_MATERIAL, km);
    modeAttributes.put(IMode.IV, iv);
    attributes.put(Assembly.DIRECTION, Direction.FORWARD);
    try {
      asm.init(attributes);
    } catch (TransformerException x) {
      harness.debug(x);
      harness.fail("Forward initialisation");
      return;
    }

    byte[] ct = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      for (int i = 0; i < 10; i++) { // transform in parts of 12-byte a time
        System.arraycopy(pt, 0, tpt, i * pt.length, pt.length);
        ct = asm.update(pt);
        baos.write(ct, 0, ct.length);
      }
    } catch (TransformerException x) {
      harness.debug(x);
      harness.fail("Forward transformation");
      return;
    }
    try {
      System.arraycopy(pt, 0, tpt, 10 * pt.length, pt.length);
      ct = asm.lastUpdate(pt);
    } catch (TransformerException x) {
      harness.debug(x);
      harness.fail("Forward last transformation");
      return;
    }
    baos.write(ct, 0, ct.length);
    ct = baos.toByteArray();

    // reversed
    attributes.put(Assembly.DIRECTION, Direction.REVERSED);
    try {
      asm.init(attributes);
    } catch (TransformerException x) {
      harness.debug(x);
      harness.fail("Reverse initialisation");
      return;
    }

    byte[] ot;
    try {
      ot = asm.lastUpdate(ct); // transform the lot in one go
    } catch (TransformerException x) {
      harness.debug(x);
      harness.fail("Reverse transformation");
      return;
    }

    harness.check(Arrays.equals(ot, tpt), "symmetric test");
  }
示例#20
0
  public void test(TestHarness harness) {
    harness.checkPoint("TestOfMD2");
    try {
      algorithm = new MD2();
      harness.check(algorithm.selfTest(), "selfTest");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.selfTest");
    }

    try {
      algorithm = new MD2();
      algorithm.update("a".getBytes(), 0, 1);
      byte[] md = algorithm.digest();
      String exp = "32EC01EC4A6DAC72C0AB96FB34C0B5D1";
      harness.check(exp.equals(Util.toString(md)), "testA");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testA");
    }

    try {
      algorithm = new MD2();
      algorithm.update("abc".getBytes(), 0, 3);
      byte[] md = algorithm.digest();
      String exp = "DA853B0D3F88D99B30283A69E6DED6BB";
      harness.check(exp.equals(Util.toString(md)), "testABC");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testABC");
    }

    try {
      algorithm = new MD2();
      algorithm.update("message digest".getBytes(), 0, 14);
      byte[] md = algorithm.digest();
      String exp = "AB4F496BFB2A530B219FF33031FE06B0";
      harness.check(exp.equals(Util.toString(md)), "testMessageDigest");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testMessageDigest");
    }

    try {
      algorithm = new MD2();
      algorithm.update("abcdefghijklmnopqrstuvwxyz".getBytes(), 0, 26);
      byte[] md = algorithm.digest();
      String exp = "4E8DDFF3650292AB5A4108C3AA47940B";
      harness.check(exp.equals(Util.toString(md)), "testAlphabet");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testAlphabet");
    }

    try {
      algorithm = new MD2();
      algorithm.update(
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".getBytes(), 0, 62);
      byte[] md = algorithm.digest();
      String exp = "DA33DEF2A42DF13975352846C30338CD";
      harness.check(exp.equals(Util.toString(md)), "testAsciiSubset");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testAsciiSubset");
    }

    try {
      algorithm = new MD2();
      algorithm.update(
          "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
              .getBytes(),
          0,
          80);
      byte[] md = algorithm.digest();
      String exp = "D5976F79D83D3A0DC9806C3C66F3EFD8";
      harness.check(exp.equals(Util.toString(md)), "testEightyNumerics");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testEightyNumerics");
    }

    try {
      algorithm = new MD2();
      algorithm.update("a".getBytes(), 0, 1);
      clone = (IMessageDigest) algorithm.clone();
      byte[] md = algorithm.digest();
      String exp = "32EC01EC4A6DAC72C0AB96FB34C0B5D1";
      harness.check(exp.equals(Util.toString(md)), "testCloning #1");

      clone.update("bc".getBytes(), 0, 2);
      md = clone.digest();
      exp = "DA853B0D3F88D99B30283A69E6DED6BB";
      harness.check(exp.equals(Util.toString(md)), "testCloning #2");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfMD2.testCloning");
    }
  }
  /** implemented. */
  public void test_list() {
    th.checkPoint("list(java.io.PrintStream)void");
    Properties p = new Properties();
    try {
      p.list((PrintStream) null);
      th.fail("should throw NullPointerException -- 1");
    } catch (NullPointerException ne) {
      th.check(true);
    }

    try {
      p.load(bin);
    } catch (Exception e) {
    }
    p.list(psout);
    byte ba[] = bout.toByteArray();
    Vector v = new Vector();
    Enumeration ek = p.keys();
    String s;
    while (ek.hasMoreElements()) {
      s = (String) ek.nextElement();
      v.add(s + "=" + p.getProperty(s));
    }
    v.add("Smart=move");
    v.add("animal=dog");

    int start, count = 0;
    v.removeElement("longvalue=I'mtryingtogiveavaluelongerthen40characters");
    v.add("longvalue=I'mtryingtogiveavaluelongerthen40char...");
    while (count < ba.length) {
      start = count;
      while (ba[count] != '\n' && count < ba.length) {
        count++;
      }
      s = new String(ba, start, count - start);
      if (!s.startsWith("--")) // list() adds a header
      th.check(v.contains(s), "v does not contain:$" + s + "$");
      v.removeElement(s);
      count++;
    }

    try {
      p.list((PrintStream) null);
      th.fail("should throw NullPointerException -- 2");
    } catch (NullPointerException ne) {
      th.check(true);
    }

    th.checkPoint("list(java.io.PrintWriter)void");
    resetStreams();
    p = new Properties();
    try {
      p.list((PrintWriter) null);
      th.fail("should throw NullPointerException -- 1");
    } catch (NullPointerException ne) {
      th.check(true);
    }

    try {
      p.load(bin);
    } catch (Exception e) {
    }
    p.list(pwout);
    ba = bout.toByteArray();
    v = new Vector();
    ek = p.keys();
    while (ek.hasMoreElements()) {
      s = (String) ek.nextElement();
      v.add(s + "=" + p.getProperty(s));
    }
    v.add("Smart=move");
    v.add("animal=dog");

    count = 0;
    v.removeElement("longvalue=I'mtryingtogiveavaluelongerthen40characters");
    v.add("longvalue=I'mtryingtogiveavaluelongerthen40char...");

    while (count < ba.length) {
      start = count;
      while (ba[count] != '\n' && count < ba.length) {
        count++;
      }
      s = new String(ba, start, count - start);
      if (!s.startsWith("--")) // list() adds a header
      th.check(v.contains(s), "v does not contain:$" + s + "$");
      v.removeElement(s);
      count++;
    }

    try {
      p.list((PrintStream) null);
      th.fail("should throw NullPointerException -- 2");
    } catch (NullPointerException ne) {
      th.check(true);
    }
  }
  /**
   * implemented. <br>
   * Add a test to determine of store generates a comment line with the current time <br>
   * <br>
   * depricated method !!!
   */
  public void test_save() {
    th.checkPoint("save(java.io.OutputStream,java.lang.String)void");
    Properties p = new Properties(defProps);
    try {
      p.save(null, "no comment");
      th.fail("should throw NullPointerException");
    } catch (NullPointerException ne) {
      th.check(true);
    } catch (Exception e) {
      th.fail("should throw an NullPointerEception instead of: " + e);
    }

    try {
      p.save(bout, null);
      th.check(true);
    } catch (NullPointerException ne) {
      th.fail("should not throw NullPointerException");
    } catch (Exception e) {
      th.fail("shouldn't throw any Exception, but got: " + e);
    }

    resetStreams();
    try {
      p.save(bout, null);
    } catch (Exception e) {
      th.fail("shouldn't throw any Exception, but got: " + e);
    }
    byte ba[] = bout.toByteArray();
    th.check((ba[0] == (byte) '#') && (ba[1] != (byte) '#'), "just date should be written");
    th.debug(ba.length + " -- got: " + new String(ba));
    th.check(ba.length < 50, "default properties are never printed out");
    resetStreams();
    try {
      p.load(bin);
    } catch (Exception e) {
    }
    try {
      p.save(bout, "no comments");
    } catch (Exception e) {
      th.fail("shouldn't throw any Exception, but got: " + e);
    }
    ba = bout.toByteArray();
    String s = new String(ba, 0, 12);
    th.check(s.equals("#no comments"), "got: " + s);
    int i = 0, count = 0;
    while (i < 2 && count < ba.length) {
      if (ba[count++] == (byte) '\n') i++;
    }
    // we will construct a vector containing all the lines with should be written
    Vector v = new Vector();
    Enumeration ek = p.keys();
    while (ek.hasMoreElements()) {
      s = (String) ek.nextElement();
      v.add(s + "=" + p.getProperty(s));
    }
    while (count < ba.length) {
      int start = count;
      while (count < ba.length) {
        if (ba[count] != '\n') count++;
        else break;
      }
      s = new String(ba, start, count - start);
      th.check(v.contains(s), "v does not contain: " + s);
      v.removeElement(s);
      count++;
    }
  }
  /**
   * implemented. <br>
   * load is used by other tests to make a propeties file <br>
   * failures in load will mak other tests fail !
   */
  public void test_load() {
    th.checkPoint("load(java.io.InputStream)void");
    Properties p = new Properties();
    try {
      p.load((ByteArrayInputStream) null);
      th.fail("should throw NullPointerException");
    } catch (NullPointerException ne) {
      th.check(true);
    } catch (Exception e) {
      th.fail("should throw an NullPointerException instead of: " + e);
    }

    try {
      p.load(bin);
    } catch (Exception e) {
    }
    Enumeration ek1 = p.keys();
    resetStreams();
    try {
      p.load(bin);
    } catch (Exception e) {
    }
    Enumeration ek2 = p.keys();
    boolean ok = true;
    while (ek1.hasMoreElements() && ek2.hasMoreElements()) {
      if (ek1.nextElement() != ek2.nextElement()) ok = false;
    }
    th.check(
        !ek1.hasMoreElements() && !ek2.hasMoreElements(),
        "no extra elements may be added with same name");
    th.check(ok, " all elements are equal ");
    bin = new ByteArrayInputStream(new String("name=yes\nSmart=move\nanimal=dog").getBytes());
    try {
      p.load(bin);
    } catch (Exception e) {
    }
    th.check(p.getProperty("name").equals("yes"), "load overrides previous values");
    Vector v = new Vector();
    v.add("name");
    v.add("Smart");
    v.add("animal");
    v.add("dog");
    v.add("test");
    v.add("date");
    v.add("longvalue");
    v.add("40chars");
    ek1 = p.keys();
    ok = true;
    Object o;
    while (ek1.hasMoreElements()) {
      o = ek1.nextElement();
      if (v.contains(o)) v.removeElement(o);
      else {
        ok = false;
        th.debug("got extra element: " + (String) o);
      }
    }

    th.check(ok, "all elements were there");
    th.check(v.isEmpty(), "all elements should be gone, got" + v);
    setUpTest();
  }
示例#24
0
  public void test(TestHarness harness) {
    harness.checkPoint("TestOfTMMH16");

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 2
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba, 0xbe, 0xba, 0xde }
    output: { 0x9d, 0x6a }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0x9d, (byte) 0x6a};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(2));
      mac.init(attributes);
      message =
          new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, (byte) 0xba, (byte) 0xde};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector1");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector1");
    }

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 2
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba }
    output: { 0xc8, 0x8e }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0xc8, (byte) 0x8e};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(2));
      mac.init(attributes);
      message = new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector2");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector2");
    }

    /*
    KEY_LENGTH: 10
    TAG_LENGTH: 4
    key: { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc }
    message: { 0xca, 0xfe, 0xba, 0xbe, 0xba, 0xde }
    output: { 0x9d, 0x6a, 0xc0, 0xd3 }
    */
    try {
      attributes.clear();
      keystream = new DummyKeystream();
      keystream.init(null);

      output = new byte[] {(byte) 0x9d, (byte) 0x6a, (byte) 0xc0, (byte) 0xd3};
      mac = new TMMH16();
      attributes.put(TMMH16.KEYSTREAM, keystream);
      attributes.put(TMMH16.TAG_LENGTH, new Integer(4));
      mac.init(attributes);
      message =
          new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, (byte) 0xba, (byte) 0xde};
      for (int i = 0; i < message.length; i++) {
        mac.update(message[i]);
      }
      result = mac.digest();
      harness.check(Arrays.equals(result, output), "testVector3");
    } catch (Exception x) {
      harness.debug(x);
      harness.fail("TestOfTMMH16.testVector3");
    }
  }