Skip to content

Commit 0747702

Browse files
committed
test: add tar unit test
1 parent 99a12c7 commit 0747702

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

test/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ set(TEST_SOURCES
8585
${TEST}/util/unit/percent_encoding_test.cpp
8686
${TEST}/util/unit/ringbuffer.cpp
8787
${TEST}/util/unit/statman.cpp
88+
${TEST}/util/unit/tar_test.cpp
8889
${TEST}/util/unit/uri_test.cpp
8990
)
9091

@@ -208,13 +209,20 @@ endif("${ARCH}" STREQUAL "ARCH_ARMv7")
208209
if(SILENT_BUILD)
209210
message(STATUS "NOTE: Building with some warnings turned off")
210211
set_property(SOURCE ${SOURCES} APPEND_STRING PROPERTY COMPILE_FLAGS
211-
"-Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -Wno-format")
212+
" -Wno-unused-variable -Wno-unused-parameter -Wno-sign-compare -Wno-format")
212213
endif()
213214

214215
add_executable(unittests ${SOURCES})
215216
install(TARGETS unittests DESTINATION ${TEST})
216217
install(DIRECTORY lest/include/lest DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
217218

219+
add_custom_command(
220+
TARGET unittests PRE_BUILD
221+
COMMAND ${CMAKE_COMMAND} -E tar cf ${CMAKE_CURRENT_BINARY_DIR}/test-single.tar ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
222+
COMMAND ${CMAKE_COMMAND} -E tar cf ${CMAKE_CURRENT_BINARY_DIR}/test-multiple.tar ${CMAKE_CURRENT_SOURCE_DIR}/*.py
223+
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ${CMAKE_CURRENT_BINARY_DIR}/test-invalid.tar
224+
)
225+
218226
add_custom_target(
219227
cppcheck
220228
COMMAND cppcheck

test/util/unit/tar_test.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#include <common.cxx>
19+
#include <tar>
20+
#include <sys/mman.h>
21+
#include <sys/stat.h>
22+
#include <fcntl.h>
23+
#include <unistd.h>
24+
25+
CASE("Reading single entry tar file")
26+
{
27+
tar::Reader r;
28+
struct stat st;
29+
int res = stat("test-single.tar", &st);
30+
EXPECT(res != -1);
31+
size_t size = st.st_size;
32+
int fd = open("test-single.tar", O_RDONLY);
33+
EXPECT_NOT(fd == -1);
34+
const uint8_t *mem = (const uint8_t *)mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
35+
tar::Tar tar = r.read_uncompressed(mem, size);
36+
auto names = tar.element_names();
37+
EXPECT(names.size() == 1);
38+
const auto& elements = tar.elements();
39+
EXPECT(elements.size() == 1);
40+
const auto& e = elements.at(0);
41+
EXPECT_NOT(e.is_dir());
42+
EXPECT(e.typeflag_is_set());
43+
EXPECT_NOT(e.is_empty());
44+
EXPECT(e.is_ustar());
45+
EXPECT_NOT(e.is_tar_gz());
46+
EXPECT(e.num_content_blocks() > 0);
47+
EXPECT(e.size() > 0);
48+
const auto& name = e.name();
49+
EXPECT(name.find("CMakeLists.txt") != std::string::npos);
50+
EXPECT_THROWS_AS(auto& missing_element = tar.element("not_there.txt"), tar::Tar_exception);
51+
close(fd);
52+
}
53+
54+
CASE("Reading multiple entry tar file")
55+
{
56+
tar::Reader r;
57+
struct stat st;
58+
int res = stat("test-multiple.tar", &st);
59+
EXPECT(res != -1);
60+
size_t size = st.st_size;
61+
int fd = open("test-multiple.tar", O_RDONLY);
62+
EXPECT_NOT(fd == -1);
63+
const uint8_t *mem = (const uint8_t *)mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
64+
tar::Tar tar = r.read_uncompressed(mem, size);
65+
auto names = tar.element_names();
66+
EXPECT(names.size() > 1);
67+
const auto& elements = tar.elements();
68+
EXPECT(elements.size() > 1);
69+
const auto& e = elements.at(elements.size() - 1);
70+
EXPECT(e.name().find(".py") != std::string::npos);
71+
close(fd);
72+
}
73+
74+
CASE("Reading invalid tar file")
75+
{
76+
tar::Reader r;
77+
struct stat st;
78+
int res = stat("test-invalid.tar", &st);
79+
EXPECT(res != -1);
80+
size_t size = st.st_size;
81+
int fd = open("test-invalid.tar", O_RDONLY);
82+
EXPECT_NOT(fd == -1);
83+
const uint8_t *mem = (const uint8_t *)mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
84+
EXPECT_THROWS_AS(tar::Tar tar = r.read_uncompressed(mem, size), tar::Tar_exception);
85+
close(fd);
86+
}

0 commit comments

Comments
 (0)