def test_list(self): data = bytes.fromhex("0400000001020304") stream = streams.StreamIn(data, settings.default()) assert stream.list(stream.u8) == [1, 2, 3, 4] data = bytes.fromhex("020000000000000014000000280000003c000000") stream = streams.StreamIn(data, settings.default()) rrs = stream.list(common.ResultRange) assert rrs[0].offset == 0 and rrs[0].size == 20 assert rrs[1].offset == 40 and rrs[1].size == 60
def test_map(self): value = {1: 10, 2: 11} data = bytes.fromhex("02000000010a00020b00") stream = streams.StreamIn(data, settings.default()) assert stream.map(stream.u8, stream.u16) == value data = bytes.fromhex("01000000393000001400000005000000") stream = streams.StreamIn(data, settings.default()) map = stream.map(stream.pid, common.ResultRange) assert map[12345].offset == 20 assert map[12345].size == 5
async def test_negotiation(): s1 = settings.default() s1["prudp.minor_version"] = 2 s2 = settings.default() s2["prudp.minor_version"] = 5 async def handler(client): assert client.minor_version() == 2 async with prudp.serve(handler, s1, HOST, 12345): async with prudp.connect(s2, HOST, 12345) as client: assert client.minor_version() == 2
async def main(): nas = nnas.NNASClient() nas.set_device(DEVICE_ID, SERIAL_NUMBER, SYSTEM_VERSION) nas.set_title(MK8.TITLE_ID_EUR, MK8.LATEST_VERSION) nas.set_locale(REGION_ID, COUNTRY_NAME, LANGUAGE) access_token = await nas.login(USERNAME, PASSWORD) nex_token = await nas.get_nex_token(access_token.token, MK8.GAME_SERVER_ID) s = settings.default() s.configure(MK8.ACCESS_KEY, MK8.NEX_VERSION) async with backend.connect(s, nex_token.host, nex_token.port) as be: async with be.login(str(nex_token.pid), nex_token.password) as client: ranking_client = ranking.RankingClient(client) order_param = ranking.RankingOrderParam() order_param.order_calc = ranking.RankingOrderCalc.ORDINAL order_param.offset = 499 #Start at 500th place order_param.count = 20 #Download 20 highscores rankings = await ranking_client.get_ranking( ranking.RankingMode.GLOBAL, TRACK_ID, order_param, 0, 0 ) ranking_stats = await ranking_client.get_stats( TRACK_ID, order_param, ranking.RankingStatFlags.ALL ) names = await nas.get_nnids([data.pid for data in rankings.data]) #Print some interesting stats stats = ranking_stats.stats print("Total:", int(stats[0])) print("Total time:", format_time(stats[1])) print("Lowest time:", format_time(stats[2])) print("Highest time:", format_time(stats[3])) print("Average time:", format_time(stats[4])) print("Rankings:") for rankdata in rankings.data: time = format_time(rankdata.score) print("\t%5i %20s %s" %(rankdata.rank, names[rankdata.pid], time)) #Let's download the replay file of whoever is in 500th place store = datastore.DataStoreClient(client) rankdata = rankings.data[0] get_param = datastore.DataStorePrepareGetParam() get_param.persistence_target.owner_id = rankdata.pid get_param.persistence_target.persistence_id = TRACK_ID - 16 get_param.extra_data = ["WUP", str(REGION_ID), REGION_NAME, str(COUNTRY_ID), COUNTRY_NAME, ""] req_info = await store.prepare_get_object(get_param) headers = {header.key: header.value for header in req_info.headers} response = await http.get(req_info.url, headers=headers) response.raise_if_error() with open("replay.bin", "wb") as f: f.write(response.body)
def test_stationurl(self): data = b"\x16\0prudps:/PID=1;CID=100\0" stream = streams.StreamIn(data, settings.default()) url = stream.stationurl() assert url.scheme() == "prudps" assert url["PID"] == 1 assert url["CID"] == 100
def test_pid(self): stream = streams.StreamOut(settings.default()) stream.pid(12345) assert stream.get() == bytes.fromhex("39300000") stream = streams.StreamOut(settings.load("switch")) stream.pid(12345) assert stream.get() == bytes.fromhex("3930000000000000")
def test_add(self): stream = streams.StreamOut(settings.default()) stream.add(common.ResultRange()) assert stream.get() == bytes.fromhex("000000000a000000") stream = streams.StreamOut(settings.load("switch")) stream.add(common.ResultRange()) assert stream.get() == bytes.fromhex("0008000000000000000a000000")
async def main(): nas = nnas.NNASClient() nas.set_device(DEVICE_ID, SERIAL_NUMBER, SYSTEM_VERSION) nas.set_title(DKCTF.TITLE_ID_EUR, DKCTF.LATEST_VERSION) nas.set_locale(REGION, COUNTRY, LANGUAGE) access_token = await nas.login(USERNAME, PASSWORD) nex_token = await nas.get_nex_token(access_token.token, DKCTF.GAME_SERVER_ID) s = settings.default() s.configure(DKCTF.ACCESS_KEY, DKCTF.NEX_VERSION) async with backend.connect(s, nex_token.host, nex_token.port) as be: async with be.login(str(nex_token.pid), nex_token.password) as client: order_param = ranking.RankingOrderParam() order_param.offset = 0 #Start with the world record order_param.count = 20 #Download 20 highscores ranking_client = ranking.RankingClient(client) rankings = await ranking_client.get_ranking( ranking.RankingMode.GLOBAL, #Get the global leaderboard 0x893EB726, #Category, this is 3-A (Magrove Cove) order_param, 0, 0) print("Total:", rankings.total) print("Rankings:") for rankdata in rankings.data: seconds = (rankdata.score >> 1) / 60 time = "%i:%02i.%02i" % (seconds / 60, seconds % 60, (seconds * 100) % 100) damage = " Damaged " if rankdata.score & 1 else "No damage" kong = ["No Kong", "Diddy", "Dixie", "Cranky"][rankdata.groups[1]] name = rankdata.common_data.decode("ascii")[:-1] print("\t%2i %20s %s (%s) %s" % (rankdata.rank, name, time, damage, kong)) #Now download the world record replay file if available world_record = rankings.data[0] if world_record.param: #If world record has a replay file store = datastore.DataStoreClient(client) get_param = datastore.DataStorePrepareGetParam() get_param.data_id = world_record.param req_info = await store.prepare_get_object(get_param) headers = { header.key: header.value for header in req_info.headers } response = await http.get(req_info.url, headers=headers) response.raise_if_error() with open("replay.bin", "wb") as f: f.write(response.body)
def test_variant(self): data = b"\0" stream = streams.StreamIn(data, settings.default()) assert stream.variant() is None data = bytes.fromhex("01c7cfffffffffffff") stream = streams.StreamIn(data, settings.default()) assert stream.variant() == -12345 data = bytes.fromhex("02cdccccccccdc5e40") stream = streams.StreamIn(data, settings.default()) assert stream.variant() == 123.45 data = bytes.fromhex("03010300") stream = streams.StreamIn(data, settings.default()) assert stream.variant() is True assert stream.variant() is False data = b"\x04\x06\0hello\0" stream = streams.StreamIn(data, settings.default()) assert stream.variant() == "hello" data = bytes.fromhex("050000000000000000") stream = streams.StreamIn(data, settings.default()) assert stream.variant().value() == 0 data = bytes.fromhex("06c7cfffffffffffff") stream = streams.StreamIn(data, settings.default()) assert stream.variant() == 0xFFFFFFFFFFFFCFC7
def test_variant(self): stream = streams.StreamOut(settings.default()) stream.variant(None) assert stream.get() == b"\0" stream = streams.StreamOut(settings.default()) stream.variant(-12345) assert stream.get() == bytes.fromhex("01c7cfffffffffffff") stream = streams.StreamOut(settings.default()) stream.variant(123.45) assert stream.get() == bytes.fromhex("02cdccccccccdc5e40") stream = streams.StreamOut(settings.default()) stream.variant(True) stream.variant(False) assert stream.get() == bytes.fromhex("03010300") stream = streams.StreamOut(settings.default()) stream.variant("hello") assert stream.get() == b"\x04\x06\0hello\0" stream = streams.StreamOut(settings.default()) stream.variant(common.DateTime.never()) assert stream.get() == bytes.fromhex("050000000000000000") stream = streams.StreamOut(settings.default()) stream.variant(12345) assert stream.get() == bytes.fromhex("063930000000000000")
def test_anydata(self): stream = streams.StreamOut(settings.default()) stream.anydata(common.NullData()) assert stream.get() == b"\x09\0NullData\0\x04\0\0\0\0\0\0\0" stream = streams.StreamOut(settings.load("switch")) stream.anydata(common.NullData()) assert stream.get( ) == b"\x09\0NullData\0\x0E\0\0\0\x0A\0\0\0\0\0\0\0\0\0\0\0\0\0"
def test_substream(self): data = b"\x08\0\0\0\x64\0\0\0\xc8\0\0\0\xff\0\0\0" stream = streams.StreamIn(data, settings.default()) substream = stream.substream() assert stream.u32() == 255 assert substream.u32() == 100 assert substream.u32() == 200 assert substream.eof()
def test_repeat(self): data = bytes.fromhex("0102030400000000140000001400000020000000") stream = streams.StreamIn(data, settings.default()) ints = stream.repeat(stream.u8, 4) rrs = stream.repeat(common.ResultRange, 2) assert ints == [1, 2, 3, 4] assert rrs[0].offset == 0 and rrs[0].size == 20 assert rrs[1].offset == 20 and rrs[1].size == 32
async def test_logout_event(): s = settings.default() server = AuthenticationServer() async with rmc.serve(s, [server], HOST, 12345): async with rmc.connect(s, HOST, 12345) as client: assert not server.flag await anyio.sleep(.1) # Wait a bit assert server.flag
async def test_pid(): s = settings.default() async def handler(client): assert client.pid() is None async with prudp.serve(handler, s, HOST, 12345): async with prudp.connect(s, HOST, 12345) as client: assert client.pid() is None
def test_anydata(self): data = b"\x09\0NullData\0\x04\0\0\0\0\0\0\0" stream = streams.StreamIn(data, settings.default()) assert isinstance(stream.anydata(), common.NullData) assert stream.eof() data = b"\x09\0NullData\0\x0E\0\0\0\x0A\0\0\0\0\0\0\0\0\0\0\0\0\0" stream = streams.StreamIn(data, settings.load("switch")) assert isinstance(stream.anydata(), common.NullData) assert stream.eof()
def test_pid(self): data = bytes.fromhex("39300000") stream = streams.StreamIn(data, settings.default()) assert stream.pid() == 12345 assert stream.eof() data = bytes.fromhex("3930000000000000") stream = streams.StreamIn(data, settings.load("switch")) assert stream.pid() == 12345 assert stream.eof()
def test_constants(): s = settings.default() assert s.TRANSPORT_UDP == 0 assert s.TRANSPORT_TCP == 1 assert s.TRANSPORT_WEBSOCKET == 2 assert s.COMPRESSION_NONE == 0 assert s.COMPRESSION_ZLIB == 1 assert s.ENCRYPTION_NONE == 0 assert s.ENCRYPTION_RC4 == 1
async def backend_setup(): global datastore_smm_client s = settings.default() s.configure(SMM.ACCESS_KEY, SMM.NEX_VERSION) async with backend.connect(s, nex_token.host, nex_token.port) as be: async with be.login(str(nex_token.pid), nex_token.password) as client: datastore_smm_client = datastore_smm.DataStoreClientSMM(client) await scrape() # start ripping courses
async def test_unreliable(): s = settings.default() async def handler(client): assert await client.recv_unreliable() == b"ping" await client.send_unreliable(b"pong") async with prudp.serve(handler, s, HOST, 12345): async with prudp.connect(s, HOST, 12345) as client: await client.send_unreliable(b"ping") assert await client.recv_unreliable() == b"pong"
async def test_simple(): s = settings.default() servers = [AuthenticationServer()] async with rmc.serve(s, servers, HOST, 12345): async with rmc.connect(s, HOST, 12345) as client: assert client.remote_address() == (HOST, 12345) auth = authentication.AuthenticationClient(client) result = await auth.get_name(12345) assert result == "12345"
def test_extract(self): data = b"\0\0\0\0\x0a\0\0\0" stream = streams.StreamIn(data, settings.default()) rr = stream.extract(common.ResultRange) assert rr.offset == 0 assert rr.size == 10 data = b"\0\x08\0\0\0\0\0\0\0\x0a\0\0\0" stream = streams.StreamIn(data, settings.load("switch")) rr = stream.extract(common.ResultRange) assert rr.offset == 0 assert rr.size == 10
async def test_backend(): s = settings.default() auth_servers = [AuthenticationServer(s)] secure_servers = [AccountManagementServer()] async with rmc.serve(s, auth_servers, HOST, 12345): async with rmc.serve(s, secure_servers, HOST, 12346, key=b"testkey"): async with backend.connect(s, HOST, 12345) as client: async with client.login("username", "password") as secure_client: act = account.AccountClient(secure_client) assert await act.get_name(1000) == "test"
async def test_fragmentation(): s = settings.default() s["prudp.fragment_size"] = 10 async def handler(client): assert await client.recv() == b"a" * 40 await client.send(b"b" * 40) async with prudp.serve(handler, s, HOST, 12345): async with prudp.connect(s, HOST, 12345) as client: await client.send(b"a" * 40) assert await client.recv() == b"b" * 40
async def test_compression(): s = settings.default() s["prudp.compression"] = s.COMPRESSION_ZLIB async def handler(client): assert await client.recv() == b"ping" await client.send(b"pong") async with prudp.serve(handler, s, HOST, 12345): async with prudp.connect(s, HOST, 12345) as client: await client.send(b"ping") assert await client.recv() == b"pong"
async def test_unimplemented_protocol(anyio_backend): s = settings.default() servers = [] async with rmc.serve(s, servers, HOST, 12345): async with rmc.connect(s, HOST, 12345) as client: assert client.remote_address() == (HOST, 12345) auth = authentication.AuthenticationClient(client) try: result = await auth.get_name(12345) except common.RMCError as e: assert e.result().name() == "Core::NotImplemented"
def test_client_ticket(): ticket = kerberos.ClientTicket() ticket.session_key = bytes(range(32)) ticket.internal = b"internal buffer" ticket.target = 123456 s = settings.default() data = ticket.encrypt(b"key", s) ticket = kerberos.ClientTicket.decrypt(data, b"key", s) assert ticket.session_key == bytes(range(32)) assert ticket.internal == b"internal buffer" assert ticket.target == 123456
def test_server_ticket(): ticket = kerberos.ServerTicket() ticket.timestamp = common.DateTime.fromtimestamp(1596279690) ticket.session_key = bytes(range(32)) ticket.source = 123456 s = settings.default() data = ticket.encrypt(b"key", s) ticket = kerberos.ServerTicket.decrypt(data, b"key", s) assert ticket.timestamp.timestamp() == 1596279690 assert ticket.session_key == bytes(range(32)) assert ticket.source == 123456
def test_basic(): s = settings.default() assert s["kerberos.key_size"] == 32 copy = s.copy() s["kerberos.key_size"] = 100 assert s["kerberos.key_size"] == 100 assert copy["kerberos.key_size"] == 32 s.reset() assert s["kerberos.key_size"] == 32 s.load("friends") assert s["kerberos.key_size"] == 16
async def test_client_transport(): s = settings.default() async def handler(client): data = await client.recv() await client.send(data[::-1]) async with prudp.serve(handler, s, HOST, 12345): async with prudp.connect_transport(s, HOST, 12345) as transport: async with transport.connect(1) as client1: async with transport.connect(1) as client2: await client1.send(b"ping") await client2.send(b"test") assert await client1.recv() == b"gnip" assert await client2.recv() == b"tset"