tor-dam

tor distributed announce mechanism (not a dht)
git clone https://git.parazyd.org/tor-dam
Log | Files | Refs | README | LICENSE

validate_test.go (4384B)


      1 package damlib
      2 
      3 /*
      4  * Copyright (c) 2018 Dyne.org Foundation
      5  * tor-dam is written and maintained by Ivan Jelincic <parazyd@dyne.org>
      6  *
      7  * This file is part of tor-dam
      8  *
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU Affero General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU Affero General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU Affero General Public License
     20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     21  */
     22 
     23 import (
     24 	"encoding/base64"
     25 	"testing"
     26 )
     27 
     28 func makeReq() map[string]string {
     29 	return map[string]string{
     30 		"address":   "gphjf5g3d5ywehwrd7cv3czymtdc6ha67bqplxwbspx7tioxt7gxqiid.onion",
     31 		"pubkey":    "M86S9NsfcWIe0R/FXYs4ZMYvHB74YPXewZPv+aHXn80=",
     32 		"message":   "I am a DAM node!",
     33 		"signature": "CWqptO9ZRIvYMIHd3XHXaVny+W23P8FGkfbn5lvUqeJbDcY3G8+B4G8iCCIQiZkxkMofe6RbstHn3L1x88c3AA==",
     34 		"secret":    "",
     35 	}
     36 }
     37 
     38 func TestValidateOnionAddress(t *testing.T) {
     39 	if !(ValidateOnionAddress("gphjf5g3d5ywehwrd7cv3czymtdc6ha67bqplxwbspx7tioxt7gxqiid.onion")) {
     40 		t.Fatal("Validating a valid address failed.")
     41 	}
     42 	if ValidateOnionAddress("gphjf5g3d5ywe1wd.onion") {
     43 		t.Fatal("Validating an invalid address succeeded.")
     44 	}
     45 }
     46 
     47 func TestValidValidateFirstHandshake(t *testing.T) {
     48 	cmd, _ := StartRedis("../../contrib/redis.conf")
     49 	defer cmd.Process.Kill()
     50 
     51 	if valid, _ := ValidateFirstHandshake(makeReq()); !(valid) {
     52 		t.Fatal("Failed to validate first handshake.")
     53 	}
     54 }
     55 
     56 func TestInvalidValidateFirstHandshake(t *testing.T) {
     57 	cmd, _ := StartRedis("../../contrib/redis.conf")
     58 	defer cmd.Process.Kill()
     59 
     60 	// Invalid message for this signature.
     61 	req := makeReq()
     62 	req["message"] = "I am a bad DAM node!"
     63 
     64 	if valid, _ := ValidateFirstHandshake(req); valid {
     65 		t.Fatal("Invalid request passed as valid.")
     66 	}
     67 }
     68 
     69 func TestValidValidateSecondHandshake(t *testing.T) {
     70 	cmd, _ := StartRedis("../../contrib/redis.conf")
     71 	defer cmd.Process.Kill()
     72 
     73 	pk, sk, _ := GenEd25519()
     74 	onionaddr := OnionFromPubkeyEd25519(pk)
     75 
     76 	sig, err := SignMsgEd25519([]byte("I am a DAM node!"), sk)
     77 	if err != nil {
     78 		t.Fatal(err)
     79 	}
     80 	encodedSig := base64.StdEncoding.EncodeToString(sig)
     81 	encodedPub := base64.StdEncoding.EncodeToString([]byte(pk))
     82 
     83 	req := map[string]string{
     84 		"address":   string(onionaddr),
     85 		"pubkey":    encodedPub,
     86 		"message":   "I am a DAM node!",
     87 		"signature": encodedSig,
     88 		"secret":    "",
     89 	}
     90 
     91 	valid, secret := ValidateFirstHandshake(req)
     92 	if !(valid) {
     93 		t.Fatal("Failed on first handshake.")
     94 	}
     95 
     96 	sig, err = SignMsgEd25519([]byte(secret), sk)
     97 	if err != nil {
     98 		t.Fatal(err)
     99 	}
    100 	encodedSig = base64.StdEncoding.EncodeToString(sig)
    101 
    102 	req = map[string]string{
    103 		"address":   string(onionaddr),
    104 		"pubkey":    encodedPub,
    105 		"message":   secret,
    106 		"signature": encodedSig,
    107 		"secret":    secret,
    108 	}
    109 
    110 	if valid, _ = ValidateSecondHandshake(req); !(valid) {
    111 		t.Fatal("Failed to validate second handshake.")
    112 	}
    113 }
    114 
    115 func TestInValidValidateSecondHandshake(t *testing.T) {
    116 	cmd, _ := StartRedis("../../contrib/redis.conf")
    117 	defer cmd.Process.Kill()
    118 
    119 	pk, sk, _ := GenEd25519()
    120 	onionaddr := OnionFromPubkeyEd25519(pk)
    121 
    122 	sig, err := SignMsgEd25519([]byte("I am a DAM node!"), sk)
    123 	if err != nil {
    124 		t.Fatal(err)
    125 	}
    126 	encodedSig := base64.StdEncoding.EncodeToString(sig)
    127 	encodedPub := base64.StdEncoding.EncodeToString([]byte(pk))
    128 
    129 	req := map[string]string{
    130 		"address":   string(onionaddr),
    131 		"pubkey":    encodedPub,
    132 		"message":   "I am a DAM node!",
    133 		"signature": encodedSig,
    134 		"secret":    "",
    135 	}
    136 
    137 	valid, secret := ValidateFirstHandshake(req)
    138 	if !(valid) {
    139 		t.Fatal("Failed on first handshake.")
    140 	}
    141 
    142 	sig, err = SignMsgEd25519([]byte(secret), sk)
    143 	if err != nil {
    144 		t.Fatal(err)
    145 	}
    146 	encodedSig = base64.StdEncoding.EncodeToString(sig)
    147 
    148 	secret = "We're malicious!"
    149 
    150 	req = map[string]string{
    151 		"address":   string(onionaddr),
    152 		"pubkey":    encodedPub,
    153 		"message":   secret,
    154 		"signature": encodedSig,
    155 		"secret":    secret,
    156 	}
    157 
    158 	if valid, _ = ValidateSecondHandshake(req); valid {
    159 		t.Fatal("Invalid second handshake passed as valid.")
    160 	}
    161 }