vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / app / bmessage / MessageMessageItemTest.h
blob65f7868378b292fc6d15499742dff828cc960d85
1 //------------------------------------------------------------------------------
2 // MessageMessageItemTest.h
3 //
4 //------------------------------------------------------------------------------
6 #ifndef MESSAGEMESSAGEITEMTEST_H
7 #define MESSAGEMESSAGEITEMTEST_H
9 // Standard Includes -----------------------------------------------------------
11 // System Includes -------------------------------------------------------------
12 #include <Debug.h>
14 // Project Includes ------------------------------------------------------------
16 // Local Includes --------------------------------------------------------------
17 #include "MessageItemTest.h"
19 // Local Defines ---------------------------------------------------------------
21 // Globals ---------------------------------------------------------------------
23 //------------------------------------------------------------------------------
24 struct TMessageFuncPolicy
26 static status_t Add(BMessage& msg, const char* name, BMessage& val);
27 static status_t AddData(BMessage& msg, const char* name, type_code type,
28 BMessage* data, ssize_t size, bool);
29 static status_t Find(BMessage& msg, const char* name, int32 index,
30 BMessage* val);
31 static status_t ShortFind(BMessage& msg, const char* name, BMessage* val);
32 static BMessage QuickFind(BMessage& msg, const char* name, int32 index);
33 static bool Has(BMessage& msg, const char* name, int32 index);
34 static status_t Replace(BMessage& msg, const char* name, int32 index,
35 BMessage& val);
36 static status_t FindData(BMessage& msg, const char* name, type_code type,
37 int32 index, const void** data, ssize_t* size);
39 private:
40 static BMessage sMsg;
42 BMessage TMessageFuncPolicy::sMsg;
43 //------------------------------------------------------------------------------
44 status_t TMessageFuncPolicy::Add(BMessage& msg, const char* name,
45 BMessage& val)
47 return msg.AddMessage(name, &val);
49 //------------------------------------------------------------------------------
50 status_t TMessageFuncPolicy::AddData(BMessage& msg, const char* name,
51 type_code type, BMessage* data,
52 ssize_t size, bool)
54 char* buf = new char[size];
55 status_t err = data->Flatten(buf, size);
56 if (!err)
58 err = msg.AddData(name, type, buf, size, false);
60 delete[] buf;
61 return err;
63 //------------------------------------------------------------------------------
64 inline status_t TMessageFuncPolicy::Find(BMessage& msg, const char* name,
65 int32 index, BMessage* val)
67 return msg.FindMessage(name, index, val);
69 //------------------------------------------------------------------------------
70 inline status_t TMessageFuncPolicy::ShortFind(BMessage& msg, const char* name,
71 BMessage* val)
73 return msg.FindMessage(name, val);
75 //------------------------------------------------------------------------------
76 BMessage TMessageFuncPolicy::QuickFind(BMessage& msg, const char* name,
77 int32 index)
79 BMessage val;
80 msg.FindMessage(name, index, &val);
81 return val;
83 //------------------------------------------------------------------------------
84 inline bool TMessageFuncPolicy::Has(BMessage& msg, const char* name,
85 int32 index)
87 return msg.HasMessage(name, index);
89 //------------------------------------------------------------------------------
90 inline status_t TMessageFuncPolicy::Replace(BMessage& msg, const char* name,
91 int32 index, BMessage& val)
93 return msg.ReplaceMessage(name, index, &val);
95 //------------------------------------------------------------------------------
96 inline status_t TMessageFuncPolicy::FindData(BMessage& msg, const char* name,
97 type_code type, int32 index,
98 const void** data, ssize_t* size)
100 *data = NULL;
101 char* ptr;
102 status_t err = msg.FindData(name, type, index, (const void**)&ptr, size);
103 if (!err)
105 err = sMsg.Unflatten(ptr);
106 if (!err)
108 *(BMessage**)data = &sMsg;
111 return err;
112 // return (msg.*FindDataFunc)(name, type, index, data, size);
114 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 template<>
119 struct ArrayTypeBase<BMessage>
121 class ArrayType
123 public:
124 ArrayType() : array(NULL), size(0) {;}
125 ArrayType(const ArrayType& rhs) : array(NULL), size(0)
126 { *this = rhs; }
127 ArrayType(BMessage* data, uint32 len) : array(NULL), size(0)
128 { Assign(data, len); }
129 ~ArrayType() { if (array) delete[] array; }
131 ArrayType& operator=(const ArrayType& rhs)
133 if (this != &rhs)
134 Assign(rhs.array, rhs.size);
135 return *this;
138 uint32 Size() { return size; }
139 BMessage& operator[](int index)
141 // We're just gonna let a segfault happen
142 return array[index];
145 private:
146 void Assign(BMessage* data, uint32 len)
148 size = len;
150 if (array)
151 delete[] array;
152 array = new BMessage[Size()];
153 for (uint32 i = 0; i < Size(); ++i)
154 array[i] = data[i];
156 BMessage* array;
157 uint32 size;
160 typedef uint32 SizeType;
161 static SizeType Size(ArrayType& array) { return array.Size(); }
163 //------------------------------------------------------------------------------
164 struct TMessageInitPolicy : public ArrayTypeBase<BMessage>
166 inline static BMessage Zero() { return BMessage(); }
167 static BMessage Test1()
169 BMessage msg('1234');
170 msg.AddInt32("int32", 1234);
171 return msg;
173 static BMessage Test2()
175 BMessage msg('5678');
176 msg.AddString("string", "5678");
177 return msg;
179 static size_t SizeOf(const BMessage& data)
181 return data.FlattenedSize();
183 inline static ArrayType Array()
185 BMessage array[3];
186 array[0] = Zero();
187 array[1] = Test1();
188 array[2] = Test2();
189 return ArrayType(array, 3);
192 //------------------------------------------------------------------------------
193 struct TMessageAssertPolicy
195 inline static BMessage Zero() { return BMessage(); }
196 inline static BMessage Invalid() { return BMessage(); }
197 static bool Size(size_t size, BMessage& msg)
198 ;//{ return size == msg.FlattenedSize(); }
200 bool TMessageAssertPolicy::Size(size_t size, BMessage& msg)
202 ssize_t msgSize = msg.FlattenedSize();
203 return size == (size_t)msgSize;
205 //------------------------------------------------------------------------------
206 template<>
207 struct TMessageItemComparePolicy<BMessage>
209 inline static bool Compare(const BMessage& lhs, const BMessage& rhs)
210 { return lhs.what == rhs.what; }
212 //------------------------------------------------------------------------------
213 typedef TMessageItemTest
215 BMessage,
216 B_MESSAGE_TYPE,
217 TMessageFuncPolicy,
218 TMessageInitPolicy,
219 TMessageAssertPolicy
221 TMessageMessageItemTest;
222 //------------------------------------------------------------------------------
224 #endif // MESSAGEMESSAGEITEMTEST_H
227 * $Log $
229 * $Id $