mirror of
https://github.com/clementine-player/Clementine
synced 2025-01-31 03:27:40 +01:00
Add truncation support to Zeroconf with test.
This commit is contained in:
parent
ebd2b1ecba
commit
def697c31a
@ -14,6 +14,8 @@
|
||||
#include "tinysvcmdns.h"
|
||||
#endif
|
||||
|
||||
#include <QTextCodec>
|
||||
|
||||
Zeroconf* Zeroconf::sInstance = NULL;
|
||||
|
||||
Zeroconf::~Zeroconf() {
|
||||
@ -35,3 +37,18 @@ Zeroconf* Zeroconf::GetZeroconf() {
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
QByteArray Zeroconf::TruncateName(const QString& name) {
|
||||
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
|
||||
QByteArray truncated_utf8;
|
||||
foreach (QChar c, name) {
|
||||
QByteArray rendered = codec->fromUnicode(&c, 1, NULL);
|
||||
if (truncated_utf8.size() + rendered.size() >= 63) {
|
||||
break;
|
||||
}
|
||||
truncated_utf8 += rendered;
|
||||
}
|
||||
// NULL-terminate the string.
|
||||
truncated_utf8.append('\0');
|
||||
return truncated_utf8;
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ class Zeroconf {
|
||||
|
||||
static Zeroconf* GetZeroconf();
|
||||
|
||||
// Truncate a QString to 63 bytes of UTF-8.
|
||||
static QByteArray TruncateName(const QString& name);
|
||||
|
||||
private:
|
||||
static Zeroconf* sInstance;
|
||||
};
|
||||
|
@ -143,6 +143,7 @@ add_test_file(utilities_test.cpp false)
|
||||
#add_test_file(xspfparser_test.cpp false)
|
||||
add_test_file(closure_test.cpp false)
|
||||
add_test_file(concurrentrun_test.cpp false)
|
||||
add_test_file(zeroconf_test.cpp false)
|
||||
|
||||
#if(LINUX AND HAVE_DBUS)
|
||||
# add_test_file(mpris1_test.cpp true)
|
||||
|
42
tests/zeroconf_test.cpp
Normal file
42
tests/zeroconf_test.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "networkremote/zeroconf.h"
|
||||
|
||||
namespace {
|
||||
|
||||
static const char k64CharAscii[] =
|
||||
"aaaaaaaaaaaaaaaa"
|
||||
"aaaaaaaaaaaaaaaa"
|
||||
"aaaaaaaaaaaaaaaa"
|
||||
"aaaaaaaaaaaaaaaa";
|
||||
|
||||
static const char kShortMultiByteString[] =
|
||||
"我会说一点汉语";
|
||||
|
||||
static const char kLongMultiByteString[] =
|
||||
"我会说一点汉语"
|
||||
"我会说一点汉语"
|
||||
"我会说一点汉语"
|
||||
"我会说一点汉语";
|
||||
|
||||
TEST(ZeroconfTest, TruncatesAscii) {
|
||||
QByteArray truncated = Zeroconf::TruncateName(
|
||||
QString::fromAscii(k64CharAscii));
|
||||
EXPECT_EQ(63, truncated.size());
|
||||
EXPECT_TRUE(truncated.endsWith('\0'));
|
||||
}
|
||||
|
||||
TEST(ZeroconfTest, DoesNotTruncateShortMultiByteUTF8) {
|
||||
EXPECT_EQ(
|
||||
sizeof(kShortMultiByteString),
|
||||
Zeroconf::TruncateName(QString::fromUtf8(kShortMultiByteString)).size());
|
||||
}
|
||||
|
||||
TEST(ZeroconfTest, TruncatesLongMultiByteUTF8) {
|
||||
QByteArray truncated = Zeroconf::TruncateName(
|
||||
QString::fromAscii(kLongMultiByteString));
|
||||
EXPECT_LE(63, truncated.size());
|
||||
EXPECT_TRUE(truncated.endsWith('\0'));
|
||||
}
|
||||
|
||||
} // namespace
|
Loading…
x
Reference in New Issue
Block a user