Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / debug_stub / packet_test.cc
1 /*
2  * Copyright 2010 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <string>
12 #include <sstream>
13 #include <vector>
14
15 #include "native_client/src/trusted/debug_stub/packet.h"
16 #include "native_client/src/trusted/debug_stub/test.h"
17
18 using gdb_rsp::Packet;
19
20 int VerifyPacket(Packet *wr, Packet *rd, void *ctx, PacketFunc_t tx) {
21   int errs = 0;
22   char ch;
23   std::string str;
24
25   uint8_t  byte = 0;
26   uint16_t word16 = 0;
27   uint32_t word32 = 0;
28
29   // Clear the packet may be reused
30   wr->Clear();
31
32   // Verify non alligned writes
33   wr->AddRawChar('X');
34   wr->AddWord16(0x1234);
35   wr->AddWord32(0x56789ABC);
36   wr->AddWord8(0xFF);
37   if (tx) tx(ctx, wr, rd);
38   rd->GetString(&str);
39   if (strcmp("X3412bc9a7856ff", str.data())) errs++;
40
41   // Verify rewind
42   rd->Rewind();
43   rd->GetRawChar(&ch);
44   if (ch != 'X') errs++;
45
46   rd->GetWord16(&word16);
47   if (word16 != 0x1234) errs++;
48
49   rd->GetWord32(&word32);
50   if (word32 != 0x56789ABC) errs++;
51
52   rd->GetWord8(&byte);
53   if (byte != 0xFF) errs++;
54
55   // Check Empty Send
56   wr->Clear();
57   if (tx) tx(ctx, wr, rd);
58   if (rd->GetRawChar(&ch)) {
59     errs++;
60     printf("Was expecting an empty packet\n");
61   }
62
63   // Check nibble/byte order
64   // Check edge cases of 1, 0, -1, -2
65   wr->AddNumberSep(0x123, ',');
66   wr->AddNumberSep(1, ',');
67   wr->AddNumberSep(0, ',');
68   wr->AddNumberSep(static_cast<uint64_t>(-1), ',');
69   wr->AddNumberSep(static_cast<uint64_t>(-2), 0);
70   if (tx) tx(ctx, wr, rd);
71   rd->GetString(&str);
72   if (strcmp("123,1,0,-1,fffffffffffffffe", str.data()) != 0) errs++;
73
74   // We push "compress" which was generated by a real gdbserver, through into
75   // our packet, then pull it out as a block to decompress, then check against
76   // the 32b values generated by a real gdb debugger. (Inline golden file)
77   const char *compress = "0*488c7280038c72800d4c72800030**fd00637702020* 23"
78                          "0*\"2b0*\"2b0*\"2b0*\"530*\"2b0*}00080f83f00c022f8"
79                          "07c022f80f3c80040*&80ff3f0**80ff3f7f030* 30*\"0f*"
80                          " 0* 58050* 85f7196c2b0*\"b0b801010*}0*}0*b801f0* ";
81   uint32_t vals[16] = { 0, 0, 0, 0x28C788, 0x28C738, 0x28c7d4, 3, 0, 0x776300FD,
82                         0x202, 0x23, 0x2B, 0x2B, 0x2B, 0x53, 0x2B };
83   uint32_t tmp[16];
84
85   wr->Clear();
86   wr->AddString(compress);
87   if (tx) tx(ctx, wr, rd);
88   rd->GetBlock(tmp, sizeof(vals));
89   if (memcmp(tmp, vals, sizeof(vals))) {
90     errs++;
91     printf("Failed to decompress as expected.\n");
92   }
93
94   if (errs)
95     printf("FAILED PACKET TEST\n");
96
97   return errs;
98 }
99
100 int TestPacket() {
101   int errs = 0;
102   Packet pkt;
103
104   errs += VerifyPacket(&pkt, &pkt, NULL, NULL);
105   return errs;
106 }