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>
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
40using namespace application;
41using namespace basis;
42using namespace configuration;
43using namespace loggers;
44using namespace mathematics;
45using namespace octopi;
46using namespace sockets;
47using namespace structures;
48using namespace textual;
49using namespace unit_test;
50
51#define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s))
52
54
55astring base_list[] = { "cli", "simp" };
56
57SAFE_STATIC_CONST(string_array, simp_classifier, (2, base_list))
58
59class simple_infoton : public infoton
60{
61public:
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
80private:
81};
82
84
85// provides a simple service to allow us to test whether the security is
86// working or not.
87
88class simple_tentacle : public tentacle
89{
90public:
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
122class test_octopus_security : virtual public unit_base, virtual public application_shell
123{
124public:
125 test_octopus_security() : application_shell() {}
126//class_name()
127 DEFINE_CLASS_NAME("test_octopus_security");
128 virtual int execute();
129};
130
131int 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
208HOOPLE_MAIN(test_octopus_security, )
209
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
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 basis::astring text_form() const
local version just makes text_form() more functional.
Definition infoton.h:108
virtual int packed_size() const =0
reports how large the infoton will be when packed.
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
virtual clonable * clone() const =0
must be provided to allow creation of a copy of this object.
virtual void pack(basis::byte_array &packed_form) const =0
stuffs the data in the infoton into the "packed_form".
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.
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
virtual basis::outcome consume(infoton &to_chow, const octopus_request_id &item_id, basis::byte_array &transformed)=0
this is the main function that processes infotons for this tentacle.
virtual basis::outcome reconstitute(const structures::string_array &classifier, basis::byte_array &packed_form, infoton *&reformed)=0
regenerates an infoton from its packed form.
virtual void expunge(const octopus_entity &to_remove)=0
called to remove traces of the entity "to_remove".
@ NO_HANDLER
no handler for that type of infoton.
Definition tentacle.h:73
An array of strings with some additional helpful methods.
#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.
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.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
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)