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
|
//
// SDLProtocolReceivedMessageRouterSpec.m
// SmartDeviceLink-iOS
#import <Foundation/Foundation.h>
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
#import <OCMock/OCMock.h>
#import "SDLProtocolReceivedMessageRouter.h"
#import "SDLV2ProtocolHeader.h"
#import "SDLV2ProtocolMessage.h"
#import "SDLNames.h"
QuickSpecBegin(SDLProtocolReceivedMessageRouterSpec)
// TODO: This should be rewritten using an actual mock (i.e. SDLProtocolListenerDelegateMock class to avoid OCMock)
describe(@"HandleReceivedMessage Tests", ^ {
context(@"When handling control message", ^ {
it(@"Should route message correctly", ^ {
id delegateMock = OCMProtocolMock(@protocol(SDLProtocolListener));
SDLV2ProtocolMessage* testMessage = [[SDLV2ProtocolMessage alloc] init];
SDLV2ProtocolHeader* testHeader = [[SDLV2ProtocolHeader alloc] init];
testHeader.frameType = SDLFrameType_Control;
testHeader.serviceType = SDLServiceType_RPC;
testHeader.frameData = SDLFrameData_StartSessionACK;
testHeader.sessionID = 0x93;
testHeader.bytesInPayload = 0;
testMessage.header = testHeader;
testMessage.payload = [NSData data];
SDLProtocolReceivedMessageRouter* router = [[SDLProtocolReceivedMessageRouter alloc] init];
router.delegate = delegateMock;
[router handleReceivedMessage:testMessage];
OCMVerify([delegateMock handleProtocolStartSessionACK:testMessage.header]);
});
});
context(@"When handling single frame message", ^ {
it(@"Should route message correctly", ^ {
id delegateMock = OCMProtocolMock(@protocol(SDLProtocolListener));
SDLV2ProtocolMessage* testMessage = [[SDLV2ProtocolMessage alloc] init];
SDLV2ProtocolHeader* testHeader = [[SDLV2ProtocolHeader alloc] init];
testHeader.frameType = SDLFrameType_Single;
testHeader.serviceType = SDLServiceType_RPC;
testHeader.frameData = SDLFrameData_SingleFrame;
testHeader.sessionID = 0x07;
testHeader.bytesInPayload = 0;
testMessage.header = testHeader;
testMessage.payload = [NSData data];
__block BOOL verified = NO;
[[[[delegateMock stub] andDo: ^(NSInvocation* invocation) {
verified = YES;
__unsafe_unretained SDLProtocolMessage* message;
[invocation getArgument:&message atIndex:2];
SDLV2ProtocolMessage* messageReceived = (SDLV2ProtocolMessage*)message;
expect(messageReceived).to(beIdenticalTo(testMessage));
}] ignoringNonObjectArgs] onProtocolMessageReceived:[OCMArg any]];
SDLProtocolReceivedMessageRouter* router = [[SDLProtocolReceivedMessageRouter alloc] init];
router.delegate = delegateMock;
[router handleReceivedMessage:testMessage];
expect(@(verified)).to(beTruthy());
});
});
context(@"When handling multi-frame message", ^ {
it(@"Should route message correctly", ^ {
//Allocate 2000 bytes and use it as test data
const NSUInteger dataLength = 2000;
char dummyBytes[dataLength];
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];
//First frame
testHeader.frameType = SDLFrameType_First;
testHeader.serviceType = SDLServiceType_BulkData;
testHeader.frameData = 1;
testHeader.sessionID = 0x33;
testHeader.bytesInPayload = 8;
testMessage.header = testHeader;
const char firstPayload[8] = {(payloadData.length >> 24) & 0xFF, (payloadData.length >> 16) & 0xFF, (payloadData.length >> 8) & 0xFF, payloadData.length & 0xFF, 0x00, 0x00, 0x00, ceil(payloadData.length / 500.0)};
testMessage.payload = [NSData dataWithBytes:firstPayload length:8];
SDLProtocolReceivedMessageRouter* router = [[SDLProtocolReceivedMessageRouter alloc] init];
[router handleReceivedMessage:testMessage];
testMessage.header.frameType = SDLFrameType_Consecutive;
testMessage.header.bytesInPayload = 500;
NSUInteger frameNumber = 1;
NSUInteger offset = 0;
while ((offset + 500) < payloadData.length) {
//Consectutive frames
testMessage.header.frameData = frameNumber;
testMessage.payload = [payloadData subdataWithRange:NSMakeRange(offset, 500)];
[router handleReceivedMessage:testMessage];
frameNumber++;
offset += 500;
}
//Final frame
testMessage.header.frameData = 0;
testMessage.payload = [payloadData subdataWithRange:NSMakeRange(offset, payloadData.length - offset)];
id delegateMock = OCMProtocolMock(@protocol(SDLProtocolListener));
__block BOOL verified = NO;
[[[delegateMock stub] andDo:^(NSInvocation* invocation) {
verified = YES;
//Without the __unsafe_unretained, a double release will occur. More information: https://github.com/erikdoe/ocmock/issues/123
__unsafe_unretained SDLProtocolMessage* message;
[invocation getArgument:&message atIndex:2];
SDLProtocolMessage* assembledMessage = message;
expect(assembledMessage.payload).to(equal(payloadData));
expect(@(assembledMessage.header.frameType)).to(equal(@(SDLFrameType_Single)));
expect(@(assembledMessage.header.serviceType)).to(equal(@(SDLServiceType_BulkData)));
expect(@(assembledMessage.header.frameData)).to(equal(@(SDLFrameData_SingleFrame)));
expect(@(assembledMessage.header.sessionID)).to(equal(@0x33));
expect(@(assembledMessage.header.bytesInPayload)).to(equal(@(payloadData.length)));
}] onProtocolMessageReceived:[OCMArg any]];
router.delegate = delegateMock;
[router handleReceivedMessage:testMessage];
expect(@(verified)).to(beTruthy());
});
});
});
QuickSpecEnd
|