libsystem: add 64 bit signed/unsigned read-write api
[platform/core/system/libsystem.git] / src / test / test-read-write.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * libsystem
5  *
6  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the License);
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdint.h>
27
28 #include "libsystem/libsystem.h"
29
30 #define TEST_READ_WRITE_FILE    "/tmp/test-read-write"
31 #define TEST_STRING             "L!i@b#s$y%s^t&e*m(T)e-s=tS`t_r+i|n~g"
32 #define TEST_INT32              0x7fabcdef
33 #define TEST_UINT32             0xffabcdef
34 #define TEST_INT64              0x7fabcdef00abcdef
35 #define TEST_UINT64             0xffabcdef00abcdef
36
37 static void test_string_read_write(void) {
38         _cleanup_free_ char *str = NULL;
39
40         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
41
42         assert(write_str_to_path(TEST_READ_WRITE_FILE, TEST_STRING, 0) >= 0);
43         assert(read_one_line_from_path(TEST_READ_WRITE_FILE, &str) >= 0);
44
45         assert(streq(str, TEST_STRING));
46
47         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
48 }
49
50 static void test_int32_read_write(void) {
51         int32_t i;
52
53         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
54
55         assert(write_int32_to_path(TEST_READ_WRITE_FILE, TEST_INT32, 0) >= 0);
56         assert(read_int32_from_path(TEST_READ_WRITE_FILE, &i) >= 0);
57
58         assert(i == TEST_INT32);
59
60         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
61 }
62
63 static void test_uint32_read_write(void) {
64         uint32_t u;
65
66         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
67
68         assert(write_uint32_to_path(TEST_READ_WRITE_FILE, TEST_UINT32, 0) >= 0);
69         assert(read_uint32_from_path(TEST_READ_WRITE_FILE, &u) >= 0);
70
71         assert(u == TEST_UINT32);
72
73         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
74 }
75
76 static void test_int64_read_write(void) {
77         int64_t i;
78
79         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
80
81         assert(write_int64_to_path(TEST_READ_WRITE_FILE, TEST_INT64, 0) >= 0);
82         assert(read_int64_from_path(TEST_READ_WRITE_FILE, &i) >= 0);
83
84         assert(i == TEST_INT64);
85
86         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
87 }
88
89 static void test_uint64_read_write(void) {
90         uint64_t u;
91
92         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
93
94         assert(write_uint64_to_path(TEST_READ_WRITE_FILE, TEST_UINT64, 0) >= 0);
95         assert(read_uint64_from_path(TEST_READ_WRITE_FILE, &u) >= 0);
96
97         assert(u == TEST_UINT64);
98
99         assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
100 }
101
102 int main(int argc, char *argv[]) {
103         test_string_read_write();
104         test_int32_read_write();
105         test_uint32_read_write();
106         test_int64_read_write();
107         test_uint64_read_write();
108
109         return 0;
110 }