@@ -6,40 +6,112 @@ public class BaseTelegramTest
66 [ Test ]
77 public void ParseTelegramSuccessful ( )
88 {
9- byte [ ] raw = new byte [ ] { 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D } ;
9+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
1010
1111 BaseTelegram telegram = new ( raw ) ;
1212
1313 Assert . That ( telegram . Type , Is . EqualTo ( BaseTelegram . TelegramType . RESPONSE ) ) ;
14- Assert . That ( telegram . Destination , Is . EqualTo ( 0xDA ) ) ;
15- Assert . That ( telegram . Source , Is . EqualTo ( 0xAA ) ) ;
14+ Assert . That ( telegram . Destination , Is . EqualTo ( 0xAA ) ) ;
15+ Assert . That ( telegram . Source , Is . EqualTo ( 0xDA ) ) ;
1616 Assert . That ( telegram . Valid , Is . EqualTo ( true ) ) ;
1717 }
1818
1919 [ Test ]
2020 public void ParseTelegramRequestSuccessful ( )
2121 {
22- byte [ ] raw = new byte [ ] { 0xC5 , 0x5C , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D } ;
22+ byte [ ] raw = [ 0xC5 , 0x5C , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
2323
2424 BaseTelegram telegram = new ( raw ) ;
2525
2626 Assert . That ( telegram . Type , Is . EqualTo ( BaseTelegram . TelegramType . REQUEST ) ) ;
27- Assert . That ( telegram . Destination , Is . EqualTo ( 0xDA ) ) ;
28- Assert . That ( telegram . Source , Is . EqualTo ( 0xAA ) ) ;
27+ Assert . That ( telegram . Destination , Is . EqualTo ( 0xAA ) ) ;
28+ Assert . That ( telegram . Source , Is . EqualTo ( 0xDA ) ) ;
2929 Assert . That ( telegram . Valid , Is . EqualTo ( true ) ) ;
3030 }
3131
3232 [ Test ]
3333 public void ParseTelegramWrongChecksum ( )
3434 {
35- byte [ ] raw = new byte [ ] { 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x10 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D } ;
35+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x10 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
3636
3737 BaseTelegram telegram = new ( raw ) ;
3838
3939 Assert . That ( telegram . Type , Is . EqualTo ( BaseTelegram . TelegramType . RESPONSE ) ) ;
40- Assert . That ( telegram . Destination , Is . EqualTo ( 0xDA ) ) ;
41- Assert . That ( telegram . Source , Is . EqualTo ( 0xAA ) ) ;
40+ Assert . That ( telegram . Destination , Is . EqualTo ( 0xAA ) ) ;
41+ Assert . That ( telegram . Source , Is . EqualTo ( 0xDA ) ) ;
4242 Assert . That ( telegram . Valid , Is . EqualTo ( false ) ) ;
4343 }
4444
45+ [ Test ]
46+ public void ParseTelegramTooShort ( )
47+ {
48+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA ] ;
49+
50+ var ex = Assert . Throws < ArgumentException > ( ( ) => new BaseTelegram ( raw ) ) ;
51+ Assert . That ( ex . Message , Is . EqualTo ( "Raw data is too short" ) ) ;
52+ }
53+
54+ [ Test ]
55+ public void ParseTelegramInvalidDataLength ( )
56+ {
57+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x21 , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
58+
59+ var ex = Assert . Throws < ArgumentException > ( ( ) => new BaseTelegram ( raw ) ) ;
60+ Assert . That ( ex . Message , Is . EqualTo ( "Invalid data len 33. Max supported: 32" ) ) ;
61+ }
62+
63+ [ Test ]
64+ public void ParseTelegramMissingEndTag ( )
65+ {
66+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C ] ;
67+
68+ var ex = Assert . Throws < ArgumentException > ( ( ) => new BaseTelegram ( raw ) ) ;
69+ Assert . That ( ex . Message , Is . EqualTo ( "Raw data does not contain End tag" ) ) ;
70+ }
71+
72+ [ Test ]
73+ public void ToStringTest ( )
74+ {
75+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
76+
77+ BaseTelegram telegram = new ( raw ) ;
78+
79+ string expected = "B6 6B AA DA 0A 02 00 04 00 00 13 00 00 02 01 1C 0D " ;
80+ Assert . That ( telegram . ToString ( ) , Is . EqualTo ( expected ) ) ;
81+ }
82+
83+ [ Test ]
84+ public void ToStringDetailedTest ( )
85+ {
86+ byte [ ] raw = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
87+
88+ BaseTelegram telegram = new ( raw ) ;
89+
90+ string expected = "B6 6B AA DA 0A 02 00 04 00 00 13 00 00 02 01 1C 0D " ;
91+ Assert . That ( telegram . ToStringDetailed ( ) , Is . EqualTo ( expected ) ) ;
92+ }
93+
94+ [ Test ]
95+ public void EqualsTest ( )
96+ {
97+ byte [ ] raw1 = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
98+ byte [ ] raw2 = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
99+
100+ BaseTelegram telegram1 = new ( raw1 ) ;
101+ BaseTelegram telegram2 = new ( raw2 ) ;
102+
103+ Assert . That ( telegram1 . Equals ( telegram2 ) , Is . EqualTo ( true ) ) ;
104+ }
105+
106+ [ Test ]
107+ public void NotEqualsTest ( )
108+ {
109+ byte [ ] raw1 = [ 0xB6 , 0x6B , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
110+ byte [ ] raw2 = [ 0xC5 , 0x5C , 0xAA , 0xDA , 0x0A , 0x02 , 0x00 , 0x04 , 0x00 , 0x00 , 0x13 , 0x00 , 0x00 , 0x02 , 0x01 , 0x1C , 0x0D ] ;
111+
112+ BaseTelegram telegram1 = new ( raw1 ) ;
113+ BaseTelegram telegram2 = new ( raw2 ) ;
114+
115+ Assert . That ( telegram1 . Equals ( telegram2 ) , Is . EqualTo ( false ) ) ;
116+ }
45117}
0 commit comments