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 1 : int VerifyPacket(Packet *wr, Packet *rd, void *ctx, PacketFunc_t tx) {
21 1 : int errs = 0;
22 : char ch;
23 1 : std::string str;
24 :
25 1 : uint8_t byte = 0;
26 1 : uint16_t word16 = 0;
27 1 : uint32_t word32 = 0;
28 :
29 : // Clear the packet may be reused
30 1 : wr->Clear();
31 :
32 : // Verify non alligned writes
33 1 : wr->AddRawChar('X');
34 1 : wr->AddWord16(0x1234);
35 1 : wr->AddWord32(0x56789ABC);
36 1 : wr->AddWord8(0xFF);
37 1 : if (tx) tx(ctx, wr, rd);
38 1 : rd->GetString(&str);
39 1 : if (strcmp("X3412bc9a7856ff", str.data())) errs++;
40 :
41 : // Verify rewind
42 1 : rd->Rewind();
43 1 : rd->GetRawChar(&ch);
44 1 : if (ch != 'X') errs++;
45 :
46 1 : rd->GetWord16(&word16);
47 1 : if (word16 != 0x1234) errs++;
48 :
49 1 : rd->GetWord32(&word32);
50 1 : if (word32 != 0x56789ABC) errs++;
51 :
52 1 : rd->GetWord8(&byte);
53 1 : if (byte != 0xFF) errs++;
54 :
55 : // Check Empty Send
56 1 : wr->Clear();
57 1 : if (tx) tx(ctx, wr, rd);
58 1 : if (rd->GetRawChar(&ch)) {
59 0 : errs++;
60 0 : printf("Was expecting an empty packet\n");
61 : }
62 :
63 : // Check nibble/byte order
64 : // Check edge cases of 1, 0, -1, -2
65 1 : wr->AddNumberSep(0x123, ',');
66 1 : wr->AddNumberSep(1, ',');
67 1 : wr->AddNumberSep(0, ',');
68 1 : wr->AddNumberSep(static_cast<uint64_t>(-1), ',');
69 1 : wr->AddNumberSep(static_cast<uint64_t>(-2), 0);
70 1 : if (tx) tx(ctx, wr, rd);
71 1 : rd->GetString(&str);
72 1 : 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 1 : " 0* 58050* 85f7196c2b0*\"b0b801010*}0*}0*b801f0* ";
81 1 : uint32_t vals[16] = { 0, 0, 0, 0x28C788, 0x28C738, 0x28c7d4, 3, 0, 0x776300FD,
82 1 : 0x202, 0x23, 0x2B, 0x2B, 0x2B, 0x53, 0x2B };
83 : uint32_t tmp[16];
84 :
85 1 : wr->Clear();
86 1 : wr->AddString(compress);
87 1 : if (tx) tx(ctx, wr, rd);
88 1 : rd->GetBlock(tmp, sizeof(vals));
89 1 : if (memcmp(tmp, vals, sizeof(vals))) {
90 0 : errs++;
91 0 : printf("Failed to decompress as expected.\n");
92 : }
93 :
94 1 : if (errs)
95 0 : printf("FAILED PACKET TEST\n");
96 :
97 1 : return errs;
98 1 : }
99 :
100 1 : int TestPacket() {
101 1 : int errs = 0;
102 1 : Packet pkt;
103 :
104 1 : errs += VerifyPacket(&pkt, &pkt, NULL, NULL);
105 1 : return errs;
106 1 : }
|