70_sasl.t 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Net::ZooKeeper - Perl extension for Apache ZooKeeper
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. use File::Spec;
  19. use Test::More tests => 7;
  20. use JSON::PP qw(decode_json);
  21. BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
  22. my $test_dir;
  23. (undef, $test_dir, undef) = File::Spec->splitpath($0);
  24. require File::Spec->catfile($test_dir, 'util.pl');
  25. my($hosts, $root_path, $node_path) = zk_test_setup(0);
  26. my $sasl_options = $ENV{'ZK_TEST_SASL_OPTIONS'};
  27. if (defined($sasl_options)) {
  28. $sasl_options = decode_json($sasl_options);
  29. }
  30. SKIP: {
  31. skip 'no sasl_options', 6 unless defined($sasl_options);
  32. my $zkh = Net::ZooKeeper->new($hosts,
  33. 'sasl_options' => $sasl_options);
  34. my $path = $zkh->create($node_path, 'foo',
  35. 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh));
  36. skip 'no connection to ZooKeeper', 36 unless
  37. (defined($path) and $path eq $node_path);
  38. ## _zk_acl_constant()
  39. my $acl_node_path = "$node_path/a1";
  40. my $sasl_acl = [
  41. {
  42. 'perms' => ZOO_PERM_READ,
  43. 'scheme' => 'world',
  44. 'id' => 'anyone'
  45. },
  46. {
  47. 'perms' => ZOO_PERM_ALL,
  48. 'scheme' => 'sasl',
  49. 'id' => $sasl_options->{user}
  50. }
  51. ];
  52. $path = $zkh->create($acl_node_path, 'foo', 'acl' => $sasl_acl);
  53. is($path, $acl_node_path,
  54. 'create(): created node with SASL ACL');
  55. ## get_acl()
  56. @acl = ('abc');
  57. @acl = $zkh->get_acl($acl_node_path);
  58. is_deeply(\@acl, $sasl_acl,
  59. 'get_acl(): retrieved SASL ACL');
  60. SKIP: {
  61. my $zkh2 = Net::ZooKeeper->new($hosts);
  62. my $ret = $zkh->exists($root_path) if (defined($zkh));
  63. skip 'no connection to ZooKeeper', 1 unless
  64. (defined($ret) and $ret);
  65. my $node = $zkh2->get($acl_node_path);
  66. is($node, 'foo',
  67. 'get(): retrieved node value with world ACL');
  68. $ret = $zkh2->set($acl_node_path, 'bar');
  69. ok((!$ret and $zkh2->get_error() == ZNOAUTH and $! eq ''),
  70. 'set(): node value unchanged if no auth');
  71. }
  72. my $ret = $zkh->set($acl_node_path, 'bar');
  73. ok($ret,
  74. 'set(): set node with SASL ACL');
  75. my $node = $zkh->get($acl_node_path);
  76. is($node, 'bar',
  77. 'get(): retrieved new node value with SASL ACL');
  78. $ret = $zkh->delete($acl_node_path);
  79. diag(sprintf('unable to delete node %s: %d, %s',
  80. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  81. $ret = $zkh->delete($node_path);
  82. diag(sprintf('unable to delete node %s: %d, %s',
  83. $node_path, $zkh->get_error(), $!)) unless ($ret);
  84. }