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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
| /* Copyright (C) 2022-2023 Martin.JM
|
| This file is part of Gadgetbridge.
|
| Gadgetbridge is free software: you can redistribute it and/or modify
| it under the terms of the GNU Affero General Public License as published
| by the Free Software Foundation, either version 3 of the License, or
| (at your option) any later version.
|
| Gadgetbridge 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 Affero General Public License for more details.
|
| You should have received a copy of the GNU Affero General Public License
| along with this program. If not, see <http://www.gnu.org/licenses/>. */
| package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets;
|
| import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
| import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV;
|
| import org.junit.Assert;
| import org.junit.Test;
|
| import java.lang.reflect.Field;
| import java.util.List;
|
| public class TestAlarms {
|
| HuaweiPacket.ParamsProvider paramsProvider = new HuaweiPacket.ParamsProvider() {
| @Override
| public byte getDeviceSupportType() {
| return 0;
| }
|
| @Override
| public byte[] getSecretKey() {
| return new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
| }
|
| @Override
| public byte[] getIv() {
| return new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
| }
|
| @Override
| public boolean areTransactionsCrypted() {
| return true;
| }
|
| @Override
| public int getMtu() {
| return 0;
| }
|
| @Override
| public int getSliceSize() {
| return 0xF4;
| }
| };
|
| @Test
| public void testEventAlarmsRequest() throws NoSuchFieldException, IllegalAccessException, HuaweiPacket.CryptoException {
| Field tlvField = HuaweiPacket.class.getDeclaredField("tlv");
| tlvField.setAccessible(true);
|
| Field alarmField = Alarms.EventAlarmsRequest.class.getDeclaredField("alarms");
| alarmField.setAccessible(true);
|
| HuaweiTLV expectedAlarmsTlv = new HuaweiTLV();
|
| Alarms.EventAlarmsRequest request = new Alarms.EventAlarmsRequest(paramsProvider);
|
| expectedAlarmsTlv.put(0x82, new HuaweiTLV()
| .put(0x03, (byte) 1)
| .put(0x04, true)
| .put(0x05, (short) 0x1337)
| .put(0x06, (byte) 0)
| .put(0x07, "Alarm1")
| );
|
| request.addEventAlarm(
| new Alarms.EventAlarm(
| (byte) 1,
| true,
| (byte) 0x13,
| (byte) 0x37,
| (byte) 0,
| "Alarm1"
| )
| );
|
| Assert.assertEquals(0x08, request.serviceId);
| Assert.assertEquals(0x01, request.commandId);
| // TODO: check count in request
| Assert.assertEquals(expectedAlarmsTlv, alarmField.get(request));
|
| // A serialize will change the tlv, so we cannot test it here
|
| expectedAlarmsTlv.put(0x82,new HuaweiTLV()
| .put(0x03, (byte) 2)
| .put(0x04, false)
| .put(0x05, (short) 0xCAFE)
| .put(0x06, (byte) 1)
| .put(0x07, "Alarm2")
| );
|
| request.addEventAlarm(
| new Alarms.EventAlarm(
| (byte) 2,
| false,
| (byte) 0xCA,
| (byte) 0xFE,
| (byte) 1,
| "Alarm2"
| )
| );
|
| Assert.assertEquals(expectedAlarmsTlv, alarmField.get(request));
|
| HuaweiTLV expectedTlv = new HuaweiTLV().put(0x81, expectedAlarmsTlv);
|
| // Different order for better assertion messages in case of failure
| List<byte[]> listOut = request.serialize();
| Assert.assertEquals(1, listOut.size());
| Assert.assertEquals(expectedAlarmsTlv, alarmField.get(request));
| Assert.assertEquals(expectedTlv, tlvField.get(request));
| }
|
| @Test
| public void testSmartAlarmRequest() throws NoSuchFieldException, IllegalAccessException, HuaweiPacket.CryptoException {
| Field tlvField = HuaweiPacket.class.getDeclaredField("tlv");
| tlvField.setAccessible(true);
|
| byte[] expectedOutput = new byte[] {(byte) 0x5a, (byte) 0x00, (byte) 0x3a, (byte) 0x00, (byte) 0x08, (byte) 0x02, (byte) 0x7c, (byte) 0x01, (byte) 0x01, (byte) 0x7d, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x7e, (byte) 0x20, (byte) 0xcd, (byte) 0x7f, (byte) 0x80, (byte) 0x67, (byte) 0x02, (byte) 0x8d, (byte) 0x46, (byte) 0xfb, (byte) 0xc1, (byte) 0x0b, (byte) 0xed, (byte) 0x6c, (byte) 0x46, (byte) 0xb7, (byte) 0x59, (byte) 0xba, (byte) 0x08, (byte) 0xfd, (byte) 0xde, (byte) 0x3b, (byte) 0xee, (byte) 0x54, (byte) 0xbd, (byte) 0x4f, (byte) 0x27, (byte) 0xf6, (byte) 0x52, (byte) 0x9a, (byte) 0xae, (byte) 0xbf, (byte) 0x55, (byte) 0xd9, (byte) 0xe0, (byte) 0xa6};
|
| HuaweiTLV expectedTlv = new HuaweiTLV()
| .put(0x81, new HuaweiTLV()
| .put(0x82, new HuaweiTLV()
| .put(0x03, (byte) 0x01)
| .put(0x04, true)
| .put(0x05, (short) 0x1337)
| .put(0x06, (byte) 1)
| .put(0x07, (byte) 2)
| )
| );
|
| Alarms.SmartAlarmRequest request = new Alarms.SmartAlarmRequest(
| paramsProvider,
| new Alarms.SmartAlarm(
| true,
| (byte) 0x13,
| (byte) 0x37,
| (byte) 1,
| (byte) 2
| )
| );
|
| Assert.assertEquals(0x08, request.serviceId);
| Assert.assertEquals(0x02, request.commandId);
| Assert.assertTrue(request.complete);
| Assert.assertEquals(expectedTlv, tlvField.get(request));
| List<byte[]> out = request.serialize();
| Assert.assertEquals(1, out.size());
| Assert.assertArrayEquals(expectedOutput, out.get(0));
| }
| }
|
|