Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / Security / Secure_Invocation / client.cpp
blob2647e950bc32a1231f5545a753cebea267ee7970
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
4 #include "FooC.h"
5 #include "orbsvcs/SecurityC.h"
6 #include "ace/SString.h"
8 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
9 const char *cert_file = "cacert.pem";
11 void
12 insecure_invocation_test (CORBA::ORB_ptr orb,
13 CORBA::Object_ptr obj)
15 // Disable protection for this insecure invocation test.
17 Security::QOP qop = Security::SecQOPNoProtection;
19 CORBA::Any no_protection;
20 no_protection <<= qop;
22 // Create the Security::QOPPolicy.
23 CORBA::Policy_var policy =
24 orb->create_policy (Security::SecQOPPolicy,
25 no_protection);
27 CORBA::PolicyList policy_list (1);
28 policy_list.length (1);
29 policy_list[0] = CORBA::Policy::_duplicate (policy.in ());
31 // Create an object reference that uses plain IIOP (i.e. no
32 // protection).
33 CORBA::Object_var object =
34 obj->_set_policy_overrides (policy_list,
35 CORBA::SET_OVERRIDE);
37 Foo::Bar_var server =
38 Foo::Bar::_narrow (object.in ());
40 if (CORBA::is_nil (server.in ()))
42 ACE_ERROR ((LM_ERROR,
43 "(%P|%t) ERROR: Object reference <%s> is "
44 "nil.\n",
45 ior));
47 throw CORBA::INTERNAL ();
50 try
52 // This invocation should result in a CORBA::NO_PERMISSION
53 // exception.
54 server->baz ();
56 catch (const CORBA::NO_PERMISSION&)
58 ACE_DEBUG ((LM_INFO,
59 "(%P|%t) Received CORBA::NO_PERMISSION from "
60 "server, as expected.\n"));
62 return;
65 ACE_ERROR ((LM_ERROR,
66 "(%P|%t) ERROR: CORBA::NO_PERMISSION was not thrown.\n"
67 "(%P|%t) ERROR: It should have been thrown.\n"));
69 throw CORBA::INTERNAL ();
72 void
73 secure_invocation_test (CORBA::Object_ptr object)
75 Foo::Bar_var server =
76 Foo::Bar::_narrow (object);
78 if (CORBA::is_nil (server.in ()))
80 ACE_ERROR ((LM_ERROR,
81 "(%P|%t) ERROR: Object reference <%s> is "
82 "nil.\n",
83 ior));
85 throw CORBA::INTERNAL ();
88 // This invocation should return successfully.
89 server->baz ();
91 server->shutdown ();
94 int
95 parse_args (int argc, ACE_TCHAR *argv[])
97 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
98 int c;
100 while ((c = get_opts ()) != -1)
101 switch (c)
103 case 'k':
104 ior = get_opts.opt_arg ();
105 break;
106 case '?':
107 default:
108 ACE_ERROR_RETURN ((LM_ERROR,
109 "Usage: %s "
110 "-k <ior> "
111 "\n",
112 argv [0]),
113 -1);
115 // Indicates successful parsing of the command line
116 return 0;
120 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
124 CORBA::ORB_var orb =
125 CORBA::ORB_init (argc, argv);
127 if (parse_args (argc, argv) != 0)
128 return 1;
130 CORBA::Object_var object =
131 orb->string_to_object (ior);
133 // This test sets creates a Security::QOPPolicy with the
134 // Quality-of-Protection set to "no protection." It then
135 // invokes a method on the server (insecurely), which should
136 // then result in a CORBA::NO_PERMISSION exception.
138 // The server is not shutdown by this test.
139 insecure_invocation_test (orb.in (), object.in ());
141 // This test uses the default secure SSLIOP settings to securely
142 // invoke a method on the server. No exception should occur.
144 // The server *is* shutdown by this test.
145 secure_invocation_test (object.in ());
147 orb->destroy ();
149 catch (const CORBA::Exception& ex)
151 ex._tao_print_exception ("Caught exception:");
152 return 1;
155 ACE_DEBUG ((LM_DEBUG,
156 "\n"
157 "Secure_Invocation test passed.\n"));
159 return 0;