1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package keyfile
import (
"bytes"
"path/filepath"
"regexp"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
const desktopFileContent0 = `#!/usr/bin/env xdg-open
[Desktop Entry]
Encoding=UTF-8
Type=Application
X-Created-By=cxoffice-407693d8-5e19-11e6-845f-2b300706ad83
Categories=chat;
# icon comments
Icon=apps.com.qq.im.light
Exec="/opt/cxoffice/support/apps.com.qq.im.light/desktopdata/cxmenu/StartMenu.C^5E3A_users_crossover_Start^2BMenu/Programs/QQ轻聊版.lnk" %u
Name=QQ轻聊版`
const desktopFileContent1 = `
Encoding=UTF-8
Type=Application
`
const desktopFileContent2 = `
[Desktop Entry]
Encoding=UTF-8
Type=Application
X-Created-By=cxoffice-407693d8-5e19-11e6-845f-2b300706ad83
Categories: chat;
`
const desktopFileContent3 = `
[Desktop Entry]
=nokey
`
const desktopFileContent4 = `
[Desktop Entry]
abc/def=nokey
`
func TestLoadFromData(t *testing.T) {
Convey("Test LoadFromData EntryNotInSectionError", t, func(c C) {
f := NewKeyFile()
err := f.LoadFromData([]byte(desktopFileContent1))
c.So(err, ShouldNotBeNil)
c.So(err, ShouldHaveSameTypeAs, EntryNotInSectionError{})
t.Log(err)
})
Convey("Test LoadFromData ParseError", t, func(c C) {
f := NewKeyFile()
err := f.LoadFromData([]byte(desktopFileContent2))
c.So(err, ShouldNotBeNil)
c.So(err, ShouldHaveSameTypeAs, ParseError{})
t.Log(err)
})
Convey("Test LoadFromData key is empty", t, func(c C) {
f := NewKeyFile()
err := f.LoadFromData([]byte(desktopFileContent3))
c.So(err, ShouldNotBeNil)
t.Log(err)
})
Convey("Test LoadFromData InvalidKeyError", t, func(c C) {
f := NewKeyFile()
keyReg := regexp.MustCompile(`^[A-Za-z0-9\-]+$`)
f.SetKeyRegexp(keyReg)
err := f.LoadFromData([]byte(desktopFileContent4))
c.So(err, ShouldNotBeNil)
t.Log(err)
ret := f.SetValue("Desktop Entry", "Abc+", "123")
c.So(ret, ShouldBeFalse)
ret = f.SetValue("Desktop Entry", "Abc", "123")
c.So(ret, ShouldBeTrue)
})
Convey("Test LoadFromData", t, func(c C) {
f := NewKeyFile()
err := f.LoadFromData([]byte(desktopFileContent0))
c.So(err, ShouldBeNil)
c.Convey("Test GetSections", func(c C) {
c.So(f.GetSections(), ShouldResemble, []string{"Desktop Entry"})
})
c.Convey("Get value that does exist", func(c C) {
v, err := f.GetValue("Desktop Entry", "Type")
c.So(err, ShouldBeNil)
c.So(v, ShouldEqual, "Application")
})
c.Convey("Get value the does not exist", func(c C) {
_, err := f.GetValue("Desktop Entry", "x")
c.So(err, ShouldNotBeNil)
})
c.Convey("Get value that section not exist", func(c C) {
_, err := f.GetValue("X", "X")
c.So(err, ShouldNotBeNil)
})
c.Convey("Test GetKeys", func(c C) {
c.So(f.GetKeys("Desktop Entry"), ShouldResemble, []string{
"Encoding", "Type", "X-Created-By", "Categories",
"Icon", "Exec", "Name",
})
})
c.Convey("Get section comments", func(c C) {
c.So(f.GetSectionComments("Desktop Entry"), ShouldEqual, "#!/usr/bin/env xdg-open\n")
})
c.Convey("Get key comments", func(c C) {
c.So(f.GetKeyComments("Desktop Entry", "Icon"), ShouldEqual, "# icon comments")
c.So(f.GetKeyComments("Desktop Entry", "Categories"), ShouldEqual, "")
})
})
}
func TestLoadFromFile(t *testing.T) {
Convey("LoadFromFile ok", t, func(c C) {
f := NewKeyFile()
err := f.LoadFromFile("testdata/deepin-screenshot.desktop")
c.So(err, ShouldBeNil)
localeName, err := f.GetLocaleString("Desktop Entry", "Name", "zh_CN")
c.So(localeName, ShouldEqual, "深度截图")
c.So(err, ShouldBeNil)
})
Convey("Load file in dir", t, func(c C) {
files, err := filepath.Glob("/usr/share/applications/*.desktop")
c.So(err, ShouldBeNil)
for _, file := range files {
f := NewKeyFile()
err := f.LoadFromFile(file)
c.So(err, ShouldBeNil)
}
})
}
const keyFileContent0 = `
[Test]
strlist0=a;b;c;d;
strlist1=a;b;c
strlist2=;
strlist3=
strlist4=abc\
strlist5=abc\befg
strlist6=a\;bc;def
strlist7=?`
func TestGetStringList(t *testing.T) {
Convey("GetStringList", t, func(c C) {
f := NewKeyFile()
keyFileContent := keyFileContent0 + string([]byte{0xff, 0xfe, 0xfd})
err := f.LoadFromData([]byte(keyFileContent))
c.So(err, ShouldBeNil)
list, err := f.GetStringList("Test", "strlist0")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{"a", "b", "c", "d"})
list, err = f.GetStringList("Test", "strlist1")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{"a", "b", "c"})
list, err = f.GetStringList("Test", "strlist2")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{""})
list, err = f.GetStringList("Test", "strlist3")
c.So(err, ShouldBeNil)
c.So(list, ShouldBeNil)
list, err = f.GetStringList("Test", "strlist4")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{"abc"})
t.Log(err)
list, err = f.GetStringList("Test", "strlist5")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{"abc\\befg"})
t.Log(err)
list, err = f.GetStringList("Test", "strlist6")
c.So(err, ShouldBeNil)
c.So(list, ShouldResemble, []string{"a;bc", "def"})
_, err = f.GetStringList("Test", "strlist7")
c.So(err, ShouldNotBeNil)
c.So(err, ShouldHaveSameTypeAs, ValueInvalidUTF8Error{})
t.Log(err)
})
}
const keyFileContent1 = `
[Test]
str0 = abcdef
str1=line\n<-newline\r<-break\t<-table\s<-space
str2=abcdef\
str3=abc\bdef
str4=abc\;def
str5=?`
func TestGetString(t *testing.T) {
Convey("GetString", t, func(c C) {
f := NewKeyFile()
keyFileContent := keyFileContent1 + string([]byte{0xff, 0xfe, 0xfd})
err := f.LoadFromData([]byte(keyFileContent))
c.So(err, ShouldBeNil)
str, err := f.GetString("Test", "str0")
c.So(err, ShouldBeNil)
c.So(str, ShouldEqual, "abcdef")
str, err = f.GetString("Test", "str1")
c.So(err, ShouldBeNil)
c.So(str, ShouldEqual, "line\n<-newline\r<-break\t<-table <-space")
str, err = f.GetString("Test", "str2")
c.So(err, ShouldBeNil)
c.So(str, ShouldEqual, "abcdef")
str, err = f.GetString("Test", "str3")
c.So(err, ShouldBeNil)
c.So(str, ShouldEqual, "abc\\bdef")
str, err = f.GetString("Test", "str4")
c.So(err, ShouldBeNil)
c.So(str, ShouldEqual, "abc\\;def")
_, err = f.GetString("Test", "str5")
c.So(err, ShouldHaveSameTypeAs, ValueInvalidUTF8Error{})
})
}
func TestSetString(t *testing.T) {
Convey("SetString", t, func(c C) {
f := NewKeyFile()
const s0 = "space newline\ncarriage-return\rtab\tbackslash\\"
f.SetString("Test", "str0", s0)
str0, err := f.GetString("Test", "str0")
c.So(err, ShouldBeNil)
c.So(str0, ShouldEqual, s0)
val0, err := f.GetValue("Test", "str0")
c.So(err, ShouldBeNil)
c.So(val0, ShouldEqual, `space newline\ncarriage-return\rtab\tbackslash\\`)
})
}
func TestSetStringList(t *testing.T) {
Convey("SetStringList", t, func(c C) {
f := NewKeyFile()
strlist := []string{"space ", "newline\n", "carriage\rreturn", "tab\t", "backslash\\", "List;Separator;"}
f.SetStringList("Test", "strlist", strlist)
strlist1, err := f.GetStringList("Test", "strlist")
c.So(err, ShouldBeNil)
c.So(strlist1, ShouldResemble, strlist)
strlistValue, err := f.GetValue("Test", "strlist")
c.So(err, ShouldBeNil)
c.So(strlistValue, ShouldEqual, `space\s;newline\n;carriage\rreturn;tab\t;backslash\\;List\;Separator\;;`)
})
}
func TestSetBoolList(t *testing.T) {
Convey("SetBoolList", t, func(c C) {
f := NewKeyFile()
blist := []bool{true, true, false, false, true, false}
f.SetBoolList("Test", "blist", blist)
blist1, err := f.GetBoolList("Test", "blist")
c.So(err, ShouldBeNil)
c.So(blist1, ShouldResemble, blist)
blistStr, err := f.GetValue("Test", "blist")
c.So(err, ShouldBeNil)
c.So(blistStr, ShouldEqual, "true;true;false;false;true;false;")
})
}
func TestSetIntList(t *testing.T) {
Convey("SetIntList", t, func(c C) {
f := NewKeyFile()
ints := []int{-345, -1, 0, 1, 3, 5, 7, 9, 11989}
f.SetIntList("Test", "ints", ints)
ints1, err := f.GetIntList("Test", "ints")
c.So(err, ShouldBeNil)
c.So(ints1, ShouldResemble, ints)
intsStr, err := f.GetValue("Test", "ints")
c.So(err, ShouldBeNil)
c.So(intsStr, ShouldResemble, "-345;-1;0;1;3;5;7;9;11989;")
})
}
const keyFileContent2 = `[Test]
KeyA=aaaa
KeyB=1234567890
KeyC=true
[Main]
KeyD=keyfile
`
func TestSaveToWriter(t *testing.T) {
Convey("SaveToWriter", t, func(c C) {
f := NewKeyFile()
f.SetValue("Test", "KeyA", "aaaa")
f.SetValue("Test", "KeyB", "1234567890")
f.SetValue("Test", "KeyC", "true")
f.SetValue("Main", "KeyD", "keyfile")
var buf bytes.Buffer
err := f.SaveToWriter(&buf)
c.So(err, ShouldBeNil)
c.So(buf.String(), ShouldEqual, keyFileContent2)
})
}
|