Avoid potential negative array index access to cached text.
[LibreOffice.git] / svl / qa / unit / test_INetContentType.cxx
blob288cfe919008bd7db25d2e76de2e8ad036569ae5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <cstring>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 #include <cppunit/plugin/TestPlugIn.h>
18 #include <rtl/ustring.hxx>
19 #include <svl/inettype.hxx>
20 #include <tools/inetmime.hxx>
22 namespace {
24 class Test: public CppUnit::TestFixture {
25 public:
26 void testBad();
28 void testFull();
30 void testFollow();
32 CPPUNIT_TEST_SUITE(Test);
33 CPPUNIT_TEST(testBad);
34 CPPUNIT_TEST(testFull);
35 CPPUNIT_TEST(testFollow);
36 CPPUNIT_TEST_SUITE_END();
39 void Test::testBad() {
40 OUString in("foo=bar");
41 CPPUNIT_ASSERT_EQUAL(
42 static_cast<void const *>(nullptr),
43 static_cast<void const *>(INetMIME::scanContentType(in)));
44 OUString t;
45 OUString s;
46 INetContentTypeParameterList ps;
47 CPPUNIT_ASSERT(!INetContentTypes::parse(in, t, s, &ps));
48 CPPUNIT_ASSERT(t.isEmpty());
49 CPPUNIT_ASSERT(s.isEmpty());
50 CPPUNIT_ASSERT(bool(ps.end() == ps.find("foo"_ostr)));
53 void Test::testFull() {
54 OUString in("foo/bar;baz=boz");
55 CPPUNIT_ASSERT_EQUAL(
56 static_cast<void const *>(in.getStr() + in.getLength()),
57 static_cast<void const *>(INetMIME::scanContentType(in)));
58 OUString t;
59 OUString s;
60 INetContentTypeParameterList ps;
61 CPPUNIT_ASSERT(INetContentTypes::parse(in, t, s, &ps));
62 CPPUNIT_ASSERT_EQUAL(OUString("foo"), t);
63 CPPUNIT_ASSERT_EQUAL(OUString("bar"), s);
64 auto iter = ps.find("baz"_ostr);
65 CPPUNIT_ASSERT(iter != ps.end());
66 CPPUNIT_ASSERT_EQUAL(OUString("boz"), iter->second.m_sValue);
69 void Test::testFollow() {
70 OUString in("foo/bar;baz=boz;base64,");
71 CPPUNIT_ASSERT_EQUAL(
72 static_cast<void const *>(in.getStr() + std::strlen("foo/bar;baz=boz")),
73 static_cast<void const *>(INetMIME::scanContentType(in)));
74 OUString t;
75 OUString s;
76 INetContentTypeParameterList ps;
77 CPPUNIT_ASSERT(!INetContentTypes::parse(in, t, s));
78 CPPUNIT_ASSERT(t.isEmpty());
79 CPPUNIT_ASSERT(s.isEmpty());
80 CPPUNIT_ASSERT(bool(ps.end() == ps.find("baz"_ostr)));
83 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
87 CPPUNIT_PLUGIN_IMPLEMENT();
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */