[1/3] nitb/ctrl: Add ctrl command to get/set auth policy
diff mbox

Message ID 1399390559-22655-1-git-send-email-kluchnikovi@gmail.com
State Changes Requested
Headers show

Commit Message

Ivan Kluchnikov May 6, 2014, 3:35 p.m. UTC
---
 openbsc/src/libbsc/bsc_ctrl_commands.c |   31 +++++++++++++++++++++++++++++++
 openbsc/tests/ctrl_test_runner.py      |   18 ++++++++++++++++++
 2 files changed, 49 insertions(+)

Comments

Holger Freyther May 7, 2014, 7:16 a.m. UTC | #1
On Tue, May 06, 2014 at 07:35:57PM +0400, Ivan Kluchnikov wrote:

hi,


> +	if ((int)gsm_auth_policy_parse(value) < 0) {
> +		return -1;
> +	}

1.) Coding-Style.
2.) The (int) cast is fishy. Most likely even undefined C usage. We have
a int(-EINVAL) -> enum -> int conversion here. Add an invalid element to
the enum, change the implementation to compare to -EINVAL...

Then your verify call becomes a simple one liner and comparison against
the invalid element.


> +    def testAuthPolicy(self):
> +        r = self.do_set('auth-policy', 'qwerty')
> +        self.assertEquals(r['mtype'], 'ERROR')
> +        self.assertEquals(r['error'], 'Value failed verification.')

Check that the previous thing has not been changed. :)

Patch
diff mbox

diff --git a/openbsc/src/libbsc/bsc_ctrl_commands.c b/openbsc/src/libbsc/bsc_ctrl_commands.c
index 3759593..3cb77cc 100644
--- a/openbsc/src/libbsc/bsc_ctrl_commands.c
+++ b/openbsc/src/libbsc/bsc_ctrl_commands.c
@@ -66,6 +66,36 @@  CTRL_CMD_DEFINE_RANGE(net_mcc, "mcc", struct gsm_network, country_code, 1, 999);
 CTRL_CMD_VTY_STRING(net_short_name, "short-name", struct gsm_network, name_short);
 CTRL_CMD_VTY_STRING(net_long_name, "long-name", struct gsm_network, name_long);
 
+static int verify_net_auth_policy(struct ctrl_cmd *cmd, const char *value, void *data)
+{
+
+	if ((int)gsm_auth_policy_parse(value) < 0) {
+		return -1;
+	}
+
+	return 0;
+}
+
+static int get_net_auth_policy(struct ctrl_cmd *cmd, void *data)
+{
+	struct gsm_network *net = cmd->node;
+	cmd->reply = talloc_asprintf(cmd, "%s", gsm_auth_policy_name(net->auth_policy));
+	if (!cmd->reply) {
+		cmd->reply = "OOM";
+		return CTRL_CMD_ERROR;
+	}
+	return CTRL_CMD_REPLY;
+}
+
+static int set_net_auth_policy(struct ctrl_cmd *cmd, void *data)
+{
+	struct gsm_network *net = cmd->node;
+	net->auth_policy = gsm_auth_policy_parse(cmd->value);
+	return get_net_auth_policy(cmd, data);
+}
+
+CTRL_CMD_DEFINE(net_auth_policy, "auth-policy");
+
 static int verify_net_apply_config(struct ctrl_cmd *cmd, const char *v, void *d)
 {
 	return 0;
@@ -200,6 +230,7 @@  int bsc_base_ctrl_cmds_install(void)
 	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_mcc);
 	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_short_name);
 	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_long_name);
+	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_auth_policy);
 	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_apply_config);
 	rc |= ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_mcc_mnc_apply);
 
diff --git a/openbsc/tests/ctrl_test_runner.py b/openbsc/tests/ctrl_test_runner.py
index b50e93c..f24d52a 100644
--- a/openbsc/tests/ctrl_test_runner.py
+++ b/openbsc/tests/ctrl_test_runner.py
@@ -362,6 +362,24 @@  class TestCtrlNITB(TestCtrlBase):
     def ctrl_app(self):
         return (4249, "./src/osmo-nitb/osmo-nitb", "OsmoBSC", "nitb")
 
+    def testAuthPolicy(self):
+        policies = ['token', 'closed', 'accept-all']
+
+        for policy in policies:
+            r = self.do_set('auth-policy', policy)
+            self.assertEquals(r['mtype'], 'SET_REPLY')
+            self.assertEquals(r['var'], 'auth-policy')
+            self.assertEquals(r['value'], policy)
+
+            r = self.do_get('auth-policy')
+            self.assertEquals(r['mtype'], 'GET_REPLY')
+            self.assertEquals(r['var'], 'auth-policy')
+            self.assertEquals(r['value'], policy)
+
+        r = self.do_set('auth-policy', 'qwerty')
+        self.assertEquals(r['mtype'], 'ERROR')
+        self.assertEquals(r['error'], 'Value failed verification.')
+
     def testSubscriberAddRemove(self):
         r = self.do_set('subscriber-modify-v1', '2620345,445566')
         self.assertEquals(r['mtype'], 'SET_REPLY')