diff options
author | Mike Frysinger <vapier@google.com> | 2020-09-08 16:47:39 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-09-08 16:47:39 +0000 |
commit | d85877289aa0d2517c02691be07bd251f4eafc91 (patch) | |
tree | f1bcd6f836f9e96aed2d6907e4166e5cace0bc4f | |
parent | c4824f9d8be75e72f9a1967bf8ab0f027c7b492e (diff) | |
parent | 70b15b82a705bfbf74f80bdee243ae486e52d033 (diff) | |
download | platform_external_minijail-d85877289aa0d2517c02691be07bd251f4eafc91.tar.gz platform_external_minijail-d85877289aa0d2517c02691be07bd251f4eafc91.tar.bz2 platform_external_minijail-d85877289aa0d2517c02691be07bd251f4eafc91.zip |
util: move unittests to the right module am: 70b15b82a7
Original change: https://android-review.googlesource.com/c/platform/external/minijail/+/1419757
Change-Id: Iff87f2fda9cb4c6f7477c2ee57e334a2815a4f02
-rw-r--r-- | libminijail_unittest.cc | 52 | ||||
-rw-r--r-- | syscall_filter_unittest.cc | 114 | ||||
-rw-r--r-- | util_unittest.cc | 179 |
3 files changed, 179 insertions, 166 deletions
diff --git a/libminijail_unittest.cc b/libminijail_unittest.cc index c6cc0c84..ed4cbaf4 100644 --- a/libminijail_unittest.cc +++ b/libminijail_unittest.cc @@ -1050,58 +1050,6 @@ TEST_F(NamespaceTest, test_enter_ns) { } } -TEST(Test, parse_size) { - size_t size; - - ASSERT_EQ(0, parse_size(&size, "42")); - ASSERT_EQ(42U, size); - - ASSERT_EQ(0, parse_size(&size, "16K")); - ASSERT_EQ(16384U, size); - - ASSERT_EQ(0, parse_size(&size, "1M")); - ASSERT_EQ(1024U * 1024, size); - - uint64_t gigabyte = 1024ULL * 1024 * 1024; - ASSERT_EQ(0, parse_size(&size, "3G")); - ASSERT_EQ(3U, size / gigabyte); - ASSERT_EQ(0U, size % gigabyte); - - ASSERT_EQ(0, parse_size(&size, "4294967294")); - ASSERT_EQ(3U, size / gigabyte); - ASSERT_EQ(gigabyte - 2, size % gigabyte); - -#if __WORDSIZE == 64 - uint64_t exabyte = gigabyte * 1024 * 1024 * 1024; - ASSERT_EQ(0, parse_size(&size, "9E")); - ASSERT_EQ(9U, size / exabyte); - ASSERT_EQ(0U, size % exabyte); - - ASSERT_EQ(0, parse_size(&size, "15E")); - ASSERT_EQ(15U, size / exabyte); - ASSERT_EQ(0U, size % exabyte); - - ASSERT_EQ(0, parse_size(&size, "18446744073709551614")); - ASSERT_EQ(15U, size / exabyte); - ASSERT_EQ(exabyte - 2, size % exabyte); - - ASSERT_EQ(-ERANGE, parse_size(&size, "16E")); - ASSERT_EQ(-ERANGE, parse_size(&size, "19E")); - ASSERT_EQ(-EINVAL, parse_size(&size, "7GTPE")); -#elif __WORDSIZE == 32 - ASSERT_EQ(-ERANGE, parse_size(&size, "5G")); - ASSERT_EQ(-ERANGE, parse_size(&size, "9G")); - ASSERT_EQ(-ERANGE, parse_size(&size, "9E")); - ASSERT_EQ(-ERANGE, parse_size(&size, "7GTPE")); -#endif - - ASSERT_EQ(-EINVAL, parse_size(&size, "")); - ASSERT_EQ(-EINVAL, parse_size(&size, "14u")); - ASSERT_EQ(-EINVAL, parse_size(&size, "14.2G")); - ASSERT_EQ(-EINVAL, parse_size(&size, "-1G")); - ASSERT_EQ(-EINVAL, parse_size(&size, "; /bin/rm -- ")); -} - void TestCreateSession(bool create_session) { int status; int pipe_fds[2]; diff --git a/syscall_filter_unittest.cc b/syscall_filter_unittest.cc index 771dced3..8ead46c2 100644 --- a/syscall_filter_unittest.cc +++ b/syscall_filter_unittest.cc @@ -88,120 +88,6 @@ struct filter_block* test_compile_policy_line( } // namespace -TEST(util, parse_constant_unsigned) { - char *end; - long int c = 0; - std::string constant; - -#if defined(BITS32) - constant = "0x80000000"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(0x80000000U, static_cast<unsigned long int>(c)); - -#elif defined(BITS64) - constant = "0x8000000000000000"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(0x8000000000000000UL, static_cast<unsigned long int>(c)); -#endif -} - -TEST(util, parse_constant_unsigned_toobig) { - char *end; - long int c = 0; - std::string constant; - -#if defined(BITS32) - constant = "0x100000000"; // Too big for 32-bit unsigned long int. - c = parse_constant(const_cast<char*>(constant.data()), &end); - // Error case should return 0. - EXPECT_EQ(0, c); - -#elif defined(BITS64) - constant = "0x10000000000000000"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - // Error case should return 0. - EXPECT_EQ(0, c); -#endif -} - -TEST(util, parse_constant_signed) { - char *end; - long int c = 0; - std::string constant = "-1"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(-1, c); -} - -TEST(util, parse_constant_signed_toonegative) { - char *end; - long int c = 0; - std::string constant; - -#if defined(BITS32) - constant = "-0x80000001"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - // Error case should return 0. - EXPECT_EQ(0, c); - -#elif defined(BITS64) - constant = "-0x8000000000000001"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - // Error case should return 0. - EXPECT_EQ(0, c); -#endif -} - -TEST(util, parse_constant_complements) { - char* end; - long int c = 0; - std::string constant; - -#if defined(BITS32) - constant = "~0x005AF0FF|~0xFFA50FFF"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(c, 0xFFFFFF00); - constant = "0x0F|~(0x005AF000|0x00A50FFF)|0xF0"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(c, 0xFF0000FF); - -#elif defined(BITS64) - constant = "~0x00005A5AF0F0FFFF|~0xFFFFA5A50F0FFFFF"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(c, 0xFFFFFFFFFFFF0000UL); - constant = "0x00FF|~(0x00005A5AF0F00000|0x0000A5A50F0FFFFF)|0xFF00"; - c = parse_constant(const_cast<char*>(constant.data()), &end); - EXPECT_EQ(c, 0xFFFF00000000FFFFUL); -#endif -} - -TEST(util, parse_parenthesized_expresions) { - char* end; - - const std::vector<const char*> bad_expressions = { - "(1", "1)", "(1)1", "|(1)", "(1)|", "()", - "(", "((", "(()", "(()1", "1(0)", - }; - for (const auto* expression : bad_expressions) { - std::string mutable_expression = expression; - long int c = - parse_constant(const_cast<char*>(mutable_expression.data()), &end); - EXPECT_EQ(reinterpret_cast<const void*>(end), - reinterpret_cast<const void*>(mutable_expression.data())); - // Error case should return 0. - EXPECT_EQ(c, 0) << "For expression: \"" << expression << "\""; - } - - const std::vector<const char*> good_expressions = { - "(3)", "(1)|2", "1|(2)", "(1)|(2)", "((3))", "0|(1|2)", "(0|1|2)", - }; - for (const auto* expression : good_expressions) { - std::string mutable_expression = expression; - long int c = - parse_constant(const_cast<char*>(mutable_expression.data()), &end); - EXPECT_EQ(c, 3) << "For expression: \"" << expression << "\""; - } -} - /* Test that setting one BPF instruction works. */ TEST(bpf, set_bpf_instr) { struct sock_filter instr; diff --git a/util_unittest.cc b/util_unittest.cc index ab4805a9..f84df8e2 100644 --- a/util_unittest.cc +++ b/util_unittest.cc @@ -13,6 +13,7 @@ #include <gtest/gtest.h> +#include "bpf.h" #include "util.h" namespace { @@ -158,3 +159,181 @@ TEST(environment, copy_and_modify) { minijail_free_env(env); } + +TEST(parse_constant, unsigned) { + char *end; + long int c = 0; + std::string constant; + +#if defined(BITS32) + constant = "0x80000000"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(0x80000000U, static_cast<unsigned long int>(c)); + +#elif defined(BITS64) + constant = "0x8000000000000000"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(0x8000000000000000UL, static_cast<unsigned long int>(c)); + +#else +# error "unknown bits!" +#endif +} + +TEST(parse_constant, unsigned_toobig) { + char *end; + long int c = 0; + std::string constant; + +#if defined(BITS32) + constant = "0x100000000"; // Too big for 32-bit unsigned long int. + c = parse_constant(const_cast<char*>(constant.data()), &end); + // Error case should return 0. + EXPECT_EQ(0, c); + +#elif defined(BITS64) + constant = "0x10000000000000000"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + // Error case should return 0. + EXPECT_EQ(0, c); + +#else +# error "unknown bits!" +#endif +} + +TEST(parse_constant, signed) { + char *end; + long int c = 0; + std::string constant = "-1"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(-1, c); +} + +TEST(parse_constant, signed_toonegative) { + char *end; + long int c = 0; + std::string constant; + +#if defined(BITS32) + constant = "-0x80000001"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + // Error case should return 0. + EXPECT_EQ(0, c); + +#elif defined(BITS64) + constant = "-0x8000000000000001"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + // Error case should return 0. + EXPECT_EQ(0, c); + +#else +# error "unknown bits!" +#endif +} + +TEST(parse_constant, complements) { + char* end; + long int c = 0; + std::string constant; + +#if defined(BITS32) + constant = "~0x005AF0FF|~0xFFA50FFF"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(c, 0xFFFFFF00); + constant = "0x0F|~(0x005AF000|0x00A50FFF)|0xF0"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(c, 0xFF0000FF); + +#elif defined(BITS64) + constant = "~0x00005A5AF0F0FFFF|~0xFFFFA5A50F0FFFFF"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(c, 0xFFFFFFFFFFFF0000UL); + constant = "0x00FF|~(0x00005A5AF0F00000|0x0000A5A50F0FFFFF)|0xFF00"; + c = parse_constant(const_cast<char*>(constant.data()), &end); + EXPECT_EQ(c, 0xFFFF00000000FFFFUL); + +#else +# error "unknown bits!" +#endif +} + +TEST(parse_constant, parenthesized_expresions) { + char* end; + + const std::vector<const char*> bad_expressions = { + "(1", "1)", "(1)1", "|(1)", "(1)|", "()", + "(", "((", "(()", "(()1", "1(0)", + }; + for (const auto* expression : bad_expressions) { + std::string mutable_expression = expression; + long int c = + parse_constant(const_cast<char*>(mutable_expression.data()), &end); + EXPECT_EQ(reinterpret_cast<const void*>(end), + reinterpret_cast<const void*>(mutable_expression.data())); + // Error case should return 0. + EXPECT_EQ(c, 0) << "For expression: \"" << expression << "\""; + } + + const std::vector<const char*> good_expressions = { + "(3)", "(1)|2", "1|(2)", "(1)|(2)", "((3))", "0|(1|2)", "(0|1|2)", + }; + for (const auto* expression : good_expressions) { + std::string mutable_expression = expression; + long int c = + parse_constant(const_cast<char*>(mutable_expression.data()), &end); + EXPECT_EQ(c, 3) << "For expression: \"" << expression << "\""; + } +} + +TEST(parse_size, complete) { + size_t size; + + ASSERT_EQ(0, parse_size(&size, "42")); + ASSERT_EQ(42U, size); + + ASSERT_EQ(0, parse_size(&size, "16K")); + ASSERT_EQ(16384U, size); + + ASSERT_EQ(0, parse_size(&size, "1M")); + ASSERT_EQ(1024U * 1024, size); + + uint64_t gigabyte = 1024ULL * 1024 * 1024; + ASSERT_EQ(0, parse_size(&size, "3G")); + ASSERT_EQ(3U, size / gigabyte); + ASSERT_EQ(0U, size % gigabyte); + + ASSERT_EQ(0, parse_size(&size, "4294967294")); + ASSERT_EQ(3U, size / gigabyte); + ASSERT_EQ(gigabyte - 2, size % gigabyte); + +#if __WORDSIZE == 64 + uint64_t exabyte = gigabyte * 1024 * 1024 * 1024; + ASSERT_EQ(0, parse_size(&size, "9E")); + ASSERT_EQ(9U, size / exabyte); + ASSERT_EQ(0U, size % exabyte); + + ASSERT_EQ(0, parse_size(&size, "15E")); + ASSERT_EQ(15U, size / exabyte); + ASSERT_EQ(0U, size % exabyte); + + ASSERT_EQ(0, parse_size(&size, "18446744073709551614")); + ASSERT_EQ(15U, size / exabyte); + ASSERT_EQ(exabyte - 2, size % exabyte); + + ASSERT_EQ(-ERANGE, parse_size(&size, "16E")); + ASSERT_EQ(-ERANGE, parse_size(&size, "19E")); + ASSERT_EQ(-EINVAL, parse_size(&size, "7GTPE")); +#elif __WORDSIZE == 32 + ASSERT_EQ(-ERANGE, parse_size(&size, "5G")); + ASSERT_EQ(-ERANGE, parse_size(&size, "9G")); + ASSERT_EQ(-ERANGE, parse_size(&size, "9E")); + ASSERT_EQ(-ERANGE, parse_size(&size, "7GTPE")); +#endif + + ASSERT_EQ(-EINVAL, parse_size(&size, "")); + ASSERT_EQ(-EINVAL, parse_size(&size, "14u")); + ASSERT_EQ(-EINVAL, parse_size(&size, "14.2G")); + ASSERT_EQ(-EINVAL, parse_size(&size, "-1G")); + ASSERT_EQ(-EINVAL, parse_size(&size, "; /bin/rm -- ")); +} |