github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/status/log_send_test.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package status
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/keybase/client/go/libkb"
    14  	"github.com/keybase/client/go/logger"
    15  	jsonw "github.com/keybase/go-jsonw"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func testTail(t *testing.T, testname, filename string, count, actual int, first, last string) {
    20  	log := logger.NewTestLogger(t)
    21  	tailed := tail(log, "tset", filename, count)
    22  	lines := strings.Split(tailed, "\n")
    23  	if len(tailed) != actual {
    24  		t.Errorf("test %s: tailed bytes: %d, expected %d", testname, len(tailed), actual)
    25  	}
    26  
    27  	if strings.TrimSpace(lines[0]) != first {
    28  		t.Errorf("test %s: first line: %q, expected %q", testname, strings.TrimSpace(lines[0]), first)
    29  	}
    30  	if strings.TrimSpace(lines[len(lines)-2]) != last {
    31  		t.Errorf("test %s: last line: %q, expected %q", testname, strings.TrimSpace(lines[len(lines)-2]), last)
    32  	}
    33  	if strings.TrimSpace(lines[len(lines)-1]) != "" {
    34  		t.Errorf("test %s: last line: %q, expected %q", testname, strings.TrimSpace(lines[len(lines)-1]), "")
    35  	}
    36  
    37  }
    38  
    39  func TestTail(t *testing.T) {
    40  	// file has 20k lines in it
    41  	filename := filepath.Join("../libkb/testfixtures", "longline.testlog")
    42  
    43  	lastLine := "19999"
    44  
    45  	testTail(t, "tail -c 1002", filename, 1002, 996, "19834", lastLine)
    46  	testTail(t, "tail -c 100002", filename, 100002, 89994, "05001", lastLine)
    47  	testTail(t, "tail -c 100002", filename, 100002, 89994, "05001", lastLine)
    48  	testTail(t, "tail -c 250002", filename, 250002, 249999, "00179", lastLine)
    49  
    50  }
    51  
    52  func TestTailMulti(t *testing.T) {
    53  	stem := filepath.Join("../libkb/testfixtures", "f.testlog")
    54  
    55  	atime := time.Date(2017, time.March, 2, 4, 5, 6, 0, time.UTC)
    56  	// Force the fact the logs are from different times, since
    57  	// on Windows on CI, we can't get the mtime set on git checkout.
    58  	for i, sffx := range []string{"", ".1", ".2"} {
    59  		mtime := time.Date(2017, time.February, 1, 3, (60 - 5*i), 0, 0, time.UTC)
    60  		if err := os.Chtimes(stem+sffx, atime, mtime); err != nil {
    61  			t.Fatal(err)
    62  		}
    63  	}
    64  	testTail(t, "follow", stem, 100000, 99996, "13334", "29999")
    65  	testTail(t, "follow", stem, 10000, 9996, "28334", "29999")
    66  }
    67  
    68  func TestMergeStatusJSON(t *testing.T) {
    69  	tc := libkb.SetupTest(t, "MergeStatusJSON", 1)
    70  	defer tc.Cleanup()
    71  
    72  	mctx := libkb.NewMetaContextForTest(tc)
    73  	fstatus, err := GetFullStatus(mctx)
    74  	require.NoError(t, err)
    75  	require.NotNil(t, fstatus)
    76  	status := `{"desktop":{"running": true}}`
    77  	mergedStatus := MergeStatusJSON(fstatus, "fstatus", status)
    78  	require.NotEqual(t, status, mergedStatus)
    79  
    80  	w, err := jsonw.Unmarshal([]byte(mergedStatus))
    81  	require.NoError(t, err)
    82  	statusW := w.AtPath("status.desktop.running")
    83  	require.NotNil(t, statusW)
    84  	running, err := statusW.GetBool()
    85  	require.NoError(t, err)
    86  	require.True(t, running)
    87  
    88  	fstatusW := w.AtPath("fstatus")
    89  	require.NotNil(t, fstatusW)
    90  }
    91  
    92  var redactTests = []struct {
    93  	in  string
    94  	out string
    95  }{
    96  	{"hello this is my feedback; with punctuation", "hello this is my feedback; with punctuation"},
    97  	{"nope agent agent agent alcohol agent agent agent nope more feedback", "[redacted feedback follows] nope [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] nope more feedback"},
    98  	{"nope agent nope agent agent alcohol agent alcohol agent nope more feedback", "[redacted feedback follows] nope agent nope [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] nope more feedback"},
    99  	{"five in a row agent agent agent agent agent five in a row", "five in a row agent agent agent agent agent five in a row"},
   100  	{"agent agent agent agent agent agent", "[redacted feedback follows] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED]"},
   101  	{"agent agent agent agent agent agent offset", "[redacted feedback follows] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] offset"},
   102  	{"offset agent agent agent agent agent agent", "[redacted feedback follows] offset [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED]"},
   103  	{"1 2 agent agent agent 3 agent agent 4 agent agent agent agent agent agent 5 agent agent agent agent agent agent agent agent 6 7 8 9 10", "[redacted feedback follows] 1 2 agent agent agent 3 agent agent 4 [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] 5 [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] 6 7 8 9 10"},
   104  	{`tricky my paper key is in quotes: "agent agent agent agent agent agent" see!`, `[redacted feedback follows] tricky my paper key is in quotes: "[REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED]" see!`},
   105  	{`mismatched "agent agent agent agent agent agent)`, `[redacted feedback follows] mismatched "[REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED])`},
   106  }
   107  
   108  func TestRedactPaperKeys(t *testing.T) {
   109  	for _, tt := range redactTests {
   110  		t.Run(tt.in, func(t *testing.T) {
   111  			ret := redactPotentialPaperKeys(tt.in)
   112  			if ret != tt.out {
   113  				t.Errorf("got %q; want %q", ret, tt.out)
   114  			}
   115  		})
   116  	}
   117  }