Skip to content

Commit 8c64c0a

Browse files
QuantumExplorerUdjinM6
authored andcommitted
fixed protobuf 3.0 (byteswap) issue on macOS (#1370)
1 parent d26916a commit 8c64c0a

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

src/Makefile.qttest.include

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
bin_PROGRAMS += qt/test/test_dash-qt
22
TESTS += qt/test/test_dash-qt
33

4-
TEST_QT_MOC_CPP = qt/test/moc_uritests.cpp
4+
TEST_QT_MOC_CPP = \
5+
qt/test/moc_compattests.cpp \
6+
qt/test/moc_uritests.cpp
57

68
if ENABLE_WALLET
79
TEST_QT_MOC_CPP += qt/test/moc_paymentservertests.cpp
810
endif
911

1012
TEST_QT_H = \
13+
qt/test/compattests.h \
1114
qt/test/uritests.h \
1215
qt/test/paymentrequestdata.h \
1316
qt/test/paymentservertests.h
@@ -16,6 +19,7 @@ qt_test_test_dash_qt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BITCOIN_QT_
1619
$(QT_INCLUDES) $(QT_TEST_INCLUDES) $(PROTOBUF_CFLAGS)
1720

1821
qt_test_test_dash_qt_SOURCES = \
22+
qt/test/compattests.cpp \
1923
qt/test/test_main.cpp \
2024
qt/test/uritests.cpp \
2125
$(TEST_QT_H)

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ BITCOIN_TESTS =\
4343
test/base64_tests.cpp \
4444
test/bip32_tests.cpp \
4545
test/bloom_tests.cpp \
46+
test/bswap_tests.cpp \
4647
test/cachemap_tests.cpp \
4748
test/cachemultimap_tests.cpp \
4849
test/checkblock_tests.cpp \

src/compat/byteswap.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
#include <byteswap.h>
1616
#endif
1717

18+
#if defined(__APPLE__)
19+
20+
#if !defined(bswap_16)
21+
22+
// Mac OS X / Darwin features; we include a check for bswap_16 because if it is already defined, protobuf has
23+
// defined these macros for us already; if it isn't, we do it ourselves. In either case, we get the exact same
24+
// result regardless which path was taken
25+
#include <libkern/OSByteOrder.h>
26+
#define bswap_16(x) OSSwapInt16(x)
27+
#define bswap_32(x) OSSwapInt32(x)
28+
#define bswap_64(x) OSSwapInt64(x)
29+
30+
#endif // !defined(bswap_16)
31+
32+
#else
33+
// Non-Mac OS X / non-Darwin
34+
1835
#if HAVE_DECL_BSWAP_16 == 0
1936
inline uint16_t bswap_16(uint16_t x)
2037
{
@@ -44,4 +61,6 @@ inline uint64_t bswap_64(uint64_t x)
4461
}
4562
#endif // HAVE_DECL_BSWAP64
4663

64+
#endif // defined(__APPLE__)
65+
4766
#endif // BITCOIN_COMPAT_BYTESWAP_H

src/qt/test/compattests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "paymentrequestplus.h" // this includes protobuf's port.h which defines its own bswap macos
6+
7+
#include "compattests.h"
8+
9+
#include "compat/byteswap.h"
10+
11+
void CompatTests::bswapTests()
12+
{
13+
// Sibling in bitcoin/src/test/bswap_tests.cpp
14+
uint16_t u1 = 0x1234;
15+
uint32_t u2 = 0x56789abc;
16+
uint64_t u3 = 0xdef0123456789abc;
17+
uint16_t e1 = 0x3412;
18+
uint32_t e2 = 0xbc9a7856;
19+
uint64_t e3 = 0xbc9a78563412f0de;
20+
QVERIFY(bswap_16(u1) == e1);
21+
QVERIFY(bswap_32(u2) == e2);
22+
QVERIFY(bswap_64(u3) == e3);
23+
}

src/qt/test/compattests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2009-2015 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QT_TEST_COMPATTESTS_H
6+
#define BITCOIN_QT_TEST_COMPATTESTS_H
7+
8+
#include <QObject>
9+
#include <QTest>
10+
11+
class CompatTests : public QObject
12+
{
13+
Q_OBJECT
14+
15+
private Q_SLOTS:
16+
void bswapTests();
17+
};
18+
19+
#endif // BITCOIN_QT_TEST_COMPATTESTS_H

src/qt/test/test_main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "util.h"
1111
#include "uritests.h"
12+
#include "compattests.h"
1213

1314
#ifdef ENABLE_WALLET
1415
#include "paymentservertests.h"
@@ -49,6 +50,9 @@ int main(int argc, char *argv[])
4950
if (QTest::qExec(&test2) != 0)
5051
fInvalid = true;
5152
#endif
53+
CompatTests test4;
54+
if (QTest::qExec(&test4) != 0)
55+
fInvalid = true;
5256

5357
return fInvalid;
5458
}

src/test/bswap_tests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2016 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "compat/byteswap.h"
6+
#include "test/test_dash.h"
7+
8+
#include <boost/test/unit_test.hpp>
9+
10+
BOOST_FIXTURE_TEST_SUITE(bswap_tests, BasicTestingSetup)
11+
12+
BOOST_AUTO_TEST_CASE(bswap_tests)
13+
{
14+
// Sibling in bitcoin/src/qt/test/compattests.cpp
15+
uint16_t u1 = 0x1234;
16+
uint32_t u2 = 0x56789abc;
17+
uint64_t u3 = 0xdef0123456789abc;
18+
uint16_t e1 = 0x3412;
19+
uint32_t e2 = 0xbc9a7856;
20+
uint64_t e3 = 0xbc9a78563412f0de;
21+
BOOST_CHECK(bswap_16(u1) == e1);
22+
BOOST_CHECK(bswap_32(u2) == e2);
23+
BOOST_CHECK(bswap_64(u3) == e3);
24+
}
25+
26+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)