t_security.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : octopus security test                                             *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Checks out the login support for octopus.  This just exercises the base  *
00009 *  support which doesn't perform any extra verification on the user.          *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
00013 * redistribute it and/or modify it under the terms of the GNU General Public  *
00014 * License as published by the Free Software Foundation; either version 2 of   *
00015 * the License or (at your option) any later version.  This is online at:      *
00016 *     http://www.fsf.org/copyleft/gpl.html                                    *
00017 * Please send any updates to: fred@gruntose.com                               *
00018 \*****************************************************************************/
00019 
00020 #include <basis/istring.h>
00021 #include <basis/mutex.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023 #include <octopus/entity_defs.h>
00024 #include <octopus/infoton.h>
00025 #include <octopus/octopus.h>
00026 #include <octopus/tentacle.h>
00027 #include <opsystem/application_shell.h>
00028 #include <loggers/console_logger.h>
00029 #include <data_struct/static_memory_gremlin.h>
00030 #include <sockets/address.h>
00031 #include <tentacles/login_tentacle.h>
00032 #include <tentacles/simple_entity_registry.h>
00033 
00035 
00036 istring base_list[] = { "cli", "simp" };
00037 
00038 SAFE_STATIC_CONST(string_array, simp_classifier, (2, base_list))
00039 
00040 class simple_infoton : public infoton
00041 {
00042 public:
00043   istring futzle;
00044 
00045   simple_infoton() : infoton(simp_classifier()) {}
00046 
00047   virtual void pack(byte_array &packed_form) const {
00048     futzle.pack(packed_form);
00049   }
00050   virtual bool unpack(byte_array &packed_form) {
00051     if (!futzle.unpack(packed_form)) return false;
00052     return true;
00053   }
00054   virtual int packed_size() const { return futzle.length() + 1; }
00055   virtual clonable *clone() const { return new simple_infoton(*this); }
00056 
00057 private:
00058 };
00059 
00061 
00062 // provides a simple service to allow us to test whether the security is
00063 // working or not.
00064 
00065 class simple_tentacle : public tentacle
00066 {
00067 public:
00068   simple_tentacle() : tentacle(simp_classifier(), true) {}
00069 
00070   virtual outcome reconstitute(const string_array &classifier,
00071           byte_array &packed_form, infoton * &reformed) {
00072     reformed = NIL;
00073     if (classifier != simp_classifier()) return NO_HANDLER;
00074     reformed = new simple_infoton;
00075     if (!reformed->unpack(packed_form)) {
00076       WHACK(reformed);
00077       return GARBAGE;
00078     }
00079     return OKAY;
00080   }
00081 
00082   virtual outcome consume(infoton &to_chow,
00083           const octopus_request_id &formal(item_id), byte_array &transformed) {
00084     transformed.reset();
00085     if (to_chow.classifier() != simp_classifier()) return NO_HANDLER;
00086     // consume without doing anything.
00087     return OKAY;
00088   }
00089 
00090   virtual void expunge(const octopus_entity &formal(to_zap)) {}
00091 };
00092 
00094 
00095 //hmmm: this test should do a sample login octopus and do a login, reside for
00096 //      a while, log out, do another one, let it time out, try to access
00097 //      something with dead id hoping to be rejected, etc.
00098 
00099 class test_octopus_security : public application_shell
00100 {
00101 public:
00102   test_octopus_security() : application_shell(class_name()) {}
00103   IMPLEMENT_CLASS_NAME("test_octopus_security");
00104   virtual int execute();
00105 };
00106 
00107 int test_octopus_security::execute()
00108 {
00109   octopus logos("local", 18 * MEGABYTE);
00110   simple_tentacle *tenty = new simple_tentacle;
00111   logos.add_tentacle(tenty);
00112   tenty = NIL;  // octopus has charge of this now.
00113 
00114   // turn on security in logos.
00115   simple_entity_registry *guardian = new simple_entity_registry;
00116   logos.add_tentacle(new login_tentacle(*guardian), true);
00117 
00118   // create an entity to work with.
00119   octopus_entity jimbo("localhost", portable::process_id(), 128, 982938);
00120   octopus_request_id req1(jimbo, 1);
00121 
00122   // add the user jimbo.
00123   guardian->add_entity(jimbo, byte_array());
00124 
00125   // create a piece of data to try running on tentacle.
00126   simple_infoton testose;
00127   simple_infoton *testose_copy = new simple_infoton(testose);
00128 
00129   // test that the simple tentacle allows the op.
00130   outcome ret = logos.evaluate(testose_copy, req1);
00131   if (ret != tentacle::OKAY)
00132     deadly_error(class_name(), "first test",
00133         istring("the operation failed with an error ")
00134             + tentacle::outcome_name(ret));
00135 
00136   // create another entity to work with.
00137   octopus_entity burfo("localhost", portable::process_id(), 372, 2989);
00138   octopus_request_id req2(burfo, 1);
00139 
00140   // try with an unlicensed user burfo...
00141   testose_copy = new simple_infoton(testose);
00142   ret = logos.evaluate(testose_copy, req2);
00143   if (ret == tentacle::OKAY)
00144     deadly_error(class_name(), "second test",
00145         istring("the operation didn't fail when it should have."));
00146   else if (ret != tentacle::DISALLOWED)
00147     deadly_error(class_name(), "second test",
00148         istring("the operation didn't provide the proper outcome, it gave: ")
00149             + tentacle::outcome_name(ret));
00150 
00151   // remove the user jimbo.
00152   guardian->zap_entity(jimbo);
00153 
00154   // test that jimbo fails too now.
00155   testose_copy = new simple_infoton(testose);
00156   ret = logos.evaluate(testose_copy, req1);
00157   if (ret == tentacle::OKAY)
00158     deadly_error(class_name(), "third test",
00159         istring("the operation didn't fail when it should have."));
00160   else if (ret != tentacle::DISALLOWED)
00161     deadly_error(class_name(), "third test",
00162         istring("the operation didn't provide the proper outcome, it gave: ")
00163             + tentacle::outcome_name(ret));
00164 
00165   // add the user burfo in now instead.
00166   guardian->add_entity(burfo, byte_array());
00167 
00168   // test that burfo works.
00169   testose_copy = new simple_infoton(testose);
00170   ret = logos.evaluate(testose_copy, req2);
00171   if (ret != tentacle::OKAY)
00172     deadly_error(class_name(), "fourth test",
00173         istring("the operation failed with an error ")
00174         + tentacle::outcome_name(ret));
00175 
00176   log("octopus:: security works for those functions tested.");
00177 
00178   WHACK(guardian); 
00179 
00180   return 0;
00181 }
00182 
00183 HOOPLE_MAIN(test_octopus_security, )
00184 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1