// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/auth/AuthorizedKeysDecoder.java
// public class AuthorizedKeysDecoder {
// 	
//     private byte[] bytes;
//     private int pos;
// 
//     public PublicKey decodePublicKey(String keyLine) throws Exception {
//         bytes = null;
//         pos = 0;
// 
//         // look for the Base64 encoded part of the line to decode
//         // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes
//         for (String part : keyLine.split(" ")) {
//             if (part.startsWith("AAAA")) {
//                 bytes = Base64.decodeBase64(part);
//                 break;
//             }
//         }
//         if (bytes == null) {
//             throw new IllegalArgumentException("no Base64 part to decode");
//         }
// 
//         String type = decodeType();
//         if (type.equals("ssh-rsa")) {
//             BigInteger e = decodeBigInt();
//             BigInteger m = decodeBigInt();
//             RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
//             return KeyFactory.getInstance("RSA").generatePublic(spec);
//         } else if (type.equals("ssh-dss")) {
//             BigInteger p = decodeBigInt();
//             BigInteger q = decodeBigInt();
//             BigInteger g = decodeBigInt();
//             BigInteger y = decodeBigInt();
//             DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g);
//             return KeyFactory.getInstance("DSA").generatePublic(spec);
//         } else {
//             throw new IllegalArgumentException("unknown type " + type);
//         }
//     }
// 
//     private String decodeType() {
//         int len = decodeInt();
//         String type = new String(bytes, pos, len);
//         pos += len;
//         return type;
//     }
// 
//     private int decodeInt() {
//         return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)
//                 | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF);
//     }
// 
//     private BigInteger decodeBigInt() {
//         int len = decodeInt();
//         byte[] bigIntBytes = new byte[len];
//         System.arraycopy(bytes, pos, bigIntBytes, 0, len);
//         pos += len;
//         return new BigInteger(bigIntBytes);
//     }
// 
// }
// 
// Path: server/core/src/test/java/tests/FilesUtils.java
// public class FilesUtils {
// 
// 	public static String tail(File file, int lines) {
// 		String[] linesBuffer = new String[lines];
// 		int pos = 0;
// 		BufferedReader br = null;
// 		try {
// 			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// 			String buffer;
// 			while ((buffer = br.readLine())!=null) {
// 				linesBuffer[pos++] = buffer;
// 				if (pos == lines) {
// 					pos=0;
// 				}
// 			}
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			if (br!=null) {
// 				try {
// 					br.close();
// 				} catch (IOException e) {
// 					e.printStackTrace();
// 				}
// 			}
// 		}
// 		StringBuffer accu = new StringBuffer();
// 		for (int i=pos; i<pos+lines; i++) {
// 			accu.append(linesBuffer[i%lines]).append('\n');
// 		}
// 		return accu.toString();
// 	}
// 	
// 	public static String readFile(File file) throws FileNotFoundException {
// 		return readStream(new FileInputStream(file));
// 	}
// 	
// 	public static String readStream(InputStream in) {
// 		StringBuffer sb = new StringBuffer();
// 		String buffer = null;
// 		BufferedReader br = null;
// 		try {
// 			br = new BufferedReader(new InputStreamReader(in));
// 			while ((buffer = br.readLine())!=null) {
// 				sb.append(buffer).append('\n');
// 			}
// 		} catch (IOException e) {
// 			return null;
// 		} finally {
// 			if (br != null) {
// 				try {
// 					br.close();
// 				} catch (IOException e) {}
// 			}
// 		}
// 		return sb.toString();
// 	}
// 
// 	public static void recursiveDelete(File path) {
// 		try {
// 			FileUtils.delete(path, FileUtils.RECURSIVE);
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 	}
// }


import static org.fest.assertions.Assertions.assertThat;
import java.security.PublicKey;
import org.junit.Before;
import org.junit.Test;
import org.kercoin.magrit.sshd.auth.AuthorizedKeysDecoder;
import tests.FilesUtils;
/*
Copyright 2011-2012 Frederic Menou and others referred in AUTHORS file.

This file is part of Magrit.

Magrit is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

Magrit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with Magrit.
If not, see <http://www.gnu.org/licenses/>.
*/
package org.kercoin.magrit.sshd.auth;





public class AuthorizedKeysDecoderTest {

	AuthorizedKeysDecoder decoder;
	
	@Before
	public void setUp() throws Exception {
		decoder = new AuthorizedKeysDecoder();
	}

	@Test
	public void testDecodePublicKey_dsa() throws Exception {
		// given
// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/auth/AuthorizedKeysDecoder.java
// public class AuthorizedKeysDecoder {
// 	
//     private byte[] bytes;
//     private int pos;
// 
//     public PublicKey decodePublicKey(String keyLine) throws Exception {
//         bytes = null;
//         pos = 0;
// 
//         // look for the Base64 encoded part of the line to decode
//         // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes
//         for (String part : keyLine.split(" ")) {
//             if (part.startsWith("AAAA")) {
//                 bytes = Base64.decodeBase64(part);
//                 break;
//             }
//         }
//         if (bytes == null) {
//             throw new IllegalArgumentException("no Base64 part to decode");
//         }
// 
//         String type = decodeType();
//         if (type.equals("ssh-rsa")) {
//             BigInteger e = decodeBigInt();
//             BigInteger m = decodeBigInt();
//             RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
//             return KeyFactory.getInstance("RSA").generatePublic(spec);
//         } else if (type.equals("ssh-dss")) {
//             BigInteger p = decodeBigInt();
//             BigInteger q = decodeBigInt();
//             BigInteger g = decodeBigInt();
//             BigInteger y = decodeBigInt();
//             DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g);
//             return KeyFactory.getInstance("DSA").generatePublic(spec);
//         } else {
//             throw new IllegalArgumentException("unknown type " + type);
//         }
//     }
// 
//     private String decodeType() {
//         int len = decodeInt();
//         String type = new String(bytes, pos, len);
//         pos += len;
//         return type;
//     }
// 
//     private int decodeInt() {
//         return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)
//                 | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF);
//     }
// 
//     private BigInteger decodeBigInt() {
//         int len = decodeInt();
//         byte[] bigIntBytes = new byte[len];
//         System.arraycopy(bytes, pos, bigIntBytes, 0, len);
//         pos += len;
//         return new BigInteger(bigIntBytes);
//     }
// 
// }
// 
// Path: server/core/src/test/java/tests/FilesUtils.java
// public class FilesUtils {
// 
// 	public static String tail(File file, int lines) {
// 		String[] linesBuffer = new String[lines];
// 		int pos = 0;
// 		BufferedReader br = null;
// 		try {
// 			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// 			String buffer;
// 			while ((buffer = br.readLine())!=null) {
// 				linesBuffer[pos++] = buffer;
// 				if (pos == lines) {
// 					pos=0;
// 				}
// 			}
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		} finally {
// 			if (br!=null) {
// 				try {
// 					br.close();
// 				} catch (IOException e) {
// 					e.printStackTrace();
// 				}
// 			}
// 		}
// 		StringBuffer accu = new StringBuffer();
// 		for (int i=pos; i<pos+lines; i++) {
// 			accu.append(linesBuffer[i%lines]).append('\n');
// 		}
// 		return accu.toString();
// 	}
// 	
// 	public static String readFile(File file) throws FileNotFoundException {
// 		return readStream(new FileInputStream(file));
// 	}
// 	
// 	public static String readStream(InputStream in) {
// 		StringBuffer sb = new StringBuffer();
// 		String buffer = null;
// 		BufferedReader br = null;
// 		try {
// 			br = new BufferedReader(new InputStreamReader(in));
// 			while ((buffer = br.readLine())!=null) {
// 				sb.append(buffer).append('\n');
// 			}
// 		} catch (IOException e) {
// 			return null;
// 		} finally {
// 			if (br != null) {
// 				try {
// 					br.close();
// 				} catch (IOException e) {}
// 			}
// 		}
// 		return sb.toString();
// 	}
// 
// 	public static void recursiveDelete(File path) {
// 		try {
// 			FileUtils.delete(path, FileUtils.RECURSIVE);
// 		} catch (IOException e) {
// 			e.printStackTrace();
// 		}
// 	}
// }


// Path: server/sshd/src/test/java/org/kercoin/magrit/sshd/auth/AuthorizedKeysDecoderTest.java
import static org.fest.assertions.Assertions.assertThat;
import java.security.PublicKey;
import org.junit.Before;
import org.junit.Test;
import org.kercoin.magrit.sshd.auth.AuthorizedKeysDecoder;
import tests.FilesUtils;

/*
Copyright 2011-2012 Frederic Menou and others referred in AUTHORS file.

This file is part of Magrit.

Magrit is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

Magrit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with Magrit.
If not, see <http://www.gnu.org/licenses/>.
*/
package org.kercoin.magrit.sshd.auth;





public class AuthorizedKeysDecoderTest {

	AuthorizedKeysDecoder decoder;
	
	@Before
	public void setUp() throws Exception {
		decoder = new AuthorizedKeysDecoder();
	}

	@Test
	public void testDecodePublicKey_dsa() throws Exception {
		// given
		String encoded = FilesUtils.readStream(getClass().getClassLoader()