1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtSerialBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QtSerialBus/qmodbusdeviceidentification.h>
38#include <QtSerialBus/qmodbuspdu.h>
39
40#include <QtTest/QtTest>
41
42class tst_QModbusDeviceIdentification : public QObject
43{
44 Q_OBJECT
45
46private slots:
47 void testConstructor()
48 {
49 QModbusDeviceIdentification qmdi;
50 QCOMPARE(qmdi.isValid(), false);
51 QCOMPARE(qmdi.objectIds().isEmpty(), true);
52 for (int i = QModbusDeviceIdentification::VendorNameObjectId;
53 i < QModbusDeviceIdentification::UndefinedObjectId; ++i) {
54 QCOMPARE(qmdi.contains(i), false);
55 qmdi.remove(objectId: i); // force crash if the behavior changes
56 QCOMPARE(qmdi.value(i), QByteArray());
57 }
58 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicConformityLevel);
59 }
60
61 void testIsValid()
62 {
63 QModbusDeviceIdentification qmdi;
64 QCOMPARE(qmdi.isValid(), false);
65 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ReservedObjectId, "Reserved"), true);
66 QCOMPARE(qmdi.isValid(), false);
67 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::VendorNameObjectId,
68 "Company identification"), true);
69 QCOMPARE(qmdi.isValid(), false);
70 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ProductCodeObjectId, "Product code"),
71 true);
72 QCOMPARE(qmdi.isValid(), false);
73 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::MajorMinorRevisionObjectId, "V2.11"),
74 true);
75 QCOMPARE(qmdi.isValid(), true);
76 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::MajorMinorRevisionObjectId, ""), true);
77 QCOMPARE(qmdi.isValid(), false);
78 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::MajorMinorRevisionObjectId, "V2.11"),
79 true);
80 QCOMPARE(qmdi.isValid(), true);
81 }
82
83 void testRemoveAndContains()
84 {
85 QModbusDeviceIdentification qmdi;
86 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::ReservedObjectId), false);
87 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ReservedObjectId, "Reserved"), true);
88 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::ReservedObjectId), true);
89 qmdi.remove(objectId: QModbusDeviceIdentification::ReservedObjectId);
90 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::ReservedObjectId), false);
91 }
92
93 void testInsertAndValue()
94 {
95 QModbusDeviceIdentification qmdi;
96 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ProductDependentObjectId, "Test"), true);
97 QCOMPARE(qmdi.value(QModbusDeviceIdentification::ProductDependentObjectId),
98 QByteArray("Test"));
99 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ProductDependentObjectId,
100 QByteArray(246, '@')), false);
101 QCOMPARE(qmdi.value(QModbusDeviceIdentification::ProductDependentObjectId),
102 QByteArray("Test"));
103 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::ProductDependentObjectId,
104 QByteArray(245, '@')), true);
105 QCOMPARE(qmdi.value(QModbusDeviceIdentification::ProductDependentObjectId),
106 QByteArray(245, '@'));
107 QCOMPARE(qmdi.insert(QModbusDeviceIdentification::UndefinedObjectId, "Test"), false);
108 QCOMPARE(qmdi.value(QModbusDeviceIdentification::UndefinedObjectId), QByteArray());
109 }
110
111 void testConformityLevel()
112 {
113 QModbusDeviceIdentification qmdi;
114 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicConformityLevel);
115 qmdi.setConformityLevel(QModbusDeviceIdentification::BasicIndividualConformityLevel);
116 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicIndividualConformityLevel);
117 }
118
119 void testConstructFromByteArray()
120 {
121 const QByteArray vendorNameObject = "Company identification";
122 QModbusResponse r(QModbusResponse::EncapsulatedInterfaceTransport,
123 QByteArray::fromHex(hexEncoded: "0e01010000010016") + vendorNameObject);
124
125 {
126 QModbusDeviceIdentification qmdi = QModbusDeviceIdentification::fromByteArray(ba: r.data());
127 QCOMPARE(qmdi.isValid(), false);
128 QCOMPARE(qmdi.objectIds(), QList<int>() << QModbusDeviceIdentification::VendorNameObjectId);
129 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::VendorNameObjectId), true);
130 QCOMPARE(qmdi.value(QModbusDeviceIdentification::VendorNameObjectId), vendorNameObject);
131 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicConformityLevel);
132 }
133
134 const QByteArray productCodeObject = "Product code";
135 r.setData(QByteArray::fromHex(hexEncoded: "0e01010000020016") + vendorNameObject
136 + QByteArray::fromHex(hexEncoded: "010c") + productCodeObject);
137 {
138 QModbusDeviceIdentification qmdi = QModbusDeviceIdentification::fromByteArray(ba: r.data());
139 QCOMPARE(qmdi.isValid(), false);
140 QCOMPARE(qmdi.objectIds(), QList<int>() << QModbusDeviceIdentification::VendorNameObjectId
141 << QModbusDeviceIdentification::ProductCodeObjectId);
142 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::VendorNameObjectId), true);
143 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::ProductCodeObjectId), true);
144 QCOMPARE(qmdi.value(QModbusDeviceIdentification::VendorNameObjectId), vendorNameObject);
145 QCOMPARE(qmdi.value(QModbusDeviceIdentification::ProductCodeObjectId), productCodeObject);
146 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicConformityLevel);
147 }
148
149 const QByteArray majorMinorRevision = "V2.11";
150 r.setData(QByteArray::fromHex(hexEncoded: "0e01010000030016") + vendorNameObject
151 + QByteArray::fromHex(hexEncoded: "010c") + productCodeObject + QByteArray::fromHex(hexEncoded: "0205")
152 + majorMinorRevision);
153 {
154 QModbusDeviceIdentification qmdi = QModbusDeviceIdentification::fromByteArray(ba: r.data());
155 QCOMPARE(qmdi.isValid(), true);
156 QCOMPARE(qmdi.objectIds(), QList<int>() << QModbusDeviceIdentification::VendorNameObjectId
157 << QModbusDeviceIdentification::ProductCodeObjectId
158 << QModbusDeviceIdentification::MajorMinorRevisionObjectId);
159 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::VendorNameObjectId), true);
160 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::ProductCodeObjectId), true);
161 QCOMPARE(qmdi.contains(QModbusDeviceIdentification::MajorMinorRevisionObjectId), true);
162 QCOMPARE(qmdi.value(QModbusDeviceIdentification::VendorNameObjectId), vendorNameObject);
163 QCOMPARE(qmdi.value(QModbusDeviceIdentification::ProductCodeObjectId), productCodeObject);
164 QCOMPARE(qmdi.value(QModbusDeviceIdentification::MajorMinorRevisionObjectId), majorMinorRevision);
165 QCOMPARE(qmdi.conformityLevel(), QModbusDeviceIdentification::BasicConformityLevel);
166 }
167 }
168};
169
170QTEST_MAIN(tst_QModbusDeviceIdentification)
171
172#include "tst_qmodbusdeviceidentification.moc"
173

source code of qtserialbus/tests/auto/qmodbusdeviceidentification/tst_qmodbusdeviceidentification.cpp