aboutsummaryrefslogtreecommitdiff
path: root/osp/go/cmd/test.go
blob: ac068f424f56c9bdec0571d9d2a5b66285c238d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// TODO(jophba): Use proper testing framework

package main

import (
	"bytes"
	"context"
	"io"
	"fmt"
	"log"

	"osp"
)

func testMdns() {
	// TODO(jophba): log error if it fails
	ctx := context.Background()
	instance := "TV"
	port := 10000
	go osp.RunMdnsServer(ctx, instance, port)
	entries, err := osp.BrowseMdns(ctx)
	if (err != nil) {
		log.Fatalf("Failed to browse mDNS: %v\n", err)
	}
	for entry := range entries {
		log.Println(fmt.Sprintf("%s at %s(%s or %s):%d", entry.Instance, entry.HostName, entry.AddrIPv4, entry.AddrIPv6, entry.Port))
	}
	log.Println("No more entries.")
}

func dialAndwriteQuicStream(ctx context.Context, hostname string, port int, msg []byte) {
	clientSession, err := osp.DialAsQuicClient(ctx, hostname, port)
	if err != nil {
		log.Fatalln("Failed to dial QUIC:", err.Error())
	}
	clientStream, err := clientSession.OpenStreamSync()
	if err != nil {
		log.Fatalln("Failed to open QUIC stream:", err.Error())
	}
	_, err = clientStream.Write(msg)
	if err != nil {
		log.Fatalln("Failed to write to a QUIC stream:", err.Error())
	}
}

func testQuic() {
	const port = 4242
	msg := []byte("Hello, World")
	tlsCert, err := osp.GenerateTlsCert()
	if err != nil {
		log.Fatalln("Failed to generate TLS cert:", err.Error())
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go dialAndwriteQuicStream(ctx, "localhost", port, msg)

	serverStreams := make(chan io.ReadWriteCloser)
	go osp.RunQuicServer(ctx, port, *tlsCert, serverStreams)
	serverStream := <-serverStreams
	b := make([]byte, len(msg))
	_, err = io.ReadFull(serverStream, b)
	if err != nil {
		log.Fatalln("Failed to read stream:", err.Error())
	}
	log.Println(string(b))
}

func testMessages() {
	msg := osp.PresentationStartRequest{
		RequestID:      1,
		PresentationID: "This is a Presentation ID.  It must be very long.",
		URL:            "https://github.com/webscreens/openscreenprotocol",
	}

	var rw bytes.Buffer
	osp.WriteMessage(msg, &rw)
	msg2, err := osp.ReadMessage(&rw)
	if err != nil {
		log.Fatalln("Failed to read message.");
	}
	log.Println(msg2)
}

func testVarint() {
	n1 := uint64(12345)
	rw := bytes.NewBuffer([]byte{})
	log.Printf("Write: %d\n", n1)
	err := osp.WriteVaruint(n1, rw)
	if err != nil {
		log.Fatalln("Failed to write varint", err.Error())
	}
	n2, err := osp.ReadVaruint(rw)
	if err != nil {
		log.Fatalln("Failed to read varint", err.Error())
	}
	log.Printf("Read: %d\n", n2)
}

func main() {
	testVarint()
	testMessages()
	testQuic()
	testMdns()
}