-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,100 @@ | ||
pending("implement rng tests", function() | ||
-- TODO: implement | ||
describe("rng's", function() | ||
|
||
describe("math_random()", function() | ||
|
||
it("generates 1 byte", function() | ||
local rng = require("uuid").rng.math_random() | ||
|
||
assert.is.string(rng(1)) | ||
assert.is.equal(1, #rng(1)) | ||
end) | ||
|
||
|
||
it("generates 256 bytes", function() | ||
local rng = require("uuid").rng.math_random() | ||
|
||
assert.is.string(rng(256)) | ||
assert.is.equal(256, #rng(256)) | ||
end) | ||
|
||
end) | ||
|
||
|
||
|
||
describe("luasystem()", function() | ||
|
||
it("picks up system.random", function() | ||
package.loaded.system = { | ||
random = function(n) | ||
return string.char(0):rep(n) | ||
end | ||
} | ||
|
||
local rng = require("uuid").rng.luasystem() | ||
|
||
assert.are.equal(package.loaded.system.random, rng) | ||
end) | ||
|
||
it("errors if not available", function() | ||
local old_require = _G.require | ||
finally(function() | ||
_G.require = old_require | ||
end) | ||
_G.require = function(name) | ||
if name ~= "system" then | ||
return old_require(name) | ||
end | ||
return error("not found") | ||
end | ||
|
||
local rng, err = require("uuid").rng.luasystem() | ||
assert.is.falsy(rng) | ||
assert.is.equal("luasystem not available", err) | ||
end) | ||
|
||
it("errors if too old", function() | ||
package.loaded.system = {} -- not having 'random' function | ||
|
||
local rng, err = require("uuid").rng.luasystem() | ||
assert.is.falsy(rng) | ||
assert.is.equal("luasystem version is too old, minimum version required is 0.3", err) | ||
end) | ||
|
||
end) | ||
|
||
|
||
|
||
describe("urandom()", function() | ||
|
||
it("generates 1 byte", function() | ||
local rng = require("uuid").rng.urandom() | ||
|
||
assert.is.string(rng(1)) | ||
assert.is.equal(1, #rng(1)) | ||
end) | ||
|
||
|
||
it("generates 256 bytes", function() | ||
local rng = require("uuid").rng.urandom() | ||
|
||
assert.is.string(rng(256)) | ||
assert.is.equal(256, #rng(256)) | ||
end) | ||
|
||
it("fails on Windows", function() | ||
local old_package_config = package.config | ||
finally(function() | ||
package.config = old_package_config -- luacheck: ignore | ||
end) | ||
-- make it think this is Windows | ||
package.config = "\\" .. package.config:sub(2,-1) -- luacheck: ignore | ||
|
||
local rng, err = require("uuid").rng.urandom() | ||
assert.is.falsy(rng) | ||
assert.is.equal("/dev/urandom is not available on Windows", err) | ||
end) | ||
|
||
end) | ||
|
||
|
||
end) |