github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/test/integration/non-utf8_test.go (about)

     1  /*
     2  Copyright 2013 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package integration
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  
    28  	"camlistore.org/pkg/test"
    29  )
    30  
    31  var nonUTF8 = "416c697ae965202d204d6f69204c6f6c6974612e6d7033" // hex-encoding
    32  
    33  func tempDir(t *testing.T) (path string, cleanup func()) {
    34  	path, err := ioutil.TempDir("", "camtest-")
    35  	if err != nil {
    36  		t.Fatalf("ioutil.TempDir(): %v", err)
    37  	}
    38  
    39  	cleanup = func() {
    40  		os.RemoveAll(path)
    41  	}
    42  
    43  	return
    44  }
    45  
    46  // Test that we can camput and camget a file whose name is not utf8,
    47  // that we don't panic in the process and that the results are
    48  // correct.
    49  func TestNonUTF8FileName(t *testing.T) {
    50  	srcDir, cleanup := tempDir(t)
    51  	defer cleanup()
    52  
    53  	base, err := hex.DecodeString(nonUTF8)
    54  	if err != nil {
    55  		t.Fatalf("hex.DecodeString(): %v", err)
    56  	}
    57  
    58  	fd, err := os.Create(filepath.Join(srcDir, string(base)))
    59  	if err != nil {
    60  		t.Fatalf("os.Create(): %v", err)
    61  	}
    62  	fd.Close()
    63  
    64  	w := test.GetWorld(t)
    65  	out := test.MustRunCmd(t, w.Cmd("camput", "file", fd.Name()))
    66  	br := strings.Split(out, "\n")[0]
    67  
    68  	// camput was a success. Can we get the file back in another directory?
    69  	dstDir, cleanup := tempDir(t)
    70  	defer cleanup()
    71  
    72  	_ = test.MustRunCmd(t, w.Cmd("camget", "-o", dstDir, br))
    73  	_, err = os.Lstat(filepath.Join(dstDir, string(base)))
    74  	if err != nil {
    75  		t.Fatalf("Failed to stat file %s in directory %s",
    76  			fd.Name(), dstDir)
    77  	}
    78  }
    79  
    80  // Test that we can camput and camget a symbolic link whose target is
    81  // not utf8, that we do no panic in the process and that the results
    82  // are correct.
    83  func TestNonUTF8SymlinkTarget(t *testing.T) {
    84  	srcDir, cleanup := tempDir(t)
    85  	defer cleanup()
    86  
    87  	base, err := hex.DecodeString(nonUTF8)
    88  	if err != nil {
    89  		t.Fatalf("hex.DecodeString(): %v", err)
    90  	}
    91  
    92  	fd, err := os.Create(filepath.Join(srcDir, string(base)))
    93  	if err != nil {
    94  		t.Fatalf("os.Create(): %v", err)
    95  	}
    96  	defer fd.Close()
    97  
    98  	err = os.Symlink(string(base), filepath.Join(srcDir, "link"))
    99  	if err != nil {
   100  		t.Fatalf("os.Symlink(): %v", err)
   101  	}
   102  
   103  	w := test.GetWorld(t)
   104  	out := test.MustRunCmd(t, w.Cmd("camput", "file", filepath.Join(srcDir, "link")))
   105  	br := strings.Split(out, "\n")[0]
   106  
   107  	// See if we can camget it back correctly
   108  	dstDir, cleanup := tempDir(t)
   109  	defer cleanup()
   110  
   111  	_ = test.MustRunCmd(t, w.Cmd("camget", "-o", dstDir, br))
   112  	target, err := os.Readlink(filepath.Join(dstDir, "link"))
   113  	if err != nil {
   114  		t.Fatalf("os.Readlink(): %v", err)
   115  	}
   116  
   117  	if !bytes.Equal([]byte(target), base) {
   118  		t.Fatalf("Retrieved symlink contains points to unexpected target")
   119  	}
   120  
   121  }