Revision 0b288282 htest/Test/Ganeti/TestCommon.hs
b/htest/Test/Ganeti/TestCommon.hs | ||
---|---|---|
38 | 38 |
import Test.QuickCheck |
39 | 39 |
import Test.QuickCheck.Monadic |
40 | 40 |
import qualified Text.JSON as J |
41 |
import Numeric |
|
41 | 42 |
|
42 | 43 |
import qualified Ganeti.BasicTypes as BasicTypes |
44 |
import Ganeti.Types |
|
43 | 45 |
|
44 | 46 |
-- * Constants |
45 | 47 |
|
... | ... | |
215 | 217 |
genSet :: (Ord a, Bounded a, Enum a) => Maybe Int -> Gen (Set.Set a) |
216 | 218 |
genSet = genSetHelper [minBound..maxBound] |
217 | 219 |
|
220 |
-- | Generate an arbitrary IPv4 address in textual form (non empty). |
|
221 |
genIp4Addr :: Gen NonEmptyString |
|
222 |
genIp4Addr = genIp4AddrStr >>= mkNonEmpty |
|
223 |
|
|
224 |
-- | Generate an arbitrary IPv4 address in textual form. |
|
225 |
genIp4AddrStr :: Gen String |
|
226 |
genIp4AddrStr = do |
|
227 |
a <- choose (1::Int, 255) |
|
228 |
b <- choose (0::Int, 255) |
|
229 |
c <- choose (0::Int, 255) |
|
230 |
d <- choose (0::Int, 255) |
|
231 |
return $ intercalate "." (map show [a, b, c, d]) |
|
232 |
|
|
233 |
-- | Generates an arbitrary IPv4 address with a given netmask in textual form. |
|
234 |
genIp4NetWithNetmask :: Int -> Gen NonEmptyString |
|
235 |
genIp4NetWithNetmask netmask = do |
|
236 |
ip <- genIp4AddrStr |
|
237 |
mkNonEmpty $ ip ++ "/" ++ show netmask |
|
238 |
|
|
239 |
-- | Generate an arbitrary IPv4 network in textual form. |
|
240 |
genIp4Net :: Gen NonEmptyString |
|
241 |
genIp4Net = do |
|
242 |
netmask <- choose (8::Int, 30) |
|
243 |
genIp4NetWithNetmask netmask |
|
244 |
|
|
245 |
-- | Helper function to compute the number of hosts in a network |
|
246 |
-- given the netmask. (For IPv4 only.) |
|
247 |
netmask2NumHosts :: Int -> Int |
|
248 |
netmask2NumHosts n = (2::Int)^((32::Int)-n) |
|
249 |
|
|
250 |
-- | Generates an arbitrary IPv6 network address in textual form. |
|
251 |
-- The generated address is not simpflified, e. g. an address like |
|
252 |
-- "2607:f0d0:1002:0051:0000:0000:0000:0004" does not become |
|
253 |
-- "2607:f0d0:1002:51::4" |
|
254 |
genIp6Addr :: Gen String |
|
255 |
genIp6Addr = do |
|
256 |
rawIp <- vectorOf 8 $ choose (0::Integer, 65535) |
|
257 |
return $ intercalate ":" (map (`showHex` "") rawIp) |
|
258 |
|
|
259 |
-- | Generates an arbitrary IPv6 network in textual form. |
|
260 |
genIp6Net :: Gen String |
|
261 |
genIp6Net = do |
|
262 |
netmask <- choose (8::Int, 126) |
|
263 |
ip <- genIp6Addr |
|
264 |
return $ ip ++ "/" ++ show netmask |
|
265 |
|
|
218 | 266 |
-- * Helper functions |
219 | 267 |
|
220 | 268 |
-- | Checks for serialisation idempotence. |
Also available in: Unified diff