Esempio n. 1
0
 public boolean equals(MerkleTreeNode node) {
   boolean retVal = true;
   MerkleTreeNode testNode;
   try {
     testNode = getNode(node.min, node.max);
     retVal = retVal && (testNode.equals(node));
   } catch (NotInTreeException e) {
     retVal = false;
   }
   return retVal;
 }
Esempio n. 2
0
  public static void main(String[] args) {
    Vector<Indexable> files = new Vector<Indexable>();
    for (int i = 0; i < 5; i++) {
      files.add(new BFTFile(new String("Taylor" + i)));
    }
    MerkleTree tree = new MerkleTree(files);
    // System.out.println(tree.getRoot().min + ":" + tree.getRoot().max);
    BFT.util.UnsignedTypes.printBytes(tree.getRoot().getBytes());

    // System.out.println("Changing a leaf!");
    BFTFile newFile = new BFTFile(new String("Hello World!"));
    tree.updateLeafData(3, newFile);
    tree.update();
    // System.out.println(tree.getRoot().min + ":" + tree.getRoot().max);
    BFT.util.UnsignedTypes.printBytes(tree.getRoot().getBytes());

    // System.out.println("Adding a leaf past the current capacity!");
    newFile = new BFTFile(new String("Goodbye Cruel World!"));
    tree.updateLeafData(12, newFile);
    tree.update();
    // System.out.println(tree.getRoot().min + ":" + tree.getRoot().max);
    BFT.util.UnsignedTypes.printBytes(tree.getRoot().getBytes());

    // System.out.println("Changing a leaf!");
    newFile = new BFTFile(new String("Hello World!"));
    tree.updateLeafData(14, newFile);
    tree.update();
    // System.out.println(tree.getRoot().min + ":" + tree.getRoot().max);
    BFT.util.UnsignedTypes.printBytes(tree.getRoot().getBytes());

    // System.out.println("Adding a leaf past the current capacity!");
    newFile = new BFTFile(new String("Goodbye Cruel World!"));
    tree.updateLeafData(49, newFile);
    tree.update();
    // System.out.println(tree.getRoot().min + ":" + tree.getRoot().max);
    BFT.util.UnsignedTypes.printBytes(tree.getRoot().getBytes());

    System.out.print("Testing leaf node retrieval 1: ");
    MerkleTreeNode test = new MerkleTreeNode(12);
    MerkleTreeNode test1 = tree.getNode(12);
    if (test.equals(test1)) {
      // System.out.println("FAIL");
      BFT.util.UnsignedTypes.printBytes(test.getBytes());
      // System.out.println();
      BFT.util.UnsignedTypes.printBytes(test1.getBytes());
    } else {
      // System.out.println("WIN");
    }

    System.out.print("Testing leaf node retrieval 2 (");
    newFile = (BFTFile) tree.getLeafData(49);
    System.out.print(newFile.getFilename() + "): ");
    test = new MerkleTreeNode(newFile.getBytes(), 49);
    test1 = tree.getNode(49);
    if (test.equals(test1)) {
      // System.out.println("WIN");
    } else {
      // System.out.println("FAIL");
      BFT.util.UnsignedTypes.printBytes(test.getBytes());
      // System.out.println();
      BFT.util.UnsignedTypes.printBytes(test1.getBytes());
    }

    System.out.print("Testing intermediate node retrieval (52,53): ");
    try {
      test = tree.getNode(52, 53);
      // if(test.min == 52 && test.max == 53) //System.out.println("WIN");
      // else //System.out.println("FAIL");
    } catch (NotInTreeException e3) {
      // System.out.println("FAIL");
    }

    System.out.print("Testing tree equality 1: ");
    Vector<Indexable> files1 = new Vector<Indexable>();
    for (int i = 0; i < 5; i++) {
      files1.add(new BFTFile(new String("Taylor" + i)));
    }
    Vector<Indexable> files2 = new Vector<Indexable>();
    for (int i = 0; i < 5; i++) {
      files2.add(new BFTFile(new String("Taylor" + i)));
    }
    MerkleTree tree1 = new MerkleTree(files1);
    MerkleTree tree2 = new MerkleTree(files2);
    if (tree1.getRoot().equals(tree2.getRoot())) // System.out.println("WIN");
      // else //System.out.println("FAIL");

      System.out.print("Testing tree equality 2: ");
    tree2.updateLeafData(7, new BFTFile(new String("IGGY IGGY")));
    tree2.update();
    if (!tree1.getRoot().equals(tree2.getRoot())) // System.out.println("WIN");
      // else //System.out.println("FAIL");

      System.out.print("Testing tree equality 3: ");
    MerkleTreeNode pullNode = null;
    try {
      pullNode = tree1.getNode(0, 3);
      // if(tree2.equals(pullNode)) //System.out.println("WIN");
      // else //System.out.println("FAIL");
    } catch (NotInTreeException e2) {
      // System.out.println("FAIL");
    }

    System.out.print("Testing tree equality 4: ");
    try {
      pullNode = tree1.getNode(0, 3);
      byte[] token = pullNode.getToken();
      // if(tree2.equals(token)) //System.out.println("WIN");
      // else //System.out.println("FAIL");
    } catch (NotInTreeException e1) {
      // System.out.println("FAIL");
    }

    System.out.print("Testing get methods 1: ");
    try {
      pullNode = tree1.getNode(32, 33);
      // System.out.println("FAIL");
    } catch (NotInTreeException e) {
      // System.out.println("WIN");
    }

    System.out.print("Testing get methods 2: ");
    tree1.updateLeafData(33, new BFTFile(new String("IGGY IGGY")));
    try {
      pullNode = tree1.getNode(32, 33);
      // System.out.println("WIN");
    } catch (NotInTreeException e) {
      // System.out.println("FAIL");
    }

    System.out.print("Testing get methods 3: ");
    if (pullNode.min == 32 && pullNode.max == 33) {
      // System.out.println("WIN");
    } else {
      // System.out.println("FAIL");
    }
  }