samples/cloneproblem/test/org/apidesign/cloneproblem/ExploitTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 Apr 2020 16:32:36 +0200
changeset 416 9ed8788a1a4e
permissions -rw-r--r--
Using HTTPS to download the libraries
     1 package org.apidesign.cloneproblem;
     2 
     3 import java.util.Date;
     4 import junit.framework.TestCase;
     5 
     6 /** Test that would be written by the API hacker.
     7  *
     8  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     9  */
    10 public class ExploitTest extends TestCase {
    11     public ExploitTest(String name) {
    12         super(name);
    13     }
    14 
    15     public void testExploitOverridableClone() {
    16         try {
    17             HackedDate now = new HackedDate();
    18             HackedDate later = new HackedDate(now.getTime() + 1000);
    19             
    20             Interval interval = new Interval(now, later);
    21             assertEquals("1s", 1000, interval.getLength());
    22             
    23             fail("And I hoped NullPointerException will be thrown!");
    24         }  catch (NullPointerException ex) {
    25             // success, the quest is to generate NullPointerException!
    26         }
    27     }
    28 
    29     // BEGIN: interval.exploit
    30     private static class HackedDate extends Date {
    31         public HackedDate() {
    32         }
    33 
    34         public HackedDate(long date) {
    35             super(date);
    36         }
    37 
    38         @Override
    39         public Object clone() {
    40             return null;
    41         }
    42     }
    43     // END: interval.exploit
    44 }