Clover coverage report - Maven Clover report
Coverage timestamp: Thu Jan 25 2007 11:17:27 IST
file stats: LOC: 288   Methods: 8
NCLOC: 253   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Main.java 0% 0% 0% 0%
coverage
 1    package org.wiztools.wizcrypt;
 2   
 3    import java.io.Console;
 4    import java.io.InputStream;
 5    import java.io.UnsupportedEncodingException;
 6    import java.util.Arrays;
 7    import org.apache.commons.cli.CommandLine;
 8    import org.apache.commons.cli.CommandLineParser;
 9    import org.apache.commons.cli.GnuParser;
 10    import org.apache.commons.cli.HelpFormatter;
 11    import org.apache.commons.cli.Option;
 12    import org.apache.commons.cli.OptionBuilder;
 13    import org.apache.commons.cli.Options;
 14    import org.apache.commons.cli.ParseException;
 15   
 16    import java.security.InvalidKeyException;
 17    import java.security.GeneralSecurityException;
 18   
 19    import java.io.IOException;
 20    import java.io.File;
 21   
 22    import java.util.ResourceBundle;
 23   
 24    /**
 25    * The class which does the commandline parsing the calls the public APIs to encrypt/decrypt.
 26    */
 27    public class Main{
 28   
 29    private static final ResourceBundle rb = ResourceBundle.getBundle("org.wiztools.wizcrypt.wizcryptmsg");
 30   
 31    // Error booleans
 32    // Note: Even when just one of the input file
 33    // triggers any of these Exceptions, the flag
 34    // is set.
 35    private static boolean IO_EXCEPTION = false;
 36    private static boolean INVALID_PWD = false;
 37    private static boolean PWD_MISMATCH = false;
 38    private static boolean SECURITY_EXCEPTION = false;
 39    private static boolean PARSE_EXCEPTION = false;
 40    private static boolean CONSOLE_NOT_AVBL_EXCEPTION = false;
 41    private static boolean DEST_FILE_EXISTS = false;
 42    private static boolean PWD_ENCODING_EXCEPTION = false;
 43   
 44    // Error codes
 45    private static final int C_IO_EXCEPTION = 1;
 46    private static final int C_INVALID_PWD = 2;
 47    private static final int C_PWD_MISMATCH = 3;
 48    private static final int C_CONSOLE_NOT_AVBL_EXCEPTION = 7;
 49    private static final int C_DEST_FILE_EXISTS = 8;
 50    // Both of the above
 51    private static final int C_MULTIPLE_EXCEPTION = 4;
 52    private static final int C_SECURITY_EXCEPTION = 5;
 53    private static final int C_PARSE_EXCEPTION = 6;
 54   
 55  0 private Options generateOptions(){
 56  0 Options options = new Options();
 57  0 Option option = OptionBuilder.withLongOpt("password")
 58    .hasOptionalArg()
 59    .isRequired(false)
 60    .withDescription(rb.getString("msg.password"))
 61    .create('p');
 62  0 options.addOption(option);
 63  0 option = OptionBuilder.withLongOpt("encrypt")
 64    .isRequired(false)
 65    .withDescription(rb.getString("msg.encrypt"))
 66    .create('e');
 67  0 options.addOption(option);
 68  0 option = OptionBuilder.withLongOpt("decrypt")
 69    .isRequired(false)
 70    .withDescription(rb.getString("msg.decrypt"))
 71    .create('d');
 72  0 options.addOption(option);
 73  0 option = OptionBuilder.withLongOpt("help")
 74    .isRequired(false)
 75    .withDescription(rb.getString("msg.help"))
 76    .create('h');
 77  0 options.addOption(option);
 78  0 option = OptionBuilder.withLongOpt("version")
 79    .isRequired(false)
 80    .withDescription(rb.getString("msg.version"))
 81    .create();
 82  0 options.addOption(option);
 83  0 option = OptionBuilder.withLongOpt("verbose")
 84    .isRequired(false)
 85    .withDescription(rb.getString("msg.verbose"))
 86    .create('v');
 87  0 options.addOption(option);
 88  0 option = OptionBuilder.withLongOpt("force-overwrite")
 89    .isRequired(false)
 90    .withDescription(rb.getString("msg.force.overwrite"))
 91    .create('f');
 92  0 options.addOption(option);
 93  0 return options;
 94    }
 95   
 96  0 private void printCommandLineHelp(Options options){
 97  0 HelpFormatter hf = new HelpFormatter();
 98  0 String cmdLine = rb.getString("display.cmdline");
 99  0 String descriptor = rb.getString("display.detail");
 100  0 String moreHelp = rb.getString("display.footer");
 101  0 hf.printHelp(cmdLine, descriptor, options, moreHelp);
 102    }
 103   
 104  0 private void printVersionInfo(){
 105  0 InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/wiztools/wizcrypt/VERSION");
 106  0 try{
 107  0 int i = -1;
 108  0 byte[] buffer = new byte[0xFFFF];
 109  0 while((i=is.read(buffer))!=-1){
 110  0 System.out.print(new String(buffer, 0, i));
 111    }
 112  0 is.close();
 113  0 System.out.println();
 114    } catch(IOException ioe) {
 115  0 assert true: "VERSION file not found in Jar!";
 116    }
 117    }
 118   
 119  0 private char[] getConsolePassword(String msg) throws PasswordMismatchException,
 120    ConsoleNotAvailableException{
 121  0 Console cons = System.console();
 122  0 if(cons == null){
 123  0 throw new ConsoleNotAvailableException();
 124    }
 125  0 char[] passwd;
 126  0 passwd = cons.readPassword("%s ", msg);
 127  0 if(passwd == null){
 128  0 throw new PasswordMismatchException(rb.getString("err.no.pwd"));
 129    }
 130   
 131  0 return passwd;
 132    }
 133   
 134  0 private char[] getConsolePassword() throws PasswordMismatchException,
 135    ConsoleNotAvailableException{
 136  0 return getConsolePassword(rb.getString("msg.interactive.password"));
 137    }
 138   
 139  0 private char[] getConsolePasswordVerify() throws PasswordMismatchException,
 140    ConsoleNotAvailableException{
 141  0 char[] passwd = getConsolePassword(rb.getString("msg.interactive.password"));
 142  0 char[] passwd_retype = getConsolePassword(rb.getString("msg.interactive.password.again"));
 143  0 if(!Arrays.equals(passwd, passwd_retype)){
 144  0 throw new PasswordMismatchException(rb.getString("err.interactive.pwd.not.match"));
 145    }
 146  0 return passwd;
 147    }
 148   
 149  0 public Main(String[] arg){
 150  0 Options options = generateOptions();
 151  0 try{
 152  0 boolean encrypt = false;
 153  0 boolean decrypt = false;
 154  0 boolean forceOverwrite = false;
 155  0 boolean verbose = false;
 156  0 CommandLineParser parser = new GnuParser();
 157  0 CommandLine cmd = parser.parse(options, arg);
 158  0 if(cmd.hasOption('h')){
 159  0 printCommandLineHelp(options);
 160  0 return;
 161    }
 162  0 if(cmd.hasOption("version")){
 163  0 printVersionInfo();
 164  0 return;
 165    }
 166   
 167    // All other options require files as argument
 168  0 String[] args = cmd.getArgs();
 169   
 170  0 if(args == null || args.length == 0){
 171  0 throw new ParseException(rb.getString("err.no.file"));
 172    }
 173   
 174  0 if(cmd.hasOption('v')){
 175  0 verbose = true;
 176    }
 177  0 if(cmd.hasOption('f')){
 178  0 forceOverwrite = true;
 179    }
 180  0 if(cmd.hasOption('e')){
 181  0 encrypt = true;
 182    }
 183  0 if(cmd.hasOption('d')){
 184  0 decrypt = true;
 185    }
 186  0 if(encrypt && decrypt){
 187  0 throw new ParseException(rb.getString("err.both.selected"));
 188    }
 189  0 if(!encrypt && !decrypt){
 190  0 throw new ParseException(rb.getString("err.none.selected"));
 191    }
 192  0 char[] pwd = null;
 193  0 if(cmd.hasOption('p')){
 194  0 String pwdStr = cmd.getOptionValue('p');
 195  0 if(pwdStr == null){
 196  0 pwd = encrypt ? getConsolePasswordVerify() : getConsolePassword();
 197    }
 198    else{
 199  0 pwd = pwdStr.toCharArray();
 200    }
 201   
 202    } else{
 203  0 pwd = encrypt ? getConsolePasswordVerify() : getConsolePassword();
 204    }
 205  0 IProcess iprocess = null;
 206  0 if(encrypt){
 207  0 iprocess = new Encrypt();
 208  0 } else if(decrypt){
 209  0 iprocess = new Decrypt();
 210    }
 211  0 iprocess.init(new String(pwd));
 212   
 213  0 for(int i=0;i<args.length;i++){
 214  0 File f = new File(args[i]);
 215  0 try{
 216  0 iprocess.process(f, forceOverwrite);
 217  0 if(verbose){
 218  0 System.out.println(rb.getString("msg.verbose.success")+
 219    f.getCanonicalPath() + ".wiz");
 220    }
 221    } catch(DestinationFileExistsException dfe){
 222  0 DEST_FILE_EXISTS = true;
 223  0 System.err.println(dfe.getMessage());
 224    } catch(PasswordMismatchException pme){
 225  0 PWD_MISMATCH = true;
 226  0 System.err.println(pme.getMessage());
 227    } catch(IOException ioe){
 228  0 IO_EXCEPTION = true;
 229  0 System.err.println(ioe.getMessage());
 230    }
 231    }
 232    } catch(ParseException pe){
 233  0 PARSE_EXCEPTION = true;
 234  0 System.err.println(pe.getMessage());
 235  0 printCommandLineHelp(options);
 236    } catch(ConsoleNotAvailableException cna){
 237  0 CONSOLE_NOT_AVBL_EXCEPTION = true;
 238  0 System.err.println(rb.getString("err.console.not.avbl"));
 239    } catch(PasswordMismatchException pme){
 240  0 INVALID_PWD = true;
 241  0 System.err.println(pme.getMessage());
 242    } catch(InvalidKeyException ike){
 243  0 INVALID_PWD = true;
 244  0 System.err.println(rb.getString("err.invalid.pwd"));
 245    } catch(UnsupportedEncodingException uee){
 246  0 PWD_ENCODING_EXCEPTION = true;
 247  0 System.err.println(uee.getMessage());
 248    } catch(GeneralSecurityException gse){
 249  0 SECURITY_EXCEPTION = true;
 250  0 System.err.println(gse.getMessage());
 251    }
 252    }
 253   
 254  0 public static void main(String[] arg){
 255  0 new Main(arg);
 256   
 257    // Set program exit value
 258  0 int exitVal = 0;
 259  0 if(PARSE_EXCEPTION){
 260  0 exitVal = C_PARSE_EXCEPTION;
 261  0 } else if(CONSOLE_NOT_AVBL_EXCEPTION){
 262  0 exitVal = C_CONSOLE_NOT_AVBL_EXCEPTION;
 263  0 } else if(INVALID_PWD){
 264  0 exitVal = C_INVALID_PWD;
 265  0 } else if(SECURITY_EXCEPTION || PWD_ENCODING_EXCEPTION){
 266  0 exitVal = C_SECURITY_EXCEPTION;
 267    } else{
 268  0 int count = 0; // count the number of exceptions
 269  0 if(DEST_FILE_EXISTS){
 270  0 exitVal = C_DEST_FILE_EXISTS;
 271  0 count++;
 272    }
 273  0 if(IO_EXCEPTION){
 274  0 exitVal = C_IO_EXCEPTION;
 275  0 count++;
 276    }
 277  0 if(PWD_MISMATCH){
 278  0 exitVal = C_PWD_MISMATCH;
 279  0 count++;
 280    }
 281  0 if(count > 1){
 282  0 exitVal = C_MULTIPLE_EXCEPTION;
 283    }
 284    }
 285  0 System.exit(exitVal);
 286    }
 287   
 288    }