From patchwork Thu Jan 21 23:03:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 60106 Delivered-To: patches@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp289325lbb; Thu, 21 Jan 2016 15:03:44 -0800 (PST) X-Received: by 10.98.71.3 with SMTP id u3mr64615747pfa.111.1453417422334; Thu, 21 Jan 2016 15:03:42 -0800 (PST) Return-Path: Received: from mail-pa0-x22c.google.com (mail-pa0-x22c.google.com. [2607:f8b0:400e:c03::22c]) by mx.google.com with ESMTPS id zw7si4862996pac.143.2016.01.21.15.03.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Jan 2016 15:03:42 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c03::22c as permitted sender) client-ip=2607:f8b0:400e:c03::22c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c03::22c as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-pa0-x22c.google.com with SMTP id uo6so31143423pac.1 for ; Thu, 21 Jan 2016 15:03:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=abdUOccgH6Jb0PFUClw8jLP1P8NpXOdvp5LV5wTwLc4=; b=J9GExyQBbKXwFJmSY2EEeMK3ygZ0+X98sw9TVayf0svgm+L7iVxhY+OGeZYI6GwmpM Nrsq6laSsMaLelx4px+9eucM3T/OIsArBwM/NUt4gKYtqLOwXxsLtxrLyjyh0sbuqD2v MmfOLaLMFEDPe0OKvdT5zJjQsg0U45WXORcGQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=abdUOccgH6Jb0PFUClw8jLP1P8NpXOdvp5LV5wTwLc4=; b=Rm45vT3aKHpUKvpAQgcJynpCMK8t8pmBL4Uz32xmYrvOiI73/ZJokk1QzsDvnmtrwM rd/zRpP1PniiAtlDm6Tm3jIC8UwokjXKSS7BPhAlxS1TvW8MLL1U5oSw+iQ+kX1Jcio2 5Jp7QFwd6kU0REGnTmtbw3jT9Vq56Lz1RgNNUQvUqL97jEHG62aElClUMvchzSOcx7Sg 77Kuwm2zFPllkOrx/K0i0Y8uf15ArPZ0akwDbp5D0LJkhOjzZMDbcFyogk8/tWhcKglT GMVHV/+mGfRFf4Kjua16XiLUAzH+Wgl7P+iFk2oZsjykLcJT3MVrzxyeA1NaO9n2qM1D PW/w== X-Gm-Message-State: ALoCoQmqEn/dk93ef5hWPAcm/4Wwb4jTFzKiL97RmrLXuolZbcVch30BQHQKdWyiqQ97PC39J/W7tmtnxyeLaaQ3mJHOTYYzRw== X-Received: by 10.66.102.106 with SMTP id fn10mr65624024pab.60.1453417421951; Thu, 21 Jan 2016 15:03:41 -0800 (PST) Return-Path: Received: from localhost.localdomain (c-76-115-103-22.hsd1.or.comcast.net. [76.115.103.22]) by smtp.gmail.com with ESMTPSA id pu3sm4959596pac.9.2016.01.21.15.03.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 21 Jan 2016 15:03:41 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Sasha Levin , Richard Cochran , Thomas Gleixner , Prarit Bhargava , Harald Hoyer , Kay Sievers , David Herrmann , Shuah Khan Subject: [PATCH 2/2] kselftests: timers: Add adjtimex SETOFFSET validity tests Date: Thu, 21 Jan 2016 15:03:35 -0800 Message-Id: <1453417415-19110-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453417415-19110-1-git-send-email-john.stultz@linaro.org> References: <1453417415-19110-1-git-send-email-john.stultz@linaro.org> Add some simple tests to check both valid and invalid offsets when using adjtimex's ADJ_SETOFFSET method. Cc: Sasha Levin Cc: Richard Cochran Cc: Thomas Gleixner Cc: Prarit Bhargava Cc: Harald Hoyer Cc: Kay Sievers Cc: David Herrmann Cc: Shuah Khan Signed-off-by: John Stultz --- tools/testing/selftests/timers/valid-adjtimex.c | 139 +++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-) -- 1.9.1 diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c index e86d937..60fe3c5 100644 --- a/tools/testing/selftests/timers/valid-adjtimex.c +++ b/tools/testing/selftests/timers/valid-adjtimex.c @@ -45,7 +45,17 @@ static inline int ksft_exit_fail(void) } #endif -#define NSEC_PER_SEC 1000000000L +#define NSEC_PER_SEC 1000000000LL +#define USEC_PER_SEC 1000000LL + +#define ADJ_SETOFFSET 0x0100 + +#include +static int clock_adjtime(clockid_t id, struct timex *tx) +{ + return syscall(__NR_clock_adjtime, id, tx); +} + /* clear NTP time_status & time_state */ int clear_time_state(void) @@ -193,10 +203,137 @@ out: } +int set_offset(long long offset, int use_nano) +{ + struct timex tmx = {}; + int ret; + + tmx.modes = ADJ_SETOFFSET; + if (use_nano) { + tmx.modes |= ADJ_NANO; + + tmx.time.tv_sec = offset / NSEC_PER_SEC; + tmx.time.tv_usec = offset % NSEC_PER_SEC; + + if (offset < 0 && tmx.time.tv_usec) { + tmx.time.tv_sec -= 1; + tmx.time.tv_usec += NSEC_PER_SEC; + } + } else { + tmx.time.tv_sec = offset / USEC_PER_SEC; + tmx.time.tv_usec = offset % USEC_PER_SEC; + + if (offset < 0 && tmx.time.tv_usec) { + tmx.time.tv_sec -= 1; + tmx.time.tv_usec += USEC_PER_SEC; + } + } + + ret = clock_adjtime(CLOCK_REALTIME, &tmx); + if (ret < 0) { + printf("(sec: %ld usec: %ld) ", tmx.time.tv_sec, tmx.time.tv_usec); + printf("[FAIL]\n"); + return -1; + } + return 0; +} + +int set_bad_offset(long sec, long usec, int use_nano) +{ + struct timex tmx = {}; + int ret; + + tmx.modes = ADJ_SETOFFSET; + if (use_nano) + tmx.modes |= ADJ_NANO; + + tmx.time.tv_sec = sec; + tmx.time.tv_usec = usec; + ret = clock_adjtime(CLOCK_REALTIME, &tmx); + if (ret >= 0) { + printf("Invalid (sec: %ld usec: %ld) did not fail! ", tmx.time.tv_sec, tmx.time.tv_usec); + printf("[FAIL]\n"); + return -1; + } + return 0; +} + +int validate_set_offset(void) +{ + printf("Testing ADJ_SETOFFSET... "); + + /* Test valid values */ + if (set_offset(NSEC_PER_SEC - 1, 1)) + return -1; + + if (set_offset(-NSEC_PER_SEC + 1, 1)) + return -1; + + if (set_offset(-NSEC_PER_SEC - 1, 1)) + return -1; + + if (set_offset(5 * NSEC_PER_SEC, 1)) + return -1; + + if (set_offset(-5 * NSEC_PER_SEC, 1)) + return -1; + + if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1)) + return -1; + + if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1)) + return -1; + + if (set_offset(USEC_PER_SEC - 1, 0)) + return -1; + + if (set_offset(-USEC_PER_SEC + 1, 0)) + return -1; + + if (set_offset(-USEC_PER_SEC - 1, 0)) + return -1; + + if (set_offset(5 * USEC_PER_SEC, 0)) + return -1; + + if (set_offset(-5 * USEC_PER_SEC, 0)) + return -1; + + if (set_offset(5 * USEC_PER_SEC + USEC_PER_SEC / 2, 0)) + return -1; + + if (set_offset(-5 * USEC_PER_SEC - USEC_PER_SEC / 2, 0)) + return -1; + + /* Test invalid values */ + if (set_bad_offset(0, -1, 1)) + return -1; + if (set_bad_offset(0, -1, 0)) + return -1; + if (set_bad_offset(0, 2 * NSEC_PER_SEC, 1)) + return -1; + if (set_bad_offset(0, 2 * USEC_PER_SEC, 0)) + return -1; + if (set_bad_offset(0, NSEC_PER_SEC, 1)) + return -1; + if (set_bad_offset(0, USEC_PER_SEC, 0)) + return -1; + if (set_bad_offset(0, -NSEC_PER_SEC, 1)) + return -1; + if (set_bad_offset(0, -USEC_PER_SEC, 0)) + return -1; + + printf("[OK]\n"); + return 0; +} + int main(int argc, char **argv) { if (validate_freq()) return ksft_exit_fail(); + if (validate_set_offset()) + return ksft_exit_fail(); + return ksft_exit_pass(); }