From 7fb8b590bf41f49c2b21e0a60bb76e7295680038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 8 Jul 2011 15:18:12 +0200 Subject: [PATCH] Java OCA: New RuleParseException for ACL rule allocation --- .../src/org/opennebula/client/acl/Acl.java | 29 +-- .../client/acl/RuleParseException.java | 29 +++ src/oca/java/test/AclTest.java | 175 ++++++++++++------ 3 files changed, 168 insertions(+), 65 deletions(-) create mode 100644 src/oca/java/src/org/opennebula/client/acl/RuleParseException.java diff --git a/src/oca/java/src/org/opennebula/client/acl/Acl.java b/src/oca/java/src/org/opennebula/client/acl/Acl.java index 62d2f1d560..357d40563d 100644 --- a/src/oca/java/src/org/opennebula/client/acl/Acl.java +++ b/src/oca/java/src/org/opennebula/client/acl/Acl.java @@ -136,8 +136,10 @@ public class Acl extends PoolElement{ * @param rule a rule string, e.g. "#5 HOST+VM/@12 INFO+CREATE+DELETE" * @return If successful the message contains the associated * id generated for this rule. + * @throws RuleParseException If the rule syntax is wrong. */ public static OneResponse allocate(Client client, String rule) + throws RuleParseException { String[] components = parseRule(rule); return allocate(client, components[0], components[1], components[2]); @@ -236,8 +238,9 @@ public class Acl extends PoolElement{ * * @param rule an ACL rule in string format * @return an Array containing 3 Strings (hex 64b numbers) + * @throws RuleParseException If the rule syntax is wrong. */ - public static String[] parseRule(String rule) + public static String[] parseRule(String rule) throws RuleParseException { String [] ret = new String[3]; @@ -245,8 +248,8 @@ public class Acl extends PoolElement{ if( components.length != 3 ) { - // TODO: throw "String needs three components: User, Resource, Rights" - return ret; + throw new RuleParseException( + "String needs three components: User, Resource, Rights"); } ret[0] = parseUsers(components[0]); @@ -262,7 +265,7 @@ public class Acl extends PoolElement{ * @param users Users component string * @return A string containing a hex number */ - private static String parseUsers(String users) + private static String parseUsers(String users) throws RuleParseException { return Long.toHexString( calculateIds(users) ); } @@ -274,14 +277,14 @@ public class Acl extends PoolElement{ * @return A string containing a hex number */ private static String parseResources(String resources) + throws RuleParseException { long ret = 0; String[] resourcesComponents = resources.split("/"); if( resourcesComponents.length != 2 ) { - // TODO: throw "Resource '#{resources}' malformed" - return ""; + throw new RuleParseException("Resource '"+resources+"' malformed"); } for( String resource : resourcesComponents[0].split("\\+") ) @@ -290,7 +293,8 @@ public class Acl extends PoolElement{ if( !RESOURCES.containsKey(resource) ) { - // TODO: throw "Resource '#{resource}' does not exist" + throw new RuleParseException("Resource '" + resource + + "' does not exist"); } ret += RESOURCES.get(resource); @@ -307,7 +311,7 @@ public class Acl extends PoolElement{ * @param rights Rights component string * @return A string containing a hex number */ - private static String parseRights(String rights) + private static String parseRights(String rights) throws RuleParseException { long ret = 0; @@ -318,8 +322,8 @@ public class Acl extends PoolElement{ if( !RIGHTS.containsKey(right) ) { - // TODO throw "Right '#{right}' does not exist" - return ""; + throw new RuleParseException("Right '" + right + + "' does not exist"); } ret += RIGHTS.get(right); @@ -335,12 +339,11 @@ public class Acl extends PoolElement{ * @param id Rule Id string * @return the numeric value for the given id_str */ - private static long calculateIds(String id) + private static long calculateIds(String id) throws RuleParseException { if( !id.matches("^([#@]\\d+|\\*)$") ) { - // TODO: throw "ID string '#{id_str}' malformed" - return 0; + throw new RuleParseException("ID string '" + id + "' malformed"); } long value = USERS.get( "" + id.charAt(0) ); diff --git a/src/oca/java/src/org/opennebula/client/acl/RuleParseException.java b/src/oca/java/src/org/opennebula/client/acl/RuleParseException.java new file mode 100644 index 0000000000..2ae5590291 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/acl/RuleParseException.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +package org.opennebula.client.acl; + +import org.opennebula.client.OneException; + +public class RuleParseException extends OneException +{ + private static final long serialVersionUID = 5992480039195389371L; + + public RuleParseException(String message) + { + super(message); + } +} diff --git a/src/oca/java/test/AclTest.java b/src/oca/java/test/AclTest.java index 9b08558e64..0b8e4e5d95 100644 --- a/src/oca/java/test/AclTest.java +++ b/src/oca/java/test/AclTest.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -80,7 +80,7 @@ public class AclTest { res = aclPool.info(); assertTrue( !res.isError() ); - + assertEquals(1, aclPool.getLength()); } @@ -90,60 +90,67 @@ public class AclTest // Allocate rule "#1 VM+HOST/@1 INFO+CREATE" res = Acl.allocate(client, "0x100000001", "0x3200000001", "0x11"); assertTrue( !res.isError() ); - + aclPool.info(); acl = aclPool.getById( res.getIntMessage() ); - + assertNotNull(acl); - + assertEquals(res.getIntMessage(), acl.id()); assertEquals(0x100000001L, acl.user()); assertEquals(0x3200000001L, acl.resource()); assertEquals(0x11L, acl.rights()); assertEquals("#1 VM+HOST/@1 CREATE+INFO", acl.toString()); } - + @Test public void numericAllocate() { // Allocate rule "#1 VM+HOST/@1 INFO+CREATE" res = Acl.allocate(client, 0x100000001L, 214748364801L, 0x11L); assertTrue( !res.isError() ); - + aclPool.info(); acl = aclPool.getById( res.getIntMessage() ); - + assertNotNull(acl); - + assertEquals(res.getIntMessage(), acl.id()); assertEquals(0x100000001L, acl.user()); assertEquals(0x3200000001L, acl.resource()); assertEquals(0x11L, acl.rights()); assertEquals("#1 VM+HOST/@1 CREATE+INFO", acl.toString()); } - + @Test public void ruleAllocate() { - res = Acl.allocate(client, "@507 IMAGE/#456 CREATE"); - assertTrue( !res.isError() ); - - aclPool.info(); - acl = aclPool.getById( res.getIntMessage() ); - - assertNotNull(acl); + try + { + res = Acl.allocate(client, "@507 IMAGE/#456 CREATE"); + assertTrue( !res.isError() ); - assertEquals(res.getIntMessage(), acl.id()); - assertEquals(0x2000001fbL, acl.user()); - assertEquals(0x81000001c8L, acl.resource()); - assertEquals(0x1L, acl.rights()); - assertEquals("@507 IMAGE/#456 CREATE", acl.toString()); + aclPool.info(); + acl = aclPool.getById( res.getIntMessage() ); + + assertNotNull(acl); + + assertEquals(res.getIntMessage(), acl.id()); + assertEquals(0x2000001fbL, acl.user()); + assertEquals(0x81000001c8L, acl.resource()); + assertEquals(0x1L, acl.rights()); + assertEquals("@507 IMAGE/#456 CREATE", acl.toString()); + } + catch (RuleParseException e) + { + assertTrue( false ); + } } - + @Test public void parseRules() { - String[] rules = { + String[] rules = { "#3 TEMPLATE/#0 INFO", "#2 IMAGE/#0 INFO", "@107 IMAGE+TEMPLATE/@100 INFO", @@ -158,7 +165,7 @@ public class AclTest 0x400000000L, 0x100000929L }; - + long[] resources = { 0x20100000000L, 0x8100000000L, @@ -166,7 +173,7 @@ public class AclTest 0x29200000064L, 0x29400000000L }; - + long[] rights = { 0x10L, 0x10L, @@ -177,37 +184,101 @@ public class AclTest for( int i = 0; i < rules.length; i++ ) { - res = Acl.allocate(client, rules[i]); - assertTrue( !res.isError() ); - - aclPool.info(); - acl = aclPool.getById( res.getIntMessage() ); - - assertNotNull(acl); + try + { + res = Acl.allocate(client, rules[i]); + assertTrue( !res.isError() ); - assertEquals(res.getIntMessage(), acl.id()); - assertEquals(users[i], acl.user()); - assertEquals(resources[i], acl.resource()); - assertEquals(rights[i], acl.rights()); + aclPool.info(); + acl = aclPool.getById( res.getIntMessage() ); + + assertNotNull(acl); + + assertEquals(res.getIntMessage(), acl.id()); + assertEquals(users[i], acl.user()); + assertEquals(resources[i], acl.resource()); + assertEquals(rights[i], acl.rights()); + } + catch (RuleParseException e) + { + assertTrue( + "Rule " + rules[i] + + " has been wrongly reported as invalid; " + + e.getMessage(), + false); + } } - - assertTrue( true ); } - + @Test public void delete() { - res = Acl.allocate(client, "#1 HOST/@2 INFO_POOL"); - assertTrue( !res.isError() ); - - aclPool.info(); - assertTrue( aclPool.getLength() == 2 ); + try + { + res = Acl.allocate(client, "#1 HOST/@2 INFO_POOL"); + assertTrue( !res.isError() ); - res = Acl.delete(client, res.getIntMessage()); - assertTrue( !res.isError() ); - - aclPool.info(); - assertTrue( aclPool.getLength() == 1 ); + aclPool.info(); + assertTrue( aclPool.getLength() == 2 ); + + res = Acl.delete(client, res.getIntMessage()); + assertTrue( !res.isError() ); + + aclPool.info(); + assertTrue( aclPool.getLength() == 1 ); + } + catch (RuleParseException e) + { + assertTrue( + "Rule has been wrongly reported as invalid; " + + e.getMessage(), + false); + } } + @Test + public void wrongRules() + { + String[] rules = { + "#-3 TEMPLATE/#0 INFO", + "#+3 TEMPLATE/#0 INFO", + "@3+ TEMPLATE/#0 INFO", + "*3 TEMPLATE/#0 INFO", + "# TEMPLATE/#0 INFO", + "@@ TEMPLATE/#0 INFO", + "@#3 TEMPLATE/#0 INFO", + "#3 TEMPLATE+HOS/#0 INFO", + "#3 /#0 INFO", + "#3 TEMPLATE/# INFO", + "#3 TEMPLATE/#5 INFO CREATE", + "#3 TEMPLATE/#5", + "#3 ", + "", + "#2 IMAGE @10654 INFO", + "#2 IMAGE/ INFO", + "#2 IMAGE#0 INFO", + "#2 IMAGE/# INFO", + "#2 IMAGE/@- INFO", + "#2 IMAGE/#0/#0 INFO", + "#2 IMAGE/#0/INFO CREATE", + "#2 IMAGE/#0/INFO+CREATE", + "#2 IMAGE/#0 IFO", + "#2 IMAGE/#0 INFO+CREAT", + }; + + for( int i = 0; i < rules.length; i++ ) + { + try + { + res = Acl.allocate(client, rules[i]); + + assertTrue( "Rule " + rules[i] + + " should have thrown an exception", + false); + } + catch (RuleParseException e) + { + } + } + } }