config get socket address
revamp configurationTest
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
#include <pv/epicsException.h>
|
||||
|
||||
#include <osiSock.h>
|
||||
|
||||
#define epicsExportSharedSymbols
|
||||
#include <pv/configuration.h>
|
||||
|
||||
@@ -337,6 +339,22 @@ string SystemConfigurationImpl::getPropertyAsString(const string &name, const st
|
||||
return _properties->getProperty(name, defaultValue);
|
||||
}
|
||||
|
||||
bool SystemConfigurationImpl::getPropertyAsAddress(const std::string& name, osiSockAddr* addr)
|
||||
{
|
||||
unsigned short dftport=0;
|
||||
if(addr->sa.sa_family==AF_INET)
|
||||
dftport = ntohs(addr->ia.sin_port);
|
||||
|
||||
std::string val(getPropertyAsString(name, ""));
|
||||
|
||||
if(val.empty()) return false;
|
||||
|
||||
addr->ia.sin_family = AF_INET;
|
||||
if(aToIPAddr(val.c_str(), dftport, &addr->ia))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SystemConfigurationImpl::hasProperty(const string &key)
|
||||
{
|
||||
const char* val = getenv(key.c_str());
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
|
||||
#include <shareLib.h>
|
||||
|
||||
union osiSockAddr; // defined in osiSock;
|
||||
|
||||
namespace epics {
|
||||
namespace pvAccess {
|
||||
|
||||
@@ -135,6 +137,17 @@ public:
|
||||
* @return environment variable value as std::string or default value if it does not exist.
|
||||
*/
|
||||
virtual std::string getPropertyAsString(const std::string &name, const std::string &defaultValue) = 0;
|
||||
/**
|
||||
* Fetch and parse as a socket address and port number (address family set accordingly).
|
||||
* At present only numeric addresses are parsed (eg. "127.0.0.1:4242").
|
||||
*
|
||||
* The storage pointed to be addr should be initialized with a default value, or zeroed.
|
||||
*
|
||||
* @param name name of the environment variable to return.
|
||||
* @pram addr pointer to the address struct to be filled in
|
||||
* @return true if addr now contains an address, false otherwise
|
||||
*/
|
||||
virtual bool getPropertyAsAddress(const std::string& name, osiSockAddr* addr) = 0;
|
||||
|
||||
virtual bool hasProperty(const std::string &name) = 0;
|
||||
};
|
||||
@@ -149,6 +162,7 @@ public:
|
||||
float getPropertyAsFloat(const std::string &name, const float defaultValue);
|
||||
float getPropertyAsDouble(const std::string &name, const double defaultValue);
|
||||
std::string getPropertyAsString(const std::string &name, const std::string &defaultValue);
|
||||
bool getPropertyAsAddress(const std::string& name, osiSockAddr* addr);
|
||||
bool hasProperty(const std::string &name);
|
||||
std::auto_ptr<Properties> _properties;
|
||||
private:
|
||||
|
||||
@@ -44,7 +44,7 @@ TESTSCRIPTS_HOST += $(TESTS:%=%.t)
|
||||
#testHarness_SRCS += namedLockPatternTest.cpp
|
||||
#TESTS += namedLockPatternTest
|
||||
|
||||
#TESTPROD_HOST += configurationTest
|
||||
#configurationTest_SRCS += configurationTest.cpp
|
||||
TESTPROD_HOST += configurationTest
|
||||
configurationTest_SRCS += configurationTest.cpp
|
||||
#testHarness_SRCS += configurationTest.cpp
|
||||
#TESTS += configurationTest
|
||||
TESTS += configurationTest
|
||||
|
||||
@@ -4,13 +4,20 @@
|
||||
*/
|
||||
|
||||
#include <pv/configuration.h>
|
||||
#include <pv/CDRMonitor.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <epicsAssert.h>
|
||||
#include <epicsExit.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
#include <envDefs.h>
|
||||
#include <osiSock.h>
|
||||
|
||||
#include <epicsUnitTest.h>
|
||||
#include <testMain.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
void setenv(char * a, char * b, int c)
|
||||
@@ -25,62 +32,102 @@ using namespace epics::pvAccess;
|
||||
using namespace epics::pvData;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
static void showEnv(const char *name)
|
||||
{
|
||||
SystemConfigurationImpl* configuration = new SystemConfigurationImpl();
|
||||
bool boolProperty = configuration->getPropertyAsBoolean("boolProperty", true);
|
||||
assert(boolProperty == true);
|
||||
testDiag("%s = \"%s\"", name, getenv(name));
|
||||
}
|
||||
|
||||
int32 intProperty = configuration->getPropertyAsInteger("intProperty", 1);
|
||||
assert(intProperty == 1);
|
||||
static void setEnv(const char *name, const char *val)
|
||||
{
|
||||
epicsEnvSet(name, val);
|
||||
testDiag("%s = \"%s\"", name, getenv(name));
|
||||
}
|
||||
|
||||
float floatProperty = configuration->getPropertyAsFloat("floatProperty", 3);
|
||||
assert(floatProperty == 3);
|
||||
static void showAddr(const osiSockAddr& addr)
|
||||
{
|
||||
char buf[40];
|
||||
sockAddrToDottedIP(&addr.sa, buf, sizeof(buf));
|
||||
testDiag("%s", buf);
|
||||
}
|
||||
|
||||
double doubleProperty = configuration->getPropertyAsDouble("doubleProperty", -3);
|
||||
assert(doubleProperty == -3);
|
||||
#define TESTVAL(TYPE, VAL1, VAL2, VAL1S) do {\
|
||||
showEnv(#TYPE "Property"); \
|
||||
testOk1(configuration->getPropertyAs##TYPE(#TYPE "Property", VAL1) == VAL1); \
|
||||
testOk1(configuration->getPropertyAs##TYPE(#TYPE "Property", VAL2) == VAL2); \
|
||||
setEnv(#TYPE "Property", VAL1S); \
|
||||
testOk1(configuration->getPropertyAs##TYPE(#TYPE "Property", VAL1) == VAL1); \
|
||||
testOk1(configuration->getPropertyAs##TYPE(#TYPE "Property", VAL2) == VAL1); \
|
||||
} while(0)
|
||||
|
||||
string stringProperty = configuration->getPropertyAsString("stringProperty", "string");
|
||||
assert(stringProperty == string("string"));
|
||||
|
||||
ConfigurationProviderImpl* configProvider = ConfigurationFactory::getProvider();
|
||||
configProvider->registerConfiguration("conf1",static_cast<Configuration*>(configuration));
|
||||
MAIN(configurationTest)
|
||||
{
|
||||
testPlan(35);
|
||||
testDiag("Default configuration");
|
||||
Configuration::shared_pointer configuration(new SystemConfigurationImpl());
|
||||
|
||||
SystemConfigurationImpl* configurationOut = static_cast<SystemConfigurationImpl*>(configProvider->getConfiguration("conf1"));
|
||||
assert(configurationOut == configuration);
|
||||
TESTVAL(String, "one", "two", "one");
|
||||
TESTVAL(Boolean, true, false, "true");
|
||||
TESTVAL(Integer, 100, 321, "100");
|
||||
TESTVAL(Float, 42.0e3, 44.0e3, "42.0e3");
|
||||
TESTVAL(Double, 42.0e3, 44.0e3, "42.0e3");
|
||||
|
||||
intProperty = configuration->getPropertyAsInteger("intProperty", 2);
|
||||
assert(intProperty == 1);
|
||||
testDiag("IP Address w/o default or explicit port");
|
||||
|
||||
floatProperty = configuration->getPropertyAsFloat("floatProperty", 4);
|
||||
assert(floatProperty == 3);
|
||||
showEnv("AddressProperty");
|
||||
osiSockAddr addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.ia.sin_family = AF_INET+1; // something not IPv4
|
||||
addr.ia.sin_port = htons(42);
|
||||
|
||||
doubleProperty = configuration->getPropertyAsDouble("doubleProperty", -4);
|
||||
assert(doubleProperty == -3);
|
||||
testOk1(configuration->getPropertyAsAddress("AddressProperty", &addr)==false);
|
||||
setEnv("AddressProperty", "127.0.0.1"); // no port
|
||||
testOk1(configuration->getPropertyAsAddress("AddressProperty", &addr)==true);
|
||||
showAddr(addr);
|
||||
|
||||
stringProperty = configuration->getPropertyAsString("stringProperty", "string1");
|
||||
assert(stringProperty == string("string"));
|
||||
testOk1(addr.ia.sin_family==AF_INET);
|
||||
testOk1(ntohl(addr.ia.sin_addr.s_addr)==INADDR_LOOPBACK);
|
||||
testOk1(ntohs(addr.ia.sin_port)==0);
|
||||
|
||||
setenv("boolProperty1", "1", 1);
|
||||
boolProperty = configuration->getPropertyAsInteger("boolProperty1", 0);
|
||||
assert(boolProperty == true);
|
||||
testDiag("IP Address w/ default port");
|
||||
|
||||
setenv("intProperty1", "45", 1);
|
||||
intProperty = configuration->getPropertyAsInteger("intProperty1", 2);
|
||||
assert(intProperty == 45);
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.ia.sin_family = AF_INET;
|
||||
addr.ia.sin_port = htons(42);
|
||||
|
||||
setenv("floatProperty1", "22", 1);
|
||||
floatProperty = configuration->getPropertyAsFloat("floatProperty1", 3);
|
||||
assert(floatProperty == 22);
|
||||
testOk1(configuration->getPropertyAsAddress("AddressProperty", &addr)==true);
|
||||
showAddr(addr);
|
||||
|
||||
setenv("dobuleProperty1", "42", 1);
|
||||
doubleProperty = configuration->getPropertyAsDouble("dobuleProperty1", -3);
|
||||
assert(doubleProperty == 42);
|
||||
testOk1(addr.ia.sin_family==AF_INET);
|
||||
testOk1(ntohl(addr.ia.sin_addr.s_addr)==INADDR_LOOPBACK);
|
||||
testOk1(ntohs(addr.ia.sin_port)==42);
|
||||
|
||||
if(configProvider) delete configProvider;
|
||||
epicsExitCallAtExits();
|
||||
CDRMonitor::get().show(stdout, true);
|
||||
return 0;
|
||||
testDiag("IP Address w/ default and explicit port");
|
||||
|
||||
setEnv("AddressProperty", "127.0.0.1:43"); // no port
|
||||
testOk1(configuration->getPropertyAsAddress("AddressProperty", &addr)==true);
|
||||
showAddr(addr);
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.ia.sin_family = AF_INET;
|
||||
addr.ia.sin_port = htons(42);
|
||||
|
||||
testOk1(configuration->getPropertyAsAddress("AddressProperty", &addr)==true);
|
||||
showAddr(addr);
|
||||
|
||||
testOk1(addr.ia.sin_family==AF_INET);
|
||||
testOk1(ntohl(addr.ia.sin_addr.s_addr)==INADDR_LOOPBACK);
|
||||
testOk1(ntohs(addr.ia.sin_port)==43);
|
||||
|
||||
testDiag("register with global configuration listings");
|
||||
|
||||
ConfigurationProvider::shared_pointer configProvider(ConfigurationFactory::getProvider());
|
||||
configProvider->registerConfiguration("conf1", configuration);
|
||||
|
||||
Configuration::shared_pointer configurationOut(configProvider->getConfiguration("conf1"));
|
||||
testOk1(configurationOut.get() == configuration.get());
|
||||
|
||||
return testDone();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user