Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaceccanti committed May 15, 2014
1 parent 828d02a commit 8a7393f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/debbuild
/RPMS
/tgz
/target
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public synchronized LSCFile parse(String vo, String hostname, InputStream is) {

lscReader.close();

if (certificateChainDescription.size() % 2 != 0){
throw new VOMSError("LSC file parsing error: "
+ "Malformed LSC file. It should contain an even number of "
+ "distinguished name entries expressed in OpenSSL slash-separated"
+ "format.");
}

if (certificateChainDescription.size() == 0){
throw new VOMSError("LSC file parsing error: "
+ "Malformed LSC file. No distinguished name entries found.");
}

lsc.setCertificateChainDescription(certificateChainDescription);

} catch (IOException e) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/italiangrid/voms/store/impl/LSCFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public boolean matches(X509Certificate[] certChain) {
return false;

}
} else {
// Cert chain description does not match certificate chain length
return false;
}

return true;
Expand Down
178 changes: 116 additions & 62 deletions src/test/java/org/italiangrid/voms/test/TestLSCParser.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Copyright (c) Istituto Nazionale di Fisica Nucleare, 2006-2012.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.italiangrid.voms.test;

Expand All @@ -29,55 +29,109 @@

public class TestLSCParser {

@Test
public void testParse() {
DefaultLSCFileParser parser = new DefaultLSCFileParser();

String lscContent = "# First line is a comment \n"+
"--- second line should skipped \n" +
"/C=it/O=org/CN=commonName\n"+
" \t\n"+
"/C=it/O=org/CN=CA\n";


LSCFile f = parser.parse("vo", "host", new ByteArrayInputStream(lscContent.getBytes()));

assertNull(f.getFilename());

assertEquals("vo",f.getVo());

assertEquals("host", f.getHostname());

assertNotNull(f.getCertificateChainDescription());

assertEquals(2, f.getCertificateChainDescription().size());

assertEquals("/C=it/O=org/CN=commonName", f.getCertificateChainDescription().get(0));
assertEquals("/C=it/O=org/CN=CA", f.getCertificateChainDescription().get(1));

}

@Test
public void testNonExistingFileParse(){

DefaultLSCFileParser parser = new DefaultLSCFileParser();

String nonExistentFile = "/this/file/doesnt/exist";

try{

@SuppressWarnings("unused")
LSCFile f = parser.parse("vo", "host", nonExistentFile);

}catch(VOMSError e){

assertEquals("LSC file does not exist: "+ nonExistentFile, e.getMessage());

return;

}

fail("VOMS error not thrown for non existing LSC file parsing attempt.");

}
@Test
public void testParse() {

DefaultLSCFileParser parser = new DefaultLSCFileParser();

String lscContent = "# First line is a comment \n"
+ "--- second line should skipped \n" + "/C=it/O=org/CN=commonName\n"
+ " \t\n" + "/C=it/O=org/CN=CA\n";

LSCFile f = parser.parse("vo", "host",
new ByteArrayInputStream(lscContent.getBytes()));

assertNull(f.getFilename());

assertEquals("vo", f.getVo());

assertEquals("host", f.getHostname());

assertNotNull(f.getCertificateChainDescription());

assertEquals(2, f.getCertificateChainDescription().size());

assertEquals("/C=it/O=org/CN=commonName", f
.getCertificateChainDescription().get(0));
assertEquals("/C=it/O=org/CN=CA", f.getCertificateChainDescription().get(1));

}

@Test
public void testOddLSCFileParseError() {

String singleEntryLSCFile = "# This is a comment \n"
+ "/C=it/O=org/CN=commonName\n";

String errorMessage = "LSC file parsing error: "
+ "Malformed LSC file. It should contain an even number of "
+ "distinguished name entries expressed in OpenSSL slash-separated"
+ "format.";

DefaultLSCFileParser parser = new DefaultLSCFileParser();

try {

@SuppressWarnings("unused")
LSCFile f = parser.parse("vo", "host", new ByteArrayInputStream(
singleEntryLSCFile.getBytes()));

} catch (VOMSError e) {

assertEquals(errorMessage, e.getMessage());
return;

}

fail("No error caught for malformed, single line LSC file parsing.");

}

@Test
public void testEmptyLSCFileParseError() {

DefaultLSCFileParser parser = new DefaultLSCFileParser();

String emptyLSCContent = "# This is a comment";
String errorMessage = "LSC file parsing error: "
+ "Malformed LSC file. No distinguished name entries found.";

try {

@SuppressWarnings("unused")
LSCFile f = parser.parse("vo", "host", new ByteArrayInputStream(
emptyLSCContent.getBytes()));

} catch (VOMSError e) {
assertEquals(errorMessage, e.getMessage());
return;
}

fail("No error caught for malformed, empty LSC file parsing.");
}

@Test
public void testNonExistingFileParse() {

DefaultLSCFileParser parser = new DefaultLSCFileParser();

String nonExistentFile = "/this/file/doesnt/exist";

try {

@SuppressWarnings("unused")
LSCFile f = parser.parse("vo", "host", nonExistentFile);

} catch (VOMSError e) {

assertEquals("LSC file does not exist: " + nonExistentFile,
e.getMessage());

return;

}

fail("VOMS error not thrown for non existing LSC file parsing attempt.");

}
}

0 comments on commit 8a7393f

Please sign in to comment.