libsystem: introduce do_cp() and do_cp_mode()
[platform/core/system/libsystem.git] / src / test / test-cp.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 <fcntl.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <time.h>
29
30 #include "libsystem/libsystem.h"
31
32 #define TEST_SRC_FILE "/tmp/test-cp-src"
33 #define TEST_DST_FILE "/tmp/test-cp-dst"
34
35 static int random_char(char **buf, size_t len) {
36         static int rand_init = 0;
37         char *b;
38         int rnd;
39         size_t s;
40
41         if (!rand_init) {
42                 srand(time(NULL));
43                 rand_init = 1;
44         }
45
46         b = new0(char, len);
47         if (!b)
48                 return -ENOMEM;
49
50         for (s = 0; s < len; s++) {
51
52                 /* 98 = number_of_chars((sp) ~ (DEL) + \t + \n + \r) */
53                 rnd = rand() % 98;
54
55                 assert(rnd < 98);
56
57                 switch (rnd) {
58                 case 95:
59                         b[s] = '\t';
60                         break;
61                 case 96:
62                         b[s] = '\n';
63                         break;
64                 case 97:
65                         b[s] = '\r';
66                         break;
67                 default:
68                         b[s] = ' ' + rnd;
69                         break;
70                 }
71         }
72
73         *buf = b;
74
75         return 0;
76 }
77
78 static int write_src_file(unsigned int n_byte) {
79         _cleanup_free_ char *buf = NULL;
80         _cleanup_close_ int fd = -1;
81
82         assert(random_char(&buf, n_byte) == 0);
83
84         fd = open(TEST_SRC_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644);
85         assert(fd >= 0);
86         assert(write(fd, buf, n_byte) == n_byte);
87
88         return 0;
89 }
90
91 static void compare_file(void) {
92         _cleanup_close_ int src_fd = -1, dst_fd = -1;
93         char src_buf[1024], dst_buf[1024];
94         ssize_t src_red, dst_red;
95
96         src_fd = open(TEST_SRC_FILE, O_RDONLY);
97         assert(src_fd >= 0);
98
99         dst_fd = open(TEST_DST_FILE, O_RDONLY);
100         assert(dst_fd >= 0);
101
102         while(src_red = read(src_fd, src_buf, 1024) > 0,
103               dst_red = read(dst_fd, dst_buf, 1024) > 0) {
104                 assert(src_red == dst_red);
105                 assert(memcmp(src_buf, dst_buf, src_red) == 0);
106         }
107 }
108
109 static void test_n_byte_cp(unsigned int n) {
110         assert(unlink(TEST_SRC_FILE) == 0 || errno == ENOENT);
111
112         assert(unlink(TEST_SRC_FILE) == 0 || errno == ENOENT);
113
114         assert(write_src_file(n) == 0);
115         assert(do_cp(TEST_SRC_FILE, TEST_DST_FILE) == 0);
116         compare_file();
117 }
118
119 int main(int argc, char *argv[]) {
120
121         unsigned int b;
122
123         for (b = 8; b < (1 << 30); b = b << 1)
124                 test_n_byte_cp(b);
125
126         unlink(TEST_SRC_FILE);
127         unlink(TEST_DST_FILE);
128
129         return 0;
130 }