Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

""" 

The obspy.arclink.client test suite. 

""" 

 

from obspy.arclink import Client 

from obspy.arclink.client import ArcLinkException, DCID_KEY_FILE 

from obspy.core.utcdatetime import UTCDateTime 

from obspy.core.util import NamedTemporaryFile, skipIf 

import numpy as np 

import os 

import unittest 

 

try: 

    from M2Crypto.EVP import EVPError 

    hasM2Crypto = True 

except ImportError: 

    hasM2Crypto = False 

 

 

class ClientTestCase(unittest.TestCase): 

    """ 

    Test cases for L{obspy.arclink.client.Client}. 

    """ 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformWithDCIDKey(self): 

        """ 

        """ 

        # test server for encryption 

        client1 = Client(host="webdc.eu", port=36000, user="test@obspy.org", 

                         dcid_keys={'BIA': 'OfH9ekhi'}) 

        # public server 

        client2 = Client(host="webdc.eu", port=18001, user="test@obspy.org") 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        stream1 = client1.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        stream2 = client2.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        # compare results 

        np.testing.assert_array_equal(stream1[0].data, stream2[0].data) 

        self.assertEqual(stream1[0].stats, stream2[0].stats) 

 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformWithDCIDKeyFile(self): 

        """ 

        """ 

        dcidfile = NamedTemporaryFile().name 

        fh = open(dcidfile, 'wt') 

        fh.write('TEST=XYZ\r\nBIA=OfH9ekhi\r\n') 

        fh.close() 

        # test server for encryption 

        client1 = Client(host="webdc.eu", port=36000, user="test@obspy.org", 

                         dcid_key_file=dcidfile) 

        # public server 

        client2 = Client(host="webdc.eu", port=18001, user="test@obspy.org") 

        # clean up dcid file 

        os.remove(dcidfile) 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        stream1 = client1.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        stream2 = client2.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        # compare results 

        np.testing.assert_array_equal(stream1[0].data, stream2[0].data) 

        self.assertEqual(stream1[0].stats, stream2[0].stats) 

 

    @skipIf(os.path.isfile(DCID_KEY_FILE), 

            '$HOME/dcidpasswords.txt already exists') 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformWithDefaultDCIDKeyFile(self): 

        """ 

        Use $HOME/dcidpasswords.txt. 

        """ 

        dcidfile = DCID_KEY_FILE 

        fh = open(dcidfile, 'wt') 

        fh.write('TEST=XYZ\r\nBIA=OfH9ekhi\r\n') 

        fh.close() 

        # test server for encryption 

        client1 = Client(host="webdc.eu", port=36000, user="test@obspy.org") 

        # public server 

        client2 = Client(host="webdc.eu", port=18001, user="test@obspy.org") 

        # clean up dcid file 

        os.remove(dcidfile) 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        stream1 = client1.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        stream2 = client2.getWaveform('GE', 'APE', '', 'BHZ', start, end) 

        # compare results 

        np.testing.assert_array_equal(stream1[0].data, stream2[0].data) 

        self.assertEqual(stream1[0].stats, stream2[0].stats) 

 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformUnknownUser(self): 

        """ 

        Unknown user raises an ArcLinkException: DENIED. 

        """ 

        client = Client(host="webdc.eu", port=36000, user="unknown@obspy.org") 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        self.assertRaises(ArcLinkException, client.getWaveform, 'GE', 'APE', 

                          '', 'BHZ', start, end) 

 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformWrongPassword(self): 

        """ 

        A wrong password password raises a "EVPError: bad decrypt". 

        """ 

        client = Client(host="webdc.eu", port=36000, user="test@obspy.org", 

                        dcid_keys={'BIA': 'WrongPassword'}) 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        self.assertRaises(EVPError, client.getWaveform, 'GE', 'APE', '', 'BHZ', 

                          start, end) 

 

    @skipIf(not hasM2Crypto, 'Module M2Crypto is not installed') 

    def test_getWaveformNoPassword(self): 

        """ 

        No password raises a "EVPError: bad decrypt". 

        """ 

        client = Client(host="webdc.eu", port=36000, user="test@obspy.org", 

                        dcid_keys={'BIA': ''}) 

        # request data 

        start = UTCDateTime(2010, 1, 1, 10, 0, 0) 

        end = start + 100 

        self.assertRaises(EVPError, client.getWaveform, 'GE', 'APE', '', 'BHZ', 

                          start, end) 

 

 

def suite(): 

    return unittest.makeSuite(ClientTestCase, 'test') 

 

 

if __name__ == '__main__': 

    unittest.main(defaultTest='suite')