"Fossies" - the Fresh Open Source Software Archive

Member "osquery-4.3.0/osquery/tables/system/tests/posix/apt_sources_tests.cpp" (14 Apr 2020, 2571 Bytes) of package /linux/misc/osquery-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "apt_sources_tests.cpp": 4.3.0_vs_4.4.0.

    1 /**
    2  *  Copyright (c) 2014-present, Facebook, Inc.
    3  *  All rights reserved.
    4  *
    5  *  This source code is licensed in accordance with the terms specified in
    6  *  the LICENSE file found in the root directory of this source tree.
    7  */
    8 
    9 #include <fstream>
   10 
   11 #include <osquery/tables/system/posix/apt_sources.h>
   12 
   13 #include <boost/filesystem.hpp>
   14 
   15 #include <gtest/gtest.h>
   16 
   17 //#include <osquery/tables/system/posix/known_hosts.h>
   18 //#include <osquery/utils/scope_guard.h>
   19 
   20 namespace osquery {
   21 namespace tables {
   22 
   23 class AptSourcesImplTests : public testing::Test {};
   24 
   25 struct AptTestCase {
   26   std::string input_line;
   27   std::string base_uri;
   28   std::string name;
   29   std::string cache_filename;
   30 };
   31 
   32 TEST_F(AptSourcesImplTests, parse_apt_source_line) {
   33   std::vector<AptTestCase> test_cases = {
   34       // "deb " is stripped off.
   35       {"[arch=amd64] https://pkg.osquery.io/deb deb main",
   36        "https://pkg.osquery.io/deb",
   37        "pkg.osquery.io/deb deb main",
   38        "pkg.osquery.io_deb_dists_deb"},
   39       // "dists" is not used as a section.
   40       {"https://pkg.osquery.io apt/source/",
   41        "https://pkg.osquery.io",
   42        "pkg.osquery.io apt source",
   43        "pkg.osquery.io_apt_source"},
   44       // trailing slashes are stripped from URI.s
   45       {"https://pkg.osquery.io/// apt/source/",
   46        "https://pkg.osquery.io///",
   47        "pkg.osquery.io apt source",
   48        "pkg.osquery.io_apt_source"},
   49       // trailing comments are ok
   50       {"https://pkg.osquery.io deb main # main",
   51        "https://pkg.osquery.io",
   52        "pkg.osquery.io deb main",
   53        "pkg.osquery.io_dists_deb"},
   54   };
   55 
   56   for (const auto& test_case : test_cases) {
   57     AptSource apt_source;
   58 
   59     auto s = parseAptSourceLine(test_case.input_line, apt_source);
   60     ASSERT_TRUE(s.ok()) << "Failed with " << s.getMessage();
   61 
   62     EXPECT_EQ(apt_source.base_uri, test_case.base_uri);
   63     EXPECT_EQ(apt_source.name, test_case.name);
   64 
   65     auto cache_filename = getCacheFilename(apt_source.cache_file);
   66     EXPECT_EQ(cache_filename, test_case.cache_filename);
   67   }
   68 }
   69 
   70 TEST_F(AptSourcesImplTests, test_failures) {
   71   AptSource apt_source;
   72   auto s = parseAptSourceLine("https://", apt_source);
   73   EXPECT_FALSE(s.ok()) << "Protocol-only line is invalid";
   74 
   75   s = parseAptSourceLine("pkg.osquery.io", apt_source);
   76   EXPECT_FALSE(s.ok()) << "Protocol is required";
   77 
   78   s = parseAptSourceLine("", apt_source);
   79   ASSERT_FALSE(s.ok()) << "Empty line is invalid";
   80 
   81   s = parseAptSourceLine(" # comment https://pkg.osquery.io", apt_source);
   82   ASSERT_FALSE(s.ok()) << "Comment line is invalid";
   83 }
   84 
   85 } // namespace tables
   86 } // namespace osquery