Пример #1
0
  @Test
  public void testBlockedInAtomic() {
    if (verifyDeadlock("+cg.enable_atomic")) {
      Runnable r =
          new Runnable() {

            @Override
            public synchronized void run() {
              System.out.println("T notifying..");
              this.notify();
            }
          };

      Thread t = new Thread(r);

      synchronized (r) {
        System.out.println("main going atomic, holding r lock");
        Verify.beginAtomic();
        t.start();

        try {
          System.out.println("main waiting on r");
          r.wait();
        } catch (InterruptedException x) {
          System.out.println("main got interrupted");
        }
        System.out.println("main leaving atomic");
        Verify.endAtomic();
      }
    }
  }
Пример #2
0
  @Test
  public void testNoRace() {
    if (verifyNoPropertyViolation("+cg.enable_atomic")) {
      Runnable r =
          new Runnable() {

            @Override
            public void run() {
              System.out.println("  enter run in Thread-0");
              assert data == 42;
              data += 1;
              assert data == 43;
              data -= 1;
              assert data == 42;
              System.out.println("  exit run in Thread-0");
            }
          };

      Thread t = new Thread(r);

      Verify.beginAtomic();
      System.out.println("enter atomic section in main");
      t.start();
      assert data == 42;
      data += 2;
      assert data == 44;
      data -= 2;
      assert data == 42;
      System.out.println("exit atomic section in main");
      Verify.endAtomic();
    }
  }
Пример #3
0
    private void unsafe() throws IOException {
      int i, expect, actual, test;
      byte buffer[];

      // System.out.print("#" + Thread.currentThread().getId() + " | Test ");
      test = Verify.getInt(0, 4);

      // System.out.println(test);

      switch (test) {
        case 0:
          m_input.close();
          break;

        case 1:
          assertEquals(4, m_input.available());
          break;

        case 2:
          expect = Verify.getInt(-1, 5);
          actual = (int) m_input.skip(expect);
          expect = Math.max(expect, 0);
          expect = Math.min(expect, 4);

          assertTrue(actual <= expect);
          break;

        case 3:
          for (i = 0; i < 4; i++) {
            assertEquals(m_expect[i], m_input.read());
          }

          break;

        case 4:
          buffer = new byte[1];

          for (i = 0; i < 4; i++) {
            assertEquals(1, m_input.read(buffer));
            assertEquals(m_expect[i], buffer[0]);
          }
          break;
      }
    }
Пример #4
0
  // makes v a symbolic object
  public static Object makeSymbolicRef(String name, Object v) {
    assert (v != null); // needed for type info
    if (Verify.randomBool()) {

      makeFieldsSymbolic(name, v);
    } else {

      v = makeSymbolicNull(name);
    }
    return v;
  }
Пример #5
0
  @Test
  public void testAddUserDefinedCGCreator() {
    if (verifyNoPropertyViolation(
        "+jpf-core.native_classpath+=;${jpf-core}/build/tests",
        "+jpf-core.test_classpath+=;${jpf-core.native_classpath}",
        "+cg-creators=TF:" + TestBoolCGCreator.class.getName())) {

      String json = "{" + "'b' : TF()" + "}";

      Object[] expected = {new B(true), new B(false)};

      B b = Verify.createFromJSON(B.class, json);
      JSONTest.checkValue(expected, b);
    }
  }
Пример #6
0
  @Test
  public void testDataCG() {
    if (verifyNoPropertyViolation("+cg.enable_atomic")) {
      Runnable r =
          new Runnable() {

            @Override
            public void run() {
              data += 10;
            }
          };

      Thread t = new Thread(r);

      Verify.beginAtomic();
      t.start();
      int i = Verify.getInt(1, 2);
      data += i;
      assert data < 45 : "data got incremented: " + data;
      Verify.incrementCounter(0);
      assert i == Verify.getCounter(0);
      Verify.endAtomic();
    }
  }
Пример #7
0
 public static void main(String[] args) {
   Verify.resetInstructionCounter();
   new ExplosionVarexJ(Integer.parseInt(args[0]));
 }
Пример #8
0
public class SplitInputStreamTest extends TestJPF {

  private static final Random s_random = Verify.isRunningInJPF() ? null : new SecureRandom();
  private static final String s_jpfArgs[] =
      new String[] {
        "+listener+=,gov.nasa.jpf.listener.PreciseRaceDetector", "+classpath=build/classes/main"
      };
  private SplitInputStream m_fixture;
  private InputStream m_input;
  private byte m_expect[];

  @Before
  public void before() throws IOException {
    initialize(10, SplitInputStream.INITIAL_BUFFER_SIZE);
  }

  public void initialize(int length, int bufferSize) throws IOException {
    ByteArrayInputStream source;
    int i;

    m_expect = new byte[length];

    if (s_random != null) {
      s_random.nextBytes(m_expect);
    } else {
      for (i = m_expect.length; --i >= 0; ) {
        m_expect[i] = (byte) i;
      }
    }

    source = new ByteArrayInputStream(m_expect);
    m_fixture = new SplitInputStream(source, 2, bufferSize);
    m_input = m_fixture.getStream(0);

    assertEquals(2, m_fixture.getStreamCount());
  }

  @After
  public void after() throws IOException {
    InputStream input;
    int i, j;

    for (i = m_fixture.getStreamCount(); --i > 0; ) {
      input = m_fixture.getStream(i);

      for (j = m_expect.length - input.available(); j < m_expect.length; j++) {
        assertEquals(m_expect[j] & 0x0FF, input.read());
      }

      assertEquals(-1, input.read());
      assertEquals(-1, input.read(new byte[1]));
    }
  }

  @Test(expected = NullPointerException.class)
  public void passNullPointerToConstructor() throws IOException {
    InputStream source;

    source = null;

    new SplitInputStream(source, 1);
  }

  @Test(expected = IllegalArgumentException.class)
  public void passZeroStreamsToConstructor() throws IOException {
    InputStream source;

    source = new ByteArrayInputStream(m_expect);

    new SplitInputStream(source, 0);
  }

  @Test(expected = IllegalArgumentException.class)
  public void passZeroBufferSizeToConstructor() throws IOException {
    InputStream source;

    source = new ByteArrayInputStream(m_expect);

    new SplitInputStream(source, 2, 0);
  }

  @Test
  public void readByte() throws IOException {
    assertEquals(m_expect[0] & 0x0FF, m_input.read());
  }

  @Test
  public void readEveryByteValue() throws IOException {
    ByteArrayInputStream source;
    int i;

    m_expect = new byte[256];

    for (i = 256; --i >= 0; ) {
      m_expect[i] = (byte) i;
    }

    source = new ByteArrayInputStream(m_expect);
    m_fixture = new SplitInputStream(source, 2, 256);
    m_input = m_fixture.getStream(0);

    for (i = 0; i < 256; i++) {
      assertEquals(i, m_input.read());
    }
  }

  @Test(expected = NullPointerException.class)
  public void readNullBuffer() throws IOException {
    m_input.read(null, 0, 1);
  }

  @Test(expected = IndexOutOfBoundsException.class)
  public void readIndexNegOne() throws IOException {
    m_input.read(new byte[0], -1, 0);
  }

  @Test(expected = IndexOutOfBoundsException.class)
  public void readLengthNegOne() throws IOException {
    m_input.read(new byte[0], 0, -1);
  }

  @Test(expected = IndexOutOfBoundsException.class)
  public void readBeyondEnd() throws IOException {
    m_input.read(new byte[16], 8, 9);
  }

  @Test
  public void readLengthZero() throws IOException {
    m_input.read(new byte[1], 0, 0);
    assertEquals(m_expect[0] & 0x0FF, m_input.read());
  }

  @Test
  public void readArray() throws IOException {
    int offset, length, delta;
    byte buffer[];

    length = m_expect.length;
    buffer = new byte[length];

    for (offset = 0; offset < length; offset += delta) {
      delta = m_input.read(buffer);

      assertTrue(delta >= 0);
    }

    assertArrayEquals(m_expect, buffer);
  }

  @Test
  public void readArrayEveryByteValue() throws IOException {
    ByteArrayInputStream source;
    int i, delta;
    byte actual[];

    m_expect = new byte[256];
    actual = new byte[256];

    for (i = 256; --i >= 0; ) {
      m_expect[i] = (byte) i;
    }

    source = new ByteArrayInputStream(m_expect);
    m_fixture = new SplitInputStream(source, 2);
    m_input = m_fixture.getStream(0);

    for (i = 0; i < 256; i += delta) {
      delta = m_input.read(actual, i, 256 - i);

      assertTrue(delta >= 0);
    }

    assertArrayEquals(m_expect, actual);
  }

  @Test
  public void skipZero() throws IOException {
    assertEquals(0, m_input.skip(0));
  }

  @Test
  public void skipNegOne() throws IOException {
    assertEquals(0, m_input.skip(-1));
  }

  @Test
  public void skip() throws IOException {
    assertEquals(m_expect.length / 2, m_input.skip(m_expect.length / 2));
  }

  @Test
  public void skipToEnd() throws IOException {
    assertEquals(m_expect.length, m_input.skip(m_expect.length));
  }

  @Test
  public void skipBeyondEnd() throws IOException {
    assertEquals(m_expect.length, m_input.skip(m_expect.length + 1));
    assertEquals(0, m_input.skip(1));
  }

  @Test
  public void readByteAfterClose() throws IOException {
    m_input.close();
    assertEquals(-1, m_input.read());
  }

  @Test
  public void readBufferAfterClose() throws IOException {
    m_input.close();
    assertEquals(-1, m_input.read(new byte[1], 0, 1));
  }

  @Test
  public void skipAfterClose() throws IOException {
    m_input.close();
    assertEquals(0, m_input.skip(1));
  }

  @Test
  public void availableAfterClose() throws IOException {
    assertEquals(m_expect.length, m_input.available());
    m_input.close();
    assertEquals(0, m_input.available());
  }

  @Test
  public void availableAtEnd() throws IOException {
    int i;

    for (i = 0; i < m_expect.length; i++) {
      assertEquals(m_expect.length - i, m_input.available());
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }

    assertEquals(0, m_input.available());
  }

  @Test
  public void availableNeverReads() throws IOException {
    SplitInputStream split;
    InputStream source, stream;

    source =
        new InputStream() {

          public int read() {
            fail();

            return (0);
          }
        };

    split = new SplitInputStream(source, 1);
    stream = split.getStream(0);

    assertEquals(0, stream.available());
  }

  @Test
  public void closeSource() throws IOException {
    SplitInputStream split;
    CloseCountInputStream source;
    InputStream input;
    int i;

    source = new CloseCountInputStream();
    split = new SplitInputStream(source, 2);
    input = split.getStream(0);

    for (i = split.getStreamCount() + 5; --i >= 0; ) {
      input.close();
    }

    assertEquals(0, source.getCloseCount());

    input = split.getStream(1);

    input.close();
    assertEquals(1, source.getCloseCount());

    for (i = split.getStreamCount() + 5; --i >= 0; ) {
      input.close();
    }

    assertEquals(1, source.getCloseCount());
  }

  @Test
  public void overflowAvailable() throws IOException {
    SplitInputStream split;
    MaxAvailableInputStream source;
    InputStream input;

    source = new MaxAvailableInputStream();
    split = new SplitInputStream(source, 1);
    input = split.getStream(0);

    assertEquals(0, input.read());
    assertEquals(Integer.MAX_VALUE, input.available());
  }

  @Test
  public void expand() throws IOException {
    int i, length;

    length = 2 * SplitInputStream.INITIAL_BUFFER_SIZE;

    initialize(length, SplitInputStream.INITIAL_BUFFER_SIZE);

    for (i = 0; i < length; i++) {
      assertEquals(length - i, m_input.available());
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }
  }

  @Test
  public void wrap() throws IOException {
    int i, length;

    length = 2 * SplitInputStream.INITIAL_BUFFER_SIZE;

    initialize(length, SplitInputStream.INITIAL_BUFFER_SIZE);

    for (i = 0; i < SplitInputStream.INITIAL_BUFFER_SIZE - 5; i++) {
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }

    m_input = m_fixture.getStream(1);

    for (i = 0; i < SplitInputStream.INITIAL_BUFFER_SIZE; i++) {
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }

    assertEquals(SplitInputStream.INITIAL_BUFFER_SIZE, m_input.available());
  }

  @Test
  public void ignoreClosedStream()
      throws IOException, NoSuchFieldException, IllegalAccessException {
    Field bufferField;
    int i, length;
    byte expect[], actual[];

    length = 2 * SplitInputStream.INITIAL_BUFFER_SIZE;

    initialize(length, SplitInputStream.INITIAL_BUFFER_SIZE);
    m_input.close();

    bufferField = SplitInputStream.class.getDeclaredField("m_buffer");

    bufferField.setAccessible(true);

    expect = (byte[]) bufferField.get(m_fixture);
    m_input = m_fixture.getStream(1);

    for (i = 0; i < length; i++) {
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }

    actual = (byte[]) bufferField.get(m_fixture);

    assertSame(expect, actual);
  }

  @Test
  public void bufferSize1() throws IOException {
    int i;

    initialize(10, 1);

    for (i = 0; i < m_expect.length; i++) {
      assertEquals(m_expect[i] & 0x0FF, m_input.read());
    }
  }

  @Ignore(
      "This test takes too long for everyone to run all the time.  There are 101,174 states which take about 4 minutes to run.")
  @Test
  public void concurrentRead() throws IOException, InterruptedException {
    InputStream source;
    Thread thread1, thread2;
    Runnable task;

    if (verifyNoPropertyViolation(s_jpfArgs)) {
      source =
          new InputStream() {

            private Thread m_reader;

            public int read() {
              if (m_reader == null) {
                m_reader =
                    Thread
                        .currentThread(); // JPF will catch the race condition if 2 threads call
                                          // read concurrently.
              } else {
                assertSame(m_reader, Thread.currentThread());
              }

              return (0);
            }
          };

      m_fixture = new SplitInputStream(source, 2);

      task =
          new Runnable() {

            public void run() {
              try {
                m_fixture.getStream(0).read();
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
          };

      thread1 = new Thread(task);

      task =
          new Runnable() {

            public void run() {
              try {
                m_fixture.getStream(1).read();
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
          };

      thread2 = new Thread(task);

      thread1.start();
      thread2.start();

      thread1.join();
      thread2.join();
    }
  }

  @Test
  public void concurrentAvailable() throws InterruptedException {
    InputStream source;
    Thread thread1, thread2;
    Runnable task;

    if (verifyNoPropertyViolation(s_jpfArgs)) {
      source =
          new InputStream() {

            private Thread m_access;

            public int read() {
              fail();

              return (0);
            }

            public int available() {
              assertNull(m_access);

              m_access =
                  Thread
                      .currentThread(); // JPF will catch the race condition if 2 threads call
                                        // concurrently.
              m_access = null;

              return (0);
            }
          };

      m_fixture = new SplitInputStream(source, 2);

      task =
          new Runnable() {

            public void run() {
              try {
                m_fixture.getStream(0).available();
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
          };

      thread1 = new Thread(task);

      task =
          new Runnable() {

            public void run() {
              try {
                m_fixture.getStream(1).available();
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
          };

      thread2 = new Thread(task);

      thread1.start();
      thread2.start();

      thread1.join();
      thread2.join();
    }
  }

  @Ignore(
      "This test takes too long for everyone to run all the time.  There are 230,360 states which take about 5 minutes to run.")
  @Test
  public void thoroughJPFTest() throws InterruptedException, IOException {
    Thread thread1, thread2;

    if (verifyNoPropertyViolation(s_jpfArgs)) {
      initialize(4, 2);

      thread1 = new Thread(new JPFTask(0));
      thread2 = new Thread(new JPFTask(1));

      thread1.start();
      thread2.start();

      thread1.join();
      thread2.join();
    }
  }

  private static class CloseCountInputStream extends InputStream {

    private int m_closeCount;

    public int read() {
      return (0);
    }

    public int available() {
      return (m_closeCount == 0 ? 1 : 0);
    }

    public void close() {
      m_closeCount++;
    }

    public int getCloseCount() {
      return (m_closeCount);
    }
  }

  private static class MaxAvailableInputStream extends InputStream {

    private int m_data;

    public int read() {
      return (m_data++);
    }

    public int available() {
      return (Integer.MAX_VALUE);
    }
  }

  private class JPFTask implements Runnable {

    private final InputStream m_input;

    public JPFTask(int index) {
      m_input = m_fixture.getStream(index);
    }

    public void run() {
      try {
        unsafe();
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }

    private void unsafe() throws IOException {
      int i, expect, actual, test;
      byte buffer[];

      // System.out.print("#" + Thread.currentThread().getId() + " | Test ");
      test = Verify.getInt(0, 4);

      // System.out.println(test);

      switch (test) {
        case 0:
          m_input.close();
          break;

        case 1:
          assertEquals(4, m_input.available());
          break;

        case 2:
          expect = Verify.getInt(-1, 5);
          actual = (int) m_input.skip(expect);
          expect = Math.max(expect, 0);
          expect = Math.min(expect, 4);

          assertTrue(actual <= expect);
          break;

        case 3:
          for (i = 0; i < 4; i++) {
            assertEquals(m_expect[i], m_input.read());
          }

          break;

        case 4:
          buffer = new byte[1];

          for (i = 0; i < 4; i++) {
            assertEquals(1, m_input.read(buffer));
            assertEquals(m_expect[i], buffer[0]);
          }
          break;
      }
    }
  }
}
Пример #9
0
 public static void assume(boolean c) {
   if (!c) Verify.ignoreIf(true);
 }
Пример #10
0
 public static void main(String[] args) {
   Verify.resetInstructionCounter();
   new NoJPF_SE(Integer.parseInt(args[0]));
 }