github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/reloader_test.go (about)

     1  /*
     2   * Copyright (c) 2016, Psiphon Inc.
     3   * All rights reserved.
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package common
    21  
    22  import (
    23  	"bytes"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"testing"
    28  	"time"
    29  )
    30  
    31  func TestReloader(t *testing.T) {
    32  
    33  	dirname, err := ioutil.TempDir("", "psiphon-reloader-test")
    34  	if err != nil {
    35  		t.Fatalf("TempDir failed: %s", err)
    36  	}
    37  	defer os.RemoveAll(dirname)
    38  
    39  	filename := filepath.Join(dirname, "reloader_test.dat")
    40  
    41  	initialContents := []byte("contents1\n")
    42  	modifiedContents := []byte("contents2\n")
    43  
    44  	var file struct {
    45  		ReloadableFile
    46  		contents []byte
    47  	}
    48  
    49  	file.ReloadableFile = NewReloadableFile(
    50  		filename,
    51  		true,
    52  		func(fileContent []byte, _ time.Time) error {
    53  			file.contents = fileContent
    54  			return nil
    55  		})
    56  
    57  	// Test: initial load
    58  
    59  	err = ioutil.WriteFile(filename, initialContents, 0600)
    60  	if err != nil {
    61  		t.Fatalf("WriteFile failed: %s", err)
    62  	}
    63  
    64  	reloaded, err := file.Reload()
    65  	if err != nil {
    66  		t.Fatalf("Reload failed: %s", err)
    67  	}
    68  
    69  	if !reloaded {
    70  		t.Fatalf("Unexpected non-reload")
    71  	}
    72  
    73  	if !bytes.Equal(file.contents, initialContents) {
    74  		t.Fatalf("Unexpected contents")
    75  	}
    76  
    77  	// Test: reload unchanged file
    78  
    79  	reloaded, err = file.Reload()
    80  	if err != nil {
    81  		t.Fatalf("Reload failed: %s", err)
    82  	}
    83  
    84  	if reloaded {
    85  		t.Fatalf("Unexpected reload")
    86  	}
    87  
    88  	if !bytes.Equal(file.contents, initialContents) {
    89  		t.Fatalf("Unexpected contents")
    90  	}
    91  
    92  	// Test: reload changed file
    93  
    94  	err = ioutil.WriteFile(filename, modifiedContents, 0600)
    95  	if err != nil {
    96  		t.Fatalf("WriteFile failed: %s", err)
    97  	}
    98  
    99  	reloaded, err = file.Reload()
   100  	if err != nil {
   101  		t.Fatalf("Reload failed: %s", err)
   102  	}
   103  
   104  	if !reloaded {
   105  		t.Fatalf("Unexpected non-reload")
   106  	}
   107  
   108  	if !bytes.Equal(file.contents, modifiedContents) {
   109  		t.Fatalf("Unexpected contents")
   110  	}
   111  }