LDAP-233: LdifParser goes into infinite loop when passed a non-LDIF file

Fixed according to patch suggestion.
This commit is contained in:
Mattias Hellborg Arthursson
2013-08-12 11:47:28 +02:00
parent f8b979d5e6
commit f15ea1cd00
5 changed files with 114 additions and 26 deletions

View File

@@ -6,5 +6,6 @@ dependencies {
"org.springframework.batch:spring-batch-infrastructure:$springBatchVersion"
testCompile "junit:junit:$junitVersion",
"log4j:log4j:$log4jVersion"
"log4j:log4j:$log4jVersion",
"commons-io:commons-io:2.4"
}

View File

@@ -15,15 +15,6 @@
*/
package org.springframework.ldap.ldif.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -41,6 +32,14 @@ import org.springframework.ldap.schema.DefaultSchemaSpecification;
import org.springframework.ldap.schema.Specification;
import org.springframework.util.Assert;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
/**
* The {@link LdifParser LdifParser} is the main class of the {@link org.springframework.ldap.ldif} package.
* This class reads lines from a resource and assembles them into an {@link LdapAttributes LdapAttributes} object.
@@ -231,7 +230,7 @@ public class LdifParser implements Parser, InitializingBean {
return null;
}
LdapAttributes record = new LdapAttributes(caseInsensitive);
LdapAttributes record = null;
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
@@ -244,8 +243,9 @@ public class LdifParser implements Parser, InitializingBean {
case NewRecord:
log.trace("Starting new record.");
//Start new record.
record = new LdapAttributes(caseInsensitive);
builder = new StringBuilder(line);
break;
case Control:
@@ -313,7 +313,10 @@ public class LdifParser implements Parser, InitializingBean {
}
line = reader.readLine();
if(line == null && record == null) {
//Never encountered a valid record.
return null;
}
}
}

View File

@@ -1,24 +1,41 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* 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.
*/
package org.springframework.ldap.ldif;
import static org.junit.Assert.*;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.ldap.core.LdapAttribute;
import org.springframework.ldap.ldif.support.DefaultAttributeValidationPolicy;
import sun.misc.BASE64Decoder;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Parses a preselected set of attributes to test the full spectrum of functionality
* expected of an attribute parser. Attributes are validated to ensure they conform to

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* 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.
*/
package org.springframework.ldap.ldif;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.ldap.ldif.parser.LdifParser;
import org.springframework.ldap.schema.BasicSchemaSpecification;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertNull;
/**
* @author Mattias Hellborg Arthursson
*/
public class Ldap233LdifParserTest {
/**
* This previously went into endless loop.
*
* @throws IOException
*/
@Test
public void ldap233Test() throws IOException {
File testFile = File.createTempFile("ldapTest", ".ldif");
FileUtils.write(testFile, "This is just some random text");
LdifParser parser = new LdifParser(testFile);
parser.setRecordSpecification(new BasicSchemaSpecification());
parser.open();
assertNull(parser.getRecord());
testFile.delete();
}
}

View File

@@ -1,9 +1,20 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* 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.
*/
package org.springframework.ldap.ldif;
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
@@ -14,6 +25,11 @@ import org.springframework.ldap.core.LdapAttributes;
import org.springframework.ldap.ldif.parser.LdifParser;
import org.springframework.ldap.schema.BasicSchemaSpecification;
import java.io.IOException;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit test for LdifParser.
*