vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / app / bmessenger / MessengerAssignmentTester.cpp
blob953f36d89bbc457caa172a3100f32bc84a14716d
1 //------------------------------------------------------------------------------
2 // MessengerAssignmentTester.cpp
3 //
4 //------------------------------------------------------------------------------
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
9 // System Includes -------------------------------------------------------------
10 #include <Message.h>
11 #include <OS.h>
12 #include <Handler.h>
13 #include <Looper.h>
14 #include <Messenger.h>
16 // Project Includes ------------------------------------------------------------
17 #include <TestUtils.h>
18 #include <ThreadedTestCaller.h>
19 #include <cppunit/TestSuite.h>
21 // Local Includes --------------------------------------------------------------
22 #include "Helpers.h"
23 #include "MessengerAssignmentTester.h"
25 // Local Defines ---------------------------------------------------------------
27 // Globals ---------------------------------------------------------------------
29 //------------------------------------------------------------------------------
31 // constructor
32 MessengerAssignmentTester::MessengerAssignmentTester()
33 : BThreadedTestCase(),
34 fHandler(NULL),
35 fLooper(NULL)
39 // constructor
40 MessengerAssignmentTester::MessengerAssignmentTester(std::string name)
41 : BThreadedTestCase(name),
42 fHandler(NULL),
43 fLooper(NULL)
47 // destructor
48 MessengerAssignmentTester::~MessengerAssignmentTester()
50 if (fLooper) {
51 fLooper->Lock();
52 if (fHandler) {
53 fLooper->RemoveHandler(fHandler);
54 delete fHandler;
56 fLooper->Quit();
61 BMessenger &operator=(const BMessenger &from)
62 @case 1 from is uninitialized
63 @results IsValid() and IsTargetLocal() should return false
64 Target() should return NULL and NULL for looper.
65 Team() should return -1.
67 void MessengerAssignmentTester::AssignmentTest1()
69 BMessenger from;
70 BMessenger messenger;
71 CHK(&(messenger = from) == &messenger);
72 CHK(messenger.IsValid() == false);
73 CHK(messenger.IsTargetLocal() == false);
74 BLooper *looper;
75 CHK(messenger.Target(&looper) == NULL);
76 CHK(looper == NULL);
77 CHK(messenger.Team() == -1);
81 BMessenger &operator=(const BMessenger &from)
82 @case 2 from is properly initialized to a local target
83 (preferred handler)
84 @results IsValid() and IsTargetLocal() should return true
85 Target() should return the same values as for from.
86 Team() should return this team.
88 void MessengerAssignmentTester::AssignmentTest2()
90 // create looper
91 status_t result = B_OK;
92 BLooper *looper = new BLooper;
93 looper->Run();
94 LooperQuitter quitter(looper);
95 // create the from messenger
96 BMessenger from(NULL, looper, &result);
97 CHK(from.IsValid() == true);
98 CHK(from.IsTargetLocal() == true);
99 BLooper *resultLooper;
100 CHK(from.Target(&resultLooper) == NULL);
101 CHK(resultLooper == looper);
102 CHK(from.Team() == get_this_team());
103 CHK(result == B_OK);
104 // create the messenger and do the checks
105 BMessenger messenger;
106 CHK(&(messenger = from) == &messenger);
107 CHK(messenger.IsValid() == true);
108 CHK(messenger.IsTargetLocal() == true);
109 resultLooper = NULL;
110 CHK(messenger.Target(&resultLooper) == NULL);
111 CHK(resultLooper == looper);
112 CHK(messenger.Team() == get_this_team());
116 BMessenger &operator=(const BMessenger &from)
117 @case 3 from is properly initialized to a local target
118 (specific handler)
119 @results IsValid() and IsTargetLocal() should return true
120 Target() should return the same values as for from.
121 Team() should return this team.
123 void MessengerAssignmentTester::AssignmentTest3()
125 // create looper and handler
126 status_t result = B_OK;
127 BLooper *looper = new BLooper;
128 looper->Run();
129 LooperQuitter quitter(looper);
130 BHandler *handler = new BHandler;
131 HandlerDeleter deleter(handler);
132 CHK(looper->Lock());
133 looper->AddHandler(handler);
134 looper->Unlock();
135 // create the from messenger
136 BMessenger from(handler, looper, &result);
137 CHK(from.IsValid() == true);
138 CHK(from.IsTargetLocal() == true);
139 BLooper *resultLooper;
140 CHK(from.Target(&resultLooper) == handler);
141 CHK(resultLooper == looper);
142 CHK(from.Team() == get_this_team());
143 CHK(result == B_OK);
144 // create the messenger and do the checks
145 BMessenger messenger;
146 CHK(&(messenger = from) == &messenger);
147 CHK(messenger.IsValid() == true);
148 CHK(messenger.IsTargetLocal() == true);
149 resultLooper = NULL;
150 CHK(messenger.Target(&resultLooper) == handler);
151 CHK(resultLooper == looper);
152 CHK(messenger.Team() == get_this_team());
156 Test* MessengerAssignmentTester::Suite()
158 typedef BThreadedTestCaller<MessengerAssignmentTester> TC;
160 TestSuite* testSuite = new TestSuite;
162 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest1);
163 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest2);
164 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest3);
166 return testSuite;