|
| 1 | +package serverLog |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMatchOrOpenFile(t *testing.T) { |
| 11 | + var err error |
| 12 | + |
| 13 | + f, err := os.CreateTemp("", "log-*") |
| 14 | + if err != nil { |
| 15 | + t.Fatal("create temp log file failed") |
| 16 | + } |
| 17 | + defer func() { |
| 18 | + f.Close() |
| 19 | + os.Remove(f.Name()) |
| 20 | + }() |
| 21 | + |
| 22 | + i, err := f.Stat() |
| 23 | + if err != nil { |
| 24 | + t.Fatal("stat temp log file failed") |
| 25 | + } |
| 26 | + |
| 27 | + // matched, returns nil |
| 28 | + file, info, err := matchOrOpenFile(f.Name(), func(_info os.FileInfo) bool { |
| 29 | + if !os.SameFile(i, _info) { |
| 30 | + t.Error("callback `_info` should point to the same file as `i`") |
| 31 | + return false |
| 32 | + } else { |
| 33 | + return true |
| 34 | + } |
| 35 | + }) |
| 36 | + if file != nil { |
| 37 | + t.Error("`file` should be nil since match func returns true") |
| 38 | + file.Close() |
| 39 | + } |
| 40 | + if info != nil { |
| 41 | + t.Error("`info` should be nil since match func returns true") |
| 42 | + } |
| 43 | + |
| 44 | + // not matched, returns file/info |
| 45 | + file, info, err = matchOrOpenFile(f.Name(), func(_info os.FileInfo) bool { |
| 46 | + return false |
| 47 | + }) |
| 48 | + if file == nil { |
| 49 | + t.Error() |
| 50 | + } else { |
| 51 | + file.Close() |
| 52 | + } |
| 53 | + if info == nil { |
| 54 | + t.Error() |
| 55 | + } else if !os.SameFile(i, info) { |
| 56 | + t.Error(i.Name(), info.Name()) |
| 57 | + } |
| 58 | + |
| 59 | + // target is directory, returns error |
| 60 | + file, info, err = matchOrOpenFile(os.TempDir(), func(_info os.FileInfo) bool { |
| 61 | + return false |
| 62 | + }) |
| 63 | + if file != nil || info != nil || err == nil { |
| 64 | + t.Error() |
| 65 | + } |
| 66 | + |
| 67 | + // create file and returns file/info if not exists |
| 68 | + file, info, err = matchOrOpenFile(f.Name()+"-1", func(_info os.FileInfo) bool { |
| 69 | + return os.SameFile(i, _info) |
| 70 | + }) |
| 71 | + if file == nil { |
| 72 | + t.Error() |
| 73 | + } else { |
| 74 | + file.Close() |
| 75 | + os.Remove(file.Name()) |
| 76 | + } |
| 77 | + if info == nil { |
| 78 | + t.Error() |
| 79 | + } else if os.SameFile(i, info) { |
| 80 | + t.Error(i.Name(), info.Name()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestFileManNewLogChan(t *testing.T) { |
| 85 | + var err error |
| 86 | + |
| 87 | + file1, err := os.CreateTemp("", "log-*") |
| 88 | + if err != nil { |
| 89 | + t.Fatal("create log file 1 failed") |
| 90 | + } |
| 91 | + file1.Close() |
| 92 | + os.Remove(file1.Name()) |
| 93 | + |
| 94 | + file2, err := os.CreateTemp("", "log-*") |
| 95 | + if err != nil { |
| 96 | + t.Fatal("create log file 2 failed") |
| 97 | + } |
| 98 | + file2.Close() |
| 99 | + os.Remove(file2.Name()) |
| 100 | + |
| 101 | + man := NewFileMan() |
| 102 | + ch1a, _ := man.newLogChan(file1.Name()) |
| 103 | + ch1b, _ := man.newLogChan(file1.Name()) |
| 104 | + if ch1a != ch1b { |
| 105 | + t.Error() |
| 106 | + } |
| 107 | + |
| 108 | + ch2, _ := man.newLogChan(file2.Name()) |
| 109 | + if ch2 == ch1a { |
| 110 | + t.Error() |
| 111 | + } |
| 112 | + man.Close() |
| 113 | +} |
| 114 | + |
| 115 | +func TestFileMan(t *testing.T) { |
| 116 | + var err error |
| 117 | + |
| 118 | + accLogFile1, err := os.CreateTemp("", "log-*") |
| 119 | + if err != nil { |
| 120 | + t.Fatal("create log file 1 failed") |
| 121 | + } |
| 122 | + accLogFile1.Close() |
| 123 | + os.Remove(accLogFile1.Name()) |
| 124 | + |
| 125 | + accLogFile2, err := os.CreateTemp("", "log-*") |
| 126 | + if err != nil { |
| 127 | + t.Fatal("create log file 2 failed") |
| 128 | + } |
| 129 | + accLogFile2.Close() |
| 130 | + os.Remove(accLogFile2.Name()) |
| 131 | + |
| 132 | + errLogFile, err := os.CreateTemp("", "log-*") |
| 133 | + if err != nil { |
| 134 | + t.Fatal("create error log file failed") |
| 135 | + } |
| 136 | + errLogFile.Close() |
| 137 | + os.Remove(errLogFile.Name()) |
| 138 | + |
| 139 | + var es []error |
| 140 | + man := NewFileMan() |
| 141 | + logger1, es := man.NewLogger(accLogFile1.Name(), errLogFile.Name()) |
| 142 | + if len(es) > 0 { |
| 143 | + t.Fatal(es) |
| 144 | + } |
| 145 | + logger2, es := man.NewLogger(accLogFile2.Name(), errLogFile.Name()) |
| 146 | + if len(es) > 0 { |
| 147 | + t.Fatal(es) |
| 148 | + } |
| 149 | + |
| 150 | + logger1.LogAccessString("host 1 access allowed") |
| 151 | + logger1.LogErrorString("host 1 access denied") |
| 152 | + logger2.LogAccess([]byte("host 2 access allowed")) |
| 153 | + logger2.LogError([]byte("host 2 access denied")) |
| 154 | + |
| 155 | + time.Sleep(100 * time.Millisecond) // wait for log written |
| 156 | + man.ReOpen() |
| 157 | + logger1.LogAccessString("HOST 1 access allowed") |
| 158 | + logger1.LogErrorString("HOST 1 access denied") |
| 159 | + logger2.LogAccessString("HOST 2 access allowed") |
| 160 | + logger2.LogErrorString("HOST 2 access denied") |
| 161 | + |
| 162 | + err = os.Rename(errLogFile.Name(), errLogFile.Name()+"-old") |
| 163 | + if err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + time.Sleep(100 * time.Millisecond) // wait for log written |
| 167 | + man.ReOpen() |
| 168 | + |
| 169 | + logger1.LogErrorString("403 HOST I ACCESS DENIED") |
| 170 | + logger2.LogErrorString("403 HOST II ACCESS DENIED") |
| 171 | + man.Close() |
| 172 | + |
| 173 | + accLog1, _ := os.ReadFile(accLogFile1.Name()) |
| 174 | + if !bytes.Equal(accLog1, []byte("host 1 access allowed\nHOST 1 access allowed\n")) { |
| 175 | + t.Error(string(accLog1)) |
| 176 | + } |
| 177 | + accLog2, _ := os.ReadFile(accLogFile2.Name()) |
| 178 | + if !bytes.Equal(accLog2, []byte("host 2 access allowed\nHOST 2 access allowed\n")) { |
| 179 | + t.Error(string(accLog2)) |
| 180 | + } |
| 181 | + |
| 182 | + errLogOld, _ := os.ReadFile(errLogFile.Name() + "-old") |
| 183 | + if !bytes.Equal(errLogOld, []byte("host 1 access denied\nhost 2 access denied\nHOST 1 access denied\nHOST 2 access denied\n")) { |
| 184 | + t.Error(string(errLogOld)) |
| 185 | + } |
| 186 | + errLogNew, _ := os.ReadFile(errLogFile.Name()) |
| 187 | + if !bytes.Equal(errLogNew, []byte("403 HOST I ACCESS DENIED\n403 HOST II ACCESS DENIED\n")) { |
| 188 | + t.Error(string(errLogNew)) |
| 189 | + } |
| 190 | +} |
0 commit comments