summaryrefslogtreecommitdiff
path: root/test/libapt
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2014-10-24 23:55:15 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2014-10-26 14:13:34 +0100
commitd8c71b3b5dc98daa247433503ad8242c9e7b77db (patch)
tree64d520c6b863554d0c593bedbcc010ce5c0a5a0b /test/libapt
parent23397c9d7d4d455461176600bb45c81185493504 (diff)
rewrite ReadMessages()
Central methods of our infrastructure like this one responsible for communication with our methods shouldn't be more complicated then they have to and not claim to have (albeit unlikely) bugs. While I am not sure about having improved the first part, the bug is now gone and a few explicit tests check that it stays that way, so nobody will notice the difference (hopefully) – expect that this should a very tiny bit faster as well as we don't manually proceed through the string. Git-Dch: Ignore
Diffstat (limited to 'test/libapt')
-rw-r--r--test/libapt/strutil_test.cc70
1 files changed, 70 insertions, 0 deletions
diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc
index 494159c87..9bc3c76fd 100644
--- a/test/libapt/strutil_test.cc
+++ b/test/libapt/strutil_test.cc
@@ -1,10 +1,13 @@
#include <config.h>
#include <apt-pkg/strutl.h>
+#include <apt-pkg/fileutl.h>
#include <string>
#include <vector>
#include <gtest/gtest.h>
+#include "file-helpers.h"
+
TEST(StrUtilTest,DeEscapeString)
{
// nothing special
@@ -143,3 +146,70 @@ TEST(StrUtilTest,Base64Encode)
EXPECT_EQ("Lg==", Base64Encode("."));
EXPECT_EQ("", Base64Encode(""));
}
+void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab)
+{
+ SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab);
+ FileFd fd;
+ std::string pkgA = "Package: pkgA\n"
+ "Version: 1\n"
+ "Size: 100\n"
+ "Description: aaa\n"
+ " aaa";
+ std::string pkgB = "Package: pkgB\n"
+ "Version: 1\n"
+ "Flag: no\n"
+ "Description: bbb";
+ std::string pkgC = "Package: pkgC\n"
+ "Version: 2\n"
+ "Flag: yes\n"
+ "Description:\n"
+ " ccc";
+
+ createTemporaryFile("readmessage", fd, NULL, (pkgA + nl + pkgB + nl + pkgC + nl).c_str());
+ std::vector<std::string> list;
+ EXPECT_TRUE(ReadMessages(fd.Fd(), list));
+ EXPECT_EQ(3, list.size());
+ EXPECT_EQ(pkgA, list[0]);
+ EXPECT_EQ(pkgB, list[1]);
+ EXPECT_EQ(pkgC, list[2]);
+
+ size_t const msgsize = 63990;
+ createTemporaryFile("readmessage", fd, NULL, NULL);
+ for (size_t j = 0; j < msgsize; ++j)
+ fd.Write(ab, strlen(ab));
+ for (size_t i = 0; i < 21; ++i)
+ {
+ std::string msg;
+ strprintf(msg, "msgsize=%zu i=%zu", msgsize, i);
+ SCOPED_TRACE(msg);
+ fd.Seek((msgsize + (i - 1)) * strlen(ab));
+ fd.Write(ab, strlen(ab));
+ fd.Write(nl, strlen(nl));
+ fd.Seek(0);
+ list.clear();
+ EXPECT_TRUE(ReadMessages(fd.Fd(), list));
+ EXPECT_EQ(1, list.size());
+ EXPECT_EQ((msgsize + i) * strlen(ab), list[0].length());
+ EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
+ }
+
+ list.clear();
+ fd.Write(pkgA.c_str(), pkgA.length());
+ fd.Write(nl, strlen(nl));
+ fd.Seek(0);
+ EXPECT_TRUE(ReadMessages(fd.Fd(), list));
+ EXPECT_EQ(2, list.size());
+ EXPECT_EQ((msgsize + 20) * strlen(ab), list[0].length());
+ EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab));
+ EXPECT_EQ(pkgA, list[1]);
+
+
+ fd.Close();
+}
+TEST(StrUtilTest,ReadMessages)
+{
+ ReadMessagesTestWithNewLine("\n\n", "a");
+ ReadMessagesTestWithNewLine("\r\n\r\n", "a");
+ ReadMessagesTestWithNewLine("\n\n", "ab");
+ ReadMessagesTestWithNewLine("\r\n\r\n", "ab");
+}