src/storage/fs_test/truncate_fixture.cc
// Test that truncate doesn't have issues dealing with larger files
// Repeatedly write to / truncate a file.
Test that truncate doesn't have issues dealing with larger files
Repeatedly write to / truncate a file.
Test that truncate doesn ' t have issues dealing with larger files Repeatedly write to / truncate a file .
TEST_P(LargeTruncateTest, RepeatedlyWritingAndTruncatingLargeFileSucceeds) {
  fbl::AllocChecker ac;
  std::unique_ptr<uint8_t[]> buf(new (&ac) uint8_t[buffer_size()]);
  ASSERT_TRUE(ac.check());
  unsigned seed = static_cast<unsigned>(zx_ticks_get());
  std::cerr << "Truncate test using seed: " << seed;
  srand(seed);
  for (unsigned n = 0; n < buffer_size(); n++) {
    buf[n] = static_cast<uint8_t>(rand_r(&seed));
  }
  const std::string filename = GetPath("alpha");
  fbl::unique_fd fd(open(filename.c_str(), O_RDWR | O_CREAT, 0644));
  ASSERT_TRUE(fd);
  ASSERT_EQ(write(fd.get(), buf.get(), buffer_size()), static_cast<ssize_t>(buffer_size()));
  if (test_type() != LargeTruncateTestType::KeepOpen) {
    ASSERT_EQ(close(fd.release()), 0);
  }
  for (size_t i = 0; i < iterations(); ++i) {
    size_t len = rand_r(&seed) % buffer_size();
    if (test_type() == LargeTruncateTestType::KeepOpen) {
      ASSERT_NO_FATAL_FAILURE(CheckedFtruncate(fd.get(), buf.get(), len));
    } else {
      ASSERT_NO_FATAL_FAILURE(CheckedTruncate(fs(), test_type() == LargeTruncateTestType::Remount,
                                              filename.c_str(), buf.get(), len));
    }
  }
  ASSERT_EQ(unlink(filename.c_str()), 0);
  if (test_type() == LargeTruncateTestType::KeepOpen) {
    ASSERT_EQ(close(fd.release()), 0);
  }
}
TEST_P ( LargeTruncateTest , RepeatedlyWritingAndTruncatingLargeFileSucceeds ) { fbl :: AllocChecker ac ; std :: unique_ptr < uint8_t [ ] > buf ( new ( & ac ) uint8_t [ buffer_size ( ) ] ) ; ASSERT_TRUE ( ac . check ( ) ) ; unsigned seed = static_cast < unsigned > ( zx_ticks_get ( ) ) ; std :: cerr << " " << seed ; srand ( seed ) ; for ( unsigned n = 0 ; n < buffer_size ( ) ; n ++ ) { buf [ n ] = static_cast < uint8_t > ( rand_r ( & seed ) ) ; } const std :: string filename = GetPath ( " " ) ; fbl :: unique_fd fd ( open ( filename . c_str ( ) , O_RDWR | O_CREAT , 0644 ) ) ; ASSERT_TRUE ( fd ) ; ASSERT_EQ ( write ( fd . get ( ) , buf . get ( ) , buffer_size ( ) ) , static_cast < ssize_t > ( buffer_size ( ) ) ) ; if ( test_type ( ) != LargeTruncateTestType :: KeepOpen ) { ASSERT_EQ ( close ( fd . release ( ) ) , 0 ) ; } for ( size_t i = 0 ; i < iterations ( ) ; ++ i ) { size_t len = rand_r ( & seed ) % buffer_size ( ) ; if ( test_type ( ) == LargeTruncateTestType :: KeepOpen ) { ASSERT_NO_FATAL_FAILURE ( CheckedFtruncate ( fd . get ( ) , buf . get ( ) , len ) ) ; } else { ASSERT_NO_FATAL_FAILURE ( CheckedTruncate ( fs ( ) , test_type ( ) == LargeTruncateTestType :: Remount , filename . c_str ( ) , buf . get ( ) , len ) ) ; } } ASSERT_EQ ( unlink ( filename . c_str ( ) ) , 0 ) ; if ( test_type ( ) == LargeTruncateTestType :: KeepOpen ) { ASSERT_EQ ( close ( fd . release ( ) ) , 0 ) ; } }
Test that truncate doesn't have issues dealing with larger files
Repeatedly write to / truncate a file.
Test that truncate doesn ' t have issues dealing with larger files Repeatedly write to / truncate a file .
// Fill a test buffer with data // Start a file filled with a buffer // Repeatedly truncate / write to the file