Esempio n. 1
0
 def test_serialize_readonly_stream(self):
     """Test serialize_stream with a read-only stream."""
     data = QByteArray()
     stream = QDataStream(data, QIODevice.ReadOnly)
     with pytest.raises(OSError, match="The data stream cannot write to "
                                       "the underlying device."):
         qtutils.serialize_stream(stream, QPoint())
Esempio n. 2
0
 def test_serialize_readonly_stream(self):
     """Test serialize_stream with a read-only stream."""
     data = QByteArray()
     stream = QDataStream(data, QIODevice.ReadOnly)
     with pytest.raises(OSError, match="The data stream cannot write to "
                                       "the underlying device."):
         qtutils.serialize_stream(stream, QPoint())
Esempio n. 3
0
 def test_serialize_readonly_stream(self):
     """Test serialize_stream with a read-only stream."""
     data = QByteArray()
     stream = QDataStream(data, QIODevice.ReadOnly)
     with pytest.raises(OSError) as excinfo:
         qtutils.serialize_stream(stream, QPoint())
     assert str(excinfo.value) == ("The data stream cannot write to the " "underlying device.")
Esempio n. 4
0
 def test_serialize_readonly_stream(self):
     """Test serialize_stream with a read-only stream."""
     data = QByteArray()
     stream = QDataStream(data, QIODevice.ReadOnly)
     with pytest.raises(OSError) as excinfo:
         qtutils.serialize_stream(stream, QPoint())
     assert str(excinfo.value) == ("The data stream cannot write to the "
                                   "underlying device.")
Esempio n. 5
0
    def test_serialize_pre_error_mock(self, stream_mock):
        """Test serialize_stream with an error already set."""
        stream_mock.status.return_value = QDataStream.ReadCorruptData

        with pytest.raises(OSError) as excinfo:
            qtutils.serialize_stream(stream_mock, QPoint())

        assert not stream_mock.__lshift__.called
        assert str(excinfo.value) == "The data stream has read corrupt data."
Esempio n. 6
0
    def test_serialize_pre_error_mock(self, stream_mock):
        """Test serialize_stream with an error already set."""
        stream_mock.status.return_value = QDataStream.ReadCorruptData

        with pytest.raises(OSError, match="The data stream has read corrupt "
                                          "data."):
            qtutils.serialize_stream(stream_mock, QPoint())

        assert not stream_mock.__lshift__.called
Esempio n. 7
0
    def test_serialize_post_error_mock(self, stream_mock):
        """Test serialize_stream with an error while serializing."""
        obj = QPoint()
        stream_mock.__lshift__.side_effect = lambda _other: self._set_status(stream_mock, QDataStream.ReadCorruptData)

        with pytest.raises(OSError) as excinfo:
            qtutils.serialize_stream(stream_mock, obj)

        assert stream_mock.__lshift__.called_once_with(obj)
        assert str(excinfo.value) == "The data stream has read corrupt data."
Esempio n. 8
0
    def test_serialize_post_error_mock(self, stream_mock):
        """Test serialize_stream with an error while serializing."""
        obj = QPoint()
        stream_mock.__lshift__.side_effect = lambda _other: self._set_status(
            stream_mock, QDataStream.ReadCorruptData)

        with pytest.raises(OSError, match="The data stream has read corrupt "
                                          "data."):
            qtutils.serialize_stream(stream_mock, obj)

        assert stream_mock.__lshift__.called_once_with(obj)
Esempio n. 9
0
    def test_round_trip_real_stream(self):
        """Test a round trip with a real QDataStream."""
        src_obj = QPoint(23, 42)
        dest_obj = QPoint()
        data = QByteArray()

        write_stream = QDataStream(data, QIODevice.WriteOnly)
        qtutils.serialize_stream(write_stream, src_obj)

        read_stream = QDataStream(data, QIODevice.ReadOnly)
        qtutils.deserialize_stream(read_stream, dest_obj)

        assert src_obj == dest_obj
Esempio n. 10
0
    def test_round_trip_real_stream(self):
        """Test a round trip with a real QDataStream."""
        src_obj = QPoint(23, 42)
        dest_obj = QPoint()
        data = QByteArray()

        write_stream = QDataStream(data, QIODevice.WriteOnly)
        qtutils.serialize_stream(write_stream, src_obj)

        read_stream = QDataStream(data, QIODevice.ReadOnly)
        qtutils.deserialize_stream(read_stream, dest_obj)

        assert src_obj == dest_obj
Esempio n. 11
0
def _serialize_item(item, stream):
    """Serialize a single WebHistoryItem into a QDataStream.

    Args:
        item: The WebHistoryItem to write.
        stream: The QDataStream to write to.
    """
    ### Thanks to Otter Browser:
    ### https://github.com/OtterBrowser/otter-browser/blob/v0.9.10/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp#L1210
    ### src/core/web_contents_adapter.cpp serializeNavigationHistory
    ## toQt(entry->GetVirtualURL());
    qtutils.serialize_stream(stream, item.url)
    ## toQt(entry->GetTitle());
    stream.writeQString(item.title)
    ## QByteArray(encodedPageState.data(), encodedPageState.size());
    qtutils.serialize_stream(stream, QByteArray())
    ## static_cast<qint32>(entry->GetTransitionType());
    # chromium/ui/base/page_transition_types.h
    stream.writeInt32(0)  # PAGE_TRANSITION_LINK
    ## entry->GetHasPostData();
    stream.writeBool(False)
    ## toQt(entry->GetReferrer().url);
    qtutils.serialize_stream(stream, QUrl())
    ## static_cast<qint32>(entry->GetReferrer().policy);
    # chromium/third_party/WebKit/public/platform/WebReferrerPolicy.h
    stream.writeInt32(0)  # WebReferrerPolicyAlways
    ## toQt(entry->GetOriginalRequestURL());
    qtutils.serialize_stream(stream, item.original_url)
    ## entry->GetIsOverridingUserAgent();
    stream.writeBool(False)
    ## static_cast<qint64>(entry->GetTimestamp().ToInternalValue());
    stream.writeInt64(int(time.time()))
    ## entry->GetHttpStatusCode();
    stream.writeInt(200)
Esempio n. 12
0
def _serialize_item(item, stream):
    """Serialize a single WebHistoryItem into a QDataStream.

    Args:
        item: The WebHistoryItem to write.
        stream: The QDataStream to write to.
    """
    ### Thanks to Otter Browser:
    ### https://github.com/OtterBrowser/otter-browser/blob/v0.9.10/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp#L1210
    ### src/core/web_contents_adapter.cpp serializeNavigationHistory
    ## toQt(entry->GetVirtualURL());
    qtutils.serialize_stream(stream, item.url)
    ## toQt(entry->GetTitle());
    stream.writeQString(item.title)
    ## QByteArray(encodedPageState.data(), encodedPageState.size());
    qtutils.serialize_stream(stream, QByteArray())
    ## static_cast<qint32>(entry->GetTransitionType());
    # chromium/ui/base/page_transition_types.h
    stream.writeInt32(0)  # PAGE_TRANSITION_LINK
    ## entry->GetHasPostData();
    stream.writeBool(False)
    ## toQt(entry->GetReferrer().url);
    qtutils.serialize_stream(stream, QUrl())
    ## static_cast<qint32>(entry->GetReferrer().policy);
    # chromium/third_party/WebKit/public/platform/WebReferrerPolicy.h
    stream.writeInt32(0)  # WebReferrerPolicyAlways
    ## toQt(entry->GetOriginalRequestURL());
    qtutils.serialize_stream(stream, item.original_url)
    ## entry->GetIsOverridingUserAgent();
    stream.writeBool(False)
    ## static_cast<qint64>(entry->GetTimestamp().ToInternalValue());
    stream.writeInt64(int(time.time()))
    ## entry->GetHttpStatusCode();
    stream.writeInt(200)
Esempio n. 13
0
def _serialize_item(item, stream):
    """Serialize a single WebHistoryItem into a QDataStream.

    Args:
        item: The WebHistoryItem to write.
        stream: The QDataStream to write to.
    """
    # Thanks to Otter Browser:
    # https://github.com/OtterBrowser/otter-browser/blob/v1.0.01/src/modules/backends/web/qtwebengine/QtWebEnginePage.cpp#L260
    #
    # Relevant QtWebEngine source:
    # src/core/web_contents_adapter.cpp serializeNavigationHistory
    #
    # Sample data:
    # [TabHistoryItem(active=True,
    #                 original_url=QUrl('file:///home/florian/proj/qutebrowser/git/tests/end2end/data/numbers/1.txt'),
    #                 title='1.txt',
    #                 url=QUrl('file:///home/florian/proj/qutebrowser/git/tests/end2end/data/numbers/1.txt'),
    #                 user_data={'zoom': 1.0, 'scroll-pos': QPoint()})]

    ## toQt(entry->GetVirtualURL());
    # \x00\x00\x00Jfile:///home/florian/proj/qutebrowser/git/tests/end2end/data/numbers/1.txt
    qtutils.serialize_stream(stream, item.url)

    ## toQt(entry->GetTitle());
    # \x00\x00\x00\n\x001\x00.\x00t\x00x\x00t
    stream.writeQString(item.title)

    ## QByteArray(encodedPageState.data(), encodedPageState.size());
    # \xff\xff\xff\xff
    qtutils.serialize_stream(stream, QByteArray())

    ## static_cast<qint32>(entry->GetTransitionType());
    # chromium/ui/base/page_transition_types.h
    # \x00\x00\x00\x00
    stream.writeInt32(0)  # PAGE_TRANSITION_LINK

    ## entry->GetHasPostData();
    # \x00
    stream.writeBool(False)

    ## toQt(entry->GetReferrer().url);
    # \xff\xff\xff\xff
    qtutils.serialize_stream(stream, QUrl())

    ## static_cast<qint32>(entry->GetReferrer().policy);
    # chromium/third_party/WebKit/public/platform/WebReferrerPolicy.h
    # \x00\x00\x00\x00
    stream.writeInt32(0)  # WebReferrerPolicyAlways

    ## toQt(entry->GetOriginalRequestURL());
    # \x00\x00\x00Jfile:///home/florian/proj/qutebrowser/git/tests/end2end/data/numbers/1.txt
    qtutils.serialize_stream(stream, item.original_url)

    ## entry->GetIsOverridingUserAgent();
    # \x00
    stream.writeBool(False)

    ## static_cast<qint64>(entry->GetTimestamp().ToInternalValue());
    # \x00\x00\x00\x00^\x97$\xe7
    if item.last_visited is None:
        unix_msecs = 0
    else:
        unix_msecs = item.last_visited.toMSecsSinceEpoch()
    # 11644516800000 is the number of milliseconds from
    # 1601-01-01T00:00 (Windows NT Epoch) to 1970-01-01T00:00 (UNIX Epoch)
    nt_usecs = (unix_msecs + 11644516800000) * 1000
    stream.writeInt64(nt_usecs)
    ## entry->GetHttpStatusCode();
    # \x00\x00\x00\xc8
    stream.writeInt(200)