Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change CompareComand implementation #15

Merged
merged 1 commit into from
Mar 18, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 98 additions & 108 deletions src/main/java/com/redhat/victims/cli/commands/CompareCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,124 +21,114 @@
* #L%
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.redhat.victims.VictimsRecord;
import com.redhat.victims.VictimsScanner;
import com.redhat.victims.cli.results.CommandResult;
import com.redhat.victims.cli.results.ExitFailure;
import com.redhat.victims.cli.results.ExitInvalid;
import com.redhat.victims.cli.results.ExitSuccess;
import com.redhat.victims.fingerprint.Algorithms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
*
*
* @author dfj
*/
public class CompareCommand implements Command {

public static final String COMMAND_NAME = "compare";

private Usage help;
private List<String> arguments;

public CompareCommand(){
help = new Usage(getName(), "Compare the hashes of two jar files");
help.addExample("file1.jar file2.jar");
}

@Override
public final String getName() {
return COMMAND_NAME;
}

@Override
public void setArguments(List<String> args) {
this.arguments = args;
}

@Override
public CommandResult execute(List<String> args) {

CommandResult result = new ExitSuccess(null);
if (args.size() != 2){
return new ExitInvalid("Two jars are required to do comparisson");
}

String lhs = args.get(0);
String rhs = args.get(1);

ArrayList<VictimsRecord> lhsRecords = new ArrayList<VictimsRecord>();
ArrayList<VictimsRecord> rhsRecords = new ArrayList<VictimsRecord>();
try {

VictimsScanner.scan(lhs, lhsRecords);
VictimsScanner.scan(rhs, rhsRecords);

if (lhsRecords.containsAll(rhsRecords)){
result.addOutput(String.format("EQUAL: %s equals the hashed contents of %s", lhs, rhs));
return result;
}

HashSet<String> lhsHashes;
HashSet<String> rhsHashes;

lhsHashes = new HashSet<String>();
for (VictimsRecord rec : lhsRecords){
lhsHashes.addAll(rec.getHashes(Algorithms.SHA512).keySet());
}

rhsHashes = new HashSet<String>();
for (VictimsRecord rec : rhsRecords){
rhsHashes.addAll(rec.getHashes(Algorithms.SHA512).keySet());
}

int lhsSize = lhsHashes.size();
int rhsSize = rhsHashes.size();
if (rhsSize > lhsSize){
result.addOutput(String.format("%s has %d more entries than %s%n", rhs, rhsSize-lhsSize, lhs));
lhsHashes.removeAll(rhsHashes);
if (lhsHashes.isEmpty()){
result.addOutput(String.format("SUBSET: %s is a subset of the hashed contents of %s", lhs, rhs));
} else {
result.addOutput(String.format("FAILURE %s has %d classes in common with %s.", lhs, lhsSize - lhsHashes.size(), rhs));
}
// TODO Verbose output listing the differences
} else {
result.addOutput(String.format("%s has %d more entries than %s%n", lhs, lhsSize-rhsSize, rhs));
rhsHashes.removeAll(lhsHashes);
if (rhsHashes.isEmpty()){
result.addOutput(String.format("SUBSET: %s is a subset of the hashed contents of %s", rhs, lhs));
} else {
result.addOutput(String.format("FAILURE: %s has %d classes in common with %s.", rhs, rhsSize - rhsHashes.size(), lhs));
}
// TODO Verbose output listing the differences
}
return result;

} catch (IOException e){
return new ExitFailure(e.toString());
}
}

@Override
public String usage() {
return help.toString();
}

@Override
public Command newInstance() {
CompareCommand cmd = new CompareCommand();
cmd.setArguments(this.arguments);
return cmd;
}

@Override
public CommandResult call() throws Exception {
return execute(this.arguments);
}

public static final String COMMAND_NAME = "compare";

private final Usage help;
private List<String> arguments;

public CompareCommand() {
help = new Usage(getName(), "Compare the hashes of two jar files");
help.addExample("file1.jar file2.jar");
}

@Override
public final String getName() {
return COMMAND_NAME;
}

@Override
public void setArguments(List<String> args) {
this.arguments = args;
}

@Override
public CommandResult execute(List<String> args) {

CommandResult result = new ExitSuccess(null);
if (args.size() != 2) {
return new ExitInvalid("Two jars are required to do comparisson");
}

String lhs = args.get(0);
String rhs = args.get(1);

try {

ArrayList<VictimsRecord> lhsRecords = VictimsScanner
.getRecords(lhs);
ArrayList<VictimsRecord> rhsRecords = VictimsScanner
.getRecords(rhs);

Integer lcount = 0;
Integer rcount = 0;

for (VictimsRecord vrl : lhsRecords) {
for (VictimsRecord vrr : rhsRecords) {
if (vrl.containsAll(vrr)) {
lcount += 1;
}

if (vrr.containsAll(vrl)) {
rcount += 1;
}
}
}

boolean lr = lcount.equals(rhsRecords.size());
boolean rl = rcount.equals(lhsRecords.size());

if (lr && rl) {
result.addOutput("IDENT: Both files have identical content");
} else {
String format = "SUBSET: %s contains %s";
if (lr) {
result.addOutput(String.format(format, lhs, rhs));
} else if (rl) {
result.addOutput(String.format(format, rhs, lhs));
} else {
result.addOutput("NO MATCH: No content match");
}
}

return result;

} catch (IOException e) {
return new ExitFailure(e.toString());
}
}

@Override
public String usage() {
return help.toString();
}

@Override
public Command newInstance() {
CompareCommand cmd = new CompareCommand();
cmd.setArguments(this.arguments);
return cmd;
}

@Override
public CommandResult call() throws Exception {
return execute(this.arguments);
}

}