diff -r 000000000000 -r 40cabcdcd2be samples/cloneproblem/test/org/apidesign/cloneproblem/ExploitTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/cloneproblem/test/org/apidesign/cloneproblem/ExploitTest.java Thu Oct 30 21:30:10 2014 +0100 @@ -0,0 +1,44 @@ +package org.apidesign.cloneproblem; + +import java.util.Date; +import junit.framework.TestCase; + +/** Test that would be written by the API hacker. + * + * @author Jaroslav Tulach + */ +public class ExploitTest extends TestCase { + public ExploitTest(String name) { + super(name); + } + + public void testExploitOverridableClone() { + try { + HackedDate now = new HackedDate(); + HackedDate later = new HackedDate(now.getTime() + 1000); + + Interval interval = new Interval(now, later); + assertEquals("1s", 1000, interval.getLength()); + + fail("And I hoped NullPointerException will be thrown!"); + } catch (NullPointerException ex) { + // success, the quest is to generate NullPointerException! + } + } + + // BEGIN: interval.exploit + private static class HackedDate extends Date { + public HackedDate() { + } + + public HackedDate(long date) { + super(date); + } + + @Override + public Object clone() { + return null; + } + } + // END: interval.exploit +}