Debian Codegraph @

parseconfig_test.go

raw
1 package main
2
3 import (
4 "os"
5 "runtime"
6 "strings"
7 "testing"
8 )
9
10 func TestParseConfig(t *testing.T) {
11 // Set permissions.
12 testfiles := []string{
13 "testdata/config001", "testdata/config002",
14 "testdata/config003", "testdata/config004",
15 }
16 for i, file := range testfiles {
17 switch i {
18 case 1:
19 err := os.Chmod(file, 0o444)
20 if err != nil {
21 t.Errorf("Failed to set permissions for %s", file)
22 }
23 default:
24 err := os.Chmod(file, 0o400)
25 if err != nil {
26 t.Errorf("Failed to set permissions for %s", file)
27 }
28 }
29 }
30 // Test good config.
31 cfg, err := parseConfig("testdata/config001")
32 if err != nil {
33 t.Fatalf("Failed to parse config: %v", err)
34 }
35 if cfg.username != "[email protected]" {
36 t.Errorf("Expected username '[email protected]', got '%s'", cfg.username)
37 }
38 if cfg.password != "hunter2" {
39 t.Errorf("Expected password 'hunter2', got '%s'", cfg.password)
40 }
41 if cfg.jserver != "xmpp.example.com" {
42 t.Errorf("Expected jserver 'xmpp.example.com', got '%s'", cfg.jserver)
43 }
44 if cfg.port != "1234" {
45 t.Errorf("Expected port '1234', got '%s'", cfg.port)
46 }
47 if cfg.alias != "bla" {
48 t.Errorf("Expected alias 'bla', got '%s'", cfg.alias)
49 }
50
51 if runtime.GOOS != "windows" {
52 // Bad permissions.
53 cfg, err = parseConfig("testdata/config002")
54 if err == nil {
55 t.Fatalf("Expected an error, got nil")
56 }
57 if !strings.Contains(err.Error(), "parse config: wrong permissions") {
58 t.Errorf("unexpected error message: got %v", err)
59 }
60 }
61
62 // Legacy sendxmpp formats.
63 cfg, err = parseConfig("testdata/config003")
64 if err != nil {
65 t.Fatalf("Failed to parse config: %v", err)
66 }
67 if cfg.username != "[email protected]" {
68 t.Errorf("Expected username '[email protected]', got '%s'", cfg.username)
69 }
70 if cfg.password != "hunter2" {
71 t.Errorf("Expected password 'hunter2', got '%s'", cfg.password)
72 }
73 if cfg.jserver != "example.com" {
74 t.Errorf("Expected jserver 'example.com', got '%s'", cfg.jserver)
75 }
76 cfg, err = parseConfig("testdata/config004")
77 if err != nil {
78 t.Fatalf("Failed to parse config: %v", err)
79 }
80 if cfg.username != "[email protected]" {
81 t.Errorf("Expected username '[email protected]', got '%s'", cfg.username)
82 }
83 if cfg.jserver != "xmpp.example.com" {
84 t.Errorf("Expected jserver 'xmpp.example.com', got '%s'", cfg.jserver)
85 }
86 if cfg.password != "hunter2" {
87 t.Errorf("Expected password 'hunter2', got '%s'", cfg.password)
88 }
89 if cfg.jserver != "xmpp.example.com" {
90 t.Errorf("Expected jserver 'xmpp.example.com', got '%s'", cfg.jserver)
91 }
92 }