tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit 77feb15fd76bea2b9b1f1814e85b5279954c992e
parent 827494327cebf6a3bd3c266f8735a91e82d08c1b
Author: parazyd <parazyd@dyne.org>
Date:   Mon, 12 Mar 2018 12:28:16 +0100

Add some test cases for damlib.

Diffstat:
Apkg/damlib/config_test.go | 31+++++++++++++++++++++++++++++++
Apkg/damlib/crypto_25519_test.go | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apkg/damlib/crypto_common_test.go | 38++++++++++++++++++++++++++++++++++++++
Apkg/damlib/net_test.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Apkg/damlib/tor_test.go | 36++++++++++++++++++++++++++++++++++++
5 files changed, 238 insertions(+), 0 deletions(-)

diff --git a/pkg/damlib/config_test.go b/pkg/damlib/config_test.go @@ -0,0 +1,31 @@ +package damlib + +/* + * Copyright (c) 2017-2018 Dyne.org Foundation + * tor-dam is written and maintained by Ivan J. <parazyd@dyne.org> + * + * This file is part of tor-dam + * + * This source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this source code. If not, see <http://www.gnu.org/licenses/>. + */ + +import ( + "os" + "testing" +) + +func TestMain(m *testing.M) { + ex := m.Run() + os.Exit(ex) +} diff --git a/pkg/damlib/crypto_25519_test.go b/pkg/damlib/crypto_25519_test.go @@ -0,0 +1,82 @@ +package damlib + +/* + * Copyright (c) 2017-2018 Dyne.org Foundation + * tor-dam is written and maintained by Ivan J. <parazyd@dyne.org> + * + * This file is part of tor-dam + * + * This source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this source code. If not, see <http://www.gnu.org/licenses/>. + */ + +import ( + "os" + "testing" +) + +func TestGenEd25519(t *testing.T) { + _, _, err := GenEd25519() + if err != nil { + t.Fatal("Failed generating ed25519 key:", err.Error()) + } + + t.Log("Successfully generated ed25519 keypair.") +} + +func TestSavePubEd25519(t *testing.T) { + pk, _, err := GenEd25519() + if err != nil { + t.Fatal("Failed generating ed25519 key:", err.Error()) + } + + err = SavePubEd25519("/tmp/ed25519pub.test", pk) + if err != nil { + t.Fatal("Failed saving pubkey:", err.Error()) + } + + os.Remove("/tmp/ed25519pub.test") + t.Log("Success saving ed25519 pubkey") +} + +func TestSavePrivEd25519(t *testing.T) { + _, sk, err := GenEd25519() + if err != nil { + t.Fatal("Failed generating ed25519 key:", err.Error()) + } + + err = SavePrivEd25519("/tmp/ed25519priv.test", sk) + if err != nil { + t.Fatal("Failed saving privkey:", err.Error()) + } + + os.Remove("/tmp/ed25519priv.test") + t.Log("Success saving ed25519 privkey") +} + +func TestOnionFromPubkeyEd25519(t *testing.T) { + pk, _, err := GenEd25519() + if err != nil { + t.Fatal("Failed generating ed25519 key:", err.Error()) + } + + res := OnionFromPubkeyEd25519(pk) + valid := ValidateOnionAddress(string(res)) + + t.Log("Got:", string(res)) + + if !valid { + t.Fatal("Address is invalid.") + } + t.Log("Address is valid") +} diff --git a/pkg/damlib/crypto_common_test.go b/pkg/damlib/crypto_common_test.go @@ -0,0 +1,38 @@ +package damlib + +/* + * Copyright (c) 2017-2018 Dyne.org Foundation + * tor-dam is written and maintained by Ivan J. <parazyd@dyne.org> + * + * This file is part of tor-dam + * + * This source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this source code. If not, see <http://www.gnu.org/licenses/>. + */ + +import ( + "testing" +) + +func TestGenRandomASCII(t *testing.T) { + res, err := GenRandomASCII(18) + if err != nil { + t.Fatal("Failed making random string:", err.Error()) + } + + if len(res) != 18 { + t.Fatal("String is of incorrect length: 18 !=", len(res)) + } + + t.Log("Got:", res) +} diff --git a/pkg/damlib/net_test.go b/pkg/damlib/net_test.go @@ -0,0 +1,51 @@ +package damlib + +/* + * Copyright (c) 2017-2018 Dyne.org Foundation + * tor-dam is written and maintained by Ivan J. <parazyd@dyne.org> + * + * This file is part of tor-dam + * + * This source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this source code. If not, see <http://www.gnu.org/licenses/>. + */ + +import ( + "io/ioutil" + "testing" +) + +func TestHTTPPost(t *testing.T) { + data := []byte("foobar") + + resp, err := HTTPPost("https://requestb.in/ykdug2yk", data) + if err != nil { + t.Fatal("Unable to HTTPPost:", err.Error()) + } + + res, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatal("Unable to read response:", err.Error()) + } + + t.Log("Got:", string(res)) +} + +func TestHTTPDownload(t *testing.T) { + data, err := HTTPDownload("https://requestb.in/ykdug2yk") + if err != nil { + t.Fatal("Unable to HTTPDownload:", err.Error()) + } + + t.Log("Got:", string(data)) +} diff --git a/pkg/damlib/tor_test.go b/pkg/damlib/tor_test.go @@ -0,0 +1,36 @@ +package damlib + +/* + * Copyright (c) 2017-2018 Dyne.org Foundation + * tor-dam is written and maintained by Ivan J. <parazyd@dyne.org> + * + * This file is part of tor-dam + * + * This source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this source code. If not, see <http://www.gnu.org/licenses/>. + */ + +import ( + "strings" + "testing" +) + +func TestFetchHSPubkey(t *testing.T) { + pubkey := FetchHSPubkey("szpvqtyw3vbgzb3s.onion") + + if !strings.HasPrefix(pubkey, "-----BEGIN") { + t.Fatal("Did not get a public key.") + } + + t.Log("Got:", pubkey) +}