1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "libsystem/libsystem.h"
32 #define TEST_SRC_FILE "/tmp/test-cp-src"
33 #define TEST_DST_FILE "/tmp/test-cp-dst"
35 static int random_char(char **buf, size_t len) {
36 static int rand_init = 0;
50 for (s = 0; s < len; s++) {
52 /* 98 = number_of_chars((sp) ~ (DEL) + \t + \n + \r) */
78 static int write_src_file(unsigned int n_byte) {
79 _cleanup_free_ char *buf = NULL;
80 _cleanup_close_ int fd = -1;
82 assert(random_char(&buf, n_byte) == 0);
84 fd = open(TEST_SRC_FILE, O_CREAT | O_WRONLY | O_TRUNC, 0644);
86 assert(write(fd, buf, n_byte) == n_byte);
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;
96 src_fd = open(TEST_SRC_FILE, O_RDONLY);
99 dst_fd = open(TEST_DST_FILE, O_RDONLY);
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);
109 static void test_n_byte_cp(unsigned int n) {
110 assert(unlink(TEST_SRC_FILE) == 0 || errno == ENOENT);
112 assert(unlink(TEST_SRC_FILE) == 0 || errno == ENOENT);
114 assert(write_src_file(n) == 0);
115 assert(do_cp(TEST_SRC_FILE, TEST_DST_FILE) == 0);
119 int main(int argc, char *argv[]) {
123 for (b = 8; b < (1 << 30); b = b << 1)
126 unlink(TEST_SRC_FILE);
127 unlink(TEST_DST_FILE);