Skip to content

Commit

Permalink
bug fixed in read_to_buffer (#32457)
Browse files Browse the repository at this point in the history
* bug fixed in read_to_buffer

* added test for #32397.

* switch back to original case

* added torture test for long encoded string randomly laced with spaced

(cherry picked from commit 4b6ab68)
  • Loading branch information
Jan Sulmont authored and KristofferC committed Feb 20, 2020
1 parent 92dad56 commit e199858
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
4 changes: 2 additions & 2 deletions stdlib/Base64/src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ end

function read_to_buffer(io::IO, buffer::Buffer)
offset = buffer.ptr - pointer(buffer.data)
copyto!(buffer.data, 1, buffer.data, offset, buffer.size)
buffer.ptr = pointer(buffer.data) + buffer.size
copyto!(buffer.data, 1, buffer.data, offset + 1, buffer.size)
buffer.ptr = pointer(buffer.data)
if !eof(io)
n = min(bytesavailable(io), capacity(buffer) - buffer.size)
unsafe_read(io, buffer.ptr + buffer.size, n)
Expand Down
35 changes: 30 additions & 5 deletions stdlib/Base64/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="""

const longEncodedText = "bmFtZSA9ICJHZW5pZSIKdXVpZCA9ICJjNDNjNzM2ZS1hMmQxLTExZTgtMTYx\nZi1hZjk1MTE3ZmJkMWUiCmF1dGhvcnMgPSBbIkFkcmlhbiBTYWxjZWFudSA8\nZUBlc3NlbmNpYXJ5LmNvbT4iXQp2ZXJzaW9uID0gIjAuOS40IgoKW2RlcHNd\nCkFyZ1BhcnNlID0gImM3ZTQ2MGM2LTJmYjktNTNh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zNi1lNjMwYmE5MGFiNjkiClJldmlzZSA9ICIy\nOTVhZjMwZi1lNGFkLTUzN2ItODk4My0wMDEyNmMyYTNhYmUiClNIQSA9ICJl\nYThlOTE5Yy0yNDNjLTUxYWYtODgyNS1hYWE2M2NkNzIxY2UiClNlcmlhbGl6\nYXRpb24gPSAiOWU4OGI0MmEtZjgyOS01YjBjLWJiZTktOWU5MjMxOTgxNjZi\nIgpTb2NrZXRzID0gIjY0NjJmZTBiLTI0ZGUtNTYzMS04Njk3LWRkOTQxZjkw\nZGVjYyIKVVJJUGFyc2VyID0gIjMwNTc4YjQ1LTlhZGMtNTk0Ni1iMjgzLTY0\nNWVjNDIwYWY2NyIKVW5pY29kZSA9ICI0ZWMwYTgzZS00OTNlLTUwZTItYjlh\nYy04ZjcyYWNmNWE4ZjUiCllBTUwgPSAiZGRiNmQ5MjgtMjg2OC01NzBmLWJk\nZGYtYWIzZjljZjk5ZWI2IgoKW2NvbXBhdF0KanVsaWEgPSAiMSIK"
const longDecodedText = "name = \"Genie\"\nuuid = \"c43c736e-a2d1-11e8-161f-af95117fbd1e\"\nauthors = [\"Adrian Salceanu <e@essenciary.com>\"]\nversion = \"0.9.4\"\n\n[deps]\nArgParse = \"c7e460c6-2fb9-53a9-8c5b-16f535851c63\"\nDates = \"ade2ca70-3891-5945-98fb-dc099432e06a\"\nDistributed = \"8ba89e20-285c-5b6f-9357-94700520ee1b\"\nGumbo = \"708ec375-b3d6-5a57-a7ce-8257bf98657a\"\nHTTP = \"cd3eb016-35fb-5094-929b-558a96fad6f3\"\nHttpCommon = \"77172c1b-203f-54ac-aa54-3f1198fe9f90\"\nJSON = \"682c06a0-de6a-54ab-a142-c8b1cf79cde6\"\nMarkdown = \"d6f4376e-aef5-505a-96c1-9c027394607a\"\nMbedTLS = \"739be429-bea8-5141-9913-cc70e7f3736d\"\nMillboard = \"39ec1447-df44-5f4c-beaa-866f30b4d3b2\"\nMiniLogging = \"f59402ec-0262-5707-a561-770af94bc5a6\"\nNettle = \"49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9\"\nNullables = \"4d1e1d77-625e-5b40-9113-a560ec7a8ecd\"\nOrderedCollections = \"bac558e1-5e72-5ebc-8fee-abe8a469f55d\"\nPkg = \"44cfe95a-1eb2-52ea-b672-e2afdf69b78f\"\nReexport = \"189a3867-3050-52da-a836-e630ba90ab69\"\nRevise = \"295af30f-e4ad-537b-8983-00126c2a3abe\"\nSHA = \"ea8e919c-243c-51af-8825-aaa63cd721ce\"\nSerialization = \"9e88b42a-f829-5b0c-bbe9-9e923198166b\"\nSockets = \"6462fe0b-24de-5631-8697-dd941f90decc\"\nURIParser = \"30578b45-9adc-5946-b283-645ec420af67\"\nUnicode = \"4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5\"\nYAML = \"ddb6d928-2868-570f-bddf-ab3f9cf99eb6\"\n\n[compat]\njulia = \"1\"\n"

@testset "Examples" begin
# Encode and decode
fname = tempname()
open(fname, "w") do f
opipe = Base64EncodePipe(f)
write(opipe,inputText)
write(opipe, inputText)
@test close(opipe) === nothing
end

Expand Down Expand Up @@ -58,15 +61,18 @@ ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="""
#@test read(ipipe, String) == inputText

# Decode with two padding characters ("==")
ipipe = Base64DecodePipe(IOBuffer(string(encodedMaxLine76[1:end-2],"==")))
@test read(ipipe, String) == inputText[1:end-1]
ipipe = Base64DecodePipe(IOBuffer(string(encodedMaxLine76[1:end - 2], "==")))
@test read(ipipe, String) == inputText[1:end - 1]

# Test incorrect format
ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end-3]))
ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end - 3]))
@test_throws ArgumentError read(ipipe, String)

# issue #21314
@test base64decode(chomp("test")) == base64decode("test")

# issue #32397
@test String(base64decode(longEncodedText)) == longDecodedText;
end

@testset "Random data" begin
Expand All @@ -85,6 +91,25 @@ Base.show(io::IO, ::MIME"image/png", ::PNG) = print(io, "PNG")
@test stringmime("text/html", "raw html data") == "raw html data"
@test stringmime("text/plain", "string") == "\"string\""
@test stringmime("image/png", UInt8[2,3,4,7]) == "AgMEBw=="
@test stringmime("text/plain", 3.141592653589793, context=:compact=>true) == "3.14159"
@test stringmime("text/plain", 3.141592653589793, context = :compact => true) == "3.14159"
@test stringmime("image/png", PNG()) == stringmime(MIME("image/png"), PNG()) == "UE5H"
end

function splace(in::String, p = 0.3)
spaces = ["\n"," "]
len = length(in)
len == 0 && return in
rc::String = ""
i = 1
for (x, v) in enumerate(sort(randsubseq(collect(1:len), p)))
rc = rc * in[i:v] * rand(spaces)^rand(Int.(1:10))
i = v + 1
end
return rc * in[i:len] * rand(spaces)^rand(Int.(1:10))
end

@testset "lstrsplaced" begin
for _ in 1:1000
@test String(base64decode(splace(longEncodedText))) == longDecodedText
end
end

0 comments on commit e199858

Please sign in to comment.