feisty meow concerns codebase  2.140
test_security.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : octopus security test *
4 * Author : Chris Koeritz *
5 * *
6 * Purpose: *
7 * *
8 * Checks out the login support for octopus. This just exercises the base *
9 * support which doesn't perform any extra verification on the user. *
10 * *
11 *******************************************************************************
12 * Copyright (c) 2002-$now By Author. This program is free software; you can *
13 * redistribute it and/or modify it under the terms of the GNU General Public *
14 * License as published by the Free Software Foundation; either version 2 of *
15 * the License or (at your option) any later version. This is online at: *
16 * http://www.fsf.org/copyleft/gpl.html *
17 * Please send any updates to: fred@gruntose.com *
18 \*****************************************************************************/
19 
22 #include <basis/astring.h>
23 #include <basis/mutex.h>
25 #include <loggers/console_logger.h>
28 #include <octopus/entity_defs.h>
29 #include <octopus/infoton.h>
30 #include <octopus/octopus.h>
31 #include <octopus/tentacle.h>
34 //#include <structures/static_memory_gremlin.h>
38 #include <unit_test/unit_base.h>
39 
40 using namespace application;
41 using namespace basis;
42 using namespace configuration;
43 using namespace loggers;
44 using namespace mathematics;
45 using namespace octopi;
46 using namespace sockets;
47 using namespace structures;
48 using namespace textual;
49 using namespace unit_test;
50 
51 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s))
52 
54 
55 astring base_list[] = { "cli", "simp" };
56 
57 SAFE_STATIC_CONST(string_array, simp_classifier, (2, base_list))
58 
59 class simple_infoton : public infoton
60 {
61 public:
62  astring futzle;
63 
64  simple_infoton() : infoton(simp_classifier()) {}
65 
66  virtual void text_form(basis::base_string &state_fill) const {
67  state_fill.concatenate_string(astring("futzle=") + futzle);
68  }
69 
70  virtual void pack(byte_array &packed_form) const {
71  futzle.pack(packed_form);
72  }
73  virtual bool unpack(byte_array &packed_form) {
74  if (!futzle.unpack(packed_form)) return false;
75  return true;
76  }
77  virtual int packed_size() const { return futzle.length() + 1; }
78  virtual clonable *clone() const { return new simple_infoton(*this); }
79 
80 private:
81 };
82 
84 
85 // provides a simple service to allow us to test whether the security is
86 // working or not.
87 
88 class simple_tentacle : public tentacle
89 {
90 public:
91  simple_tentacle() : tentacle(simp_classifier(), true) {}
92 
93  virtual outcome reconstitute(const string_array &classifier,
94  byte_array &packed_form, infoton * &reformed) {
95  reformed = NULL_POINTER;
96  if (classifier != simp_classifier()) return NO_HANDLER;
97  reformed = new simple_infoton;
98  if (!reformed->unpack(packed_form)) {
99  WHACK(reformed);
100  return GARBAGE;
101  }
102  return OKAY;
103  }
104 
105  virtual outcome consume(infoton &to_chow,
106  const octopus_request_id &formal(item_id), byte_array &transformed) {
107  transformed.reset();
108  if (to_chow.classifier() != simp_classifier()) return NO_HANDLER;
109  // consume without doing anything.
110  return OKAY;
111  }
112 
113  virtual void expunge(const octopus_entity &formal(to_zap)) {}
114 };
115 
117 
118 //hmmm: this test should do a sample login octopus and do a login, reside for
119 // a while, log out, do another one, let it time out, try to access
120 // something with dead id hoping to be rejected, etc.
121 
122 class test_octopus_security : virtual public unit_base, virtual public application_shell
123 {
124 public:
125  test_octopus_security() : application_shell() {}
126 //class_name()
127  DEFINE_CLASS_NAME("test_octopus_security");
128  virtual int execute();
129 };
130 
131 int test_octopus_security::execute()
132 {
133  FUNCDEF("execute")
134  octopus logos("local", 18 * MEGABYTE);
135  simple_tentacle *tenty = new simple_tentacle;
136  logos.add_tentacle(tenty);
137  tenty = NULL_POINTER; // octopus has charge of this now.
138 
139  // turn on security in logos.
141  logos.add_tentacle(new login_tentacle(*guardian), true);
142 
143  // create an entity to work with.
144  octopus_entity jimbo("localhost", application_configuration::process_id(), 128, 982938);
145  octopus_request_id req1(jimbo, 1);
146 
147  // add the user jimbo.
148  guardian->add_entity(jimbo, byte_array());
149 
150  // create a piece of data to try running on tentacle.
151  simple_infoton testose;
152  simple_infoton *testose_copy = new simple_infoton(testose);
153 
154  // test that the simple tentacle allows the op.
155  outcome ret = logos.evaluate(testose_copy, req1);
156  if (ret != tentacle::OKAY)
157  deadly_error(class_name(), "first test",
158  astring("the operation failed with an error ")
159  + tentacle::outcome_name(ret));
160 
161  // create another entity to work with.
162  octopus_entity burfo("localhost", application_configuration::process_id(), 372, 2989);
163  octopus_request_id req2(burfo, 1);
164 
165  // try with an unlicensed user burfo...
166  testose_copy = new simple_infoton(testose);
167  ret = logos.evaluate(testose_copy, req2);
168  if (ret == tentacle::OKAY)
169  deadly_error(class_name(), "second test",
170  astring("the operation didn't fail when it should have."));
171  else if (ret != tentacle::DISALLOWED)
172  deadly_error(class_name(), "second test",
173  astring("the operation didn't provide the proper outcome, it gave: ")
174  + tentacle::outcome_name(ret));
175 
176  // remove the user jimbo.
177  guardian->zap_entity(jimbo);
178 
179  // test that jimbo fails too now.
180  testose_copy = new simple_infoton(testose);
181  ret = logos.evaluate(testose_copy, req1);
182  if (ret == tentacle::OKAY)
183  deadly_error(class_name(), "third test",
184  astring("the operation didn't fail when it should have."));
185  else if (ret != tentacle::DISALLOWED)
186  deadly_error(class_name(), "third test",
187  astring("the operation didn't provide the proper outcome, it gave: ")
188  + tentacle::outcome_name(ret));
189 
190  // add the user burfo in now instead.
191  guardian->add_entity(burfo, byte_array());
192 
193  // test that burfo works.
194  testose_copy = new simple_infoton(testose);
195  ret = logos.evaluate(testose_copy, req2);
196  if (ret != tentacle::OKAY)
197  deadly_error(class_name(), "fourth test",
198  astring("the operation failed with an error ")
199  + tentacle::outcome_name(ret));
200 
201  LOG(astring(class_name()) + ":: security works for those functions tested.");
202 
203  WHACK(guardian);
204 
205  return 0;
206 }
207 
208 HOOPLE_MAIN(test_octopus_security, )
209 
The application_shell is a base object for console programs.
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition: astring.cpp:964
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
bool unpack(byte_array &source)
retrieves a string (packed with pack()) from "source" into this string.
Definition: astring.cpp:967
Defines the base class for all string processing objects in hoople.
Definition: base_string.h:28
virtual base_string & concatenate_string(const base_string &s)=0
Modifies "this" by concatenating "s" onto it.
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
A clonable object knows how to make copy of itself.
Definition: contracts.h:109
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
Defines installation-specific locations in the file system.
An infoton is an individual request parcel with accompanying information.
Definition: infoton.h:32
virtual bool unpack(basis::byte_array &packed_form)=0
restores an infoton from a packed form.
const structures::string_array & classifier() const
this array of strings is the "name" for this infoton.
Definition: infoton.cpp:85
Provides rudimentary login services.
Provides a way of identifying users of an octopus object.
Definition: entity_defs.h:35
Identifies requests made on an octopus by users.
Definition: entity_defs.h:114
Octopus is a design pattern for generalized request processing systems.
Definition: octopus.h:47
Provides a basic implementation of an entity_registry for octopus.
Manages a service within an octopus by processing certain infotons.
Definition: tentacle.h:36
An array of strings with some additional helpful methods.
Definition: string_array.h:32
#define deadly_error(c, f, i)
#define formal(parameter)
This macro just eats what it's passed; it marks unused formal parameters.
Definition: definitions.h:48
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
const int MEGABYTE
Number of bytes in a megabyte.
Definition: definitions.h:135
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
Provides access to the operating system's socket methods.
Definition: base_address.h:26
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
bool unpack(basis::byte_array &packed_form, set< contents > &to_unpack)
provides a way to unpack any set that stores packable objects.
Definition: set.h:139
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
Definition: set.h:131
int packed_size(const byte_array &packed_form)
Reports the size required to pack a byte array into a byte array.
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define SAFE_STATIC_CONST(type, func_name, parms)
this version returns a constant object instead.
astring base_list[]
#define LOG(s)
#define test(expr)