func TestEncryptDecryptAll(t *testing.T) { log.SetMinLevel(log.LevelError) msg := []byte("This is a small test message for verification, it just has to be not too short to be not boring") sender := new(Sender) // Default sender msgEnc, meta, err := sender.Encrypt(1, msg) if err != nil { t.Fatalf("Encryption failed: %s", err) } receiver := &Receiver{ // Temporary message key from meta ReceiveConstantPrivateKey: meta.MessageKey, } message, metaRec, err := receiver.Decrypt(msgEnc) if err != nil { t.Fatalf("Decryption failed: %s", err) } if metaRec.MessageID != meta.MessageID { t.Error("MessageIDs do not match") } if metaRec.MessageType != 1 { t.Error("Message type does not match") } if !bytes.Equal(msg, message) { t.Error("Message corrupted") } }
func TestGenPostHandler(t *testing.T) { pubKey, _ := hex.DecodeString("39d8913ab046428e409cf1fa7cee6f63c1f6bf701356a44a8c8c2559bdb2526f") privKey, _ := hex.DecodeString("20a2633e422090a4f4a102f8e3d112f2b4378dbd9957e8c892067fc09239d36c39d8913ab046428e409cf1fa7cee6f63c1f6bf701356a44a8c8c2559bdb2526f") log.SetMinLevel(log.LevelDebug) testDir := path.Join(os.TempDir(), "repbin") if err := os.MkdirAll(testDir, 0700); err != nil { t.Fatal(err) } ms, err := New(testDir+string(os.PathSeparator), pubKey, privKey) if err != nil { t.Fatalf("New: %s", err) } enforceTimeOuts = false debug = true ms.NotifyDuration = 0 ms.FetchDuration = 0 ms.LoadPeers() ms.NotifyPeers() ms.FetchPeers() http.HandleFunc("/id", ms.ServeID) http.HandleFunc("/keyindex", ms.GetKeyIndex) http.HandleFunc("/globalindex", ms.GetGlobalIndex) http.HandleFunc("/post", ms.GenPostHandler(false)) http.HandleFunc("/local/post", ms.GenPostHandler(true)) http.HandleFunc("/fetch", ms.Fetch) http.HandleFunc("/notify", ms.GetNotify) http.HandleFunc("/delete", ms.Delete) go http.ListenAndServe(":8080", nil) time.Sleep(time.Second / 100) if !testing.Short() { // only necessary for long test in getpost_test.go time.Sleep(time.Second * 10) } }
func TestEncryptDecryptReceiver(t *testing.T) { log.SetMinLevel(log.LevelError) msg := []byte("This is a small test message for verification, it just has to be not too short to be not boring") senderPrivKeyConstant, _ := GenLongTermKey(false, false) senderPrivKeyTemporary, _ := GenLongTermKey(false, false) sender := &Sender{ ReceiveConstantPublicKey: CalcPub(senderPrivKeyConstant), ReceiveTemporaryPublicKey: CalcPub(senderPrivKeyTemporary), } msgEnc, meta, err := sender.Encrypt(0, msg) if err != nil { t.Fatalf("Encryption failed: %s", err) } receiver := &Receiver{ // Temporary message key from meta ReceiveConstantPrivateKey: senderPrivKeyConstant, ReceiveTemporaryPrivateKey: senderPrivKeyTemporary, } message, metaRec, err := receiver.Decrypt(msgEnc) if err != nil { t.Fatalf("Decryption failed: %s", err) } if metaRec.MessageID != meta.MessageID { t.Error("MessageIDs do not match") } if metaRec.MessageType != 0 { t.Error("Message type does not match") } if !bytes.Equal(msg, message) { t.Error("Message corrupted") } }
func TestEncryptDecryptRewrap(t *testing.T) { log.SetMinLevel(log.LevelError) msg := []byte("This is a small test message for verification, it just has to be not too short to be not boring") sender := new(Sender) // Default sender msgPrePad, meta, err := sender.EncryptRepost(0, msg) if err != nil { t.Fatalf("Encryption failed: %s", err) } msgPreEnc := RePad(msgPrePad, meta.PadKey, DefaultTotalLength) msgEnc := EncodeBase64(msgPreEnc) receiver := &Receiver{ // Temporary message key from meta ReceiveConstantPrivateKey: meta.MessageKey, } _ = msgEnc message, metaRec, err := receiver.Decrypt(msgEnc) if err != nil { t.Fatalf("Decryption failed rewrap: %s", err) } if metaRec.MessageID != meta.MessageID { t.Error("MessageIDs do not match") } if metaRec.MessageType != 0 { t.Error("Message type does not match") } if !bytes.Equal(msg, message) { t.Error("Message corrupted") } }
func runCommands() (retval int) { log.SetMinLevel(log.LevelPrint) if options.Appdata { log.SetMinLevel(log.LevelData) } if options.Verbose { log.SetMinLevel(log.LevelDebug) } if options.KEYVERB { log.SetMinLevel(log.LevelSecret) } usercfgfile := client.UserConfigFile() if options.Configfile != "" || usercfgfile != "" { configFile := usercfgfile if options.Configfile != "" { configFile = options.Configfile } globalconfigT, err := client.LoadConfigFile(configFile) if err == nil { globalconfig = globalconfigT } else { log.Errorf("Config file failed to load: %s\n", err) } } for k, v := range commands { if *v { if callFunc[k] != nil { client.OptionsVar = options client.GlobalConfigVar = globalconfig retval = callFunc[k]() } else { log.Fatal("Command not implemented") return 100 } log.Sync() break } } return }
func init() { version := flag.Bool("version", false, "Print version information") showconfig := flag.Bool("showconfig", false, "Print config file template") configfile := flag.String("configfile", "", "Path to configuration file") verbose := flag.Bool("verbose", false, "Show some verbose output") start = flag.Bool("start", false, "Start server") stat = flag.Bool("stat", false, "Enable usage statistics") flag.Parse() if *version { fmt.Printf("Repserver: %s\n", Version) fmt.Printf("MessageStore: %s\n", messagestore.Version) fmt.Printf("Handlers: %s\n", handlers.Version) fmt.Printf("FileBack: %s\n", fileback.Version) fmt.Printf("HashCash: %s\n", hashcash.Version) fmt.Printf("Utils: %s\n", utils.Version) fmt.Printf("KeyAuth: %s\n", keyauth.Version) fmt.Printf("KeyProof: %s\n", keyproof.Version) fmt.Printf("Protocol: %s\n", repproto.Version) fmt.Printf("Protocol Structures: %s\n", structs.Version) fmt.Printf("Message: %s\n", message.VersionID) fmt.Printf("Message Format: %d\n", message.Version) os.Exit(0) } if *showconfig { showConfig() os.Exit(0) } if *configfile == "" || *configfile == "/" || len(*configfile) < 4 { fmt.Println("No configuration file found. Specify with --configfile=FILE") os.Exit(0) } if *verbose { log.SetMinLevel(log.LevelDebug) } if *stat && !*verbose { fmt.Println("Error: option --stat requires option --verbose") os.Exit(1) } err := loadConfig(*configfile) if err != nil { fmt.Printf("Error: %s\n", err) os.Exit(1) } }
func TestEncryptDecryptCallback(t *testing.T) { log.SetMinLevel(log.LevelError) keys := make(map[Curve25519Key]Curve25519Key) msg := []byte("This is a small test message for verification, it just has to be not too short to be not boring") senderPrivKeyConstant, _ := GenLongTermKey(false, false) senderPubKeyConstant := CalcPub(senderPrivKeyConstant) keys[*senderPubKeyConstant] = *senderPrivKeyConstant senderPrivKeyTemporary, _ := GenLongTermKey(false, false) senderPubKeyTemporary := CalcPub(senderPrivKeyTemporary) keys[*senderPubKeyTemporary] = *senderPrivKeyTemporary // Callback function for key lookup callback := func(key *Curve25519Key) *Curve25519Key { pub := keys[*key] return &pub } sender := &Sender{ ReceiveConstantPublicKey: senderPubKeyConstant, ReceiveTemporaryPublicKey: senderPubKeyTemporary, } msgEnc, meta, err := sender.Encrypt(0, msg) if err != nil { t.Fatalf("Encryption failed: %s", err) } receiver := &Receiver{ // Temporary message key from meta KeyCallBack: callback, } message, metaRec, err := receiver.Decrypt(msgEnc) if err != nil { t.Fatalf("Decryption failed: %s", err) } if metaRec.MessageID != meta.MessageID { t.Error("MessageIDs do not match") } if metaRec.MessageType != 0 { t.Error("Message type does not match") } if !bytes.Equal(msg, message) { t.Error("Message corrupted") } }