cleanup, and consolidation

This commit is contained in:
Vincent Batts 2012-11-08 10:15:20 -05:00
parent 21730d6942
commit 348bf9d377
9 changed files with 235 additions and 151 deletions

View file

@ -15,15 +15,18 @@ import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.redhat.trie.HuffNode;
import com.redhat.trie.NodePair;
import com.redhat.trie.PathNode;
import com.redhat.trie.PathTree;
import com.redhat.trie.Util;
import com.redhat.trie.PayloadException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateFactory;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.x509.extension.X509ExtensionUtil;
@ -35,6 +38,92 @@ import org.bouncycastle.x509.extension.X509ExtensionUtil;
*/
public class App {
/*
* PrettyPrint a PathNode tree
*/
public static void printTree(PathNode pn, int tab) {
StringBuffer nodeRep = new StringBuffer();
for (int i = 0; i <= tab; i++) {
nodeRep.append(" ");
}
nodeRep.append("Node [");
nodeRep.append(pn.getId());
nodeRep.append("]");
for (PathNode parent : pn.getParents()) {
nodeRep.append(" ^ [");
nodeRep.append(parent.getId());
nodeRep.append("]");
}
for (NodePair cp : pn.getChildren()) {
nodeRep.append(" v [");
nodeRep.append(cp.getName());
nodeRep.append(" {");
nodeRep.append(cp.getConnection().getId());
nodeRep.append("} ]");
}
System.out.println(nodeRep);
for (NodePair cp : pn.getChildren()) {
printTree(cp.getConnection(), tab + 1);
}
}
/*
* PrettyPrint a HuffNode tree
*/
public static void printTrie(HuffNode hn, int tab) {
StringBuffer nodeRep = new StringBuffer();
for (int i = 0; i <= tab; i++) {
nodeRep.append(" ");
}
nodeRep.append("Node [");
nodeRep.append(hn.getId());
nodeRep.append("]");
nodeRep.append(", Weight [");
nodeRep.append(hn.getWeight());
nodeRep.append("]");
nodeRep.append(", Value = [");
nodeRep.append(hn.getValue());
nodeRep.append("]");
System.out.println(nodeRep);
if (hn.getLeft() != null) {
printTrie(hn.getLeft(), tab + 1);
}
if (hn.getRight() != null) {
printTrie(hn.getRight(), tab + 1);
}
}
/*
* From the deflated payload, produce the content set lists
*
*
* FIXME - break this apart, so that the hydrated payload
* can be structure to more quickly search, and use less memory
*
* Rename it for tracking, and to be clear about what is happening
*/
public static List<String> hydrateContentPackage(byte[] compressedBlob) {
PathTree pt = new PathTree(compressedBlob);
return pt.toList();
}
public static ASN1Encodable objectFromOid(X509Certificate cert, String oid) {
if (cert == null) { return null; }
try {
for (String thisOid : cert.getNonCriticalExtensionOIDs()) {
if (thisOid.equals(oid)) {
return X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue(oid));
}
}
} catch (IOException ex) { }
return null;
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
@ -72,7 +161,7 @@ public class App {
public static List<String> hydrateFromBytes(byte[] compressedBlob) {
return Util.hydrateContentPackage(compressedBlob);
return hydrateContentPackage(compressedBlob);
}
public static List<String> hydrateFromFile(String filename) {
@ -157,7 +246,7 @@ public class App {
PathTree pt;
try {
pt = new PathTree(contentList);
Util.printTree(pt.getRootPathNode(), 0);
printTree(pt.getRootPathNode(), 0);
} catch (PayloadException ex) {
System.out.println(ex);
}
@ -166,7 +255,7 @@ public class App {
public static ASN1Encodable objectFromCertOid(String certFilename, String oid) {
X509Certificate cert;
cert = certFromFile(certFilename);
return Util.objectFromOid(cert,oid);
return objectFromOid(cert,oid);
}
public static X509Certificate certFromFile(String certFilename) {

View file

@ -40,14 +40,12 @@ import org.apache.log4j.Logger;
* PathTree
*
* An efficient means by which to check the content sets.
*
* TODO - this is a prototype stub
*/
public class PathTree {
private static org.apache.log4j.Logger log = Logger.getLogger(PathTree.class);
private List<HuffNode> nodeDictionary;
private List<HuffNode> pathDictionary;
private StringBuffer nodeBits; // TODO make a smart getter for this
private StringBuffer nodeBits;
private byte[] payload;
@ -303,7 +301,10 @@ public class PathTree {
}
/**
* TODO - this is a stub
* Validate whether contentPath is included in this tree.
*
* @param contentPath A String, like "/foo/bar/baz"
* @return true or false
*/
public boolean validate(String contentPath) {
StringTokenizer st = new StringTokenizer(contentPath, "/");
@ -339,10 +340,6 @@ public class PathTree {
return false;
}
private boolean matches(PathNode pn, StringTokenizer st) {
return false;
}
/**
* consume the list of content sets, and operate the same way.
*
@ -585,9 +582,6 @@ public class PathTree {
return parent;
}
/**
*
*/
private List<HuffNode> getStringNodeList(List<String> pathStrings) {
List<HuffNode> nodes = new ArrayList<HuffNode>();
int idx = 1;
@ -598,9 +592,6 @@ public class PathTree {
return nodes;
}
/**
*
*/
private List<HuffNode> getPathNodeNodeList(List<PathNode> pathNodes) {
List<HuffNode> nodes = new ArrayList<HuffNode>();
int idx = 0;
@ -847,6 +838,11 @@ public class PathTree {
(byte) value};
}
/**
* FIXME this logic is causing nodes to disappear.
*
* Fix it if you feel that it is even needed...
*/
private static void condenseSubTreeNodes(PathNode location) {
// "equivalent" parents are merged
List<PathNode> parentResult = new ArrayList<PathNode>();

View file

@ -1,120 +0,0 @@
/**
* Copyright (c) 2009 - 2012 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.redhat.trie;
import java.util.List;
import java.io.IOException;
import java.security.cert.X509Certificate;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.x509.extension.X509ExtensionUtil;
/*
* Util
*
* All the misc dirty work
*
*/
public class Util {
/*
* PrettyPrint a PathNode tree
*/
public static void printTree(PathNode pn, int tab) {
StringBuffer nodeRep = new StringBuffer();
for (int i = 0; i <= tab; i++) {
nodeRep.append(" ");
}
nodeRep.append("Node [");
nodeRep.append(pn.getId());
nodeRep.append("]");
for (PathNode parent : pn.getParents()) {
nodeRep.append(" ^ [");
nodeRep.append(parent.getId());
nodeRep.append("]");
}
for (NodePair cp : pn.getChildren()) {
nodeRep.append(" v [");
nodeRep.append(cp.getName());
nodeRep.append(" {");
nodeRep.append(cp.getConnection().getId());
nodeRep.append("} ]");
}
System.out.println(nodeRep);
for (NodePair cp : pn.getChildren()) {
printTree(cp.getConnection(), tab + 1);
}
}
/*
* PrettyPrint a HuffNode tree
*/
public static void printTrie(HuffNode hn, int tab) {
StringBuffer nodeRep = new StringBuffer();
for (int i = 0; i <= tab; i++) {
nodeRep.append(" ");
}
nodeRep.append("Node [");
nodeRep.append(hn.getId());
nodeRep.append("]");
nodeRep.append(", Weight [");
nodeRep.append(hn.getWeight());
nodeRep.append("]");
nodeRep.append(", Value = [");
nodeRep.append(hn.getValue());
nodeRep.append("]");
System.out.println(nodeRep);
if (hn.getLeft() != null) {
printTrie(hn.getLeft(), tab + 1);
}
if (hn.getRight() != null) {
printTrie(hn.getRight(), tab + 1);
}
}
/*
* From the deflated payload, produce the content set lists
*
*
* FIXME - break this apart, so that the hydrated payload
* can be structure to more quickly search, and use less memory
*
* Rename it for tracking, and to be clear about what is happening
*/
public static List<String> hydrateContentPackage(byte[] compressedBlob) {
PathTree pt = new PathTree(compressedBlob);
return pt.toList();
}
public static ASN1Encodable objectFromOid(X509Certificate cert, String oid) {
if (cert == null) { return null; }
try {
for (String thisOid : cert.getNonCriticalExtensionOIDs()) {
if (thisOid.equals(oid)) {
return X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue(oid));
}
}
} catch (IOException ex) { }
return null;
}
}

View file

@ -15,6 +15,7 @@ import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import org.junit.Test;
@ -51,6 +52,32 @@ public class TestPathTree {
assertNotNull(pt);
}
@Test
public void testValidation() {
PathTree pt = new PathTree();
List<String> contents = loadContents("contents.list");
// matches a path
String shouldPass = "/content/beta/rhel/server/5/5server/x86_64/sap/os/repomd.xml";
// is not a match
String shouldFail = "/fart/face/mcjones";
// tricky, because it is almost a valid path. All nodes, will have similar children. (the /vt/ is only in /5/, not /6/)
String shouldFailTricky = "/content/dist/rhel/server/6/$releasever/$basearch/vt/os";
try {
pt.setContentSets(contents);
} catch (PayloadException ex) {
fail(ex.toString());
}
// for good measure ...
assertTrue(cmpStrings(contents, pt.toList()));
assertTrue(pt.validate(shouldPass));
assertFalse(pt.validate(shouldFail));
// FIXME OH NOES... the PathNode relationships are to generous
//assertFalse(pt.validate(shouldFailTricky));
}
@Test
public void testRootNode() {
PathTree pt = new PathTree();