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
4 : * be found in the LICENSE file.
5 : */
6 :
7 : #include <stdarg.h>
8 : #include <stdio.h>
9 : #include <stdlib.h>
10 : #include <string.h>
11 :
12 : #include "native_client/src/trusted/gdb_rsp/test.h"
13 : #include "native_client/src/trusted/port/platform.h"
14 :
15 : // Mock portability objects
16 : namespace port {
17 1 : void IPlatform::Relinquish(uint32_t msec) {
18 : (void) msec;
19 : return;
20 : }
21 :
22 0 : void IPlatform::LogInfo(const char *fmt, ...) {
23 : va_list argptr;
24 0 : va_start(argptr, fmt);
25 :
26 0 : vprintf(fmt, argptr);
27 0 : }
28 :
29 0 : void IPlatform::LogWarning(const char *fmt, ...) {
30 : va_list argptr;
31 0 : va_start(argptr, fmt);
32 :
33 0 : vprintf(fmt, argptr);
34 0 : }
35 :
36 0 : void IPlatform::LogError(const char *fmt, ...) {
37 : va_list argptr;
38 0 : va_start(argptr, fmt);
39 :
40 0 : vprintf(fmt, argptr);
41 0 : }
42 :
43 : // The unit tests are singly threaded, so we just do nothing
44 : // for synchronization
45 14 : class MutexMock : public IMutex {
46 29 : void Lock() {}
47 29 : void Unlock() {}
48 0 : bool Try() { return true; }
49 : };
50 :
51 7 : IMutex* IMutex::Allocate() { return new MutexMock; }
52 7 : void IMutex::Free(IMutex* mtx) { delete static_cast<MutexMock*>(mtx); }
53 :
54 4 : class EventMock : public IEvent {
55 3 : void Signal() {}
56 1 : void Wait() {}
57 : };
58 :
59 2 : IEvent* IEvent::Allocate() { return new EventMock; }
60 2 : void IEvent::Free(IEvent* e) { delete static_cast<EventMock*>(e); }
61 :
62 : class ThreadMock : public IThread {
63 : public:
64 4 : explicit ThreadMock(uint32_t id) {
65 4 : id_ = id;
66 4 : ctx_ = new uint8_t[gdb_rsp::Abi::Get()->GetContextSize()];
67 4 : }
68 0 : ~ThreadMock() { delete[] ctx_; }
69 :
70 2 : uint32_t GetId() { return id_; }
71 0 : State GetState() { return IThread::SIGNALED; }
72 :
73 0 : bool SetStep(bool on) {
74 : (void) on;
75 0 : return true;
76 : }
77 :
78 16 : bool GetRegister(uint32_t index, void *dst, uint32_t len) {
79 16 : const gdb_rsp::Abi* abi = gdb_rsp::Abi::Get();
80 16 : const gdb_rsp::Abi::RegDef *reg = abi->GetRegisterDef(index);
81 16 : memcpy(dst, ctx_ + reg->offset_, len);
82 16 : return true;
83 : }
84 :
85 64 : bool SetRegister(uint32_t index, void *src, uint32_t len) {
86 64 : const gdb_rsp::Abi* abi = gdb_rsp::Abi::Get();
87 64 : const gdb_rsp::Abi::RegDef *reg = abi->GetRegisterDef(index);
88 64 : memcpy(ctx_ + reg->offset_, src, len);
89 64 : return true;
90 : }
91 :
92 0 : bool Suspend() { return true; }
93 0 : bool Resume() { return true; }
94 :
95 0 : virtual void *GetContext() { return ctx_; }
96 :
97 : private:
98 : uint8_t *ctx_;
99 : uint32_t id_;
100 : };
101 :
102 4 : IThread* IThread::Acquire(uint32_t id, bool create) {
103 4 : if (create) return new ThreadMock(id);
104 0 : return NULL;
105 : }
106 :
107 :
108 0 : bool port::IPlatform::GetMemory(uint64_t addr, uint32_t len, void *dst) {
109 0 : intptr_t iptr = static_cast<intptr_t>(addr);
110 0 : void *src = reinterpret_cast<void*>(iptr);
111 0 : memcpy(dst, src, len);
112 0 : return true;
113 : }
114 :
115 0 : bool port::IPlatform::SetMemory(uint64_t addr, uint32_t len, void *src) {
116 0 : intptr_t iptr = static_cast<intptr_t>(addr);
117 0 : void *dst = reinterpret_cast<void*>(iptr);
118 0 : memcpy(dst, src, len);
119 0 : return true;
120 : }
121 :
122 :
123 : } // End of namespace port
124 :
125 1 : int main(int argc, const char *argv[]) {
126 1 : int errs = 0;
127 :
128 : (void) argc;
129 : (void) argv;
130 :
131 1 : printf("Testing Utils.\n");
132 1 : errs += TestUtil();
133 :
134 1 : printf("Testing ABI.\n");
135 1 : errs += TestAbi();
136 :
137 1 : printf("Testing Packets.\n");
138 1 : errs += TestPacket();
139 :
140 1 : printf("Testing Session.\n");
141 1 : errs += TestSession();
142 :
143 1 : printf("Testing Host.\n");
144 1 : errs += TestHost();
145 :
146 1 : printf("Testing Target.\n");
147 1 : errs += TestTarget();
148 :
149 1 : if (errs) printf("FAILED with %d errors.\n", errs);
150 1 : return errs;
151 : }
152 :
|