zircon/system/ulib/fs/test/posix/truncate.cc
// Test that the really simple cases of truncate are operational
Test that the really simple cases of truncate are operational
Test that the really simple cases of truncate are operational
void TestTruncateSingleBlockFile(FilesystemTest* ops) {
  const char* str = "Hello, World!\n";
  char filename[PATH_MAX];
  ASSERT_LT(snprintf(filename, sizeof(filename), "%s/%s", ops->mount_path(), __func__),
            sizeof(filename));
  fbl::unique_fd fd(open(filename, O_RDWR | O_CREAT, 0644));
  ASSERT_TRUE(fd);
  ASSERT_STREAM_ALL(write, fd.get(), str, strlen(str));
  check_file_contains(filename, str, strlen(str));
  fbl::unique_fd fd2(open(filename, O_RDWR | O_TRUNC, 0644));
  ASSERT_TRUE(fd2);
  check_file_empty(filename);
  ASSERT_EQ(lseek(fd.get(), 0, SEEK_SET), 0);
  ASSERT_STREAM_ALL(write, fd.get(), str, strlen(str));
  check_file_contains(filename, str, strlen(str));
  ASSERT_EQ(truncate(filename, 5), 0);
  check_file_contains(filename, str, 5);
  ASSERT_EQ(truncate(filename, 0), 0);
  check_file_empty(filename);
  ASSERT_EQ(truncate(filename, 0), 0);
  check_file_empty(filename);
  char empty[5] = {0, 0, 0, 0, 0};
  ASSERT_EQ(truncate(filename, 5), 0);
  check_file_contains(filename, empty, 5);
  ASSERT_EQ(close(fd.release()), 0);
  ASSERT_EQ(close(fd2.release()), 0);
  ASSERT_EQ(unlink(filename), 0);
}
void TestTruncateSingleBlockFile ( FilesystemTest * ops ) { const char * str = " \n " ; char filename [ PATH_MAX ] ; ASSERT_LT ( snprintf ( filename , sizeof ( filename ) , " " , ops -> mount_path ( ) , __func__ ) , sizeof ( filename ) ) ; fbl :: unique_fd fd ( open ( filename , O_RDWR | O_CREAT , 0644 ) ) ; ASSERT_TRUE ( fd ) ; ASSERT_STREAM_ALL ( write , fd . get ( ) , str , strlen ( str ) ) ; check_file_contains ( filename , str , strlen ( str ) ) ; fbl :: unique_fd fd2 ( open ( filename , O_RDWR | O_TRUNC , 0644 ) ) ; ASSERT_TRUE ( fd2 ) ; check_file_empty ( filename ) ; ASSERT_EQ ( lseek ( fd . get ( ) , 0 , SEEK_SET ) , 0 ) ; ASSERT_STREAM_ALL ( write , fd . get ( ) , str , strlen ( str ) ) ; check_file_contains ( filename , str , strlen ( str ) ) ; ASSERT_EQ ( truncate ( filename , 5 ) , 0 ) ; check_file_contains ( filename , str , 5 ) ; ASSERT_EQ ( truncate ( filename , 0 ) , 0 ) ; check_file_empty ( filename ) ; ASSERT_EQ ( truncate ( filename , 0 ) , 0 ) ; check_file_empty ( filename ) ; char empty [ 5 ] = { 0 , 0 , 0 , 0 , 0 } ; ASSERT_EQ ( truncate ( filename , 5 ) , 0 ) ; check_file_contains ( filename , empty , 5 ) ; ASSERT_EQ ( close ( fd . release ( ) ) , 0 ) ; ASSERT_EQ ( close ( fd2 . release ( ) ) , 0 ) ; ASSERT_EQ ( unlink ( filename ) , 0 ) ; }
Test that the really simple cases of truncate are operational
Test that the really simple cases of truncate are operational
// Try writing a string to a file // Check that opening a file with O_TRUNC makes it empty // Check that we can still write to a file that has been truncated // Check that we can truncate the file using the "truncate" function // Check that truncating an already empty file does not cause problems // Check that we can use truncate to extend a file