File: OPCServerTest.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-7.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,492 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (153 lines) | stat: -rw-r--r-- 4,346 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * OPCServerTest.cpp
 * Test fixture for the OPCServer class
 * Copyright (C) 2014 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>

#include <memory>
#include "ola/base/Array.h"
#include "ola/Callback.h"
#include "ola/DmxBuffer.h"
#include "ola/Logging.h"
#include "ola/io/SelectServer.h"
#include "ola/network/IPV4Address.h"
#include "ola/network/SocketAddress.h"
#include "ola/network/TCPSocket.h"
#include "ola/testing/TestUtils.h"
#include "ola/util/Utils.h"
#include "plugins/openpixelcontrol/OPCServer.h"


using ola::DmxBuffer;
using ola::network::IPV4Address;
using ola::network::IPV4SocketAddress;
using ola::network::TCPSocket;
using ola::plugin::openpixelcontrol::OPCServer;
using std::auto_ptr;

class OPCServerTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(OPCServerTest);
  CPPUNIT_TEST(testReceiveDmx);
  CPPUNIT_TEST(testUnknownCommand);
  CPPUNIT_TEST(testLargeFrame);
  CPPUNIT_TEST(testHangingFrame);
  CPPUNIT_TEST_SUITE_END();

 public:
  OPCServerTest()
      : CppUnit::TestFixture(),
        m_ss(NULL) {
  }
  void setUp();

  void testReceiveDmx();
  void testUnknownCommand();
  void testLargeFrame();
  void testHangingFrame();

 private:
  ola::io::SelectServer m_ss;
  auto_ptr<OPCServer> m_server;
  auto_ptr<TCPSocket> m_client_socket;
  DmxBuffer m_received_data;
  uint8_t m_command;

  void SendDataAndCheck(uint8_t channel,
                        const DmxBuffer &data);

  void CaptureData(uint8_t command, const uint8_t *data, unsigned int length) {
    m_received_data.Set(data, length);
    m_command = command;
    m_ss.Terminate();
  }

  static const uint8_t CHANNEL = 1;
  static const uint8_t SET_PIXELS_COMMAND = 0;
};

CPPUNIT_TEST_SUITE_REGISTRATION(OPCServerTest);

void OPCServerTest::setUp() {
  IPV4SocketAddress listen_addr(IPV4Address::Loopback(), 0);
  m_server.reset(new OPCServer(&m_ss, listen_addr));
  m_server->SetCallback(
      CHANNEL,
      ola::NewCallback(this, &OPCServerTest::CaptureData));

  OLA_ASSERT_TRUE(m_server->Init());

  m_client_socket.reset(TCPSocket::Connect(m_server->ListenAddress()));
}

void OPCServerTest::SendDataAndCheck(uint8_t channel,
                                     const DmxBuffer &buffer) {
  unsigned int dmx_size = buffer.Size();
  uint8_t data[dmx_size + 4];
  data[0] = channel;
  data[1] = SET_PIXELS_COMMAND;
  ola::utils::SplitUInt16(static_cast<uint16_t>(dmx_size), &data[2], &data[3]);
  buffer.Get(data + 4, &dmx_size);
  m_client_socket->Send(data, dmx_size + 4);

  m_ss.Run();
  OLA_ASSERT_EQ(m_received_data, buffer);
}

void OPCServerTest::testReceiveDmx() {
  DmxBuffer buffer;
  buffer.SetFromString("1,2,3,4");
  SendDataAndCheck(CHANNEL, buffer);
  buffer.SetFromString("5,6");
  SendDataAndCheck(CHANNEL, buffer);
  buffer.SetFromString("5,6,7,8,89,9,5,6,7,8,3");
  SendDataAndCheck(CHANNEL, buffer);
}

void OPCServerTest::testUnknownCommand() {
  uint8_t data[] = {1, 1, 0, 2, 3, 4};
  m_client_socket->Send(data, arraysize(data));
  m_ss.Run();

  DmxBuffer buffer;
  buffer.SetFromString("3,4");
  OLA_ASSERT_EQ(static_cast<uint8_t>(1), m_command);
  OLA_ASSERT_EQ(m_received_data, buffer);
}

void OPCServerTest::testLargeFrame() {
  uint8_t data[1028];
  data[0] = 1;
  data[1] = 0;
  data[2] = 4;
  data[3] = 0;
  for (unsigned int i = 0; i < 1024; i++) {
    data[i + 4] = i;
  }

  m_client_socket->Send(data, arraysize(data));
  m_ss.Run();

  DmxBuffer buffer(data + 4, ola::DMX_UNIVERSE_SIZE);
  OLA_ASSERT_EQ(m_received_data, buffer);
}

void OPCServerTest::testHangingFrame() {
  uint8_t data[] = {1, 0};
  m_client_socket->Send(data, arraysize(data));
}