You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.5 KiB

  1. /*
  2. * Minio .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using Microsoft.VisualStudio.TestTools.UnitTesting;
  17. using Minio.DataModel;
  18. using Minio.DataModel.Policy;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Text;
  23. namespace Minio.Tests
  24. {
  25. class TestHelper
  26. {
  27. private static Random rnd = new Random();
  28. // Generate a random string
  29. public static String GetRandomName(int length = 5)
  30. {
  31. string characters = "0123456789abcdefghijklmnopqrstuvwxyz";
  32. StringBuilder result = new StringBuilder(length);
  33. for (int i = 0; i < length; i++)
  34. {
  35. result.Append(characters[rnd.Next(characters.Length)]);
  36. }
  37. return result.ToString();
  38. }
  39. // Generate an empty statement
  40. internal static Statement GenerateStatement(string resource)
  41. {
  42. Statement stmt = new Statement();
  43. stmt.resources = new Resources(resource);
  44. return stmt;
  45. }
  46. // Generate a resource prefix
  47. internal static string GenerateResourcesPrefix(string bucketName, string objectName)
  48. {
  49. return PolicyConstants.AWS_RESOURCE_PREFIX + bucketName + "/" + objectName;
  50. }
  51. // Generate a new statement
  52. internal static Statement GenerateStatement(List<string> actions,string resourcePrefix, string effect = "Allow", string aws = "*",bool withConditions=false,string withStringSet="hello",string condition="StringEquals")
  53. {
  54. Statement stmt = new Statement();
  55. stmt.resources = new Resources(resourcePrefix);
  56. stmt.actions = actions;
  57. stmt.effect = effect;
  58. stmt.principal = new Principal(aws);
  59. if (withConditions)
  60. {
  61. stmt.conditions = new ConditionMap();
  62. ConditionKeyMap ckmap = new ConditionKeyMap();
  63. if (withStringSet != null)
  64. ckmap.Add("s3:prefix", new HashSet<string>() {withStringSet });
  65. if (condition != null && ckmap != null)
  66. stmt.conditions.Add(condition, ckmap);
  67. }
  68. return stmt;
  69. }
  70. // Get List with Read and Write bucket actions
  71. internal static List<string> GetReadAndWriteBucketActions()
  72. {
  73. List<string> res = new List<string>();
  74. res.AddRange(PolicyConstants.READ_ONLY_BUCKET_ACTIONS);
  75. res.AddRange(PolicyConstants.WRITE_ONLY_BUCKET_ACTIONS);
  76. return res;
  77. }
  78. // Hydrate a bucket policy from JSON string
  79. internal static BucketPolicy GenerateBucketPolicy(string policyString,string bucketName)
  80. {
  81. var contentBytes = System.Text.Encoding.UTF8.GetBytes(policyString);
  82. var stream = new MemoryStream(contentBytes);
  83. return BucketPolicy.ParseJson(stream, bucketName);
  84. }
  85. }
  86. }