golang.zx2c4.com/wireguard/windows@v0.5.4-0.20230123132234-dcc0eb72a04b/conf/store_test.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package conf
     7  
     8  import (
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestStorage(t *testing.T) {
    14  	c, err := FromWgQuick(testInput, "golangTest")
    15  	if err != nil {
    16  		t.Errorf("Unable to parse test config: %s", err.Error())
    17  		return
    18  	}
    19  
    20  	err = c.Save(true)
    21  	if err != nil {
    22  		t.Errorf("Unable to save config: %s", err.Error())
    23  	}
    24  
    25  	configs, err := ListConfigNames()
    26  	if err != nil {
    27  		t.Errorf("Unable to list configs: %s", err.Error())
    28  	}
    29  
    30  	found := false
    31  	for _, name := range configs {
    32  		if name == "golangTest" {
    33  			found = true
    34  			break
    35  		}
    36  	}
    37  	if !found {
    38  		t.Error("Unable to find saved config in list")
    39  	}
    40  
    41  	loaded, err := LoadFromName("golangTest")
    42  	if err != nil {
    43  		t.Errorf("Unable to load config: %s", err.Error())
    44  		return
    45  	}
    46  
    47  	if !reflect.DeepEqual(loaded, c) {
    48  		t.Error("Loaded config is not the same as saved config")
    49  	}
    50  
    51  	k, err := NewPrivateKey()
    52  	if err != nil {
    53  		t.Errorf("Unable to generate new private key: %s", err.Error())
    54  	}
    55  	c.Interface.PrivateKey = *k
    56  
    57  	err = c.Save(false)
    58  	if err == nil {
    59  		t.Error("Config disappeared or was unexpectedly overwritten")
    60  	}
    61  	err = c.Save(true)
    62  	if err != nil {
    63  		t.Errorf("Unable to save config a second time: %s", err.Error())
    64  	}
    65  
    66  	loaded, err = LoadFromName("golangTest")
    67  	if err != nil {
    68  		t.Errorf("Unable to load config a second time: %s", err.Error())
    69  		return
    70  	}
    71  
    72  	if !reflect.DeepEqual(loaded, c) {
    73  		t.Error("Second loaded config is not the same as second saved config")
    74  	}
    75  
    76  	err = DeleteName("golangTest")
    77  	if err != nil {
    78  		t.Errorf("Unable to delete config: %s", err.Error())
    79  	}
    80  
    81  	configs, err = ListConfigNames()
    82  	if err != nil {
    83  		t.Errorf("Unable to list configs: %s", err.Error())
    84  	}
    85  	found = false
    86  	for _, name := range configs {
    87  		if name == "golangTest" {
    88  			found = true
    89  			break
    90  		}
    91  	}
    92  	if found {
    93  		t.Error("Config wasn't actually deleted")
    94  	}
    95  }