1f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes/*
2f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * Copyright (C) 2013 The Android Open Source Project
3f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes *
4f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * you may not use this file except in compliance with the License.
6f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * You may obtain a copy of the License at
7f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes *
8f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes *
10f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * Unless required by applicable law or agreed to in writing, software
11f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * See the License for the specific language governing permissions and
14f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes * limitations under the License.
15f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes */
16f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes
17f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes#include <gtest/gtest.h>
18f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes
19f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes#include <errno.h>
203002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes#include <sys/syscall.h>
21f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes#include <sys/time.h>
22f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes
2327586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes#include "TemporaryFile.h"
2427586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes
25f8fcfbc85a3ce3e195626b90736d3a484331494bElliott HughesTEST(sys_time, utimes) {
26f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  timeval tv[2];
27f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  memset(&tv, 0, sizeof(tv));
28f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes
29f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  tv[0].tv_usec = -123;
30f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(-1, utimes("/", tv));
31f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(EINVAL, errno);
32f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  tv[0].tv_usec = 1234567;
33f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(-1, utimes("/", tv));
34f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(EINVAL, errno);
35f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  tv[0].tv_usec = 0;
36f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes
37f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  tv[1].tv_usec = -123;
38f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(-1, utimes("/", tv));
39f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(EINVAL, errno);
40f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  tv[1].tv_usec = 1234567;
41f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(-1, utimes("/", tv));
42f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes  ASSERT_EQ(EINVAL, errno);
43f8fcfbc85a3ce3e195626b90736d3a484331494bElliott Hughes}
4427586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes
4527586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes// http://b/11383777
4627586ebe1a7f2f45651b855a26b3203d63d015d6Elliott HughesTEST(sys_time, utimes_NULL) {
4727586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes  TemporaryFile tf;
4827586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes  ASSERT_EQ(0, utimes(tf.filename, NULL));
4927586ebe1a7f2f45651b855a26b3203d63d015d6Elliott Hughes}
503002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes
513002131da33401cf1b45abbdbec58b7c751fc43aElliott HughesTEST(sys_time, gettimeofday) {
523002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  // Try to ensure that our vdso gettimeofday is working.
533002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  timeval tv1;
543002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  ASSERT_EQ(0, gettimeofday(&tv1, NULL));
553002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  timeval tv2;
563002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, NULL));
573002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes
583002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  // What's the difference between the two?
593002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  tv2.tv_sec -= tv1.tv_sec;
603002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  tv2.tv_usec -= tv1.tv_usec;
613002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  if (tv2.tv_usec < 0) {
623002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes    --tv2.tv_sec;
633002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes    tv2.tv_usec += 1000000;
643002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  }
653002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes
663002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  // Should be less than (a very generous, to try to avoid flakiness) 1000us.
673002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  ASSERT_EQ(0, tv2.tv_sec);
683002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes  ASSERT_LT(tv2.tv_usec, 1000);
693002131da33401cf1b45abbdbec58b7c751fc43aElliott Hughes}
70