From d06e7a04c43dad29d8bd79ec6f98af07add0181f Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 11 Oct 2020 14:33:04 -0400 Subject: Add unit tests for encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They don’t currently pass, because they expose some bugs in encoding. --- .gitignore | 1 + build.ninja | 5 ++- encoding_test.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 encoding_test.cc diff --git a/.gitignore b/.gitignore index 723499b..c4dd684 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Ninja .ninja_* *.o +*_test scoville # Local Variables: diff --git a/build.ninja b/build.ninja index d61cc39..a20615b 100644 --- a/build.ninja +++ b/build.ninja @@ -21,7 +21,6 @@ cflags = -O2 -DNDEBUG -D_FORTIFY_SOURCE -D_GLIBCXX_DEBUG $ -fno-rtti -fuse-cxa-atexit -pipe -ffunction-sections -fdata-sections $ -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse ldflags = -Wl,--gc-sections -Wl,-Bsymbolic-functions -pie -Wl,-z,now -Wl,-z,relro -Wl,--hash-style=gnu -Wl,--no-copy-dt-needed-entries -Wl,--as-needed -pthread -libs = -lfuse -lglog -lgflags rule cxx command = $cxx -MMD -MT $out -MF $out.d $cflags -c $in -o $out @@ -34,8 +33,12 @@ rule link description = LINK $out build encoding.o: cxx encoding.cc +build encoding_test.o: cxx encoding_test.cc build operations.o: cxx operations.cc build posix_extras.o: cxx posix_extras.cc build scoville.o: cxx scoville.cc +build encoding_test: link encoding.o encoding_test.o + libs = -lgtest -lgtest_main -labsl_str_format_internal -lglog build scoville: link encoding.o operations.o posix_extras.o scoville.o + libs = -lfuse -lglog -lgflags diff --git a/encoding_test.cc b/encoding_test.cc new file mode 100644 index 0000000..9dad6b5 --- /dev/null +++ b/encoding_test.cc @@ -0,0 +1,98 @@ +// Copyright 2020 Benjamin Barenblat +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +#include "encoding.h" + +#include +#include + +namespace scoville { +namespace { + +const char kAllGoodCharacters[] = + " !#$&'()+,-.0123456789;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqr" + "stuvwxyz{}~\177"; + +TEST(ScovilleEncodingTest, EncodesEmptyToEmpty) { EXPECT_EQ(Encode(""), ""); } + +TEST(ScovilleEncodingTest, EncodesBadCharacters) { + for (int i = 1; i < 0x20; ++i) { + EXPECT_EQ(Encode(absl::StrFormat("foo%cbar", i)), + absl::StrFormat("foo%%%02xbar", i)); + } + EXPECT_EQ(Encode("foo*bar"), "foo%2abar"); + EXPECT_EQ(Encode("foo?bar"), "foo%3fbar"); + EXPECT_EQ(Encode("foobar"), "foo%3ebar"); + EXPECT_EQ(Encode("foo|bar"), "foo%7cbar"); + EXPECT_EQ(Encode("foo\"bar"), "foo%22bar"); + EXPECT_EQ(Encode("foo:bar"), "foo%3abar"); + EXPECT_EQ(Encode("foo\\bar"), "foo%5cbar"); +} + +TEST(ScovilleEncodingTest, EncodesPercent) { + EXPECT_EQ(Encode("foo%bar"), "foo%%bar"); +} + +TEST(ScovilleEncodingTest, EncodesGoodCharacters) { + EXPECT_EQ(Encode(kAllGoodCharacters), kAllGoodCharacters); +} + +TEST(ScovilleEncodingTest, EncodesTrailingBadCharacters) { + EXPECT_EQ(Encode("foo."), "foo%2e"); + EXPECT_EQ(Encode("foo "), "foo%20"); +} + +TEST(ScovilleEncodingTest, EncodesDirectoryTrailingBadCharacters) { + EXPECT_EQ(Encode("foo./bar"), "foo%2e/bar"); + EXPECT_EQ(Encode("foo /bar"), "foo%20/bar"); +} + +TEST(ScovilleDecodingTest, DecodesEmptyToEmpty) { EXPECT_EQ(Decode(""), ""); } + +TEST(ScovilleDecodingTest, DecodesBadCharacters) { + for (int i = 1; i < 0x20; ++i) { + EXPECT_EQ(Decode(absl::StrFormat("foo%%%02xbar", i)), + absl::StrFormat("foo%cbar", i)); + } + EXPECT_EQ(Decode("foo%2abar"), "foo*bar"); + EXPECT_EQ(Decode("foo%3fbar"), "foo?bar"); + EXPECT_EQ(Decode("foo%3cbar"), "foobar"); + EXPECT_EQ(Decode("foo%7cbar"), "foo|bar"); + EXPECT_EQ(Decode("foo%22bar"), "foo\"bar"); + EXPECT_EQ(Decode("foo%3abar"), "foo:bar"); + EXPECT_EQ(Decode("foo%5cbar"), "foo\\bar"); +} + +TEST(ScovilleDecodingTest, DecodesPercent) { + EXPECT_EQ(Decode("foo%%bar"), "foo%bar"); +} + +TEST(ScovilleDecodingTest, DecodesGoodCharacters) { + EXPECT_EQ(Decode(kAllGoodCharacters), kAllGoodCharacters); +} + +TEST(ScovilleDecodingTest, DecodesTrailingBadCharacters) { + EXPECT_EQ(Decode("foo%2e"), "foo."); + EXPECT_EQ(Decode("foo%20"), "foo "); +} + +TEST(ScovilleDecodingTest, DecodesDirectoryTrailingBadCharacters) { + EXPECT_EQ(Decode("foo%2e/bar"), "foo./bar"); + EXPECT_EQ(Decode("foo%20/bar"), "foo /bar"); +} + +} // namespace +} // namespace scoville -- cgit v1.2.3