--[[-- 京东读书双 MD5 签名与下载 AES 离线用例。 @module tests.moonsource_jdread.protocol_spec --]] ---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch, cast-type-mismatch, return-type-mismatch, cast-local-type local Assert = require("support.assert") package.preload["json"] = function() return { decode = require("support.json_stub").decode, encode = require("support.json_stub").encode, } end package.loaded["json"] = nil package.loaded["moonsource_jdread.protocol"] = nil local Protocol = require("moonsource_jdread.protocol") do local params = Protocol.signedParams( "/jdread/api/marker/sync", "h500000000000000000000000000000000", nil, 1700000000123 ) Assert.eq(params.sign, "6f219d40caff3860cb21cebbfb9ce5c7") end do Assert.eq(Protocol.evenTime(1700000000001), 1700000000002) Assert.eq(Protocol.evenTime(1700000000000), 1700000000000) local tm = 1700000000000 local query = "app=jdread-m&tm=1700000000000&uuid=h5-test" local enc = Protocol.encryptQuery(query, tm) Assert.is_true(enc:find("[-_A-Za-z0-9]+") ~= nil) Assert.is_nil(enc:find("+", 1, true)) Assert.is_nil(enc:find("/", 1, true)) local function encrypted(json) return require("utils.text").base64Encode( require("crypto.aes").ecb_encrypt( json, (require("ffi/sha2").md5(tostring(tm) .. "jdread-m"):gsub("(%x%x)", function(pair) return string.char(tonumber(pair, 16)) end)) ) ) end local wire, err = Protocol.decodeDownload( encrypted('{"data":{"chapter":[{"content":"
一
"}]},"result_code":0,"message":"SUCCESS"}'), tm ) Assert.is_nil(err) Assert.eq(wire.data.chapter[1].content, "一
") -- 网页阅读器实测:未购买章节的错误信封同样被加密 local denied, denied_err, code = Protocol.decodeDownload( encrypted('{"result_code":101,"message":"can not download"}'), tm ) Assert.is_nil(denied) Assert.eq(denied_err, "can not download") Assert.eq(code, 101) end do local wire, err, code = Protocol.decodeDownload('{"result_code":1,"message":"UNKNOWN_ERROR"}', 1) Assert.is_nil(wire) Assert.eq(err, "UNKNOWN_ERROR") Assert.eq(code, 1) end