From 459c52e5d11a840bd990988064d7580180247acb Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Thu, 5 Sep 2019 19:18:23 +0530 Subject: [PATCH 1/5] Add User Method to Internet Class --- lib/faker/default/internet.rb | 8 ++++++++ test/faker/default/test_faker_internet.rb | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/faker/default/internet.rb b/lib/faker/default/internet.rb index 176b12d293..686802548e 100644 --- a/lib/faker/default/internet.rb +++ b/lib/faker/default/internet.rb @@ -277,6 +277,14 @@ def uuid '%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString end + def user(*args) + user_hash = {} + args.each do |arg| + user_hash[:"#{arg}"] = send(arg) + end + user_hash + end + alias user_name username end end diff --git a/test/faker/default/test_faker_internet.rb b/test/faker/default/test_faker_internet.rb index 5590dfb74a..d231c3f74a 100644 --- a/test/faker/default/test_faker_internet.rb +++ b/test/faker/default/test_faker_internet.rb @@ -295,4 +295,23 @@ def test_uuid assert_equal(36, uuid.size) assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid) end + + def test_user_with_args + user = @tester.user('name', 'email', 'password') + assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) + assert user['email'].match(/.+@.+\.\w+/) + assert user['password'].match(/\w{3}/) + end + + def test_user_without_args + user = @tester.user + assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) + assert user['email'].match(/.+@.+\.\w+/) + end + + def test_user_with_invalid_args + assert_raises NoMethodError do + @tester.user('xyz') + end + end end From cc8390849f3bae8c1f26e261275ba403f7c5b063 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Thu, 5 Sep 2019 22:41:51 +0530 Subject: [PATCH 2/5] Add default user if no args --- lib/faker/internet.rb | 184 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 lib/faker/internet.rb diff --git a/lib/faker/internet.rb b/lib/faker/internet.rb new file mode 100644 index 0000000000..b4c4a6bebe --- /dev/null +++ b/lib/faker/internet.rb @@ -0,0 +1,184 @@ +module Faker + class Internet < Base + class << self + + def email(name = nil) + [user_name(name), domain_name].join('@') + end + + def free_email(name = nil) + [user_name(name), fetch('internet.free_email')].join('@') + end + + def safe_email(name = nil) + [user_name(name), 'example.'+ sample(%w[org com net])].join('@') + end + + def user_name(specifier = nil, separators = %w(. _)) + with_locale(:en) do + if specifier.respond_to?(:scan) + return specifier.scan(/\w+/).shuffle.join(sample(separators)).downcase + elsif specifier.kind_of?(Integer) + # If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error + raise ArgumentError, "Given argument is too large" if specifier > 10**6 + tries = 0 # Don't try forever in case we get something like 1_000_000. + begin + result = user_name(nil, separators) + tries += 1 + end while result.length < specifier && tries < 7 + return result * (specifier/result.length + 1) if specifier > 0 + elsif specifier.kind_of?(Range) + tries = 0 + begin + result = user_name(specifier.min, separators) + tries += 1 + end while !specifier.include?(result.length) && tries < 7 + return result[0...specifier.max] + end + + sample([ + Char.prepare(Name.first_name), + [Name.first_name, Name.last_name].map{ |name| + Char.prepare(name) + }.join(sample(separators)) + ]) + end + end + + def password(min_length = 8, max_length = 16, mix_case = true, special_chars = false) + temp = Lorem.characters(min_length) + diff_length = max_length - min_length + if diff_length > 0 + diff_rand = rand(diff_length + 1) + temp += Lorem.characters(diff_rand) + end + + if mix_case + temp.chars.each_with_index do |char, index| + temp[index] = char.upcase if index.even? + end + end + + if special_chars + chars = %w(! @ # $ % ^ & *) + rand(min_length).times do |i| + temp[i] = chars[rand(chars.length)] + end + end + + temp + end + + def domain_name + with_locale(:en) { [Char.prepare(domain_word), domain_suffix].join('.') } + end + + def fix_umlauts(string='') + Char.fix_umlauts(string) + end + + def domain_word + return Char.prepare(Company.name.split(' ')[1]) if Config.locale == 'uk' + Char.prepare(Company.name.split(' ').first) + end + + def domain_suffix + fetch('internet.domain_suffix') + end + + def mac_address(prefix='') + prefix_digits = prefix.split(':').map{ |d| d.to_i(16) } + address_digits = (6 - prefix_digits.size).times.map{ rand(256) } + (prefix_digits + address_digits).map{ |d| '%02x' % d }.join(':') + end + + def ip_v4_address + ary = (2..254).to_a + [ sample(ary), sample(ary), sample(ary), sample(ary) ].join('.') + end + + def private_ip_v4_address + begin + addr = ip_v4_address + end while !private_net_checker[addr] + addr + end + + def public_ip_v4_address + begin + addr = ip_v4_address + end while reserved_net_checker[addr] + addr + end + + def private_nets_regex + [ + /^10\./, # 10.0.0.0 – 10.255.255.255 + /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 + /^127\./, # 127.0.0.0 – 127.255.255.255 + /^169\.254\./, # 169.254.0.0 – 169.254.255.255 + /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 + /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 + /^192\.168\./, # 192.168.0.0 – 192.168.255.255 + /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 + ] + end + + def private_net_checker + lambda { |addr| private_nets_regex.any? { |net| net =~ addr } } + end + + def reserved_nets_regex + [ + /^0\./, # 0.0.0.0 – 0.255.255.255 + /^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255 + /^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255 + /^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255 + /^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255 + /^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255 + /^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255 + ] + end + + def reserved_net_checker + ->(addr){ (private_nets_regex + reserved_nets_regex).any? { |net| net =~ addr } } + end + + def ip_v4_cidr + "#{ip_v4_address}/#{1 + rand(31)}" + end + + def ip_v6_address + (1..8).map { rand(65536).to_s(16) }.join(':') + end + + def ip_v6_cidr + "#{ip_v6_address}/#{1 + rand(127)}" + end + + def url(host = domain_name, path = "/#{user_name}", scheme = 'http') + "#{scheme}://#{host}#{path}" + end + + def slug(words = nil, glue = nil) + glue ||= sample(%w[- _ .]) + (words || Faker::Lorem::words(2).join(' ')).gsub(' ', glue).downcase + end + + def device_token + shuffle(rand(16 ** 64).to_s(16).rjust(64, '0').chars.to_a).join + end + + def user(*args) + user_obj = {} + if args.empty? + user_obj = { user: 'John', password: 'Ad3M9dL7e7' } + end + args.each do |arg| + user_obj[arg] = send(arg) + end + user_obj + end + end + end +end From 72859548e3a588d4bbd75ee95ece2f01ac817016 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Fri, 6 Sep 2019 00:53:14 +0530 Subject: [PATCH 3/5] Add default args --- lib/faker/internet.rb | 8 +- test/test_faker_internet.rb | 239 ++++++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 test/test_faker_internet.rb diff --git a/lib/faker/internet.rb b/lib/faker/internet.rb index b4c4a6bebe..881b43e14d 100644 --- a/lib/faker/internet.rb +++ b/lib/faker/internet.rb @@ -170,13 +170,9 @@ def device_token end def user(*args) + args = %w[name email] if args.empty? user_obj = {} - if args.empty? - user_obj = { user: 'John', password: 'Ad3M9dL7e7' } - end - args.each do |arg| - user_obj[arg] = send(arg) - end + args.each { |arg| user_obj[arg] = send(arg) } user_obj end end diff --git a/test/test_faker_internet.rb b/test/test_faker_internet.rb new file mode 100644 index 0000000000..6b23517be4 --- /dev/null +++ b/test/test_faker_internet.rb @@ -0,0 +1,239 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') + +class TestFakerInternet < Test::Unit::TestCase + + def setup + @tester = Faker::Internet + end + + def test_email + assert @tester.email.match(/.+@.+\.\w+/) + end + + def test_free_email + assert @tester.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/) + end + + def test_safe_email + assert @tester.safe_email.match(/.+@example.(com|net|org)/) + end + + def test_user_name + assert @tester.user_name.match(/[a-z]+((_|\.)[a-z]+)?/) + end + + def test_user_name_with_string_arg + assert @tester.user_name('bo peep').match(/(bo(_|\.)peep|peep(_|\.)bo)/) + end + + def test_user_name_with_integer_arg + (1..32).each do |min_length| + assert @tester.user_name(min_length).length >= min_length + end + end + + def test_user_name_with_very_large_integer_arg + exception = assert_raises(ArgumentError) { @tester.user_name(10000000) } + assert_equal('Given argument is too large', exception.message) + end + + def test_user_name_with_closed_range_arg + (1..32).each do |min_length| + (min_length..32).each do |max_length| + l = @tester.user_name((min_length..max_length)).length + assert l >= min_length + assert l <= max_length + end + end + end + + def test_user_name_with_open_range_arg + (1..32).each do |min_length| + (min_length+1..33).each do |max_length| + l = @tester.user_name((min_length...max_length)).length + assert l >= min_length + assert l <= max_length-1 + end + end + end + + def test_user_name_with_range_and_separators + (1..32).each do |min_length| + (min_length+1..33).each do |max_length| + u = @tester.user_name((min_length...max_length), %w(=)) + assert u.length.between? min_length, max_length-1 + assert u.match(/\A[a-z]+((=)?[a-z]*)*\z/) + end + end + end + + def test_password + assert @tester.password.match(/\w{3}/) + end + + def test_password_with_integer_arg + (1..32).each do |min_length| + assert @tester.password(min_length).length >= min_length + end + end + + def test_password_max_with_integer_arg + (1..32).each do |min_length| + max_length = min_length + 4 + assert @tester.password(min_length, max_length).length <= max_length + end + end + + def test_password_with_mixed_case + assert @tester.password.match(/[A-Z]+/) + end + + def test_password_without_mixed_case + assert @tester.password(8, 12, false).match(/[^A-Z]+/) + end + + def test_password_with_special_chars + assert @tester.password(8, 12, true, true).match(/[!@#\$%\^&\*]+/) + end + + def test_password_without_special_chars + assert @tester.password(8, 12, true).match(/[^!@#\$%\^&\*]+/) + end + + def test_domain_name + assert @tester.domain_name.match(/\w+\.\w+/) + end + + def test_domain_word + assert @tester.domain_word.match(/^\w+$/) + end + + def test_domain_suffix + assert @tester.domain_suffix.match(/^\w+(\.\w+)?/) + end + + def test_ip_v4_address + assert_equal 3, @tester.ip_v4_address.count('.') + + 100.times do + assert @tester.ip_v4_address.split('.').map{|octet| octet.to_i}.max <= 255 + end + end + + def test_private_ip_v4_address + regexps = [ + /^10\./, # 10.0.0.0 – 10.255.255.255 + /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 + /^127\./, # 127.0.0.0 – 127.255.255.255 + /^169\.254\./, # 169.254.0.0 – 169.254.255.255 + /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 + /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 + /^192\.168\./, # 192.168.0.0 – 192.168.255.255 + /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 + ] + expected = Regexp.new regexps.collect{|reg| "(#{reg})"}.join('|') + + 1000.times do + address = @tester.private_ip_v4_address + assert_match expected, address + end + end + + def test_public_ip_v4_address + private = [ + /^10\./, # 10.0.0.0 – 10.255.255.255 + /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 + /^127\./, # 127.0.0.0 – 127.255.255.255 + /^169\.254\./, # 169.254.0.0 – 169.254.255.255 + /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 + /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 + /^192\.168\./, # 192.168.0.0 – 192.168.255.255 + /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 + ] + + reserved = [ + /^0\./, # 0.0.0.0 – 0.255.255.255 + /^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255 + /^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255 + /^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255 + /^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255 + /^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255 + /^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255 + ] + + 1000.times do + address = @tester.public_ip_v4_address + private.each { |reg| assert_not_match reg, address } + reserved.each { |reg| assert_not_match reg, address } + end + end + + def test_ip_v4_cidr + assert @tester.ip_v4_cidr.match(/\/\d{1,2}$/) + + 1000.times do + assert (1..32).include?(@tester.ip_v4_cidr.split('/').last.to_i) + end + end + + def test_mac_address + assert_equal 5, @tester.mac_address.count(':') + assert_equal 5, @tester.mac_address("").count(':') + + 100.times do + assert @tester.mac_address.split(':').map{|d| d.to_i(16)}.max <= 255 + end + + assert @tester.mac_address("fa:fa:fa").start_with?("fa:fa:fa") + assert @tester.mac_address("01:02").start_with?("01:02") + end + + def test_ip_v6_address + assert_equal 7, @tester.ip_v6_address.count(':') + + 100.times do + assert @tester.ip_v6_address.split('.').map{|h| "0x#{h}".hex}.max <= 65535 + end + end + + def test_ip_v6_cidr + assert @tester.ip_v6_cidr.match(/\/\d{1,3}$/) + + 1000.times do + assert (1..128).include?(@tester.ip_v6_cidr.split('/').last.to_i) + end + end + + def test_slug + assert @tester.slug.match(/^[a-z]+(_|\.|\-)[a-z]+$/) + end + + def test_slug_with_content_arg + assert @tester.slug('Foo bAr baZ').match(/^foo(_|\.|\-)bar(_|\.|\-)baz$/) + end + + def test_slug_with_glue_arg + assert @tester.slug(nil, '+').match(/^[a-z]+\+[a-z]+$/) + end + + def test_url + assert @tester.url('domain.com', '/username', 'https').match(/^https:\/\/domain\.com\/username$/) + end + + def test_device_token + assert_equal 64, @tester.device_token.size + end + + def test_user_with_args + user = @tester.user('name', 'email', 'password') + assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) + assert user['email'].match(/.+@.+\.\w+/) + assert user['password'].match(/\w{3}/) + end + + def test_user_without_args + user = @tester.user + assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) + assert user['email'].match(/.+@.+\.\w+/) + end +end From 522cc9211045bb9dd2fa385314bc64fe39e21e82 Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Fri, 6 Sep 2019 09:45:03 +0530 Subject: [PATCH 4/5] Add test for invalid args --- test/test_faker_internet.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_faker_internet.rb b/test/test_faker_internet.rb index 6b23517be4..2263a5ae6f 100644 --- a/test/test_faker_internet.rb +++ b/test/test_faker_internet.rb @@ -236,4 +236,10 @@ def test_user_without_args assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) assert user['email'].match(/.+@.+\.\w+/) end + + def test_user_with_invalid_args + assert_raises NoMethodError do + @tester.user('xyz') + end + end end From 5719e9380eb66675fa30da99c5c6cc5d9379de0a Mon Sep 17 00:00:00 2001 From: Ashish Rao Date: Fri, 6 Sep 2019 10:03:39 +0530 Subject: [PATCH 5/5] moved to default --- lib/faker/internet.rb | 180 -------------------------- test/test_faker_internet.rb | 245 ------------------------------------ 2 files changed, 425 deletions(-) delete mode 100644 lib/faker/internet.rb delete mode 100644 test/test_faker_internet.rb diff --git a/lib/faker/internet.rb b/lib/faker/internet.rb deleted file mode 100644 index 881b43e14d..0000000000 --- a/lib/faker/internet.rb +++ /dev/null @@ -1,180 +0,0 @@ -module Faker - class Internet < Base - class << self - - def email(name = nil) - [user_name(name), domain_name].join('@') - end - - def free_email(name = nil) - [user_name(name), fetch('internet.free_email')].join('@') - end - - def safe_email(name = nil) - [user_name(name), 'example.'+ sample(%w[org com net])].join('@') - end - - def user_name(specifier = nil, separators = %w(. _)) - with_locale(:en) do - if specifier.respond_to?(:scan) - return specifier.scan(/\w+/).shuffle.join(sample(separators)).downcase - elsif specifier.kind_of?(Integer) - # If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error - raise ArgumentError, "Given argument is too large" if specifier > 10**6 - tries = 0 # Don't try forever in case we get something like 1_000_000. - begin - result = user_name(nil, separators) - tries += 1 - end while result.length < specifier && tries < 7 - return result * (specifier/result.length + 1) if specifier > 0 - elsif specifier.kind_of?(Range) - tries = 0 - begin - result = user_name(specifier.min, separators) - tries += 1 - end while !specifier.include?(result.length) && tries < 7 - return result[0...specifier.max] - end - - sample([ - Char.prepare(Name.first_name), - [Name.first_name, Name.last_name].map{ |name| - Char.prepare(name) - }.join(sample(separators)) - ]) - end - end - - def password(min_length = 8, max_length = 16, mix_case = true, special_chars = false) - temp = Lorem.characters(min_length) - diff_length = max_length - min_length - if diff_length > 0 - diff_rand = rand(diff_length + 1) - temp += Lorem.characters(diff_rand) - end - - if mix_case - temp.chars.each_with_index do |char, index| - temp[index] = char.upcase if index.even? - end - end - - if special_chars - chars = %w(! @ # $ % ^ & *) - rand(min_length).times do |i| - temp[i] = chars[rand(chars.length)] - end - end - - temp - end - - def domain_name - with_locale(:en) { [Char.prepare(domain_word), domain_suffix].join('.') } - end - - def fix_umlauts(string='') - Char.fix_umlauts(string) - end - - def domain_word - return Char.prepare(Company.name.split(' ')[1]) if Config.locale == 'uk' - Char.prepare(Company.name.split(' ').first) - end - - def domain_suffix - fetch('internet.domain_suffix') - end - - def mac_address(prefix='') - prefix_digits = prefix.split(':').map{ |d| d.to_i(16) } - address_digits = (6 - prefix_digits.size).times.map{ rand(256) } - (prefix_digits + address_digits).map{ |d| '%02x' % d }.join(':') - end - - def ip_v4_address - ary = (2..254).to_a - [ sample(ary), sample(ary), sample(ary), sample(ary) ].join('.') - end - - def private_ip_v4_address - begin - addr = ip_v4_address - end while !private_net_checker[addr] - addr - end - - def public_ip_v4_address - begin - addr = ip_v4_address - end while reserved_net_checker[addr] - addr - end - - def private_nets_regex - [ - /^10\./, # 10.0.0.0 – 10.255.255.255 - /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 - /^127\./, # 127.0.0.0 – 127.255.255.255 - /^169\.254\./, # 169.254.0.0 – 169.254.255.255 - /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 - /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 - /^192\.168\./, # 192.168.0.0 – 192.168.255.255 - /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 - ] - end - - def private_net_checker - lambda { |addr| private_nets_regex.any? { |net| net =~ addr } } - end - - def reserved_nets_regex - [ - /^0\./, # 0.0.0.0 – 0.255.255.255 - /^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255 - /^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255 - /^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255 - /^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255 - /^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255 - /^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255 - ] - end - - def reserved_net_checker - ->(addr){ (private_nets_regex + reserved_nets_regex).any? { |net| net =~ addr } } - end - - def ip_v4_cidr - "#{ip_v4_address}/#{1 + rand(31)}" - end - - def ip_v6_address - (1..8).map { rand(65536).to_s(16) }.join(':') - end - - def ip_v6_cidr - "#{ip_v6_address}/#{1 + rand(127)}" - end - - def url(host = domain_name, path = "/#{user_name}", scheme = 'http') - "#{scheme}://#{host}#{path}" - end - - def slug(words = nil, glue = nil) - glue ||= sample(%w[- _ .]) - (words || Faker::Lorem::words(2).join(' ')).gsub(' ', glue).downcase - end - - def device_token - shuffle(rand(16 ** 64).to_s(16).rjust(64, '0').chars.to_a).join - end - - def user(*args) - args = %w[name email] if args.empty? - user_obj = {} - args.each { |arg| user_obj[arg] = send(arg) } - user_obj - end - end - end -end diff --git a/test/test_faker_internet.rb b/test/test_faker_internet.rb deleted file mode 100644 index 2263a5ae6f..0000000000 --- a/test/test_faker_internet.rb +++ /dev/null @@ -1,245 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') - -class TestFakerInternet < Test::Unit::TestCase - - def setup - @tester = Faker::Internet - end - - def test_email - assert @tester.email.match(/.+@.+\.\w+/) - end - - def test_free_email - assert @tester.free_email.match(/.+@(gmail|hotmail|yahoo)\.com/) - end - - def test_safe_email - assert @tester.safe_email.match(/.+@example.(com|net|org)/) - end - - def test_user_name - assert @tester.user_name.match(/[a-z]+((_|\.)[a-z]+)?/) - end - - def test_user_name_with_string_arg - assert @tester.user_name('bo peep').match(/(bo(_|\.)peep|peep(_|\.)bo)/) - end - - def test_user_name_with_integer_arg - (1..32).each do |min_length| - assert @tester.user_name(min_length).length >= min_length - end - end - - def test_user_name_with_very_large_integer_arg - exception = assert_raises(ArgumentError) { @tester.user_name(10000000) } - assert_equal('Given argument is too large', exception.message) - end - - def test_user_name_with_closed_range_arg - (1..32).each do |min_length| - (min_length..32).each do |max_length| - l = @tester.user_name((min_length..max_length)).length - assert l >= min_length - assert l <= max_length - end - end - end - - def test_user_name_with_open_range_arg - (1..32).each do |min_length| - (min_length+1..33).each do |max_length| - l = @tester.user_name((min_length...max_length)).length - assert l >= min_length - assert l <= max_length-1 - end - end - end - - def test_user_name_with_range_and_separators - (1..32).each do |min_length| - (min_length+1..33).each do |max_length| - u = @tester.user_name((min_length...max_length), %w(=)) - assert u.length.between? min_length, max_length-1 - assert u.match(/\A[a-z]+((=)?[a-z]*)*\z/) - end - end - end - - def test_password - assert @tester.password.match(/\w{3}/) - end - - def test_password_with_integer_arg - (1..32).each do |min_length| - assert @tester.password(min_length).length >= min_length - end - end - - def test_password_max_with_integer_arg - (1..32).each do |min_length| - max_length = min_length + 4 - assert @tester.password(min_length, max_length).length <= max_length - end - end - - def test_password_with_mixed_case - assert @tester.password.match(/[A-Z]+/) - end - - def test_password_without_mixed_case - assert @tester.password(8, 12, false).match(/[^A-Z]+/) - end - - def test_password_with_special_chars - assert @tester.password(8, 12, true, true).match(/[!@#\$%\^&\*]+/) - end - - def test_password_without_special_chars - assert @tester.password(8, 12, true).match(/[^!@#\$%\^&\*]+/) - end - - def test_domain_name - assert @tester.domain_name.match(/\w+\.\w+/) - end - - def test_domain_word - assert @tester.domain_word.match(/^\w+$/) - end - - def test_domain_suffix - assert @tester.domain_suffix.match(/^\w+(\.\w+)?/) - end - - def test_ip_v4_address - assert_equal 3, @tester.ip_v4_address.count('.') - - 100.times do - assert @tester.ip_v4_address.split('.').map{|octet| octet.to_i}.max <= 255 - end - end - - def test_private_ip_v4_address - regexps = [ - /^10\./, # 10.0.0.0 – 10.255.255.255 - /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 - /^127\./, # 127.0.0.0 – 127.255.255.255 - /^169\.254\./, # 169.254.0.0 – 169.254.255.255 - /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 - /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 - /^192\.168\./, # 192.168.0.0 – 192.168.255.255 - /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 - ] - expected = Regexp.new regexps.collect{|reg| "(#{reg})"}.join('|') - - 1000.times do - address = @tester.private_ip_v4_address - assert_match expected, address - end - end - - def test_public_ip_v4_address - private = [ - /^10\./, # 10.0.0.0 – 10.255.255.255 - /^100\.(6[4-9]|[7-9]\d|1[0-1]\d|12[0-7])\./, # 100.64.0.0 – 100.127.255.255 - /^127\./, # 127.0.0.0 – 127.255.255.255 - /^169\.254\./, # 169.254.0.0 – 169.254.255.255 - /^172\.(1[6-9]|2\d|3[0-1])\./, # 172.16.0.0 – 172.31.255.255 - /^192\.0\.0\./, # 192.0.0.0 – 192.0.0.255 - /^192\.168\./, # 192.168.0.0 – 192.168.255.255 - /^198\.(1[8-9])\./ # 198.18.0.0 – 198.19.255.255 - ] - - reserved = [ - /^0\./, # 0.0.0.0 – 0.255.255.255 - /^192\.0\.2\./, # 192.0.2.0 – 192.0.2.255 - /^192\.88\.99\./, # 192.88.99.0 – 192.88.99.255 - /^198\.51\.100\./, # 198.51.100.0 – 198.51.100.255 - /^203\.0\.113\./, # 203.0.113.0 – 203.0.113.255 - /^(22[4-9]|23\d)\./, # 224.0.0.0 – 239.255.255.255 - /^(24\d|25[0-5])\./ # 240.0.0.0 – 255.255.255.254 and 255.255.255.255 - ] - - 1000.times do - address = @tester.public_ip_v4_address - private.each { |reg| assert_not_match reg, address } - reserved.each { |reg| assert_not_match reg, address } - end - end - - def test_ip_v4_cidr - assert @tester.ip_v4_cidr.match(/\/\d{1,2}$/) - - 1000.times do - assert (1..32).include?(@tester.ip_v4_cidr.split('/').last.to_i) - end - end - - def test_mac_address - assert_equal 5, @tester.mac_address.count(':') - assert_equal 5, @tester.mac_address("").count(':') - - 100.times do - assert @tester.mac_address.split(':').map{|d| d.to_i(16)}.max <= 255 - end - - assert @tester.mac_address("fa:fa:fa").start_with?("fa:fa:fa") - assert @tester.mac_address("01:02").start_with?("01:02") - end - - def test_ip_v6_address - assert_equal 7, @tester.ip_v6_address.count(':') - - 100.times do - assert @tester.ip_v6_address.split('.').map{|h| "0x#{h}".hex}.max <= 65535 - end - end - - def test_ip_v6_cidr - assert @tester.ip_v6_cidr.match(/\/\d{1,3}$/) - - 1000.times do - assert (1..128).include?(@tester.ip_v6_cidr.split('/').last.to_i) - end - end - - def test_slug - assert @tester.slug.match(/^[a-z]+(_|\.|\-)[a-z]+$/) - end - - def test_slug_with_content_arg - assert @tester.slug('Foo bAr baZ').match(/^foo(_|\.|\-)bar(_|\.|\-)baz$/) - end - - def test_slug_with_glue_arg - assert @tester.slug(nil, '+').match(/^[a-z]+\+[a-z]+$/) - end - - def test_url - assert @tester.url('domain.com', '/username', 'https').match(/^https:\/\/domain\.com\/username$/) - end - - def test_device_token - assert_equal 64, @tester.device_token.size - end - - def test_user_with_args - user = @tester.user('name', 'email', 'password') - assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) - assert user['email'].match(/.+@.+\.\w+/) - assert user['password'].match(/\w{3}/) - end - - def test_user_without_args - user = @tester.user - assert user['name'].match(/[a-z]+((_|\.)[a-z]+)?/) - assert user['email'].match(/.+@.+\.\w+/) - end - - def test_user_with_invalid_args - assert_raises NoMethodError do - @tester.user('xyz') - end - end -end