1## a few tests split off the test_digest.R file
2
3suppressMessages(library(digest))
4
5## test 'length' parameter and file input
6##fname <- file.path(R.home(),"COPYING")  ## not invariant across OSs
7fname <- system.file("GPL-2", package="digest")
8x <- readChar(fname, file.info(fname)$size) # read file
9xskip <- substring(x, first=20+1)
10for (alg in c("sha1", "md5", "crc32", "sha256", "sha512",
11              "xxhash32", "xxhash64", "murmur32")) {
12                                        # partial file
13    h1 <- digest(x    , length=18000, algo=alg, serialize=FALSE)
14    h2 <- digest(fname, length=18000, algo=alg, serialize=FALSE, file=TRUE)
15    expect_true(identical(h1,h2))
16    h3 <- digest(substr(x,1,18000)  , algo=alg, serialize=FALSE)
17    expect_true(identical(h1,h3))
18    #cat(h1, "\n", h2, "\n", h3, "\n")
19    expect_identical(
20      getVDigest(alg)(x, length = 18e3, serialize = FALSE),
21      getVDigest(alg)(fname, length = 18e3, serialize = FALSE, file = TRUE)
22    )
23                                        # whole file
24    h4 <- digest(x    , algo=alg, serialize=FALSE)
25    h5 <- digest(fname, algo=alg, serialize=FALSE, file=TRUE)
26    expect_true( identical(h4,h5) )
27
28    expect_identical(
29      getVDigest(alg)(x, serialize = FALSE),
30      getVDigest(alg)(fname, serialize = FALSE, file = TRUE)
31    )
32
33    ## Assert that 'skip' works
34    h6 <- digest(xskip, algo=alg, serialize=FALSE)
35    h7 <- digest(fname, algo=alg, serialize=FALSE, skip=20, file=TRUE)
36    expect_true( identical(h6, h7) )
37    expect_identical(
38      getVDigest(alg)(xskip, serialize = FALSE),
39      getVDigest(alg)(fname, serialize = FALSE, skip = 20, file = TRUE)
40    )
41}
42
43## compare md5 algorithm to other tools
44library(tools)
45##fname <- file.path(R.home(),"COPYING")  ## not invariant across OSs
46fname <- system.file("GPL-2", package="digest")
47h1 <- as.character(md5sum(fname))
48h2 <- digest(fname, algo="md5", file=TRUE)
49expect_true( identical(h1,h2) )
50
51## Make sure we don't core dump with unreadable files.
52fname <- tempfile()
53#cat("Hello World, you won't have access to read me", file=fname)
54on.exit(unlink(fname))
55Sys.chmod(fname, mode="0000")
56try(res <- digest(fname, file=TRUE), silent=TRUE)
57