File indexing completed on 2025-01-05 04:37:19
0001 /* 0002 SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QObject> 0008 #include <QtTest> 0009 #include <net/poll.h> 0010 #include <net/socket.h> 0011 #include <util/log.h> 0012 #include <util/pipe.h> 0013 0014 using namespace net; 0015 using namespace bt; 0016 0017 class PollTest : public QEventLoop 0018 { 0019 Q_OBJECT 0020 public: 0021 public Q_SLOTS: 0022 0023 private Q_SLOTS: 0024 void initTestCase() 0025 { 0026 bt::InitLog("polltest.log"); 0027 } 0028 0029 void cleanupTestCase() 0030 { 0031 } 0032 0033 void testInput() 0034 { 0035 Poll p; 0036 Pipe pipe; 0037 0038 QVERIFY(pipe.readerSocket() >= 0); 0039 QVERIFY(pipe.writerSocket() >= 0); 0040 QVERIFY(p.add(pipe.readerSocket(), Poll::INPUT) == 0); 0041 char test[] = "TEST"; 0042 QVERIFY(pipe.write((const bt::Uint8 *)test, 4) == 4); 0043 QVERIFY(p.poll() == 1); 0044 QVERIFY(p.ready(0, net::Poll::INPUT)); 0045 0046 bt::Uint8 tmp[20]; 0047 QVERIFY(pipe.read(tmp, 20) == 4); 0048 QVERIFY(memcmp(tmp, test, 4) == 0); 0049 } 0050 0051 void testOutput() 0052 { 0053 Poll p; 0054 Pipe pipe; 0055 0056 QVERIFY(pipe.readerSocket() >= 0); 0057 QVERIFY(pipe.writerSocket() >= 0); 0058 QVERIFY(p.add(pipe.writerSocket(), Poll::OUTPUT) == 0); 0059 QVERIFY(p.poll() == 1); 0060 } 0061 0062 void testMultiplePolls() 0063 { 0064 Poll p; 0065 Pipe pipe; 0066 0067 QVERIFY(pipe.readerSocket() >= 0); 0068 QVERIFY(pipe.writerSocket() >= 0); 0069 0070 char test[] = "TEST"; 0071 QVERIFY(pipe.write((const bt::Uint8 *)test, 4) == 4); 0072 0073 for (int i = 0; i < 10; i++) { 0074 QVERIFY(p.add(pipe.readerSocket(), Poll::INPUT) == 0); 0075 QVERIFY(p.poll() == 1); 0076 QVERIFY(p.ready(0, net::Poll::INPUT)); 0077 p.reset(); 0078 } 0079 0080 bt::Uint8 tmp[20]; 0081 QVERIFY(pipe.read(tmp, 20) == 4); 0082 QVERIFY(memcmp(tmp, test, 4) == 0); 0083 QVERIFY(p.poll(100) == 0); 0084 } 0085 0086 void testTimeout() 0087 { 0088 Poll p; 0089 Pipe pipe; 0090 0091 QVERIFY(pipe.readerSocket() >= 0); 0092 QVERIFY(pipe.writerSocket() >= 0); 0093 QVERIFY(p.add(pipe.readerSocket(), Poll::INPUT) == 0); 0094 QVERIFY(p.poll(100) == 0); 0095 } 0096 0097 void testSocket() 0098 { 0099 net::Socket sock(true, 4); 0100 QVERIFY(sock.bind("127.0.0.1", 0, true)); 0101 0102 net::Address local_addr = sock.getSockName(); 0103 net::Socket writer(true, 4); 0104 writer.setBlocking(false); 0105 writer.connectTo(local_addr); 0106 0107 net::Address dummy; 0108 net::Poll poll; 0109 sock.prepare(&poll, net::Poll::INPUT); 0110 0111 QVERIFY(poll.poll(1000) > 0); 0112 int fd = sock.accept(dummy); 0113 QVERIFY(fd >= 0); 0114 0115 poll.reset(); 0116 QVERIFY(writer.connectSuccesFull()); 0117 0118 net::Socket reader(fd, 6); 0119 0120 bt::Uint8 data[20]; 0121 memset(data, 0xFF, 20); 0122 QVERIFY(writer.send(data, 20) == 20); 0123 reader.prepare(&poll, net::Poll::INPUT); 0124 0125 QVERIFY(poll.poll(1000) > 0); 0126 0127 bt::Uint8 tmp[20]; 0128 QVERIFY(reader.recv(tmp, 20) == 20); 0129 QVERIFY(memcmp(tmp, data, 20) == 0); 0130 } 0131 0132 private: 0133 }; 0134 0135 QTEST_MAIN(PollTest) 0136 0137 #include "polltest.moc"