You are on page 1of 11

1 2 3 4 5 6 7 8 9Fake

10 11

Server Framework for Windows Mobile

Page 2 of 11
3

12

Contents

13 Contents.........................................................................................................................................................................2 141 Introduction..................................................................................................................................................................3 15 1.1 Testing the device web application ......................................................................................................................3 16 1.2 Issues with the test architecture............................................................................................................................4 17 1.3 Alternate Test Application Architecture ..............................................................................................................4 18 1.4 Dependencies........................................................................................................................................................5 19 1.5 Terminology..........................................................................................................................................................5 202 High-Level Architecture..............................................................................................................................................7 21 2.1 Components of the fake Server Library................................................................................................................7 22 2.2 How does it all work?...........................................................................................................................................8 23 2.3 Managed fake server applications using Callbacks..............................................................................................9 24 2.4 Managed fake server applications using Response Queues..................................................................................9 25 2.5 Important Note about the Callbacks & Response Queues....................................................................................9 263 Detailed Design ...........................................................................................................................................................9 27 3.1 Microsoft.WindowsMobile.Samples.FakeServer.dll............................................................................................9 28 3.1.1 FakeServer class...........................................................................................................................................10 29 3.1.2 FakeResponse class......................................................................................................................................11 30 3.1.3 FakeResponseQueue class...........................................................................................................................11 314 Appendix....................................................................................................................................................................11 32

Microsoft Confidential

Page 3 of 11
5

331

Introduction

34This document details the design of the fake Server framework for Mobile Devices. There are many CE services 35and applications that use the TCP/IP networking stack to talk to external servers on the internet/intranet. For e.g. a 36on device weather reporting service, a stock reporting service, applications like pocket IE etc. Developing 37automated tests for such services has often been challenging due to networking dependencies, lack of control over 38the server environment and the lack of a library that encapsulates the networking functionality needed for such tests. 39Lets look at a sample web application that talks to a web server sitting on the internet and talk about how we would 40go about testing it etc. 41 42Lets take the case that you are developing a device application that gathers stock quotes from a server and displays 43them on the device. Your application is typically composed of a set of core APIS (call them Client Application API) 44that gather the stock quote from a server and a UI framework that renders the stock quote information on the device. 45The Client Application API would either use the networking layer directly through sockets or indirectly (via 46WININET or .NET CF libraries) to talk to the stock quote web server on the internet.

471.1 Testing the device web application


48Now if you are asked to test this application your tests would consist of API level tests and some UI tests. Your 49high level test application architecture would look something like this 50 51 STOCK Quote WEB SERVICE

DEVICE

TEST APPLICATION

Stock Quote Client Application API

RESPONSE

INTERNET/ INTRANET
REQUESTS

Network Layer

53

54 55 56

Microsoft Confidential

Page 4 of 11
7

571.2 Issues with the test architecture


58There are a few issues with this model 59 601. You need internet connectivity at all times to run the tests. 612. You would need a dedicated server running the web server at all times. 623. Due to the server dependency, your device will not be able to run stand alone tests. 634. Dependency on an external server can introduce multiple points of failure in your tests. For example if the server 64is down all your tests will fail or if there is a firewall blocking connections then the tests will fail as well etc. 655. Lack of programmatic control over the server environment. For example you cannot force the web server to send 66down malformed responses to test how your device application behaves etc. 676. In many case, the web server code will be developed in parallel to the web client (the device code). In such cases 68it wont be possible to test the client side of the code till the server side code is fully developed. This can be a big 69blocker and prevent testing till the very end.

701.3 Alternate Test Application Architecture


71The fake server library was developed to address the above issues. Lets say that there was an easy way to develop a 72light weight test stock quote web server that ran on your device itself. Then our client application (i.e the 73application under test in this case the stock quote application) could talk to the stock quote web server that runs on 74the device! 75 76If we could accomplish that our tests would not have any external server dependency or any connectivity 77dependency. The tests could be run stand alone! Furthur more imagine if we could send down any response to our 78device application. That could open up infinite possibilities to test our device application in a bullet proof manner. 79 80The fake server library allows you to do exactly these above things in a very easy and a clean fashion. Using the 81fake server library you can develop your test stock quote service that runs on the device. The service will be 82ruinning on the Loop Back interface on the device (IP address: 127.0.0.1) and will be started on port 80. Please 83close any applications that run on that port for e.g. on device IIS server. In most cases port 80 is readily available. 84The test service can be programmed to send back any response you intend. For e.g. the stock quote service could 85send back a response similar to the web server response from a real web server. 86 87In just a few lines of code you can have your very own test stock quote service to test your client application. The 88possibilities are further expanded by the fact that you have complete control over your test service, and as an added 89bonus you dont need any connectivity dependency to test your device application! 90 91All you need to do is 92 1. Develop the test stock quote service that runs locally on the device. Can be done under 10 lines of 93 managed code using the fake server library 94 2. Set up your application to send stock quote requests to the loop back address (IP: 127.0.0.1) at port 80 95 where the test quote service is running. 96 97The test application architecture now looks like this. 98

Microsoft Confidential

Page 5 of 11
9

DEVICE

TEST APPLICATION

Local Server Library

Client Application API

REQUESTS

RESPONSE

NETWORKING LAYER

100 101 102 103

99

1041.4 Dependencies
105The framework needs NET CF 2.0 to be installed on the device. There are no external dependencies besides this.

1061.5 Terminology
Loopback Socket Local host interface on the device Winsock sockets on CE

10

Microsoft Confidential

Page 6 of 11
11

.NET CF 2.0 Native Code Managed Code


107

DotNet Compact Framework 2.0 C++ C#

12

Microsoft Confidential

Page 7 of 11
13

1082

High-Level Architecture

109The fake server framework can be used by developers to write custom server applications that run on the device 110itself. These applications are very simple to write using the fake server library (as we can see you can write your 111own in less than 10 lines of code!). Before we go ahead with the design details lets have a look at a high level 112architecture. 113 114In the sections below we detail the architecture diagram of the fake Server library & also a component diagram 115explaining the various pieces.

1162.1 Components of the fake Server Library


117The framework is broadly composed of managed and native components built on top of windows sockets. The 118components are as follows 119 120FakeServer.dll 121 This is a native C++ DLL built directly on top of windows sockets. This DLL contains the complete 122implementation of the fake Server Library. This has the logic for receiving a connection and handling the exchange 123of data between the server and the client application. 124 125Microsoft.WindowsMobile.Samples.FakeServer.dll 126 This is the managed DLL built on top of fakeserver.dll. It has well defined interfaces to start a server 127thread on port 80 on the device (ofcourse the port should not be in use). The interfaces also implement the ability to 128handle data received from the client and send response data back to the client. Test Applications can exchange data 129with their clients using managed callbacks or using response queues. We will look at the two methods in detail in 130the sections to follow. 131 132So over all 2 types of test applications can be built using the fake server library. 133 134Type1: Managed Test Server Applications using callbacks 135Type2: Managed Test server applications using request queues. 136 137More on both of these types of applications later, but for now diagrammatically the modules appear like this. 138

14

Microsoft Confidential

Page 8 of 11
15

DEVICE
Type 1: Managed Test Server App Using Callbacks Type 2: Managed Test server App using Response Queues

Managed Fake Server. DLL

Fake Server.DLL

LOOP BACK ADAPTER (NETWORKING STACK)


139
140

1412.2 How does it all work?


142In this section we will provide details on the inner working of fake server library and the interaction between the 143native and the managed components. 144 145The managed fake server DLL loads up the native (C++) fakeserver.DLL and requests it to start a server thread on 146the devices local host interface (IP:127.0.0.1) on port 80. 147 148Lets assume that the test application has request that the server thread listen on port 80. Now when a TCP 149connection is made to 127.0.0.1 port 80, the server thread accepts the connection and waits for the client to send data 150over the connection. Once the data arrives, the Native (C++) fakeserver.DLL calls into the Managed fake server 151DLL (Microsoft.WindowsMobile.Samples.FakeServer.DLL) with the data that was received over the connection. 152The managed DLL then either reads the responses to be sent from a response queue or invokes a callback into the 153test application requesting it to supply the response that it wants to send back. Once the response is generated, the 154managed DLL sends the response to the native DLL which sends it back to the client application. 155 156The server framework is protocol agnostic, the server application built on top of the framework is responsible for 157generating the right response to be sent to the caller. 158 159 160

16

Microsoft Confidential

Page 9 of 11
17

1612.3 Managed fake server applications using Callbacks


162As we mentioned in section 3.1, the managed library provides two methods to the test application to exchange data 163with the clients. In this section we will discuss the callback approach. The callback approach is ideal for test server 164applications who want to send responses to their client application (i.e the application under test) depending on the 165request that they received. If you use the callback approach, you will be able to see the data that was sent by your 166client application (i.e the application under test). Based on the client request you may choose the response you need 167to send.

1682.4 Managed fake server applications using Response Queues


169As we mentioned in section 3.1, the managed library provides two methods to the test application to exchange data 170with the clients. In this section we will discuss the response queue approach. The response queue approach is ideal 171for test server applications that need to send a certain set of responses in sequence to the client application (Ii.e. the 172application under test). For e.g. if you know before hand that you need to send a set of invalid responses to every 173request the client (the application under test) ever made, then you could add such responses to the response queue 174and not worry about callbacks or anything. The managed framework will send out the responses in the order you 175added them to the response queue. 176 177There is also another special queue called the default response queue. Say you added a set of responses to the 178response queue and your test server sent all of them out, in this case the managed library turns to the default 179response queue. This is a queue of one or more default responses that need to be sent when the response queue is 180EMPTY. This queue is special in the fact that it is a circular queue, if add say 3 responses (call it R1, R2, R3) to the 181default response queue, the managed library will send out the responses in order R1, R2, R3, R1, R2, R3

1822.5 Important Note about the Callbacks & Response Queues


183Its important to note that the two approaches to exchange data with the client are completely exclusive. You cannot 184and should not mix or match both of them. For e.g. dont set a callback function and also add responses to the 185response queue at the same time. OR for that matter dont add responses to the response queue and set the callback 186function later. 187 188Pick one of the two supported data exchange mechanisms and stick to it. The behavior of the fake server library is 189undefined if you attempt to mix and match the two approaches. 190 191

1923

Detailed Design

193In the sections below we will discuss the interfaces exposed by 194Microsoft.WindowsMobile.Samples.FakeServer.DLL in greater detail.

1953.1 Microsoft.WindowsMobile.Samples.FakeServer.dll
196 197 198 199 200 201 202 203

This is a managed dll that acts as a wrapper for FakeServer.dll. Its functionality can be broken down into the following areas: 1. Imports the methods that are exported by FakeServer.dll to control the server (like init, start, stop, etc) and exposes them as static methods of FakeServer class 2. Provides a managed class FakeResponse that represents a response that tests might want to send back to the client application. 3. Test applications using this dll can register their own callback function (using the FakeServerRequestCallback delegate) that will be called everytime the server receives a request. This

18

Microsoft Confidential

Page 10 of 11
19 204 205 206 207 208 209 210

4. 5.

can be used to test applications that need to read the request arriving at the server before deciding what response to send. Please refer to section 3.3 for more details. The dll exposes a ResponseQueue This queue can be used by tests if they know the responses that need to be sent back to the client. If you choose the ResponseQueue (from the application that is being tested). Please refer to section 3.4 for more details. The dll also exposes a DefaultResponses list This is a rolling list that is used when the ResponseQueue is empty. Please refer to section 3.4 for more details.

2113.1.1
212 213 214 215 216 217 218 219 220 221 222

FakeServer class
3.1.1.1 Initialize() Initializes the server to listen on port 80 3.1.1.2 InitializeEx(string pbPortNum, Int32 fSecure, string pbCertPath, string pbCertPwd, FakeServerRequestCallback pfnRequestCallback) Initializes the server with the following parameters pbPortNum The port number that the service needs to be started on fSecure This parameter MUST always be 0. pbCertPath Not used, MUST Be null pbCertPwd Not used MUST be NULL pfnRequestCallback User defined call back function

20

Microsoft Confidential

Page 11 of 11
21 223 224 225 226 227 228 229 230 231 232 233 234 235 236

3.1.1.3 Start() Starts the server. Now the server will start listening at the port for requests. 3.1.1.4 Stop() Stops the server from listening for requests. 3.1.1.5 SetRequestCallback(FakeServerRequestCallback) Registers a callback function that will be called eveytime the server receives a request. This will override the ResponseQueue and DefaultResponse functionality. 3.1.1.6 ResponseQueue (PROPERTY) This is a queue of FakeResponse objects used when no callback function is registered. When the server receives a request and this queue is not empty, then it dequeues a response off this queue and sends that to the client. 3.1.1.7 DefaultResponses (PROPERTY) This is used by the server when the ResponseQueue is empty. This is a rolling list of responses. When the end is reached, the server starts looking at this list from the beginning.

2373.1.2
238 239 240 241 242 243

FakeResponse class
3.1.2.1 FakeResponse() Empty constructor 3.1.2.2 FakeResponse(byte[]) Constructor that accepts a byte array to initialize the object. 3.1.2.3 Bytes (PROPERTY) Gets and Sets the array of bytes that representing the FakeResponse object.

2443.1.3
245 246 247 248 249 250

FakeResponseQueue class
3.1.3.1 FakeResponseQueue() Empty constructor 3.1.3.2 Enqueue(FakeResponse) Adds a reponse to the queue at the end. 3.1.3.3 Dequeue() Dequeues a response off the queue

2524

251

Appendix

253Sample applications built on top of the fake server are shipped with the SDK. Please download the windows mobile 254SDK for referring to such samples.

22

Microsoft Confidential

You might also like