How to use Names method of test_helpers Package

Best Ginkgo code snippet using test_helpers.Names

cli_test.go

Source:cli_test.go Github

copy

Full Screen

1package cli2// Test CLI operations like "-init", "-password" etc3import (4 "fmt"5 "io/ioutil"6 "os"7 "os/exec"8 "strconv"9 "strings"10 "sync"11 "syscall"12 "testing"13 "time"14 "github.com/HorizonLiu/gocryptfs/internal/configfile"15 "github.com/HorizonLiu/gocryptfs/internal/exitcodes"16 "github.com/HorizonLiu/gocryptfs/tests/test_helpers"17)18var testPw = []byte("test")19func TestMain(m *testing.M) {20 test_helpers.ResetTmpDir(false)21 before := test_helpers.ListFds(0, "")22 r := m.Run()23 after := test_helpers.ListFds(0, "")24 if len(before) != len(after) {25 fmt.Printf("fd leak in test process? before, after:\n%v\n%v\n", before, after)26 os.Exit(1)27 }28 os.Exit(r)29}30// Test -init flag31func TestInit(t *testing.T) {32 dir := test_helpers.InitFS(t)33 _, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfDefaultName, testPw)34 if err != nil {35 t.Fatal(err)36 }37 if c.IsFeatureFlagSet(configfile.FlagAESSIV) {38 t.Error("AESSIV flag should not be set")39 }40}41// Test that gocryptfs.conf and gocryptfs.diriv are there with the expected42// permissions after -init43func TestInitFilePerms(t *testing.T) {44 dir := test_helpers.InitFS(t)45 var st syscall.Stat_t46 syscall.Stat(dir+"/gocryptfs.conf", &st)47 perms := st.Mode & 077748 if perms != 0400 {49 t.Errorf("Wrong permissions for gocryptfs.conf: %#o", perms)50 }51 st = syscall.Stat_t{}52 syscall.Stat(dir+"/gocryptfs.diriv", &st)53 perms = st.Mode & 077754 // From v1.7.1, these are created with 0440 permissions, see55 // https://github.com/HorizonLiu/gocryptfs/issues/387 .56 // From v2.0, created with 0444 perms, see57 // https://github.com/HorizonLiu/gocryptfs/issues/539 .58 if perms != 0444 {59 t.Errorf("Wrong permissions for gocryptfs.diriv: %#o", perms)60 }61}62// Test -init with -devrandom flag63func TestInitDevRandom(t *testing.T) {64 test_helpers.InitFS(t, "-devrandom")65}66// Test -init with -aessiv67func TestInitAessiv(t *testing.T) {68 dir := test_helpers.InitFS(t, "-aessiv")69 _, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfDefaultName, testPw)70 if err != nil {71 t.Fatal(err)72 }73 if !c.IsFeatureFlagSet(configfile.FlagAESSIV) {74 t.Error("AESSIV flag should be set but is not")75 }76}77// Test -init with -reverse78func TestInitReverse(t *testing.T) {79 dir := test_helpers.InitFS(t, "-reverse")80 _, c, err := configfile.LoadAndDecrypt(dir+"/"+configfile.ConfReverseName, testPw)81 if err != nil {82 t.Fatal(err)83 }84 if !c.IsFeatureFlagSet(configfile.FlagAESSIV) {85 t.Error("AESSIV flag should be set but is not")86 }87}88// testPasswd changes the password from "test" to "test" using89// the -extpass method, then from "test" to "newpasswd" using the90// stdin method.91func testPasswd(t *testing.T, dir string, extraArgs ...string) {92 // Change password using "-extpass"93 args := []string{"-q", "-passwd", "-extpass", "echo test"}94 args = append(args, extraArgs...)95 args = append(args, dir)96 cmd := exec.Command(test_helpers.GocryptfsBinary, args...)97 cmd.Stdout = os.Stdout98 cmd.Stderr = os.Stderr99 err := cmd.Run()100 if err != nil {101 t.Error(err)102 }103 // Change password using stdin104 args = []string{"-q", "-passwd"}105 args = append(args, extraArgs...)106 args = append(args, dir)107 cmd = exec.Command(test_helpers.GocryptfsBinary, args...)108 cmd.Stdout = os.Stdout109 cmd.Stderr = os.Stderr110 p, err := cmd.StdinPipe()111 if err != nil {112 t.Fatal(err)113 }114 err = cmd.Start()115 if err != nil {116 t.Error(err)117 }118 // Old password119 p.Write([]byte("test\n"))120 // New password121 p.Write([]byte("newpasswd\n"))122 p.Close()123 err = cmd.Wait()124 if err != nil {125 t.Error(err)126 }127}128// Test -passwd flag129func TestPasswd(t *testing.T) {130 // Create FS131 dir := test_helpers.InitFS(t)132 mnt := dir + ".mnt"133 // Add content134 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")135 file1 := mnt + "/file1"136 err := ioutil.WriteFile(file1, []byte("somecontent"), 0600)137 if err != nil {138 t.Fatal(err)139 }140 err = test_helpers.UnmountErr(mnt)141 if err != nil {142 t.Fatal(err)143 }144 // Change password to "newpasswd"145 testPasswd(t, dir)146 // Mount and verify147 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo newpasswd")148 content, err := ioutil.ReadFile(file1)149 if err != nil {150 t.Error(err)151 } else if string(content) != "somecontent" {152 t.Errorf("wrong content: %q", string(content))153 }154 err = test_helpers.UnmountErr(mnt)155 if err != nil {156 t.Fatal(err)157 }158}159// cp copies file at `src` to `dst`, overwriting160// `dst` if it already exists. Calls t.Fatal on failure.161func cp(t *testing.T, src string, dst string) {162 conf, err := ioutil.ReadFile(src)163 if err != nil {164 t.Fatal(err)165 }166 syscall.Unlink(dst)167 err = ioutil.WriteFile(dst, conf, 0600)168 if err != nil {169 t.Fatal(err)170 }171}172// Test -passwd with -masterkey173func TestPasswdMasterkey(t *testing.T) {174 // Create FS175 dir := test_helpers.InitFS(t)176 // Overwrite with config with known master key177 cp(t, "gocryptfs.conf.b9e5ba23", dir+"/gocryptfs.conf")178 // Add content179 mnt := dir + ".mnt"180 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")181 file1 := mnt + "/file1"182 err := ioutil.WriteFile(file1, []byte("somecontent"), 0600)183 if err != nil {184 t.Fatal(err)185 }186 test_helpers.UnmountPanic(mnt)187 // Change password using stdin188 args := []string{"-q", "-passwd", "-masterkey",189 "b9e5ba23-981a22b8-c8d790d8-627add29-f680513f-b7b7035f-d203fb83-21d82205"}190 args = append(args, dir)191 cmd := exec.Command(test_helpers.GocryptfsBinary, args...)192 cmd.Stdout = os.Stdout193 cmd.Stderr = os.Stderr194 p, err := cmd.StdinPipe()195 if err != nil {196 t.Fatal(err)197 }198 err = cmd.Start()199 if err != nil {200 t.Error(err)201 }202 // New password203 p.Write([]byte("newpasswd\n"))204 p.Close()205 err = cmd.Wait()206 if err != nil {207 t.Error(err)208 }209 // Mount and verify210 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo newpasswd")211 content, err := ioutil.ReadFile(file1)212 if err != nil {213 t.Error(err)214 } else if string(content) != "somecontent" {215 t.Errorf("wrong content: %q", string(content))216 }217 test_helpers.UnmountPanic(mnt)218}219// Test -passwd with -masterkey=stdin220func TestPasswdMasterkeyStdin(t *testing.T) {221 // Create FS222 dir := test_helpers.InitFS(t)223 // Overwrite with config with known master key224 cp(t, "gocryptfs.conf.b9e5ba23", dir+"/gocryptfs.conf")225 // Add content226 mnt := dir + ".mnt"227 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo test")228 file1 := mnt + "/file1"229 err := ioutil.WriteFile(file1, []byte("somecontent"), 0600)230 if err != nil {231 t.Fatal(err)232 }233 test_helpers.UnmountPanic(mnt)234 // Change password using stdin235 args := []string{"-q", "-passwd", "-masterkey=stdin"}236 args = append(args, dir)237 cmd := exec.Command(test_helpers.GocryptfsBinary, args...)238 cmd.Stdout = os.Stdout239 cmd.Stderr = os.Stderr240 p, err := cmd.StdinPipe()241 if err != nil {242 t.Fatal(err)243 }244 err = cmd.Start()245 if err != nil {246 t.Error(err)247 }248 // Masterkey249 p.Write([]byte("b9e5ba23-981a22b8-c8d790d8-627add29-f680513f-b7b7035f-d203fb83-21d82205\n"))250 // New password251 p.Write([]byte("newpasswd\n"))252 p.Close()253 err = cmd.Wait()254 if err != nil {255 t.Fatal(err)256 }257 // Mount and verify258 test_helpers.MountOrFatal(t, dir, mnt, "-extpass", "echo newpasswd")259 content, err := ioutil.ReadFile(file1)260 if err != nil {261 t.Fatal(err)262 } else if string(content) != "somecontent" {263 t.Errorf("wrong content: %q", string(content))264 }265 test_helpers.UnmountPanic(mnt)266}267// Test -passwd with -reverse268func TestPasswdReverse(t *testing.T) {269 // Create FS270 dir := test_helpers.InitFS(t, "-reverse")271 testPasswd(t, dir, "-reverse")272}273// Test -passwd with -scryptn274func TestPasswdScryptn(t *testing.T) {275 dir := test_helpers.InitFS(t)276 cf, err := configfile.Load(dir + "/gocryptfs.conf")277 if err != nil {278 t.Fatal(err)279 }280 testPasswd(t, dir, "-scryptn", strconv.Itoa(cf.ScryptObject.LogN()+1))281 cf2, err := configfile.Load(dir + "/gocryptfs.conf")282 if err != nil {283 t.Fatal(err)284 }285 if cf2.ScryptObject.LogN() != cf.ScryptObject.LogN()+1 {286 t.Errorf("wrong logN value %d", cf2.ScryptObject.LogN())287 }288}289// Test -init & -config flag290func TestInitConfig(t *testing.T) {291 config := test_helpers.TmpDir + "/TestInitConfig.conf"292 dir := test_helpers.InitFS(t, "-config="+config)293 _, err := os.Stat(config)294 if err != nil {295 t.Fatal(err)296 }297 // Test -passwd & -config298 cmd2 := exec.Command(test_helpers.GocryptfsBinary, "-q", "-passwd", "-extpass", "echo test",299 "-config", config, dir)300 cmd2.Stdout = os.Stdout301 cmd2.Stderr = os.Stderr302 err = cmd2.Run()303 if err != nil {304 t.Error(err)305 }306}307// Test -ro308func TestRo(t *testing.T) {309 dir := test_helpers.InitFS(t)310 mnt := dir + ".mnt"311 test_helpers.MountOrFatal(t, dir, mnt, "-ro", "-extpass=echo test")312 defer test_helpers.UnmountPanic(mnt)313 file := mnt + "/file"314 err := os.Mkdir(file, 0777)315 if err == nil {316 t.Errorf("Mkdir should have failed")317 }318 _, err = os.Create(file)319 if err == nil {320 t.Errorf("Create should have failed")321 }322}323// Test "-nonempty"324func TestNonempty(t *testing.T) {325 dir := test_helpers.InitFS(t)326 mnt := dir + ".mnt"327 err := os.Mkdir(mnt, 0700)328 if err != nil {329 t.Fatal(err)330 }331 err = ioutil.WriteFile(mnt+"/somefile", []byte("xyz"), 0600)332 if err != nil {333 t.Fatal(err)334 }335 err = test_helpers.Mount(dir, mnt, false, "-extpass=echo test")336 if err == nil {337 t.Errorf("Mounting over a file should fail per default")338 }339 // Should work with "-nonempty"340 test_helpers.MountOrFatal(t, dir, mnt, "-nonempty", "-extpass=echo test")341 test_helpers.UnmountPanic(mnt)342}343// -nofail should be ignored and the mount should succeed344func TestNofail(t *testing.T) {345 dir := test_helpers.InitFS(t)346 mnt := dir + ".mnt"347 test_helpers.MountOrFatal(t, dir, mnt, "-nofail", "-extpass=echo test")348 defer test_helpers.UnmountPanic(mnt)349}350// Test "mountpoint shadows cipherdir" handling351func TestShadows(t *testing.T) {352 mnt := test_helpers.InitFS(t)353 cipher := mnt + ".cipher"354 err := os.Rename(mnt, cipher)355 if err != nil {356 t.Fatal(err)357 }358 // This should work359 // (note that MountOrFatal creates "mnt" again)360 test_helpers.MountOrFatal(t, cipher, mnt, "-extpass=echo test")361 test_helpers.UnmountPanic(mnt)362 cipher2 := mnt + "/cipher"363 err = os.Rename(cipher, cipher2)364 if err != nil {365 t.Fatal(err)366 }367 // This should fail368 err = test_helpers.Mount(cipher2, mnt, false, "-extpass=echo test")369 if err == nil {370 t.Errorf("Should have failed")371 }372}373// TestMountPasswordIncorrect makes sure the correct exit code is used when the password374// was incorrect while mounting375func TestMountPasswordIncorrect(t *testing.T) {376 cDir := test_helpers.InitFS(t) // Create filesystem with password "test"377 pDir := cDir + ".mnt"378 err := test_helpers.Mount(cDir, pDir, false, "-extpass", "echo WRONG", "-wpanic=false")379 exitCode := test_helpers.ExtractCmdExitCode(err)380 if exitCode != exitcodes.PasswordIncorrect {381 t.Errorf("want=%d, got=%d", exitcodes.PasswordIncorrect, exitCode)382 }383}384// TestPasswdPasswordIncorrect makes sure the correct exit code is used when the password385// was incorrect while changing the password386func TestPasswdPasswordIncorrect(t *testing.T) {387 cDir := test_helpers.InitFS(t) // Create filesystem with password "test"388 // Change password389 cmd := exec.Command(test_helpers.GocryptfsBinary, "-passwd", cDir)390 childStdin, err := cmd.StdinPipe()391 if err != nil {392 t.Fatal(err)393 }394 err = cmd.Start()395 if err != nil {396 t.Fatal(err)397 }398 _, err = childStdin.Write([]byte("WRONGPASSWORD\nNewPassword"))399 if err != nil {400 t.Fatal(err)401 }402 err = childStdin.Close()403 if err != nil {404 t.Fatal(err)405 }406 err = cmd.Wait()407 exitCode := test_helpers.ExtractCmdExitCode(err)408 if exitCode != exitcodes.PasswordIncorrect {409 t.Errorf("want=%d, got=%d", exitcodes.PasswordIncorrect, exitCode)410 }411}412// Check that we correctly background on mount and close stderr and stdout.413// Something like414// gocryptfs a b | cat415// must not hang ( https://github.com/HorizonLiu/gocryptfs/issues/130 ).416func TestMountBackground(t *testing.T) {417 dir := test_helpers.InitFS(t)418 mnt := dir + ".mnt"419 err := os.Mkdir(mnt, 0700)420 if err != nil {421 t.Fatal(err)422 }423 // Manually create a pipe pair and connect the child's stdout and stderr424 // to it. We cannot use StdoutPipe because that will close the pipe425 // when the child forks away.426 pr, pw, err := os.Pipe()427 if err != nil {428 t.Fatal(err)429 }430 args := []string{"-extpass", "echo test", dir, mnt}431 cmd := exec.Command(test_helpers.GocryptfsBinary, args...)432 cmd.Stdout = pw433 cmd.Stderr = pw434 err = cmd.Run()435 if err != nil {436 t.Error(err)437 }438 pw.Close()439 defer test_helpers.UnmountPanic(mnt)440 // Read until we get EOF.441 c1 := make(chan struct{}, 1)442 go func() {443 buf := make([]byte, 1000)444 for {445 _, err = pr.Read(buf)446 // We should get io.EOF when the child closes stdout447 // and stderr.448 if err != nil {449 pr.Close()450 c1 <- struct{}{}451 return452 }453 }454 }()455 select {456 case <-c1:457 return458 case <-time.After(time.Second * 5):459 t.Fatal("timeout")460 }461}462// Test that "gocryptfs -init -info CIPHERDIR" returns an error to the463// user. Only one operation flag is allowed.464func TestMultipleOperationFlags(t *testing.T) {465 // Test all combinations466 opFlags := []string{"-init", "-info", "-passwd", "-fsck"}467 for _, flag1 := range opFlags {468 var flag2 string469 for _, flag2 = range opFlags {470 if flag1 == flag2 {471 continue472 }473 args := []string{flag1, flag2, "/tmp"}474 //t.Logf("testing %v", args)475 cmd := exec.Command(test_helpers.GocryptfsBinary, args...)476 err := cmd.Run()477 exitCode := test_helpers.ExtractCmdExitCode(err)478 if exitCode != exitcodes.Usage {479 t.Fatalf("this should have failed with code %d, but returned %d",480 exitcodes.Usage, exitCode)481 }482 }483 }484}485func TestNoexec(t *testing.T) {486 dir := test_helpers.InitFS(t)487 mnt := dir + ".mnt"488 test_helpers.MountOrFatal(t, dir, mnt, "-extpass=echo test", "-noexec")489 defer test_helpers.UnmountPanic(mnt)490 sh := mnt + "/x.sh"491 content := `#!/bin/bash492echo hello493`494 err := ioutil.WriteFile(sh, []byte(content), 0755)495 if err != nil {496 t.Fatal(err)497 }498 err = exec.Command(sh).Run()499 exitCode := test_helpers.ExtractCmdExitCode(err)500 if exitCode != int(syscall.EACCES) {501 t.Errorf("got exitcode %d instead of EPERM (%d)", exitCode, syscall.EPERM)502 }503}504// Test that a missing argument to "-o" triggers exit code 1.505// See also cli_args_test.go for comprehensive tests of "-o" parsing.506func TestMissingOArg(t *testing.T) {507 cmd := exec.Command(test_helpers.GocryptfsBinary, "foo", "bar", "-o")508 err := cmd.Run()509 exitCode := test_helpers.ExtractCmdExitCode(err)510 if exitCode != exitcodes.Usage {511 t.Fatalf("this should have failed with code %d, but returned %d",512 exitcodes.Usage, exitCode)513 }514}515// -exclude must return an error in forward mode516func TestExcludeForward(t *testing.T) {517 dir := test_helpers.InitFS(t)518 mnt := dir + ".mnt"519 err := test_helpers.Mount(dir, mnt, false, "-extpass", "echo test", "-exclude", "foo")520 if err == nil {521 t.Errorf("-exclude in forward mode should fail")522 }523 t.Log(err)524}525// Check that the config file can be read from a named pipe.526// Make sure bug https://github.com/HorizonLiu/gocryptfs/issues/258 does not come527// back.528func TestConfigPipe(t *testing.T) {529 dir := test_helpers.InitFS(t)530 mnt := dir + ".mnt"531 err := os.Mkdir(mnt, 0700)532 if err != nil {533 t.Fatal(err)534 }535 bashLine := fmt.Sprintf("%s -q -extpass \"echo test\" -config <(cat %s/gocryptfs.conf) %s %s", test_helpers.GocryptfsBinary, dir, dir, mnt)536 cmd := exec.Command("bash", "-c", bashLine)537 cmd.Stdout = os.Stdout538 cmd.Stderr = os.Stdout539 err = cmd.Run()540 exitCode := test_helpers.ExtractCmdExitCode(err)541 if exitCode != 0 {542 t.Errorf("bash command\n%q\nresulted in exit code %d", bashLine, exitCode)543 return544 }545 test_helpers.UnmountPanic(mnt)546}547// Ciphertext dir and mountpoint contains a comma548// https://github.com/HorizonLiu/gocryptfs/issues/262549func TestComma(t *testing.T) {550 dir0 := test_helpers.InitFS(t)551 dir := dir0 + ",foo,bar"552 err := os.Rename(dir0, dir)553 if err != nil {554 t.Fatal(err)555 }556 mnt := dir + ".mnt"557 err = test_helpers.Mount(dir, mnt, false, "-extpass", "echo test", "-wpanic=0")558 if err != nil {559 t.Fatalf("Failed to mount %q on %q: %v", dir, mnt, err)560 }561 test_helpers.UnmountPanic(mnt)562}563// Mount with idle timeout 10ms and check that the process exits by itself564// within 5 seconds.565func TestIdle(t *testing.T) {566 dir := test_helpers.InitFS(t)567 mnt := dir + ".mnt"568 err := os.Mkdir(mnt, 0700)569 if err != nil {570 t.Fatal(err)571 }572 cmd := exec.Command(test_helpers.GocryptfsBinary,573 "-q", "-nosyslog", "-fg", "-extpass", "echo test", "-i", "10ms", dir, mnt)574 cmd.Stdout = os.Stdout575 cmd.Stderr = os.Stderr576 err = cmd.Start()577 if err != nil {578 t.Fatal(err)579 }580 timer := time.AfterFunc(5*time.Second, func() {581 t.Error("timeout waiting for umount")582 cmd.Process.Kill()583 })584 err = cmd.Wait()585 timer.Stop()586 if err != nil {587 t.Error(err)588 }589}590// Mount with idle timeout of 100ms read something every 10ms. The fs should591// NOT get unmounted. Regression test for https://github.com/HorizonLiu/gocryptfs/issues/421592func TestNotIdle(t *testing.T) {593 dir := test_helpers.InitFS(t)594 mnt := dir + ".mnt"595 err := test_helpers.Mount(dir, mnt, false, "-extpass", "echo test", "-i=100ms")596 if err != nil {597 t.Fatal(err)598 }599 err = ioutil.WriteFile(mnt+"/foo", []byte("foo"), 0600)600 if err != nil {601 t.Fatal(err)602 }603 // Read every 10 milliseconds for a total of 1 second604 for i := 1; i < 100; i++ {605 _, err = ioutil.ReadFile(mnt + "/foo")606 if err != nil {607 t.Fatalf("iteration %d failed: %v", i, err)608 }609 time.Sleep(10 * time.Millisecond)610 }611 // Keep a file handle open for 1 second612 fd, err := os.Open(mnt + "/foo")613 if err != nil {614 t.Fatal(err)615 }616 time.Sleep(1 * time.Second)617 buf := make([]byte, 100)618 _, err = fd.Read(buf)619 if err != nil {620 t.Fatal(err)621 }622 fd.Close()623 // All good.624 test_helpers.UnmountPanic(mnt)625}626// TestSymlinkedCipherdir checks that if CIPHERDIR itself is a symlink, it is627// followed.628// https://github.com/HorizonLiu/gocryptfs/issues/450629func TestSymlinkedCipherdir(t *testing.T) {630 dir := test_helpers.InitFS(t)631 dirSymlink := dir + ".symlink"632 err := os.Symlink(dir, dirSymlink)633 if err != nil {634 t.Fatal(err)635 }636 mnt := dir + ".mnt"637 test_helpers.MountOrFatal(t, dirSymlink, mnt, "-extpass=echo test")638 defer test_helpers.UnmountPanic(mnt)639 file := mnt + "/file"640 f, err := os.Create(file)641 if err != nil {642 t.Fatal(err)643 }644 f.Close()645 f, err = os.Open(mnt)646 if err != nil {647 t.Fatal(err)648 }649 defer f.Close()650 names, err := f.Readdirnames(0)651 if err != nil {652 t.Fatal(err)653 }654 if len(names) != 1 || names[0] != "file" {655 t.Errorf("wrong Readdirnames result: %v", names)656 }657}658// TestBadname tests the `-badname` option659func TestBadname(t *testing.T) {660 dir := test_helpers.InitFS(t)661 mnt := dir + ".mnt"662 validFileName := "file"663 invalidSuffix := ".invalid_file"664 // use static suffix for testing665 test_helpers.MountOrFatal(t, dir, mnt, "-badname=*", "-extpass=echo test")666 defer test_helpers.UnmountPanic(mnt)667 // write one valid filename (empty content)668 file := mnt + "/" + validFileName669 err := ioutil.WriteFile(file, nil, 0600)670 if err != nil {671 t.Fatal(err)672 }673 // read encrypted file name674 fread, err := os.Open(dir)675 if err != nil {676 t.Fatal(err)677 }678 defer fread.Close()679 encryptedfilename := ""680 ciphernames, err := fread.Readdirnames(0)681 if err != nil {682 t.Fatal(err)683 }684 for _, ciphername := range ciphernames {685 if ciphername != "gocryptfs.conf" && ciphername != "gocryptfs.diriv" {686 encryptedfilename = ciphername687 // found cipher name of "file"688 break689 }690 }691 // write invalid file which should be decodable692 err = ioutil.WriteFile(dir+"/"+encryptedfilename+invalidSuffix, nil, 0600)693 if err != nil {694 t.Fatal(err)695 }696 // write invalid file which is not decodable (cropping the encrpyted file name)697 err = ioutil.WriteFile(dir+"/"+encryptedfilename[:len(encryptedfilename)-2]+invalidSuffix, nil, 0600)698 if err != nil {699 t.Fatal(err)700 }701 // check for filenames702 f, err := os.Open(mnt)703 if err != nil {704 t.Fatal(err)705 }706 defer f.Close()707 names, err := f.Readdirnames(0)708 if err != nil {709 t.Fatal(err)710 }711 foundDecodable := false712 foundUndecodable := false713 for _, name := range names {714 if strings.Contains(name, validFileName+invalidSuffix+" GOCRYPTFS_BAD_NAME") {715 foundDecodable = true716 } else if strings.Contains(name, encryptedfilename[:len(encryptedfilename)-2]+invalidSuffix+" GOCRYPTFS_BAD_NAME") {717 foundUndecodable = true718 }719 }720 if !foundDecodable {721 t.Errorf("did not find invalid name %s in %v", validFileName+invalidSuffix+" GOCRYPTFS_BAD_NAME", names)722 }723 if !foundUndecodable {724 t.Errorf("did not find invalid name %s in %v", encryptedfilename[:len(encryptedfilename)-2]+invalidSuffix+" GOCRYPTFS_BAD_NAME", names)725 }726}727// TestPassfile tests the `-passfile` option728func TestPassfile(t *testing.T) {729 dir := test_helpers.InitFS(t)730 mnt := dir + ".mnt"731 passfile1 := mnt + ".1.txt"732 ioutil.WriteFile(passfile1, []byte("test"), 0600)733 test_helpers.MountOrFatal(t, dir, mnt, "-passfile="+passfile1)734 defer test_helpers.UnmountPanic(mnt)735}736// TestPassfileX2 tests that the `-passfile` option can be passed twice737func TestPassfileX2(t *testing.T) {738 dir := test_helpers.InitFS(t)739 mnt := dir + ".mnt"740 passfile1 := mnt + ".1.txt"741 passfile2 := mnt + ".2.txt"742 ioutil.WriteFile(passfile1, []byte("te"), 0600)743 ioutil.WriteFile(passfile2, []byte("st"), 0600)744 test_helpers.MountOrFatal(t, dir, mnt, "-passfile="+passfile1, "-passfile="+passfile2)745 defer test_helpers.UnmountPanic(mnt)746}747// TestInitNotEmpty checks that `gocryptfs -init` returns the right error code748// if CIPHERDIR is not empty. See https://github.com/HorizonLiu/gocryptfs/pull/503749func TestInitNotEmpty(t *testing.T) {750 dir := test_helpers.TmpDir + "/" + t.Name()751 if err := os.Mkdir(dir, 0700); err != nil {752 t.Fatal(err)753 }754 if err := ioutil.WriteFile(dir+"/foo", nil, 0700); err != nil {755 t.Fatal(err)756 }757 cmd := exec.Command(test_helpers.GocryptfsBinary, "-init", "-extpass", "echo test", dir)758 cmd.Stdout = os.Stdout759 cmd.Stderr = os.Stderr760 err := cmd.Run()761 exitCode := test_helpers.ExtractCmdExitCode(err)762 if exitCode != exitcodes.CipherDir {763 t.Fatalf("wrong exit code: have=%d, want=%d", exitCode, exitcodes.CipherDir)764 }765}766// TestSharedstorage checks that `-sharedstorage` hands out arbitrary inode767// numbers (no hard link tracking)768func TestSharedstorage(t *testing.T) {769 dir := test_helpers.InitFS(t)770 mnt := dir + ".mnt"771 test_helpers.MountOrFatal(t, dir, mnt, "-extpass=echo test", "-sharedstorage")772 defer test_helpers.UnmountPanic(mnt)773 foo1 := mnt + "/foo1"774 foo2 := mnt + "/foo2"775 if err := ioutil.WriteFile(foo1, nil, 0755); err != nil {776 t.Fatal(err)777 }778 if err := os.Link(foo1, foo2); err != nil {779 t.Fatal(err)780 }781 var st1, st2, st3 syscall.Stat_t782 if err := syscall.Stat(foo1, &st1); err != nil {783 t.Fatal(err)784 }785 // The link show show a new inode number786 if err := syscall.Stat(foo2, &st2); err != nil {787 t.Fatal(err)788 }789 // Stat()'ing again should give us again a new inode number790 if err := syscall.Stat(foo2, &st3); err != nil {791 t.Fatal(err)792 }793 if st1.Ino == st2.Ino || st2.Ino == st3.Ino || st1.Ino == st3.Ino {794 t.Error(st1.Ino, st2.Ino, st3.Ino)795 }796 // Check that we we don't have stat caching. New length should show up797 // on the hard link immediately.798 if err := ioutil.WriteFile(foo1, []byte("xxxxxx"), 0755); err != nil {799 t.Fatal(err)800 }801 if err := syscall.Stat(foo2, &st2); err != nil {802 t.Fatal(err)803 }804 if st2.Size != 6 {805 t.Fatal(st2.Size)806 }807}808// Test that the filesystem is immediately ready for Creat() after mount returns809func TestMountCreat(t *testing.T) {810 const concurrency = 2811 const repeat = 2812 dir := test_helpers.InitFS(t)813 mnt := dir + ".mnt"814 for j := 0; j < repeat; j++ {815 test_helpers.MountOrFatal(t, dir, mnt, "-extpass=echo test")816 var wg sync.WaitGroup817 wg.Add(concurrency)818 for i := 0; i < concurrency; i++ {819 go func(i int) {820 path := fmt.Sprintf("%s/%d", mnt, i)821 fd, err := syscall.Creat(path, 0600)822 syscall.Close(fd)823 if err != nil {824 t.Errorf("Creat %q: %v", path, err)825 }826 wg.Done()827 }(i)828 }829 wg.Wait()830 test_helpers.UnmountPanic(mnt)831 }832}...

Full Screen

Full Screen

matrix_test.go

Source:matrix_test.go Github

copy

Full Screen

...431 if len(name) != 255 {432 t.Errorf("maxlen=%d", len(name))433 }434}435func TestLongNames(t *testing.T) {436 fi, err := ioutil.ReadDir(test_helpers.DefaultCipherDir)437 if err != nil {438 t.Fatal(err)439 }440 cnt1 := len(fi)441 wd := test_helpers.DefaultPlainDir + "/"442 // Create file with long name443 n255x := string(bytes.Repeat([]byte("x"), 255))444 f, err := os.Create(wd + n255x)445 if err != nil {446 t.Fatalf("Could not create n255x: %v", err)447 }448 f.Close()449 if !test_helpers.VerifyExistence(t, wd+n255x) {450 t.Errorf("n255x is not in directory listing")451 }452 // Rename long to long (target does not exist)453 n255y := string(bytes.Repeat([]byte("y"), 255))454 err = os.Rename(wd+n255x, wd+n255y)455 if err != nil {456 t.Fatalf("Could not rename n255x to n255y: %v", err)457 }458 if !test_helpers.VerifyExistence(t, wd+n255y) {459 t.Errorf("n255y is not in directory listing")460 }461 // Rename long to long (target exists)462 f, err = os.Create(wd + n255x)463 if err != nil {464 t.Fatalf("Could not create n255x: %v", err)465 }466 f.Close()467 err = os.Rename(wd+n255x, wd+n255y)468 if err != nil {469 t.Fatalf("Could not rename n255x to n255y: %v", err)470 }471 if !test_helpers.VerifyExistence(t, wd+n255y) {472 t.Errorf("n255y is not in directory listing")473 }474 // Rename long to short (target does not exist)475 err = os.Rename(wd+n255y, wd+"short")476 if err != nil {477 t.Fatalf("Could not rename n255y to short: %v", err)478 }479 if !test_helpers.VerifyExistence(t, wd+"short") {480 t.Errorf("short is not in directory listing")481 }482 // Rename long to short (target exists)483 f, err = os.Create(wd + n255y)484 if err != nil {485 t.Fatalf("Could not create n255y: %v", err)486 }487 f.Close()488 err = os.Rename(wd+n255y, wd+"short")489 if err != nil {490 t.Fatalf("Could not rename n255y to short: %v", err)491 }492 if !test_helpers.VerifyExistence(t, wd+"short") {493 t.Errorf("short is not in directory listing")494 }495 // Rename short to long (target does not exist)496 err = os.Rename(wd+"short", wd+n255x)497 if err != nil {498 t.Fatalf("Could not rename short to n255x: %v", err)499 }500 if !test_helpers.VerifyExistence(t, wd+n255x) {501 t.Errorf("255x is not in directory listing II")502 }503 // Rename short to long (target exists)504 f, err = os.Create(wd + "short")505 if err != nil {506 t.Fatalf("Could not create short: %v", err)507 }508 f.Close()509 err = os.Rename(wd+"short", wd+n255x)510 if err != nil {511 t.Fatalf("Could not rename short to n255x: %v", err)512 }513 if !test_helpers.VerifyExistence(t, wd+n255x) {514 t.Errorf("n255x is not in directory listing")515 }516 // Unlink517 err = syscall.Unlink(wd + n255x)518 if err != nil {519 t.Fatalf("Could not unlink n255x: %v", err)520 }521 if test_helpers.VerifyExistence(t, wd+n255x) {522 t.Errorf("n255x still there after unlink")523 }524 // Long symlink525 n255s := string(bytes.Repeat([]byte("s"), 255))526 err = os.Symlink("/", wd+n255s)527 if err != nil {528 t.Fatal(err)529 }530 if !test_helpers.VerifyExistence(t, wd+n255s) {531 t.Errorf("n255s is not in directory listing")532 }533 err = syscall.Unlink(wd + n255s)534 if err != nil {535 t.Error(err)536 }537 // Long dir538 n255d := string(bytes.Repeat([]byte("d"), 255))539 err = os.Mkdir(wd+n255d, 0777)540 if err != nil {541 t.Fatal(err)542 }543 err = syscall.Rmdir(wd + n255d)544 if err != nil {545 t.Error(err)546 }547 // Check for orphaned files548 fi, err = ioutil.ReadDir(test_helpers.DefaultCipherDir)549 if err != nil {550 t.Fatal(err)551 }552 cnt2 := len(fi)553 if cnt1 != cnt2 {554 t.Errorf("Leftover files, cnt1=%d cnt2=%d", cnt1, cnt2)555 }556}557// Create hard link with long name.558// This was broken up to v1.2.559func TestLongLink(t *testing.T) {560 wd := test_helpers.DefaultPlainDir + "/"561 target := wd + "TestLongLink.target"562 f, err := os.Create(target)563 if err != nil {564 t.Fatalf("%v", err)565 }566 f.Close()567 l255 := string(bytes.Repeat([]byte("l"), 255))568 err = os.Link(target, wd+l255)569 if err != nil {570 t.Error(err)571 }572}573func TestLchown(t *testing.T) {574 name := test_helpers.DefaultPlainDir + "/symlink"575 err := os.Symlink("/target/does/not/exist", name)576 if err != nil {577 t.Fatal(err)578 }579 err = os.Chown(name, os.Getuid(), os.Getgid())580 if err == nil {581 t.Error("Chown on dangling symlink should fail")582 }583 err = os.Lchown(name, os.Getuid(), os.Getgid())584 if err != nil {585 t.Error(err)586 }587}588// Set nanoseconds by path, symlink589func TestUtimesNanoSymlink(t *testing.T) {590 if runtime.GOOS == "darwin" {591 t.Skipf("MacOS \"touch\" does not support \"--no-dereference\"")592 }593 path := test_helpers.DefaultPlainDir + "/utimesnano_symlink"594 err := os.Symlink("/some/nonexisting/file", path)595 if err != nil {596 t.Fatal(err)597 }598 // syscall.UtimesNano does not provide a way to pass AT_SYMLINK_NOFOLLOW,599 // so we call the external utility "touch", which does.600 cmd := exec.Command("touch", "--no-dereference", path)601 cmd.Stderr = os.Stderr602 cmd.Stdout = os.Stdout603 err = cmd.Run()604 if err != nil {605 t.Error(err)606 }607}608type utimesTestcaseStruct struct {609 // Input atime and mtime610 in [2]syscall.Timespec611 // Expected output atime and mtime612 out [2]syscall.Timespec613}614// compareTimespec return true if the two passed Timespec are identical.615func compareTimespec(want syscall.Timespec, actual syscall.Timespec) bool {616 if want.Sec != actual.Sec {617 return false618 }619 if want.Nsec != actual.Nsec {620 return false621 }622 return true623}624const _UTIME_OMIT = ((1 << 30) - 2)625// doTestUtimesNano verifies that setting nanosecond-precision times on "path"626// works correctly. Pass "/proc/self/fd/N" to test a file descriptor.627func doTestUtimesNano(t *testing.T, path string) {628 utimeTestcases := []utimesTestcaseStruct{629 {630 in: [2]syscall.Timespec{{Sec: 50, Nsec: 0}, {Sec: 51, Nsec: 0}},631 out: [2]syscall.Timespec{{Sec: 50, Nsec: 0}, {Sec: 51, Nsec: 0}},632 },633 {634 in: [2]syscall.Timespec{{Sec: 1, Nsec: 2}, {Sec: 3, Nsec: 4}},635 out: [2]syscall.Timespec{{Sec: 1, Nsec: 2}, {Sec: 3, Nsec: 4}},636 },637 {638 in: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 99, Nsec: _UTIME_OMIT}},639 out: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 3, Nsec: 4}},640 },641 {642 in: [2]syscall.Timespec{{Sec: 99, Nsec: _UTIME_OMIT}, {Sec: 5, Nsec: 6}},643 out: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 5, Nsec: 6}},644 },645 }646 if runtime.GOOS == "darwin" {647 // darwin neither supports UTIME_OMIT nor nanoseconds (!?)648 utimeTestcases = utimeTestcases[:1]649 }650 for i, tc := range utimeTestcases {651 err := syscall.UtimesNano(path, tc.in[:])652 if err != nil {653 t.Fatalf("%q: %v", path, err)654 }655 var st syscall.Stat_t656 err = syscall.Stat(path, &st)657 if err != nil {658 t.Fatal(err)659 }660 want := tc.out661 have := extractAtimeMtime(st)662 if !compareTimespec(want[0], have[0]) {663 t.Errorf("Testcase %d: atime: want=%+v, have=%+v", i, want[0], have[0])664 }665 if !compareTimespec(want[1], have[1]) {666 t.Errorf("Testcase %d: mtime: want=%+v, have=%+v", i, want[1], have[1])667 }668 }669}670// Set nanoseconds by path, normal file671func TestUtimesNano(t *testing.T) {672 path := test_helpers.DefaultPlainDir + "/utimesnano"673 err := ioutil.WriteFile(path, []byte("foobar"), 0600)674 if err != nil {675 t.Fatal(err)676 }677 doTestUtimesNano(t, path)678}679// Set nanoseconds by fd680func TestUtimesNanoFd(t *testing.T) {681 if runtime.GOOS == "darwin" {682 t.Skipf("MacOS does not have /proc")683 }684 path := test_helpers.DefaultPlainDir + "/utimesnanofd"685 f, err := os.Create(path)686 if err != nil {687 t.Fatal(err)688 }689 defer f.Close()690 procPath := fmt.Sprintf("/proc/self/fd/%d", f.Fd())691 doTestUtimesNano(t, procPath)692}693// Make sure the Mknod call works by creating a fifo (named pipe)694func TestMkfifo(t *testing.T) {695 path := test_helpers.DefaultPlainDir + "/fifo1"696 err := syscall.Mkfifo(path, 0700)697 if err != nil {698 t.Fatal(err)699 }700 path = test_helpers.DefaultPlainDir + "/gocryptfs.longname.XXX"701 err = syscall.Mkfifo(path, 0700)702 if err != nil {703 t.Fatal(err)704 }705 err = os.Remove(path)706 if err != nil {707 t.Fatal(err)708 }709}710// TestMagicNames verifies that "magic" names are handled correctly711// https://github.com/HorizonLiu/gocryptfs/issues/174712func TestMagicNames(t *testing.T) {713 names := []string{"warmup1", "warmup2", "gocryptfs.longname.QhUr5d9FHerwEs--muUs6_80cy6JRp89c1otLwp92Cs", "gocryptfs.diriv"}714 for _, n := range names {715 t.Logf("Testing n=%q", n)716 p := test_helpers.DefaultPlainDir + "/" + n717 // Create file718 err := ioutil.WriteFile(p, []byte("xxxxxxx"), 0200)719 if err != nil {720 t.Fatalf("creating file %q failed: %v", n, err)721 }722 // Rename magic to normal723 err = os.Rename(p, test_helpers.DefaultPlainDir+"/x")724 if err != nil {725 t.Fatalf("rename 1 failed: %v", err)726 }...

Full Screen

Full Screen

sanity_test.go

Source:sanity_test.go Github

copy

Full Screen

...17 t.Error("this should fail")18 }19 test_helpers.UnmountPanic(pDir)20}21func TestBrokenNames(t *testing.T) {22 cDir := "broken_names"23 pDir := test_helpers.TmpDir + "/" + cDir24 test_helpers.MountOrFatal(t, cDir, pDir, "-extpass", "echo test", "-wpanic=false")25 _, err := os.Stat(pDir + "/status.txt")26 if err == nil {27 t.Error("this should fail")28 }29 test_helpers.UnmountPanic(pDir)30}

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(test_helpers.Names())4}5func Names() []string {6 return []string{"John", "Paul", "George", "Ringo"}7}8func Names() []string {9 return []string{"John", "Paul", "George", "Ringo"}10}11import (12func main() {13 fmt.Println(test_helpers.Names())14}15import (16func main() {17 fmt.Println(test_helpers.Names())18}19import (20func main() {21 fmt.Println(test_helpers.Names())22}23import (24func main() {25 fmt.Println(test_helpers.Names())26}27import (28func main() {29 fmt.Println(test_helpers.Names())30}31import (32func main() {33 fmt.Println(test_helpers.Names())34}35import (36func main() {37 fmt.Println(test_helpers.Names())38}39import (40func main() {41 fmt.Println(test_helpers.Names())42}43import (44func main() {45 fmt.Println(test_helpers.Names())46}47import (48func main() {49 fmt.Println(test_helpers.Names())50}51import (

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(test_helpers.Names())4}5func Names() []string {6 return []string{"A", "B", "C"}7}

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(test_helpers.Names())4}5import (6func main() {7 fmt.Println(test_helpers.Names())8}9If you want to test the behavior of your code when there is more than one instance of a package, you can write a test that imports the package multiple times. For example, the following code imports the test_helpers package twice and calls the Names method in each instance:10import (11func TestNames(t *testing.T) {12 fmt.Println(test_helpers.Names())13 fmt.Println(test_helpers.Names())14}15--- PASS: TestNames (0.00s)16If you want to test the behavior of your code when there is more than one instance of a package, you can write a test that imports the package multiple times. For example, the following code imports the test_helpers package twice and calls the Names method in each instance:17import (18func TestNames(t *testing.T) {19 fmt.Println(test_helpers.Names())20 fmt.Println(test_helpers.Names())21}22--- PASS: TestNames (0.00s)

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import "test_helpers"2func main() {3 test_helpers.Names()4}5func Names() {6}7func Names() {8}

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(test_helpers.Names())4}5import (6func main() {7 fmt.Println(test_helpers.Names())8}9import (10func main() {11 fmt.Println(test_helpers.Names())12}13import (14func main() {15 fmt.Println(test_helpers.Names())16}17import (18func main() {19 fmt.Println(test_helpers.Names())20}21import (22func main() {23 fmt.Println(test_helpers.Names())24}25import (26func main() {27 fmt.Println(test_helpers.Names())28}29import (30func main() {31 fmt.Println(test

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1func main() {2 fmt.Println(test_helpers.Names())3}4func Names() []string {5 return []string{"foo", "bar"}6}

Full Screen

Full Screen

Names

Using AI Code Generation

copy

Full Screen

1import "github.com/PraveenKumarP/test_helpers"2func main() {3 test_helpers.Names()4}5import "fmt"6func Names() {7 fmt.Println("Praveen")8}9--- PASS: TestNames (0.00s)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful