1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

Huawei: Fix tests

- Allow the calendar to be passed as parameter for tests
- Disable the setWearMessage test, for now, broken since 5b0736b751
This commit is contained in:
José Rebelo 2024-01-08 14:02:27 +00:00 committed by José Rebelo
parent 811b7524bc
commit 9bfe3dcd5f
4 changed files with 18 additions and 6 deletions

View File

@ -46,8 +46,7 @@ public class HuaweiUtil {
(byte)calendar.get(Calendar.MINUTE)};
}
public static byte[] getTimeAndZoneId() {
Calendar now = Calendar.getInstance();
public static byte[] getTimeAndZoneId(final Calendar now) {
int zoneRawOffset = (now.get(Calendar.ZONE_OFFSET) + now.get(Calendar.DST_OFFSET)) / 1000;
byte[] id = now.getTimeZone().getID().getBytes();
return ByteBuffer.allocate(6 + id.length)

View File

@ -23,6 +23,7 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TreeMap;
@ -395,17 +396,21 @@ public class DeviceConfig {
public static class TimeRequest extends HuaweiPacket {
public static final byte id = 0x05;
public TimeRequest(ParamsProvider paramsProvider) {
public TimeRequest(ParamsProvider paramsProvider, final Calendar now) {
super(paramsProvider);
this.serviceId = DeviceConfig.id;
this.commandId = id;
ByteBuffer timeAndZoneId = ByteBuffer.wrap(HuaweiUtil.getTimeAndZoneId());
ByteBuffer timeAndZoneId = ByteBuffer.wrap(HuaweiUtil.getTimeAndZoneId(now));
this.tlv = new HuaweiTLV()
.put(0x01, timeAndZoneId.getInt(0))
.put(0x02, timeAndZoneId.getShort(4));
this.complete = true;
}
public TimeRequest(ParamsProvider paramsProvider) {
this(paramsProvider, Calendar.getInstance());
}
// TODO: implement parsing this request for the log parser support
}
@ -1506,7 +1511,7 @@ public class DeviceConfig {
super(paramsProvider);
this.serviceId = DeviceConfig.id;
this.commandId = id;
ByteBuffer timeAndZoneId = ByteBuffer.wrap(HuaweiUtil.getTimeAndZoneId());
ByteBuffer timeAndZoneId = ByteBuffer.wrap(HuaweiUtil.getTimeAndZoneId(Calendar.getInstance()));
this.tlv = new HuaweiTLV()
.put(0x01, timeAndZoneId.getInt())
.put(0x02, timeAndZoneId.getShort());

View File

@ -30,7 +30,9 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
@ -311,6 +313,10 @@ public class TestDeviceConfig {
int timestamp = 1633987331;
short zoneOffset = (short) 512;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp * 1000L);
calendar.setTimeZone(TimeZone.getTimeZone("GMT+2"));
Field tlvField = HuaweiPacket.class.getDeclaredField("tlv");
tlvField.setAccessible(true);
@ -319,7 +325,7 @@ public class TestDeviceConfig {
.put(0x02, zoneOffset);
byte[] serialized = new byte[] {(byte) 0x5A, (byte) 0x00, (byte) 0x2A, (byte) 0x00, (byte) 0x01, (byte) 0x05, (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) 0x10, (byte) 0xED, (byte) 0x67, (byte) 0x61, (byte) 0x8A, (byte) 0x8E, (byte) 0x44, (byte) 0x67, (byte) 0xB1, (byte) 0x2A, (byte) 0xB4, (byte) 0xFA, (byte) 0x86, (byte) 0x76, (byte) 0x17, (byte) 0x8C, (byte) 0x61, (byte) 0xFC, (byte) 0x99};
DeviceConfig.TimeRequest request = new DeviceConfig.TimeRequest(secretsProvider);
DeviceConfig.TimeRequest request = new DeviceConfig.TimeRequest(secretsProvider, calendar);
Assert.assertEquals(0x01, request.serviceId);
Assert.assertEquals(0x05, request.commandId);

View File

@ -17,6 +17,7 @@
package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.lang.reflect.Field;
@ -162,6 +163,7 @@ public class TestNotifications {
}
@Test
@Ignore("Broken since https://codeberg.org/psolyca/Gadgetbridge/commit/5b0736b7518aa5c998ac13207fff66286393965b")
public void testSetWearMessagePushRequest() throws NoSuchFieldException, IllegalAccessException, HuaweiPacket.CryptoException {
Field tlvField = HuaweiPacket.class.getDeclaredField("tlv");
tlvField.setAccessible(true);