[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [dferry] /: Add tests for size limits in Arguments::Writer.
From:       Andreas Hartmetz <ahartmetz () gmail ! com>
Date:       2016-10-12 0:57:08
Message-ID: E1bu7r2-0007PZ-0x () code ! kde ! org
[Download RAW message or body]

Git commit b20dde6a993dbc736aac63ae3d4e749502f720bd by Andreas Hartmetz.
Committed on 11/10/2016 at 22:08.
Pushed by ahartmetz into branch 'master'.

Add tests for size limits in Arguments::Writer.

I don't see a good way to make them fast like the other tests, so
instead put them into a separate file. Maybe it's even a good idea
to exclude them from the "test" target to be able to run all other
tests that aren't horribly slow.
Note: slow means that depending on machine (only decently fast x86
considered) and debug / release, they take about 2-30 seconds.

M  +4    -0    serialization/arguments.cpp
M  +1    -1    tests/serialization/CMakeLists.txt
M  +0    -2    tests/serialization/tst_arguments.cpp
A  +102  -0    tests/serialization/tst_arguments_slow.cpp     [License: LGPL (v2+)]

http://commits.kde.org/dferry/b20dde6a993dbc736aac63ae3d4e749502f720bd

diff --git a/serialization/arguments.cpp b/serialization/arguments.cpp
index f42f9cc..1e9b97c 100644
--- a/serialization/arguments.cpp
+++ b/serialization/arguments.cpp
@@ -2418,6 +2418,10 @@ void Arguments::Writer::finishInternal()
             }
         }
 
+        // OK, so this length check is more like a sanity check. The actual limit is \
about the size of the +        // full message. Here we take the size of the \
"payload" plus the signature string and its alignment +        // padding. However, \
messages have a header of nontrivial size, so our "estimate" of full message +        \
// size is still too small.  if (success && bufferPos > SpecMaxMessageLength) {
             success = false;
             d->m_error.setCode(Error::ArgumentsTooLong);
diff --git a/tests/serialization/CMakeLists.txt b/tests/serialization/CMakeLists.txt
index 9162249..e03f0ee 100644
--- a/tests/serialization/CMakeLists.txt
+++ b/tests/serialization/CMakeLists.txt
@@ -6,4 +6,4 @@ MACRO(SERIALIZATIONTESTS)
     ENDFOREACH(_testname)
 ENDMACRO(SERIALIZATIONTESTS)
 
-SERIALIZATIONTESTS(arguments message)
+SERIALIZATIONTESTS(arguments arguments_slow message)
diff --git a/tests/serialization/tst_arguments.cpp \
b/tests/serialization/tst_arguments.cpp index 50c5a14..5c4a3af 100644
--- a/tests/serialization/tst_arguments.cpp
+++ b/tests/serialization/tst_arguments.cpp
@@ -1537,8 +1537,6 @@ int main(int, char *[])
     test_nesting();
     test_roundtrip();
     test_writerMisuse();
-    // TODO test_maxArrayLength
-    // TODO test_maxFullLength
     // TODO test arrays where array length does not align with end of an element
     //      (corruption of serialized data)
     test_complicated();
diff --git a/tests/serialization/tst_arguments_slow.cpp \
b/tests/serialization/tst_arguments_slow.cpp new file mode 100644
index 0000000..f88fb45
--- /dev/null
+++ b/tests/serialization/tst_arguments_slow.cpp
@@ -0,0 +1,102 @@
+/*
+   Copyright (C) 2016 Andreas Hartmetz <ahartmetz@gmail.com>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public
+   License as published by the Free Software Foundation; either
+   version 2 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LGPL.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+
+   Alternatively, this file is available under the Mozilla Public License
+   Version 1.1.  You may obtain a copy of the License at
+   http://www.mozilla.org/MPL/
+*/
+
+#include "arguments.h"
+
+#include "../testutil.h"
+
+#include <iostream>
+
+// ### should probably be officially exposed by the API
+enum {
+    SpecMaxArrayLength = 67108864, // 64 MiB
+    SpecMaxMessageLength = 134217728 // 128 MiB
+};
+
+static void test_payloadLengths()
+{
+    const uint32 maxInt32Count = SpecMaxArrayLength / sizeof(uint32);
+    {
+        Arguments::Writer writer;
+        writer.beginArray();
+        for (uint32 i = 0; i < maxInt32Count; i++) {
+            writer.writeUint32(i);
+        }
+        writer.endArray();
+        Arguments arg = writer.finish();
+        TEST(writer.state() == Arguments::Finished);
+    }
+    {
+        Arguments::Writer writer;
+        writer.beginArray();
+        for (uint32 i = 0; i < maxInt32Count + 1; i++) {
+            writer.writeUint32(i);
+        }
+        writer.endArray();
+        // The goal of the next check is to ensure that bevior doesn't change by \
accident, even +        // though the behavior may be dubious. Because it is dubious, \
it may still get changed. +        TEST(writer.state() != Arguments::InvalidData);
+        Arguments arg = writer.finish();
+        TEST(writer.state() == Arguments::InvalidData);
+    }
+    // Note: No need to test dicts, they are implemented pretty much like arrays \
regarding limits +
+    // The following two tests are overspecific to the implementation - it can only \
"guess" the full final +    // message size because it simply isn't known in the \
ArgumentList. Still better than nothing. +    {
+        Arguments::Writer writer;
+        for (uint32 i = 0; i < 2; i++) {
+            writer.beginArray();
+            // -2 because array length prefix adds one uint32 per array, and we must \
also subtract +            // (signature + alignment padding to 8 byte boundary), \
i.e. 8 bytes, / 2 = one more +            // sizeof(uint32) per array.
+            for (uint32 j = 0; j < maxInt32Count - 2; j++) {
+                writer.writeUint32(j);
+            }
+            writer.endArray();
+        }
+        TEST(writer.state() != Arguments::InvalidData);
+        Arguments arg = writer.finish();
+        TEST(writer.state() == Arguments::Finished);
+    }
+    {
+        Arguments::Writer writer;
+        for (uint32 i = 0; i < 2; i++) {
+            writer.beginArray();
+            for (uint32 j = 0; j < maxInt32Count - 1; j++) {
+                writer.writeUint32(j);
+            }
+            writer.endArray();
+        }
+        writer.writeByte(123); // one byte too many!
+        TEST(writer.state() != Arguments::InvalidData);
+        Arguments arg = writer.finish();
+        TEST(writer.state() == Arguments::InvalidData);
+    }
+}
+
+int main(int, char *[])
+{
+    test_payloadLengths();
+    std::cout << "Passed!\n";
+}


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic