summaryrefslogtreecommitdiff
path: root/TAO/tests/Permanent_Forward/StubTest.cpp
blob: 21fbd5409bdb30e61f6213e0da91cdbfcc371844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// $Id$

//========================================================================
/**
 * @file  StubTest.cpp
 *
 * This program tests the basic functionality of the TAO_Stub in case
 * of permanent_forward
 *
 * @author Frank Rehberger
 */
//=========================================================================


#include "ace/Log_Msg.h"
#include "ace/OS_NS_string.h"
#include "tao/ORB.h"
#include "tao/Environment.h"
#include "tao/SystemException.h"
#include "tao/Object.h"
#include "tao/Stub.h"
#include "tao/Profile.h"
#include "tao/IIOP_Profile.h"

#ifndef FRANKS_ASSERT
#define FRANKS_ASSERT(X) \
  ((X)                                                                  \
   ? static_cast<void>(0)                                               \
   : ACE_VERSIONED_NAMESPACE_NAME::__ace_assert(__FILE__, __LINE__, ACE_TEXT_CHAR_TO_TCHAR (#X)))
#endif

ACE_RCSID (Permanent_Forward,
           StubTest,
           "$Id$")

static bool
is_endpoint (TAO_Profile *profile, const char *host, unsigned short port)
  {
    TAO_IIOP_Profile *iiop = dynamic_cast<TAO_IIOP_Profile*> (profile);
    TAO_Endpoint *endpoint = iiop->endpoint ();
    TAO_IIOP_Endpoint *iiop_endpoint = dynamic_cast<TAO_IIOP_Endpoint*> (endpoint);
    const char     * endpoint_host = iiop_endpoint->host();
    unsigned short   endpoint_port = iiop_endpoint->port();

    bool retval =
      ACE_OS::strcmp (endpoint_host, host)==0
      && endpoint_port == port;

    return retval;
  }

static bool
equal_endpoint (TAO_Profile *profile, TAO_Profile *other)
  {
    TAO_IIOP_Profile *iiop = dynamic_cast<TAO_IIOP_Profile*> (profile);
    TAO_Endpoint *endpoint = iiop->endpoint ();
    TAO_IIOP_Endpoint *iiop_endpoint = dynamic_cast<TAO_IIOP_Endpoint*> (endpoint);
    const char     * endpoint_host = iiop_endpoint->host();
    unsigned short   endpoint_port = iiop_endpoint->port();

    TAO_IIOP_Profile *other_iiop = dynamic_cast<TAO_IIOP_Profile*> (other);
    TAO_Endpoint *other_endpoint = other_iiop->endpoint ();
    TAO_IIOP_Endpoint *other_iiop_endpoint = dynamic_cast<TAO_IIOP_Endpoint*> (other_endpoint);
    const char     * other_endpoint_host = other_iiop_endpoint->host();
    unsigned short   other_endpoint_port = other_iiop_endpoint->port();

    bool retval =
      ACE_OS::strcmp (endpoint_host, other_endpoint_host)==0
      && endpoint_port == other_endpoint_port;

    return retval;
  }

static bool
marshaled_equal_to_other (CORBA::ORB_ptr orb, CORBA::Object_ptr obj, TAO_MProfile *other_mprofile)
{
  CORBA::String_var str = orb->object_to_string (obj ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj_copy = orb->string_to_object (str.in () ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  TAO_Stub *stub_copy = obj_copy->_stubobj ();
  TAO_MProfile *copy_mprofile  = &(stub_copy->base_profiles ());

  if ( copy_mprofile->size() != other_mprofile->size())
    return false;

  for (size_t i=0; i<copy_mprofile->size(); ++i)
    {
      TAO_Profile *copy_profile  = copy_mprofile->get_profile (i);
      TAO_Profile *other_profile = other_mprofile->get_profile (i);

      if ( ! equal_endpoint (copy_profile, other_profile) )
        return false;
    }

  return true;
}

static void
test_forward_permanent (CORBA::ORB_ptr orb)
{
  CORBA::Object_var obj1 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:1111,iiop:192.168.1.3:1111/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  // the permanent profile
  CORBA::Object_var obj4 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:4444,iiop:192.168.1.3:4444/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  TAO_Stub *stub1 = obj1->_stubobj ();
  TAO_Stub *stub4 = obj4->_stubobj ();

  TAO_Profile *profile = NULL;

  profile = stub1->profile_in_use ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 1111));

  // ----- forward to obj2 permanently
  stub1->add_forward_profiles (stub4->base_profiles(), true /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 4444));

  // ----- stringified object reference must be equal to obj4->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub4->base_profiles())) );

  // ----- consume second profile of obj2

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 4444));


  // ----- reached end, next_profile() must yield NULL

  profile = stub1->next_profile ();

  FRANKS_ASSERT (profile == NULL);

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);
}

static void
test_forward_permanent_mix (CORBA::ORB_ptr orb)
{
  CORBA::Object_var obj1 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:1111,iiop:192.168.1.3:1111/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj2 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:2222,iiop:192.168.1.3:2222/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj3 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:3333,iiop:192.168.1.3:3333/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj4 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:4444,iiop:192.168.1.3:4444/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj5 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:5555,iiop:192.168.1.3:5555/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  TAO_Stub *stub1 = obj1->_stubobj ();
  TAO_Stub *stub2 = obj2->_stubobj ();
  TAO_Stub *stub3 = obj3->_stubobj ();
  TAO_Stub *stub4 = obj4->_stubobj ();
  TAO_Stub *stub5 = obj5->_stubobj ();

  TAO_Profile *profile = NULL;

  profile = stub1->profile_in_use ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 1111));

  // ----- forward to obj2
  stub1->add_forward_profiles (stub2->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 2222));

  // ----- forward to obj3
  stub1->add_forward_profiles (stub3->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 3333));

  // ----- stringified object reference must be equal to obj1->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub1->base_profiles())) );

  // ----- forward to obj4 permanently
  stub1->add_forward_profiles (stub4->base_profiles(), true /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 4444));

  // ----- stringified object reference must be equal to obj4->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub4->base_profiles())) );

  // ----- consume second profile from obj4
  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 4444));

  // ----- forward to obj2
  stub1->add_forward_profiles (stub2->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 2222));

  // ----- forward to obj3
  stub1->add_forward_profiles (stub3->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 3333));

  // ----- stringified object reference must be equal to obj4->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub4->base_profiles())) );

  // ----- forward to obj5 permanently
  stub1->add_forward_profiles (stub5->base_profiles(), true /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 5555));

  // ----- stringified object reference must be equal to obj5->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub5->base_profiles())) );

  // ----- consume second profile from obj5
  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 5555));

  // ----- stringified object reference must be equal to obj1->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub5->base_profiles())) );

  // reached end of profiles, next_profile must return NULL

  profile = stub1->next_profile ();

  FRANKS_ASSERT (profile == NULL);
}

static void
test_forward (CORBA::ORB_ptr orb)
{
  CORBA::Object_var obj1 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:1111,iiop:192.168.1.3:1111/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj2 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:2222,iiop:192.168.1.3:2222/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  CORBA::Object_var obj3 = orb->string_to_object ("corbaloc:iiop:192.168.1.2:3333,iiop:192.168.1.3:3333/NameService" ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;

  TAO_Stub *stub1 = obj1->_stubobj ();
  TAO_Stub *stub2 = obj2->_stubobj ();
  TAO_Stub *stub3 = obj3->_stubobj ();

  TAO_Profile *profile = NULL;

  profile = stub1->profile_in_use ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 1111));

  // ----- forward to obj2
  stub1->add_forward_profiles (stub2->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 2222));

  // ----- forward to obj3

  stub1->add_forward_profiles (stub3->base_profiles(), false /* permanent */ );

  FRANKS_ASSERT (stub1->forward_profiles () != NULL);

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.2", 3333));

  // ----- stringified object reference must be equal to obj1->base_prpfiles().
  FRANKS_ASSERT (marshaled_equal_to_other (orb, obj1.in (), &(stub1->base_profiles())) );

  // ----- consume second profile of obj3

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 3333));

  // ----- consume second profile of obj2

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 2222));

  // ----- consume second profile of obj1

  profile = stub1->next_profile ();

  FRANKS_ASSERT (is_endpoint (profile, "192.168.1.3", 1111));

  FRANKS_ASSERT (stub1->forward_profiles () == NULL);

  // ----- reached end, next_profile() must yield NULL

  profile = stub1->next_profile ();
}

int
main (int argc, char *argv[])
{
  ACE_DEBUG ((LM_DEBUG, "---------------------------------------------\n"));
  ACE_DEBUG ((LM_DEBUG, "Running the Stub Tests.\n"));

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Retrieve the ORB.
      CORBA::ORB_var orb = CORBA::ORB_init (argc,
                                            argv,
                                            ""
                                            ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      test_forward (orb.in());
      test_forward_permanent (orb.in());
      test_forward_permanent_mix (orb.in());
    }
  ACE_CATCH (CORBA::SystemException, sysex)
    {
      ACE_PRINT_EXCEPTION (sysex,
                           "Unexpected system Exception!!\n");
      return -1;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Unexpected ACE_CATCHANY Exception!\n");
      return -1;
    }
  ACE_ENDTRY;

  ACE_DEBUG ((LM_DEBUG, "Stub Tests Successfully Completed!\n"));
  ACE_DEBUG ((LM_DEBUG, "---------------------------------------------\n"));

  return 0;
}