2016-02-26 13:25:22 +03:00
/***
This file is part of systemd .
Copyright ( C ) 2016 Canonical Ltd .
systemd is free software ; you can redistribute it and / or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
# include <unistd.h>
# include <fcntl.h>
2016-02-29 16:03:32 +03:00
# include "clock-util.h"
# include "fd-util.h"
2016-02-26 13:25:22 +03:00
# include "fileio.h"
# include "log.h"
2016-02-29 16:03:32 +03:00
# include "macro.h"
2016-02-26 13:25:22 +03:00
static void test_clock_is_localtime ( void ) {
char adjtime [ ] = " /tmp/test-adjtime.XXXXXX " ;
2016-02-29 16:03:32 +03:00
_cleanup_close_ int fd = - 1 ;
2016-02-26 13:25:22 +03:00
FILE * f ;
2016-02-29 16:03:32 +03:00
static const struct scenario {
2016-02-26 13:25:22 +03:00
const char * contents ;
int expected_result ;
} scenarios [ ] = {
/* adjtime configures UTC */
{ " 0.0 0 0 \n 0 \n UTC \n " , 0 } ,
/* adjtime configures local time */
{ " 0.0 0 0 \n 0 \n LOCAL \n " , 1 } ,
/* no final EOL */
{ " 0.0 0 0 \n 0 \n UTC " , 0 } ,
{ " 0.0 0 0 \n 0 \n LOCAL " , 1 } ,
2016-02-26 14:33:41 +03:00
/* empty value -> defaults to UTC */
{ " 0.0 0 0 \n 0 \n " , 0 } ,
2016-02-26 13:25:22 +03:00
/* unknown value -> defaults to UTC */
{ " 0.0 0 0 \n 0 \n FOO \n " , 0 } ,
2016-02-26 14:33:41 +03:00
/* no third line */
{ " 0.0 0 0 " , 0 } ,
{ " 0.0 0 0 \n " , 0 } ,
{ " 0.0 0 0 \n 0 " , 0 } ,
2016-02-26 13:25:22 +03:00
} ;
/* without an adjtime file we default to UTC */
assert_se ( clock_is_localtime ( " /nonexisting/adjtime " ) = = 0 ) ;
fd = mkostemp_safe ( adjtime , O_WRONLY | O_CLOEXEC ) ;
2016-02-29 17:16:11 +03:00
assert_se ( fd > = 0 ) ;
2016-02-26 13:25:22 +03:00
log_info ( " adjtime test file: %s " , adjtime ) ;
f = fdopen ( fd , " w " ) ;
2016-02-29 17:16:11 +03:00
assert_se ( f ) ;
2016-02-26 13:25:22 +03:00
for ( size_t i = 0 ; i < ELEMENTSOF ( scenarios ) ; + + i ) {
log_info ( " scenario #%zu:, expected result %i " , i , scenarios [ i ] . expected_result ) ;
log_info ( " %s " , scenarios [ i ] . contents ) ;
rewind ( f ) ;
ftruncate ( fd , 0 ) ;
assert_se ( write_string_stream ( f , scenarios [ i ] . contents , false ) = = 0 ) ;
assert_se ( clock_is_localtime ( adjtime ) = = scenarios [ i ] . expected_result ) ;
}
unlink ( adjtime ) ;
}
/* Test with the real /etc/adjtime */
static void test_clock_is_localtime_system ( void ) {
int r ;
r = clock_is_localtime ( NULL ) ;
if ( access ( " /etc/adjtime " , F_OK ) = = 0 ) {
log_info ( " /etc/adjtime exists, clock_is_localtime() == %i " , r ) ;
2016-02-26 14:33:41 +03:00
/* if /etc/adjtime exists we expect some answer, no error or
* crash */
2016-02-29 17:16:11 +03:00
assert_se ( r = = 0 | | r = = 1 ) ;
2016-02-26 13:25:22 +03:00
} else
/* default is UTC if there is no /etc/adjtime */
2016-02-29 17:16:11 +03:00
assert_se ( r = = 0 ) ;
2016-02-26 13:25:22 +03:00
}
int main ( int argc , char * argv [ ] ) {
test_clock_is_localtime ( ) ;
test_clock_is_localtime_system ( ) ;
return 0 ;
}