/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2021 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/o.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"

char testlib_enable_tmp_setup_teardown;

void SetUpOnce(void) {
  ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath fattr proc", 0));
}

TEST(lseek, ebadf) {
  ASSERT_SYS(EBADF, -1, lseek(-1, 0, SEEK_SET));
  ASSERT_SYS(EBADF, -1, lseek(+3, 0, SEEK_SET));
}

TEST(lseek, badWhence_einval) {
  ASSERT_SYS(0, 3, creat("foo", 0644));
  ASSERT_SYS(EINVAL, -1, lseek(3, 0, -1));
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, negativeComputedOffset_einval) {
  ASSERT_SYS(0, 3, creat("foo", 0644));
  ASSERT_SYS(EINVAL, -1, lseek(3, -1, SEEK_SET));
  ASSERT_SYS(EINVAL, -1, lseek(3, -1, SEEK_CUR));
  ASSERT_SYS(EINVAL, -1, lseek(3, -1, SEEK_END));
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, 64bit) {
  ASSERT_SYS(0, 3, creat("foo", 0644));
  ASSERT_SYS(0, 0x100000001, lseek(3, 0x100000001, SEEK_SET));
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, nonSeekableFd_espipe) {
  int fds[2];
  ASSERT_SYS(0, 0, pipe(fds));
  ASSERT_SYS(ESPIPE, -1, lseek(3, 0, SEEK_SET));
  EXPECT_SYS(0, 0, close(4));
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, filePositionChanges_areObservableAcrossDup) {
  ASSERT_SYS(0, 3, creat("wut", 0644));
  ASSERT_SYS(0, 4, dup(3));
  ASSERT_SYS(0, 0, lseek(3, 0, SEEK_CUR));
  ASSERT_SYS(0, 1, lseek(4, 1, SEEK_SET));
  ASSERT_SYS(0, 1, lseek(3, 0, SEEK_CUR));
  EXPECT_SYS(0, 0, close(4));
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, filePositionChanges_areObservableAcrossProcesses) {
  char buf[8] = {0};
  ASSERT_SYS(0, 3, open("wut", O_RDWR | O_CREAT, 0644));
  ASSERT_SYS(0, 3, write(3, "wut", 3));
  ASSERT_SYS(0, 0, lseek(3, 0, SEEK_SET));
  SPAWN(fork);
  ASSERT_SYS(0, 1, lseek(3, 1, SEEK_SET));
  EXITS(0);
  EXPECT_SYS(0, 1, read(3, buf, 1));
  EXPECT_EQ('u', buf[0]);
  EXPECT_SYS(0, 0, close(3));
}

TEST(lseek, beyondEndOfFile_isZeroExtendedUponSubsequentWrite) {
  char buf[8] = {1, 1};
  ASSERT_SYS(0, 3, open("foo", O_RDWR | O_CREAT | O_TRUNC, 0644));
  ASSERT_SYS(0, 2, lseek(3, 2, SEEK_SET));
  ASSERT_SYS(0, 2, lseek(3, 0, SEEK_CUR));
  ASSERT_SYS(0, 0, pread(3, buf, 8, 0));  // lseek() alone doesn't extend
  ASSERT_SYS(0, 2, write(3, buf, 2));     // does extend once i/o happens
  ASSERT_SYS(0, 4, pread(3, buf, 8, 0));
  ASSERT_EQ(0, buf[0]);
  ASSERT_EQ(0, buf[1]);
  ASSERT_EQ(1, buf[2]);
  ASSERT_EQ(1, buf[3]);
  ASSERT_SYS(0, 0, close(3));
}
