|
| 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