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
|
//
// SDLProtocolMessageDisassemblerSpec.m
// SmartDeviceLink-iOS
#import <Foundation/Foundation.h>
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>
#import "SDLGlobals.h"
#import "SDLProtocolMessageDisassembler.h"
#import "SDLV2ProtocolHeader.h"
#import "SDLV2ProtocolMessage.h"
#import "SDLNames.h"
QuickSpecBegin(SDLProtocolMessageDisassemblerSpec)
describe(@"Disassemble Tests", ^ {
it(@"Should assemble the message properly", ^ {
//Allocate 2000 bytes, and use it as sample data
const NSUInteger dataLength = 2000;
char dummyBytes[dataLength];
SDLGlobals *globals = [[SDLGlobals alloc] init];
globals.maxHeadUnitVersion = @"2.0.0";
const char testPayloadHeader[12] = {0x20, 0x55, 0x64, 0x73, 0x12, 0x34, 0x43, 0x21, (dataLength >> 24) & 0xFF, (dataLength >> 16) & 0xFF, (dataLength >> 8) & 0xFF, dataLength & 0xFF};
NSMutableData* payloadData = [NSMutableData dataWithBytes:testPayloadHeader length:12];
[payloadData appendBytes:dummyBytes length:dataLength];
SDLV2ProtocolMessage* testMessage = [[SDLV2ProtocolMessage alloc] init];
SDLV2ProtocolHeader* testHeader = [[SDLV2ProtocolHeader alloc] init];
testHeader.frameType = SDLFrameTypeSingle;
testHeader.serviceType = SDLServiceTypeBulkData;
testHeader.frameData = SDLFrameInfoSingleFrame;
testHeader.sessionID = 0x84;
testHeader.bytesInPayload = (UInt32)payloadData.length;
testMessage.header = testHeader;
testMessage.payload = payloadData;
NSArray<SDLProtocolMessage *> *messageList = [SDLProtocolMessageDisassembler disassemble:testMessage withLimit:[globals mtuSizeForServiceType:testHeader.serviceType]];
//Payload length per message
UInt32 payloadLength = 1012; // v1/2 MTU(1024) - header length(12)
const char firstPayload[8] = {(payloadData.length >> 24) & 0xFF, (payloadData.length >> 16) & 0xFF, (payloadData.length >> 8) & 0xFF, payloadData.length & 0xFF, 0x00, 0x00, 0x00, ceil(1.0 * payloadData.length / payloadLength)};
SDLProtocolMessage* message = messageList[0];
//First frame
expect(message.payload).to(equal([NSData dataWithBytes:firstPayload length:8]));
expect(@(message.header.frameType)).to(equal(@(SDLFrameTypeFirst)));
expect(@(message.header.serviceType)).to(equal(@(SDLServiceTypeBulkData)));
expect(@(message.header.frameData)).to(equal(@(SDLFrameInfoFirstFrame)));
expect(@(message.header.sessionID)).to(equal(@0x84));
expect(@(message.header.bytesInPayload)).to(equal(@8));
NSUInteger offset = 0;
for (int i = 1; i < messageList.count - 1; i++) {
message = messageList[i];
//Consecutive frames
expect(message.payload).to(equal([NSData dataWithData:[payloadData subdataWithRange:NSMakeRange(offset, payloadLength)]]));
expect(@(message.header.frameType)).to(equal(@(SDLFrameTypeConsecutive)));
expect(@(message.header.serviceType)).to(equal(@(SDLServiceTypeBulkData)));
expect(@(message.header.frameData)).to(equal(@(i)));
expect(@(message.header.sessionID)).to(equal(@0x84));
expect(@(message.header.bytesInPayload)).to(equal(@(payloadLength)));
offset += payloadLength;
}
message = [messageList lastObject];
NSUInteger remaining = payloadData.length - offset;
//Last frame
expect(message.payload).to(equal([NSData dataWithData:[payloadData subdataWithRange:NSMakeRange(offset, remaining)]]));
expect(@(message.header.frameType)).to(equal(@(SDLFrameTypeConsecutive)));
expect(@(message.header.serviceType)).to(equal(@(SDLServiceTypeBulkData)));
expect(@(message.header.frameData)).to(equal(@(SDLFrameInfoConsecutiveLastFrame)));
expect(@(message.header.sessionID)).to(equal(@0x84));
expect(@(message.header.bytesInPayload)).to(equal(@(remaining)));
});
});
QuickSpecEnd
|