Skip to content

Commit

Permalink
Fix some failing XML tests.
Browse files Browse the repository at this point in the history
Change-Id: I7504e849b9fe67900248a2e223d189c8fcef224d
  • Loading branch information
Jesse Wilson committed May 17, 2011
1 parent 159b073 commit 28b429a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 82 deletions.
4 changes: 2 additions & 2 deletions expectations/icebox.txt
Expand Up @@ -42,13 +42,13 @@
description: "Dalvik doesn't support XML Schemas, DTDs or validation",
bug: 3268630,
name: "libcore.xml.DeclarationTest#testGetXmlEncoding",
substring: "This implementation doesn't parse the encoding from the XML declaration expected:<ISO-8859-1> but was:<null>"
substring: "This implementation doesn't parse the encoding from the XML declaration"
},
{
description: "Dalvik doesn't support XML Schemas, DTDs or validation",
bug: 3268630,
name: "libcore.xml.DeclarationTest#testGetXmlStandalone",
substring: "This implementation doesn't parse standalone from the XML declaration expected:<true> but was:<false>"
substring: "This implementation doesn't parse standalone from the XML declaration"
},
{
description: "Dalvik doesn't support XML Schemas, DTDs or validation",
Expand Down
5 changes: 5 additions & 0 deletions expectations/knownfailures.txt
Expand Up @@ -10,6 +10,11 @@
],
bug: 3483365
},
{
description: "Expat uses an unbounded number of global references",
name: "libcore.xml.ExpatSaxParserTest#testGlobalReferenceTableOverflow",
bug: 2772628
},
{
description: "Test fails, Intermediate certificate lacks BasicConstraints",
name: "com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpiTest#testTrustAndRemoteCertificatesWithDifferentEncodings",
Expand Down
104 changes: 24 additions & 80 deletions luni/src/test/java/libcore/xml/ExpatSaxParserTest.java
Expand Up @@ -16,6 +16,16 @@

package libcore.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.harmony.xml.ExpatReader;
Expand All @@ -27,20 +37,8 @@
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;
import org.xml.sax.helpers.DefaultHandler;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.ServerSocket;
import java.net.Socket;
import tests.http.MockResponse;
import tests.http.MockWebServer;

public class ExpatSaxParserTest extends TestCase {

Expand Down Expand Up @@ -586,98 +584,44 @@ public void characters(char ch[], int start, int length)
}

public void testExternalEntityDownload() throws IOException, SAXException {
class Server implements Runnable {

private final ServerSocket serverSocket;

Server() throws IOException {
serverSocket = new ServerSocket(8080);
}

public void run() {
try {
Socket socket = serverSocket.accept();

final InputStream in = socket.getInputStream();
Thread inputThread = new Thread() {
public void run() {
try {
byte[] buffer = new byte[1024];
while (in.read(buffer) > -1) { /* ignore */ }
} catch (IOException e) {
e.printStackTrace();
}
}
};
inputThread.setDaemon(true);
inputThread.start();

OutputStream out = socket.getOutputStream();

String body = "<bar></bar>";
String response = "HTTP/1.0 200 OK\n"
+ "Content-Length: " + body.length() + "\n"
+ "\n"
+ body;

out.write(response.getBytes("UTF-8"));
out.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
final MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("<bar></bar>"));
server.play();

class Handler extends DefaultHandler {
final List<String> elementNames = new ArrayList<String>();

List<String> elementNames = new ArrayList<String>();

public InputSource resolveEntity(String publicId, String systemId)
throws IOException, SAXException {
@Override public InputSource resolveEntity(String publicId, String systemId)
throws IOException {
// The parser should have resolved the systemId.
assertEquals("http://localhost:8080/systemBar", systemId);
assertEquals(server.getUrl("/systemBar").toString(), systemId);
return new InputSource(systemId);
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
@Override public void startElement(String uri, String localName, String qName,
Attributes attributes) {
elementNames.add(localName);
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
@Override public void endElement(String uri, String localName, String qName) {
elementNames.add("/" + localName);
}
}

// Start server to serve up the XML for 'systemBar'.
Thread serverThread = new Thread(new Server());
serverThread.setDaemon(true);
serverThread.start();

// 'systemBar', the external entity, is relative to 'systemFoo':
Reader in = new StringReader("<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE foo [\n"
+ " <!ENTITY bar SYSTEM 'systemBar'>\n"
+ "]>\n"
+ "<foo>&bar;</foo>");

ExpatReader reader = new ExpatReader();

Handler handler = new Handler();

reader.setContentHandler(handler);
reader.setEntityResolver(handler);

InputSource source = new InputSource(in);
source.setSystemId("http://localhost:8080/systemFoo");
source.setSystemId(server.getUrl("/systemFoo").toString());
reader.parse(source);

assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"),
handler.elementNames);
assertEquals(Arrays.asList("foo", "bar", "/bar", "/foo"), handler.elementNames);
}

/**
Expand Down

0 comments on commit 28b429a

Please sign in to comment.