| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
require 'minitest/autorun' |
| 21 |
require "securerandom" |
| 22 |
require "qpid_proton" |
| 23 |
|
| 24 |
# in Ruby 1.8 there is no SecureRandom module |
| 25 |
if RUBY_VERSION < "1.9" |
| 26 |
module SecureRandom |
| 27 |
class << self |
| 28 |
def method_missing(method_sym, *arguments, &block) |
| 29 |
case method_sym |
| 30 |
when :urlsafe_base64 |
| 31 |
r19_urlsafe_base64(*arguments) |
| 32 |
when :uuid |
| 33 |
r19_uuid(*arguments) |
| 34 |
else |
| 35 |
super |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
private |
| 40 |
def r19_urlsafe_base64(n=nil, padding=false) |
| 41 |
s = [random_bytes(n)].pack("m*") |
| 42 |
s.delete!("\n") |
| 43 |
s.tr!("+/", "-_") |
| 44 |
s.delete!("=") if !padding |
| 45 |
s |
| 46 |
end |
| 47 |
|
| 48 |
def r19_uuid |
| 49 |
ary = random_bytes(16).unpack("NnnnnN") |
| 50 |
ary[2] = (ary[2] & 0x0fff) | 0x4000 |
| 51 |
ary[3] = (ary[3] & 0x3fff) | 0x8000 |
| 52 |
"%08x-%04x-%04x-%04x-%04x%08x" % ary |
| 53 |
end |
| 54 |
end |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
# Generates a random string of the specified length |
| 59 |
def random_string(length = 8) |
| 60 |
(0...length).map{65.+(rand(25)).chr}.join |
| 61 |
end |
| 62 |
|
| 63 |
# Generates a random list of the specified length. |
| 64 |
def random_list(length) |
| 65 |
result = [] |
| 66 |
(0...length).each do |element| |
| 67 |
low = rand(512) |
| 68 |
high = rand(8192) |
| 69 |
|
| 70 |
case |
| 71 |
when element == 0 then result << rand(128) |
| 72 |
when element == 1 then result << random_string(rand(128)) |
| 73 |
when element == 2 then result << rand * (low - high).abs + low |
| 74 |
when element == 3 then result << SecureRandom.uuid |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
return result |
| 79 |
end |
| 80 |
|
| 81 |
# Generates a random array of a random type. |
| 82 |
# Returns both the array and the type. |
| 83 |
def random_array(length, described = false, description = nil) |
| 84 |
choice = rand(128) % 4 |
| 85 |
type = [Qpid::Proton::Types::INT, |
| 86 |
Qpid::Proton::Types::STRING, |
| 87 |
Qpid::Proton::Types::DOUBLE, |
| 88 |
Qpid::Proton::Types::UUID][choice] |
| 89 |
result = Qpid::Proton::Types::UniformArray.new(type) |
| 90 |
|
| 91 |
low = rand(512) |
| 92 |
high = rand(8192) |
| 93 |
|
| 94 |
(0...length).each do |element| |
| 95 |
case |
| 96 |
when choice == 0 then result << rand(1024) |
| 97 |
when choice == 1 then result << random_string(rand(128)) |
| 98 |
when choice == 2 then result << rand * (low - high).abs + low |
| 99 |
when choice == 3 then result << SecureRandom.uuid |
| 100 |
end |
| 101 |
end |
| 102 |
return result |
| 103 |
end |
| 104 |
|
| 105 |
# Generates a random hash of values. |
| 106 |
def random_hash(length) |
| 107 |
result = {} |
| 108 |
values = random_list(length) |
| 109 |
values.each do |value| |
| 110 |
result[random_string(64)] = value |
| 111 |
end |
| 112 |
return result |
| 113 |
end |
| 114 |
|